Java Code Examples for org.apache.pdfbox.pdmodel.PDDocumentCatalog#getAcroForm()

The following examples show how to use org.apache.pdfbox.pdmodel.PDDocumentCatalog#getAcroForm() . 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 vote down vote up
/**
 * 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: FillAndFlatten.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/60964782/pdfbox-inconsistent-pdtextfield-behaviour-after-setvalue">
 * PDFBox Inconsistent PDTextField Behaviour after setValue
 * </a>
 * </br/>
 * <a href="https://s3-us-west-2.amazonaws.com/kx-filing-docs/b3-3.pdf">
 * b3-3.pdf
 * </a>
 * <p>
 * Indeed, PDFBox assumes in some fields that it should not create
 * field appearances which a formatting additional action would do
 * differently anyways in a viewer. In a flattening use case this is
 * obviously incorrect.
 * </p>
 * @see #testLikeAbubakarRemoveAction()
 */
@Test
public void testLikeAbubakar() throws IOException {
    try (   InputStream resource = getClass().getResourceAsStream("b3-3.pdf");
            PDDocument pdDocument = Loader.loadPDF(resource)    ) {
        PDDocumentCatalog catalog = pdDocument.getDocumentCatalog();
        PDAcroForm acroForm = catalog.getAcroForm();
        int i = 0;
        for (PDField field : acroForm.getFields()) {
            i=i+1;
            if (field instanceof PDTextField) {
                PDTextField textField = (PDTextField) field;
                textField.setValue(Integer.toString(i));
            }
        }

        pdDocument.getDocumentCatalog().getAcroForm().flatten();

        pdDocument.save(new File(RESULT_FOLDER, "b3-3-filled-and-flattened.pdf"));
    }
}
 
Example 3
Source File: FillAndFlatten.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/60964782/pdfbox-inconsistent-pdtextfield-behaviour-after-setvalue">
 * PDFBox Inconsistent PDTextField Behaviour after setValue
 * </a>
 * </br/>
 * <a href="https://s3-us-west-2.amazonaws.com/kx-filing-docs/b3-3.pdf">
 * b3-3.pdf
 * </a>
 * <p>
 * After removing the actions, PDFBox again sets appearances in
 * all fields.
 * </p>
 * @see #testLikeAbubakar()
 */
@Test
public void testLikeAbubakarRemoveAction() throws IOException {
    try (   InputStream resource = getClass().getResourceAsStream("b3-3.pdf");
            PDDocument pdDocument = Loader.loadPDF(resource)    ) {
        PDDocumentCatalog catalog = pdDocument.getDocumentCatalog();
        PDAcroForm acroForm = catalog.getAcroForm();
        int i = 0;
        for (PDField field : acroForm.getFields()) {
            i=i+1;
            if (field instanceof PDTextField) {
                PDTextField textField = (PDTextField) field;
                textField.setActions(null);
                textField.setValue(Integer.toString(i));
            }
        }

        pdDocument.getDocumentCatalog().getAcroForm().flatten();

        pdDocument.save(new File(RESULT_FOLDER, "b3-3-remove-action-filled-and-flattened.pdf"));
    }
}
 
Example 4
Source File: FillInForm.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <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 5
Source File: PDFMergerUtility.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Merge the contents of the source form into the destination form for the
 * destination file.
 *
 * @param cloner the object cloner for the destination document
 * @param destAcroForm the destination form
 * @param srcAcroForm the source form
 * @throws IOException If an error occurs while adding the field.
 */
private void mergeAcroForm(PDFCloneUtility cloner, PDDocumentCatalog destCatalog,
        PDDocumentCatalog srcCatalog ) throws IOException
{
    try
    {
        PDAcroForm destAcroForm = destCatalog.getAcroForm();
        PDAcroForm srcAcroForm = srcCatalog.getAcroForm();
        
        if (destAcroForm == null && srcAcroForm != null)
        {
            destCatalog.getCOSObject().setItem(COSName.ACRO_FORM,
                    cloner.cloneForNewDocument(srcAcroForm.getCOSObject()));       
            
        }
        else
        {
            if (srcAcroForm != null)
            {
                if (acroFormMergeMode == AcroFormMergeMode.PDFBOX_LEGACY_MODE)
                {
                    acroFormLegacyMode(cloner, destAcroForm, srcAcroForm);
                }
                else if (acroFormMergeMode == AcroFormMergeMode.JOIN_FORM_FIELDS_MODE)
                {
                    acroFormJoinFieldsMode(cloner, destAcroForm, srcAcroForm);
                }
            }
        }
    }
    catch (IOException e)
    {
        // if we are not ignoring exceptions, we'll re-throw this
        if (!ignoreAcroFormErrors)
        {
            throw new IOException(e);
        }
    }
}
 
Example 6
Source File: AddFormField.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
     * <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 7
Source File: RightAlignField.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <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 8
Source File: RightAlignField.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <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 9
Source File: ReadXfaForm.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
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;
    }
}