Search in sources :

Example 6 with PDField

use of com.tom_roush.pdfbox.pdmodel.interactive.form.PDField in project PdfBox-Android by TomRoush.

the class MainActivity method fillForm.

/**
 * Fills in a PDF form and saves the result
 */
public void fillForm(View v) {
    try {
        // Load the document and get the AcroForm
        PDDocument document = PDDocument.load(assetManager.open("FormTest.pdf"));
        PDDocumentCatalog docCatalog = document.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();
        // Fill the text field
        PDTextField field = (PDTextField) acroForm.getField("TextField");
        field.setValue("Filled Text Field");
        // Optional: don't allow this field to be edited
        field.setReadOnly(true);
        PDField checkbox = acroForm.getField("Checkbox");
        ((PDCheckBox) checkbox).check();
        PDField radio = acroForm.getField("Radio");
        ((PDRadioButton) radio).setValue("Second");
        PDField listbox = acroForm.getField("ListBox");
        List<Integer> listValues = new ArrayList<>();
        listValues.add(1);
        listValues.add(2);
        ((PDListBox) listbox).setSelectedOptionsIndex(listValues);
        PDField dropdown = acroForm.getField("Dropdown");
        ((PDComboBox) dropdown).setValue("Hello");
        String path = root.getAbsolutePath() + "/FilledForm.pdf";
        tv.setText("Saved filled form to " + path);
        document.save(path);
        document.close();
    } catch (IOException e) {
        Log.e("PdfBox-Android-Sample", "Exception thrown while filling form fields", e);
    }
}
Also used : PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) PDCheckBox(com.tom_roush.pdfbox.pdmodel.interactive.form.PDCheckBox) ArrayList(java.util.ArrayList) PDComboBox(com.tom_roush.pdfbox.pdmodel.interactive.form.PDComboBox) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) IOException(java.io.IOException) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog) PDRadioButton(com.tom_roush.pdfbox.pdmodel.interactive.form.PDRadioButton) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDTextField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDTextField) PDListBox(com.tom_roush.pdfbox.pdmodel.interactive.form.PDListBox)

Example 7 with PDField

use of com.tom_roush.pdfbox.pdmodel.interactive.form.PDField in project PdfBox-Android by TomRoush.

the class PDFMergerUtilityTest method checkWithNumberTree.

/**
 * PDFBOX-4408: Check that /StructParents values from pages and /StructParent values from
 * annotations are found in the /ParentTree.
 *
 * @param document
 */
void checkWithNumberTree(PDDocument document) throws IOException {
    PDDocumentCatalog documentCatalog = document.getDocumentCatalog();
    PDNumberTreeNode parentTree = documentCatalog.getStructureTreeRoot().getParentTree();
    Map<Integer, COSObjectable> numberTreeAsMap = PDFMergerUtility.getNumberTreeAsMap(parentTree);
    Set<Integer> keySet = numberTreeAsMap.keySet();
    PDAcroForm acroForm = documentCatalog.getAcroForm();
    if (acroForm != null) {
        for (PDField field : acroForm.getFieldTree()) {
            for (PDAnnotationWidget widget : field.getWidgets()) {
                if (widget.getStructParent() >= 0) {
                    assertTrue("field '" + field.getFullyQualifiedName() + "' /StructParent " + widget.getStructParent() + " missing in /ParentTree", keySet.contains(widget.getStructParent()));
                }
            }
        }
    }
    for (PDPage page : document.getPages()) {
        if (page.getStructParents() >= 0) {
            assertTrue(keySet.contains(page.getStructParents()));
        }
        for (PDAnnotation ann : page.getAnnotations()) {
            if (ann.getStructParent() >= 0) {
                assertTrue("/StructParent " + ann.getStructParent() + " missing in /ParentTree", keySet.contains(ann.getStructParent()));
            }
        }
    }
// might also test image and form dictionaries...
}
Also used : PDNumberTreeNode(com.tom_roush.pdfbox.pdmodel.common.PDNumberTreeNode) COSObjectable(com.tom_roush.pdfbox.pdmodel.common.COSObjectable) PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDAnnotation(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)

Example 8 with PDField

use of com.tom_roush.pdfbox.pdmodel.interactive.form.PDField in project PdfBox-Android by TomRoush.

the class MergeAcroFormsTest method testLegacyModeMerge.

/*
     * Test LegacyMode merge
     */
@Test
public void testLegacyModeMerge() throws IOException {
    PDFMergerUtility merger = new PDFMergerUtility();
    File toBeMerged = new File(IN_DIR, "AcroFormForMerge.pdf");
    File pdfOutput = new File(OUT_DIR, "PDFBoxLegacyMerge-SameMerged.pdf");
    merger.setDestinationFileName(pdfOutput.getAbsolutePath());
    merger.addSource(toBeMerged);
    merger.addSource(toBeMerged);
    merger.mergeDocuments(null);
    merger.setAcroFormMergeMode(AcroFormMergeMode.PDFBOX_LEGACY_MODE);
    PDDocument compliantDocument = null;
    PDDocument toBeCompared = null;
    try {
        compliantDocument = PDDocument.load(new File(IN_DIR, "PDFBoxLegacyMerge-SameMerged.pdf"));
        toBeCompared = PDDocument.load(new File(OUT_DIR, "PDFBoxLegacyMerge-SameMerged.pdf"));
        PDAcroForm compliantAcroForm = compliantDocument.getDocumentCatalog().getAcroForm();
        PDAcroForm toBeComparedAcroForm = toBeCompared.getDocumentCatalog().getAcroForm();
        assertEquals("There shall be the same number of root fields", compliantAcroForm.getFields().size(), toBeComparedAcroForm.getFields().size());
        for (PDField compliantField : compliantAcroForm.getFieldTree()) {
            assertNotNull("There shall be a field with the same FQN", toBeComparedAcroForm.getField(compliantField.getFullyQualifiedName()));
            PDField toBeComparedField = toBeComparedAcroForm.getField(compliantField.getFullyQualifiedName());
            compareFieldProperties(compliantField, toBeComparedField);
        }
        for (PDField toBeComparedField : toBeComparedAcroForm.getFieldTree()) {
            assertNotNull("There shall be a field with the same FQN", compliantAcroForm.getField(toBeComparedField.getFullyQualifiedName()));
            PDField compliantField = compliantAcroForm.getField(toBeComparedField.getFullyQualifiedName());
            compareFieldProperties(toBeComparedField, compliantField);
        }
    } finally {
        IOUtils.closeQuietly(compliantDocument);
        IOUtils.closeQuietly(toBeCompared);
    }
}
Also used : PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) File(java.io.File) Test(org.junit.Test)

