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);
}
}
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...
}
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);
}
}
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);
}
}
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");
}
Aggregations