use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDAcroFormTest method createAcroFormWithMissingResourceInformation.
private byte[] createAcroFormWithMissingResourceInformation() throws IOException {
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDAcroForm newAcroForm = new PDAcroForm(document);
document.getDocumentCatalog().setAcroForm(newAcroForm);
PDTextField textBox = new PDTextField(newAcroForm);
textBox.setPartialName("SampleField");
newAcroForm.getFields().add(textBox);
PDAnnotationWidget widget = textBox.getWidgets().get(0);
PDRectangle rect = new PDRectangle(50, 750, 200, 20);
widget.setRectangle(rect);
widget.setPage(page);
page.getAnnotations().add(widget);
// acroForm.setNeedAppearances(true);
// acroForm.getField("SampleField").getCOSObject().setString(COSName.V, "content");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// this is a working PDF
document.save(baos);
document.close();
return baos.toByteArray();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDAcroFormTest method testFlattenSpecificFieldsOnly.
// testFlatten is an instrumentation test
// testFlattenWidgetNoRef is an instrumentation test
@Test
public void testFlattenSpecificFieldsOnly() throws IOException {
File file = new File(OUT_DIR, "AlignmentTests-flattened-specificFields.pdf");
List<PDField> fieldsToFlatten = new ArrayList<PDField>();
PDDocument testPdf = null;
try {
testPdf = PDDocument.load(new File(IN_DIR, "AlignmentTests.pdf"));
PDAcroForm acroFormToFlatten = testPdf.getDocumentCatalog().getAcroForm();
int numFieldsBeforeFlatten = acroFormToFlatten.getFields().size();
int numWidgetsBeforeFlatten = countWidgets(testPdf);
fieldsToFlatten.add(acroFormToFlatten.getField("AlignLeft-Border_Small-Filled"));
fieldsToFlatten.add(acroFormToFlatten.getField("AlignLeft-Border_Medium-Filled"));
fieldsToFlatten.add(acroFormToFlatten.getField("AlignLeft-Border_Wide-Filled"));
fieldsToFlatten.add(acroFormToFlatten.getField("AlignLeft-Border_Wide_Clipped-Filled"));
acroFormToFlatten.flatten(fieldsToFlatten, true);
int numFieldsAfterFlatten = acroFormToFlatten.getFields().size();
int numWidgetsAfterFlatten = countWidgets(testPdf);
assertEquals(numFieldsBeforeFlatten, numFieldsAfterFlatten + fieldsToFlatten.size());
assertEquals(numWidgetsBeforeFlatten, numWidgetsAfterFlatten + fieldsToFlatten.size());
testPdf.save(file);
} finally {
IOUtils.closeQuietly(testPdf);
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDAcroFormTest method testAcroFormDefaultFonts.
/**
* PDFBOX-3732, PDFBOX-4303, PDFBOX-4393: Test whether /Helv and /ZaDb get added, but only if
* they don't exist.
*/
@Test
public void testAcroFormDefaultFonts() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PDDocument doc = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
doc.addPage(page);
PDAcroForm acroForm2 = new PDAcroForm(doc);
doc.getDocumentCatalog().setAcroForm(acroForm2);
PDResources defaultResources = acroForm2.getDefaultResources();
assertNull(defaultResources);
defaultResources = new PDResources();
acroForm2.setDefaultResources(defaultResources);
assertNull(defaultResources.getFont(COSName.HELV));
assertNull(defaultResources.getFont(COSName.ZA_DB));
// getting AcroForm sets the two fonts
acroForm2 = doc.getDocumentCatalog().getAcroForm();
defaultResources = acroForm2.getDefaultResources();
assertNotNull(defaultResources.getFont(COSName.HELV));
assertNotNull(defaultResources.getFont(COSName.ZA_DB));
// repeat with a new AcroForm (to delete AcroForm cache) and thus missing /DR
doc.getDocumentCatalog().setAcroForm(new PDAcroForm(doc));
acroForm2 = doc.getDocumentCatalog().getAcroForm();
defaultResources = acroForm2.getDefaultResources();
PDFont helv = defaultResources.getFont(COSName.HELV);
PDFont zadb = defaultResources.getFont(COSName.ZA_DB);
assertNotNull(helv);
assertNotNull(zadb);
doc.save(baos);
doc.close();
doc = PDDocument.load(baos.toByteArray());
acroForm2 = doc.getDocumentCatalog().getAcroForm();
defaultResources = acroForm2.getDefaultResources();
helv = defaultResources.getFont(COSName.HELV);
zadb = defaultResources.getFont(COSName.ZA_DB);
assertNotNull(helv);
assertNotNull(zadb);
// make sure that font wasn't overwritten
assertNotEquals(PDType1Font.HELVETICA, helv);
assertNotEquals(PDType1Font.ZAPF_DINGBATS, zadb);
doc.close();
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDAcroFormTest method testAddMissingInformationOnAcroFormAccess.
/*
* Test that we add missing ressouce information to an AcroForm
* when accessing the AcroForm on the PD level
* (PDFBOX-3752)
*/
@Test
public void testAddMissingInformationOnAcroFormAccess() {
try {
byte[] pdfBytes = createAcroFormWithMissingResourceInformation();
PDDocument pdfDocument = PDDocument.load(pdfBytes);
PDDocumentCatalog documentCatalog = pdfDocument.getDocumentCatalog();
// this call shall trigger the generation of missing information
PDAcroForm theAcroForm = documentCatalog.getAcroForm();
// ensure that the missing information has been generated
// DA entry
assertEquals("/Helv 0 Tf 0 g ", theAcroForm.getDefaultAppearance());
assertNotNull(theAcroForm.getDefaultResources());
// DR entry
PDResources acroFormResources = theAcroForm.getDefaultResources();
assertNotNull(acroFormResources.getFont(COSName.getPDFName("Helv")));
assertEquals("Helvetica", acroFormResources.getFont(COSName.getPDFName("Helv")).getName());
assertNotNull(acroFormResources.getFont(COSName.getPDFName("ZaDb")));
assertEquals("ZapfDingbats", acroFormResources.getFont(COSName.getPDFName("ZaDb")).getName());
pdfDocument.close();
} catch (IOException e) {
System.err.println("Couldn't create test document, test skipped");
return;
}
}
use of com.tom_roush.pdfbox.pdmodel.PDDocument in project PdfBox-Android by TomRoush.
the class PDButtonTest method testRadioButtonWithOptions.
@Test
public /**
* PDFBOX-3656
*
* Test a radio button with options.
* This was causing an ArrayIndexOutOfBoundsException when trying to set to "Off", as this
* wasn't treated to be a valid option.
*
* @throws IOException
*/
void testRadioButtonWithOptions() {
File file;
PDDocument pdfDocument = null;
try {
file = new File(TARGET_PDF_DIR, "PDFBOX-3656.pdf");
assumeTrue(file.exists());
pdfDocument = PDDocument.load(file);
PDRadioButton radioButton = (PDRadioButton) pdfDocument.getDocumentCatalog().getAcroForm().getField("Checking/Savings");
radioButton.setValue("Off");
for (PDAnnotationWidget widget : radioButton.getWidgets()) {
assertEquals("The widget should be set to Off", COSName.Off, widget.getCOSObject().getItem(COSName.AS));
}
} catch (IOException e) {
fail("Unexpected IOException " + e.getMessage());
} finally {
if (pdfDocument != null) {
try {
pdfDocument.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Aggregations