List of usage examples for org.apache.pdfbox.pdmodel PDDocument save
public void save(OutputStream output) throws IOException
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public void protect(final String inputUri, final String outputUri, final String password) throws IOException, BadSecurityHandlerException, COSVisitorException { if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && StringUtils.isNotBlank(password)) { final PDDocument doc = PDDocument.load(inputUri); final StandardProtectionPolicy pp = new StandardProtectionPolicy(password, password, new AccessPermission()); doc.protect(pp);//from w ww.jav a 2s . co m doc.save(outputUri); doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public void unProtect(final String inputUri, final String outputUri, final String password) throws IOException, COSVisitorException, BadSecurityHandlerException, CryptographyException { if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && StringUtils.isNotBlank(password)) { final PDDocument doc = PDDocument.load(inputUri); final DecryptionMaterial decryptionMaterial = new StandardDecryptionMaterial(password); doc.openProtection(decryptionMaterial); final StandardProtectionPolicy pp = new StandardProtectionPolicy(null, null, new AccessPermission()); doc.protect(pp);/* w w w. j a v a 2 s. c o m*/ doc.save(outputUri); doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public void putWatermark(final String inputUri, final String outputUri, final String text, final Color color, final Float alpha, final WatermarkPosition watermarkPosition, final List<Integer> pages) throws IOException, COSVisitorException, WatermarkOutOfLengthException { if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && StringUtils.isNotBlank(text) && color != null && alpha != null && watermarkPosition != null) { // If watermark position is not centered, then max length is the // same for landscape and portrait, text length is tested here so // there is no need to continue. if (!WatermarkPosition.CENTER.equals(watermarkPosition) && text.length() > watermarkPosition.getMaxLengthPortrait()) { throw new WatermarkOutOfLengthException(Constants.WATERMARK_OUT_OF_LENGTH_EXCEPTION_MESSAGE); }//from ww w . j a va 2 s . c o m final PDDocument doc = PDDocument.load(inputUri); final List<?> allPages = doc.getDocumentCatalog().getAllPages(); this.converterUtils.deleteNonSelectedPositions(allPages, pages); if (CollectionUtils.isNotEmpty(allPages)) { for (final Object object : allPages) { final PDPage page = (PDPage) object; // The transparency, opacity of graphic objects can be set // directly // on the drawing commands but need to be set to a graphic // state // which will become part of the resources. Graphic state is // set // up. this.watermarkUtils.setUpGraphicState(page, alpha); // Now we will be able to call the state definition before // doing // the // drawing try { this.watermarkUtils.addWatermark(doc, page, color, text, watermarkPosition); } catch (final WatermarkOutOfLengthException e) { doc.close(); throw e; } } } doc.save(outputUri); doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public void addBookmarks(final String inputUri, final String outputUri, final String title, final List<PDFGalBookmark> pdfGalBookmarksList) throws IOException, COSVisitorException { if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && StringUtils.isNotEmpty(title) && CollectionUtils.isNotEmpty(pdfGalBookmarksList)) { final PDDocument doc = PDDocument.load(inputUri); final PDDocumentOutline outline = new PDDocumentOutline(); doc.getDocumentCatalog().setDocumentOutline(outline); final PDOutlineItem pagesOutline = new PDOutlineItem(); pagesOutline.setTitle(title);/* w w w . ja v a 2 s. co m*/ @SuppressWarnings("unchecked") final List<PDPage> pages = doc.getDocumentCatalog().getAllPages(); outline.appendChild(pagesOutline); for (final PDFGalBookmark pdfGalBookmark : pdfGalBookmarksList) { if (pdfGalBookmark != null && pdfGalBookmark.isInitializated()) { final PDPage page = pages.get(pdfGalBookmark.getPage() - 1); final PDPageFitWidthDestination dest = new PDPageFitWidthDestination(); dest.setPage(page); final PDOutlineItem bookmark = new PDOutlineItem(); bookmark.setDestination(dest); bookmark.setTitle(pdfGalBookmark.getText()); pagesOutline.appendChild(bookmark); } } pagesOutline.openNode(); outline.openNode(); doc.save(outputUri); doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public void reIndexPageNumbers(final String inputUri, final String outputUri, final List<PDFGalPageNumbering> pdfGalPageNumberingList) throws IOException, COSVisitorException { if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && CollectionUtils.isNotEmpty(pdfGalPageNumberingList)) { final PDDocument doc = PDDocument.load(inputUri); final PDPageLabels pdPageLabels = new PDPageLabels(doc); for (final PDFGalPageNumbering pageNumbering : pdfGalPageNumberingList) { if (pageNumbering.isInitializated()) { final PDPageLabelRange pdPageLabelRange = new PDPageLabelRange(); pdPageLabelRange.setStyle(pageNumbering.getNumberingStyle().getValue()); pdPageLabels.setLabelItem(pageNumbering.getPageNumber() - 1, pdPageLabelRange); }/*from w w w . j av a 2 s . c o m*/ } doc.getDocumentCatalog().setPageLabels(pdPageLabels); doc.save(outputUri); doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } }
From source file:org.pdfmetamodifier.IOHelper.java
License:Apache License
/** * Update Outlines (bookmarks).//from ww w. j a v a 2 s .co m * * @param pdfFile * Source PDF file. * @param outlinesFile * File with Outlines (bookmarks) in user-frendly format. * @throws IOException */ /* * See: * https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/CreateBookmarks.java?view=markup */ public static void updateOutlines(final File pdfFile, final File outlinesFile) throws IOException { // Read bookmark list from text file. final List<String> lines = Files.readAllLines(outlinesFile.toPath()); PDDocument document = null; try { // Open PDF file. document = PDDocument.load(pdfFile); if (document.isEncrypted()) { throw new IOException("Document is encrypted."); } // Get data from PDF file. final PDDocumentCatalog catalog = document.getDocumentCatalog(); final PDPageTree pages = catalog.getPages(); // Convert. final PDDocumentOutline outlines = OutlineHelper.lineListToOutlines(pages, lines); // Set outlines. catalog.setDocumentOutline(outlines); // Create temporary PDF file for result. if (TEMP_PDF.exists()) { TEMP_PDF.delete(); } // Save result to temporary PDF file. document.save(TEMP_PDF); // Replace original PDF file. pdfFile.delete(); Files.move(Paths.get(TEMP_PDF.toURI()), Paths.get(pdfFile.toURI())); } finally { if (document != null) { document.close(); } } }
From source file:org.pdfmetamodifier.IOHelper.java
License:Apache License
/** * Update Metadata.// w w w . j ava 2s.c o m * * @param pdfFile * Source PDF file. * @param metadataFile * File with Metadata in user-frendly format. * @throws IOException */ /* * See: * https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractMetadata.java?view=markup */ public static void updateMetadata(final File pdfFile, final File metadataFile) throws IOException { // Read bookmark list from text file. final List<String> lines = Files.readAllLines(metadataFile.toPath()); PDDocument document = null; try { // Open PDF file. document = PDDocument.load(pdfFile); if (document.isEncrypted()) { throw new IOException("Document is encrypted."); } // Convert. final PDDocumentInformation information = MetadataHelper.stringListToMetadata(lines); // Set Metadata. document.setDocumentInformation(information); // Create temporary PDF file for result. if (TEMP_PDF.exists()) { TEMP_PDF.delete(); } // Save result to temporary PDF file. document.save(TEMP_PDF); // Replace original PDF file. pdfFile.delete(); Files.move(Paths.get(TEMP_PDF.toURI()), Paths.get(pdfFile.toURI())); } finally { if (document != null) { document.close(); } } }
From source file:org.pdfmetamodifier.IOHelper.java
License:Apache License
/** * Remove all Attached (embedded) files. * /* ww w . ja v a 2 s.c om*/ * @param pdfFile * Source PDF file. * @throws IOException */ public static void removeAttachments(final File pdfFile) throws IOException { PDDocument document = null; try { // Read PDF file. document = PDDocument.load(pdfFile); if (document.isEncrypted()) { throw new IOException("Document is encrypted."); } // Clean the tree to the document catalog. document.getDocumentCatalog().setNames(null); // Create temporary PDF file for result. if (TEMP_PDF.exists()) { TEMP_PDF.delete(); } // Save result to temporary PDF file. document.save(TEMP_PDF); // Replace original PDF file. pdfFile.delete(); Files.move(Paths.get(TEMP_PDF.toURI()), Paths.get(pdfFile.toURI())); } finally { if (document != null) { document.close(); } } }
From source file:org.pdfmetamodifier.IOHelper.java
License:Apache License
/** * Add new Attached (embedded) files./*from w ww . j ava 2s . c o m*/ * * @param pdfFile * Source PDF file. * @param attachmentFiles * Files that will be attached (embedded). * @throws IOException */ /* * See: * https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFiles.java?view=markup */ public static void addAttachments(final File pdfFile, final List<File> attachmentFiles) throws IOException { PDDocument document = null; try { // Read PDF file. document = PDDocument.load(pdfFile); if (document.isEncrypted()) { throw new IOException("Document is encrypted."); } // Embedded (attached) files are stored in a named tree. final PDEmbeddedFilesNameTreeNode root = new PDEmbeddedFilesNameTreeNode(); final List<PDEmbeddedFilesNameTreeNode> kids = new ArrayList<PDEmbeddedFilesNameTreeNode>(); root.setKids(kids); // Add the tree to the document catalog. final PDDocumentNameDictionary namesDictionary = new PDDocumentNameDictionary( document.getDocumentCatalog()); namesDictionary.setEmbeddedFiles(root); document.getDocumentCatalog().setNames(namesDictionary); // For all Embedded (attached) files. for (File file : attachmentFiles) { final String filename = file.getName(); // First create the file specification, which holds the Embedded (attached) file. final PDComplexFileSpecification complexFileSpecification = new PDComplexFileSpecification(); complexFileSpecification.setFile(filename); // Create a dummy file stream, this would probably normally be a FileInputStream. final ByteArrayInputStream fileStream = new ByteArrayInputStream(Files.readAllBytes(file.toPath())); final PDEmbeddedFile embededFile = new PDEmbeddedFile(document, fileStream); complexFileSpecification.setEmbeddedFile(embededFile); // Create a new tree node and add the Embedded (attached) file. final PDEmbeddedFilesNameTreeNode embeddedFilesNameTree = new PDEmbeddedFilesNameTreeNode(); embeddedFilesNameTree.setNames(Collections.singletonMap(filename, complexFileSpecification)); // Add the new node as kid to the root node. kids.add(embeddedFilesNameTree); } // Create temporary PDF file for result. if (TEMP_PDF.exists()) { TEMP_PDF.delete(); } // Save result to temporary PDF file. document.save(TEMP_PDF); // Replace original PDF file. pdfFile.delete(); Files.move(Paths.get(TEMP_PDF.toURI()), Paths.get(pdfFile.toURI())); } finally { if (document != null) { document.close(); } } }
From source file:org.pensco.MiscUtils.java
License:Open Source License
public static FileBlob saveInTempFile(PDDocument inPdfDoc) throws IOException, COSVisitorException { FileBlob result = null;/* w ww . j a v a 2 s .c o m*/ File tempFile = File.createTempFile("pensco-utils-", ".pdf"); inPdfDoc.save(tempFile); result = new FileBlob(tempFile); result.setMimeType("application/pdf"); Framework.trackFile(tempFile, result); return result; }