Example 9 with PDField

use of com.tom_roush.pdfbox.pdmodel.interactive.form.PDField in project PdfBox-Android by TomRoush.

the class MergeAcroFormsTest method testAPEntry.

/*
     * PDFBOX-1100 Ensure that after merging the PDFs there is an AP and V entry.
     */
@Test
public void testAPEntry() throws IOException {
    InputStream is1 = null;
    InputStream is2 = null;
    // Merge the PDFs form PDFBOX-1100
    PDFMergerUtility merger = new PDFMergerUtility();
    try {
        File file1 = new File(TARGET_PDF_DIR, "PDFBOX-1100-1.pdf");
        assumeTrue(file1.exists());
        is1 = new FileInputStream(file1);
        File file2 = new File(TARGET_PDF_DIR, "PDFBOX-1100-2.pdf");
        assumeTrue(file2.exists());
        is2 = new FileInputStream(file2);
        File pdfOutput = new File(OUT_DIR, "PDFBOX-1100.pdf");
        merger.setDestinationFileName(pdfOutput.getAbsolutePath());
        merger.addSource(is1);
        merger.addSource(is2);
        merger.mergeDocuments(null);
        // Test merge result
        PDDocument mergedPDF = PDDocument.load(pdfOutput);
        assertEquals("There shall be 2 pages", 2, mergedPDF.getNumberOfPages());
        PDAcroForm acroForm = mergedPDF.getDocumentCatalog().getAcroForm();
        PDField formField = acroForm.getField("Testfeld");
        assertNotNull("There shall be an /AP entry for the field", formField.getCOSObject().getDictionaryObject(COSName.AP));
        assertNotNull("There shall be a /V entry for the field", formField.getCOSObject().getDictionaryObject(COSName.V));
        formField = acroForm.getField("Testfeld2");
        assertNotNull("There shall be an /AP entry for the field", formField.getCOSObject().getDictionaryObject(COSName.AP));
        assertNotNull("There shall be a /V entry for the field", formField.getCOSObject().getDictionaryObject(COSName.V));
        mergedPDF.close();
    } finally {
        IOUtils.closeQuietly(is1);
        IOUtils.closeQuietly(is2);
    }
}
Also used : PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 10 with PDField

use of com.tom_roush.pdfbox.pdmodel.interactive.form.PDField in project PdfBox-Android by TomRoush.

the class PDVisibleSigBuilder method createAcroFormDictionary.

@Override
public void createAcroFormDictionary(PDAcroForm acroForm, PDSignatureField signatureField) throws IOException {
    @SuppressWarnings("unchecked") List<PDField> acroFormFields = acroForm.getFields();
    COSDictionary acroFormDict = acroForm.getCOSObject();
    acroForm.setSignaturesExist(true);
    acroForm.setAppendOnly(true);
    acroFormDict.setDirect(true);
    acroFormFields.add(signatureField);
    acroForm.setDefaultAppearance("/sylfaen 0 Tf 0 g");
    pdfStructure.setAcroFormFields(acroFormFields);
    pdfStructure.setAcroFormDictionary(acroFormDict);
    Log.i("PdfBox-Android", "AcroForm dictionary has been created");
}
Also used : PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary)

Aggregations

PDField (com.tom_roush.pdfbox.pdmodel.interactive.form.PDField)11 PDAcroForm (com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm)8 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)4 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)4 COSArray (com.tom_roush.pdfbox.cos.COSArray)3 PDSignatureField (com.tom_roush.pdfbox.pdmodel.interactive.form.PDSignatureField)3 File (java.io.File)3 Test (org.junit.Test)3 COSBase (com.tom_roush.pdfbox.cos.COSBase)2 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)2 COSArrayList (com.tom_roush.pdfbox.pdmodel.common.COSArrayList)2 PDAnnotation (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation)2 PDAnnotationWidget (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)2 ArrayList (java.util.ArrayList)2 COSDocument (com.tom_roush.pdfbox.cos.COSDocument)1 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)1 COSObjectable (com.tom_roush.pdfbox.pdmodel.common.COSObjectable)1 PDNumberTreeNode (com.tom_roush.pdfbox.pdmodel.common.PDNumberTreeNode)1 PDCheckBox (com.tom_roush.pdfbox.pdmodel.interactive.form.PDCheckBox)1 PDComboBox (com.tom_roush.pdfbox.pdmodel.interactive.form.PDComboBox)1