Java Code Examples for org.apache.pdfbox.pdmodel.PDDocument#getDocumentCatalog()
The following examples show how to use
org.apache.pdfbox.pdmodel.PDDocument#getDocumentCatalog() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDFParser.java From document-management-software with GNU Lesser General Public License v3.0 | 6 votes |
/** * Extract the text from the form fields */ private void parseForm(PDDocument pdfDocument, StringBuffer content) throws IOException { PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); if (acroForm == null) return; content.append("\n"); List<PDField> fields = acroForm.getFields(); log.debug("{} top-level fields were found on the form", fields.size()); for (PDField field : fields) { content.append(field.getPartialName()); content.append(" = "); content.append(field.getValueAsString()); content.append(" \n "); } }
Example 2
Source File: PdfBleachSession.java From DocBleach with MIT License | 6 votes |
void sanitize(RandomAccessRead source, OutputStream outputStream) throws IOException, BleachException { final PDDocument doc = getDocument(source); final PDDocumentCatalog docCatalog = doc.getDocumentCatalog(); sanitizeNamed(doc, docCatalog.getNames()); PDDocumentCatalogBleach catalogBleach = new PDDocumentCatalogBleach(this); catalogBleach.sanitize(docCatalog); sanitizeDocumentOutline(doc.getDocumentCatalog().getDocumentOutline()); cosObjectBleach.sanitizeObjects(doc.getDocument().getObjects()); doc.save(outputStream); doc.close(); }
Example 3
Source File: PDFPainterBase.java From ctsms with GNU Lesser General Public License v2.1 | 6 votes |
public void setMetadata(PDDocument doc) throws Exception { PDDocumentCatalog catalog = doc.getDocumentCatalog(); PDDocumentInformation info = doc.getDocumentInformation(); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(now); XMPMetadata metadata = new XMPMetadata(); XMPSchemaPDF pdfSchema = metadata.addPDFSchema(); pdfSchema.setKeywords(info.getKeywords()); pdfSchema.setProducer(info.getProducer()); XMPSchemaBasic basicSchema = metadata.addBasicSchema(); basicSchema.setModifyDate(cal); basicSchema.setCreateDate(cal); basicSchema.setCreatorTool(info.getCreator()); basicSchema.setMetadataDate(cal); XMPSchemaDublinCore dcSchema = metadata.addDublinCoreSchema(); dcSchema.setTitle(info.getTitle()); dcSchema.addCreator("PDFBox"); dcSchema.setDescription(info.getSubject()); PDMetadata metadataStream = new PDMetadata(doc); metadataStream.importXMPMetadata(metadata); catalog.setMetadata(metadataStream); }
Example 4
Source File: LayerUtility.java From gcs with Mozilla Public License 2.0 | 6 votes |
/** * Imports OCProperties from source document to target document so hidden layers can still be * hidden after import. * * @param sourceDoc The source PDF document that contains the /OCProperties to be copied. * @throws IOException If an I/O error occurs. */ private void importOcProperties(PDDocument srcDoc) throws IOException { PDDocumentCatalog srcCatalog = srcDoc.getDocumentCatalog(); PDOptionalContentProperties srcOCProperties = srcCatalog.getOCProperties(); if (srcOCProperties == null) { return; } PDDocumentCatalog dstCatalog = targetDoc.getDocumentCatalog(); PDOptionalContentProperties dstOCProperties = dstCatalog.getOCProperties(); if (dstOCProperties == null) { dstCatalog.setOCProperties(new PDOptionalContentProperties( (COSDictionary) cloner.cloneForNewDocument(srcOCProperties))); } else { cloner.cloneMerge(srcOCProperties, dstOCProperties); } }
Example 5
Source File: CreatePortableCollection.java From testarea-pdfbox2 with Apache License 2.0 | 6 votes |
/** * <a href="https://stackoverflow.com/questions/46642994/how-to-create-pdf-package-using-pdfbox"> * How to create pdf package using PdfBox? * </a> * <p> * This test implements the equivalent of the OP's code turning * a regular PDF into a portable collection. * </p> */ @Test public void test() throws IOException { try ( InputStream resource = getClass().getResourceAsStream("/mkl/testarea/pdfbox2/sign/test.pdf")) { PDDocument pdDocument = Loader.loadPDF(resource); COSDictionary collectionDictionary = new COSDictionary(); collectionDictionary.setName(COSName.TYPE, "Collection"); collectionDictionary.setName("View", "T"); PDDocumentCatalog catalog = pdDocument.getDocumentCatalog(); catalog.getCOSObject().setItem("Collection", collectionDictionary); pdDocument.save(new File(RESULT_FOLDER, "test-collection.pdf")); } }
Example 6
Source File: FillInForm.java From testarea-pdfbox2 with Apache License 2.0 | 6 votes |
/** * <a href="https://stackoverflow.com/questions/52059931/pdfbox-setvalue-for-multiple-pdtextfield"> * PDFBox setValue for multiple PDTextField * </a> * <br/> * <a href="https://ufile.io/z8jzj"> * testform.pdf * </a> * <p> * Cannot reproduce the issue. * </p> */ @Test public void testFillLikeJuvi() throws IOException { try ( InputStream originalStream = getClass().getResourceAsStream("testform.pdf") ) { PDDocument document = Loader.loadPDF(originalStream); PDDocumentCatalog docCatalog = document.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); PDTextField field = (PDTextField) acroForm.getField("Check1"); field.setValue("1111"); PDTextField field2 = (PDTextField) acroForm.getField("Check2"); field2.setValue("2222"); PDTextField field3 = (PDTextField) acroForm.getField("HelloWorld"); field3.setValue("HelloWorld"); document.save(new File(RESULT_FOLDER, "testform-filled.pdf")); document.close(); } }
Example 7
Source File: AbstractPdfBoxSignatureDrawer.java From dss with GNU Lesser General Public License v2.1 | 6 votes |
/** * Method to check if the target image's colro space is present in the document's catalog * @param pdDocument {@link PDDocument} to check color profiles in * @param image {@link DSSDocument} image * @throws IOException in case of image reading error */ protected void checkColorSpace(PDDocument pdDocument, DSSDocument image) throws IOException { if (image != null) { PDDocumentCatalog catalog = pdDocument.getDocumentCatalog(); List<PDOutputIntent> profiles = catalog.getOutputIntents(); if (Utils.isCollectionEmpty(profiles)) { LOG.warn("No color profile is present in the document. Not compatible with PDF/A"); return; } String colorSpaceName = getColorSpaceName(image); if (COSName.DEVICECMYK.getName().equals(colorSpaceName) && isProfilePresent(profiles, RGB_PROFILE_NAME)) { LOG.warn("A CMYK image will be added to an RGB color space PDF. Be aware: not compatible with PDF/A."); } else if (COSName.DEVICERGB.getName().equals(colorSpaceName) && isProfilePresent(profiles, CMYK_PROFILE_NAME)) { LOG.warn("An RGB image will be added to a CMYK color space PDF. Be aware: not compatible with PDF/A."); } } }
Example 8
Source File: AddFormField.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** * <a href="https://stackoverflow.com/questions/46433388/pdfbox-could-not-find-font-helv"> * PDFbox Could not find font: /Helv * </a> * <br/> * <a href="https://drive.google.com/file/d/0B2--NSDOiujoR3hOZFYteUl2UE0/view?usp=sharing"> * 4.pdf * </a> * <p> * The cause is a combination of the OP and the source PDF not providing * a default appearance for the text field and PDFBox providing defaults * inconsequentially. * </p> * <p> * This is fixed here by setting the default appearance explicitly. * </p> */ @Test public void testAddFieldLikeEugenePodoliako() throws IOException { try ( InputStream originalStream = getClass().getResourceAsStream("4.pdf") ) { PDDocument pdf = Loader.loadPDF(originalStream); PDDocumentCatalog docCatalog = pdf.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); PDPage page = pdf.getPage(0); PDTextField textBox = new PDTextField(acroForm); textBox.setPartialName("SampleField"); acroForm.getFields().add(textBox); PDAnnotationWidget widget = textBox.getWidgets().get(0); PDRectangle rect = new PDRectangle(0, 0, 0, 0); widget.setRectangle(rect); widget.setPage(page); // Unnecessary code from OP // widget.setAppearance(acroForm.getFields().get(0).getWidgets().get(0).getAppearance()); // Fix added to set default appearance accordingly textBox.setDefaultAppearance(acroForm.getFields().get(0).getCOSObject().getString("DA")); widget.setPrinted(false); page.getAnnotations().add(widget); acroForm.refreshAppearances(); acroForm.flatten(); pdf.save(new File(RESULT_FOLDER, "4-add-field.pdf")); pdf.close(); } }
Example 9
Source File: RightAlignField.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** * <a href="https://stackoverflow.com/questions/55355800/acroform-text-field-not-align-to-right"> * acroform text field not align to right * </a> * <br/> * <a href="https://drive.google.com/uc?id=1jFbsYGFOnx8EMiHgDsE8LQtfwJHSa5Gh&export=download"> * form.pdf * </a> as "formBee2.pdf" * <p> * Indeed, the way the OP does this, quadding does not apply. * But see {@link #testAlignLikeBeeImproved()}. * </p> */ @Test public void testAlignLikeBee() throws IOException { try ( InputStream resource = getClass().getResourceAsStream("formBee2.pdf") ) { PDDocument document = Loader.loadPDF(resource); PDDocumentCatalog documentCatalog = document.getDocumentCatalog(); PDAcroForm acroForm = documentCatalog.getAcroForm(); acroForm.getField("NewRentWithoutChargesChf").setValue("1.00"); ((PDTextField) acroForm.getField("NewRentWithoutChargesChf")).setQ(PDVariableText.QUADDING_RIGHT); document.save(new File(RESULT_FOLDER, "formBee2-AlignLikeBee.pdf")); document.close(); } }
Example 10
Source File: RightAlignField.java From testarea-pdfbox2 with Apache License 2.0 | 5 votes |
/** * <a href="https://stackoverflow.com/questions/55355800/acroform-text-field-not-align-to-right"> * acroform text field not align to right * </a> * <br/> * <a href="https://drive.google.com/uc?id=1jFbsYGFOnx8EMiHgDsE8LQtfwJHSa5Gh&export=download"> * form.pdf * </a> as "formBee2.pdf" * <p> * Indeed, in {@link #testAlignLikeBee()} quadding does not apply. * But by changing the order of field value setting and quadding * value setting it suddenly does apply. * </p> */ @Test public void testAlignLikeBeeImproved() throws IOException { try ( InputStream resource = getClass().getResourceAsStream("formBee2.pdf") ) { PDDocument document = Loader.loadPDF(resource); PDDocumentCatalog documentCatalog = document.getDocumentCatalog(); PDAcroForm acroForm = documentCatalog.getAcroForm(); ((PDTextField) acroForm.getField("NewRentWithoutChargesChf")).setQ(PDVariableText.QUADDING_RIGHT); acroForm.getField("NewRentWithoutChargesChf").setValue("1.00"); document.save(new File(RESULT_FOLDER, "formBee2-AlignLikeBeeImproved.pdf")); document.close(); } }
Example 11
Source File: ReadXfaForm.java From testarea-pdfbox2 with Apache License 2.0 | 4 votes |
public static byte[] getParsableXFAForm(InputStream file) { if (file == null) return null; PDDocument doc; PDDocumentCatalog catalog; PDAcroForm acroForm; PDXFAResource xfa; try { // String pass = null; doc = Loader.loadPDF(file); if (doc == null) return null; // flattenPDF(doc); doc.setAllSecurityToBeRemoved(true); // System.out.println("Security " + doc.isAllSecurityToBeRemoved()); catalog = doc.getDocumentCatalog(); if (catalog == null) { doc.close(); return null; } acroForm = catalog.getAcroForm(); if (acroForm == null) { doc.close(); return null; } xfa = acroForm.getXFA(); if (xfa == null) { doc.close(); return null; } // TODO return byte[] byte[] xfaBytes = xfa.getBytes(); doc.close(); return xfaBytes; } catch (IOException e) { // handle IOException // happens when the file is corrupt. e.printStackTrace(); System.out.println("XFAUtils-getParsableXFAForm-IOException"); return null; } }
Example 12
Source File: PdfPanel.java From swift-explorer with Apache License 2.0 | 4 votes |
public synchronized void setPdf(PDDocument pdf) { listImagePages.clear(); if (pdf == null) return ; try { if (pdf.isEncrypted()) { logger.info("Failed attempt at previewing an encrypted PDF"); return ; } PDDocumentCatalog cat = pdf.getDocumentCatalog() ; @SuppressWarnings("unchecked") List<PDPage> pages = cat.getAllPages() ; if (pages != null && !pages.isEmpty()) { for (PDPage page : pages) { listImagePages.add(page.convertToImage()) ; if (listImagePages.size() >= maxPageToPreview) break ; } } } catch (IOException e) { logger.error("Error occurred while opening the pdf document", e); } finally { if (pdf != null) { try { pdf.close(); } catch (IOException ex) { logger.error("Error occurred while closing the pdf document", ex); } } } repaint(); }