use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDButtonTest method setUp.
@Before
public void setUp() throws IOException {
document = new PDDocument();
acroForm = new PDAcroForm(document);
acrobatDocument = PDDocument.load(new File(IN_DIR, NAME_OF_PDF));
acrobatAcroForm = acrobatDocument.getDocumentCatalog().getAcroForm();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDButtonTest method testOptionsAndNamesNotNumbers.
@Test
public /**
* PDFBOX-3682
*
* Test a radio button with options.
* Special handling for a radio button with /Opt and the On state not being named
* after the index.
*
* @throws IOException
*/
void testOptionsAndNamesNotNumbers() {
File file;
PDDocument pdfDocument = null;
try {
file = new File(TARGET_PDF_DIR, "PDFBOX-3682.pdf");
assumeTrue(file.exists());
pdfDocument = PDDocument.load(file);
pdfDocument.getDocumentCatalog().getAcroForm().getField("RadioButton").setValue("c");
PDRadioButton radioButton = (PDRadioButton) pdfDocument.getDocumentCatalog().getAcroForm().getField("RadioButton");
radioButton.setValue("c");
// test that the old behavior is now invalid
assertFalse("This shall no longer be 2", "2".equals(radioButton.getValueAsString()));
assertFalse("This shall no longer be 2", "2".equals(radioButton.getWidgets().get(2).getCOSObject().getNameAsString(COSName.AS)));
// test for the correct behavior
assertTrue("This shall be c", "c".equals(radioButton.getValueAsString()));
assertTrue("This shall be c", "c".equals(radioButton.getWidgets().get(2).getCOSObject().getNameAsString(COSName.AS)));
} catch (IOException e) {
fail("Unexpected IOException " + e.getMessage());
} finally {
if (pdfDocument != null) {
try {
pdfDocument.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument 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.PDDocument in project PdfBox-Android by TomRoush.
the class MainActivity method stripText.
/**
* Strips the text from a PDF and displays the text on screen
*/
public void stripText(View v) {
String parsedText = null;
PDDocument document = null;
try {
document = PDDocument.load(assetManager.open("Hello.pdf"));
} catch (IOException e) {
Log.e("PdfBox-Android-Sample", "Exception thrown while loading document to strip", e);
}
try {
PDFTextStripper pdfStripper = new PDFTextStripper();
pdfStripper.setStartPage(0);
pdfStripper.setEndPage(1);
parsedText = "Parsed text: " + pdfStripper.getText(document);
} catch (IOException e) {
Log.e("PdfBox-Android-Sample", "Exception thrown while stripping text", e);
} finally {
try {
if (document != null)
document.close();
} catch (IOException e) {
Log.e("PdfBox-Android-Sample", "Exception thrown while closing document", e);
}
}
tv.setText(parsedText);
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDStructureElementTest method testPDFBox4197.
/**
* PDFBOX-4197: test that object references in array attributes of a PDStructureElement are caught.
*
* @throws IOException
*/
@Test
public // TODO: PdfBox-Android - provide test file
void testPDFBox4197() throws IOException {
File testFile = new File(TARGETPDFDIR, "PDFBOX-4197.pdf");
assumeTrue(testFile.exists());
PDDocument doc = PDDocument.load(testFile);
PDStructureTreeRoot structureTreeRoot = doc.getDocumentCatalog().getStructureTreeRoot();
Set<Revisions<PDAttributeObject>> attributeSet = new HashSet<Revisions<PDAttributeObject>>();
checkElement(structureTreeRoot.getK(), attributeSet);
doc.close();
// collect attributes and check their count.
Assert.assertEquals(117, attributeSet.size());
int cnt = 0;
for (Revisions<PDAttributeObject> attributes : attributeSet) {
cnt += attributes.size();
}
// this one was 105 before PDFBOX-4197 was fixed
Assert.assertEquals(111, cnt);
}
Aggregations