Search in sources :

Example 6 with PDDocument

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();
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with PDDocument

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);
    }
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) ArrayList(java.util.ArrayList) File(java.io.File) Test(org.junit.Test)

Example 8 with PDDocument

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();
}
Also used : PDFont(com.tom_roush.pdfbox.pdmodel.font.PDFont) PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 9 with PDDocument

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;
    }
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDResources(com.tom_roush.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog) Test(org.junit.Test)

Example 10 with PDDocument

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();
            }
        }
    }
}
Also used : PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Aggregations

PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)137 File (java.io.File)80 Test (org.junit.Test)69 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)37 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)22 InputStream (java.io.InputStream)21 Bitmap (android.graphics.Bitmap)18 IOException (java.io.IOException)14 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)11 PDFRenderer (com.tom_roush.pdfbox.rendering.PDFRenderer)11 ArrayList (java.util.ArrayList)11 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileInputStream (java.io.FileInputStream)9 FileOutputStream (java.io.FileOutputStream)9 COSArray (com.tom_roush.pdfbox.cos.COSArray)8 PDEmbeddedFile (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile)8 PDFont (com.tom_roush.pdfbox.pdmodel.font.PDFont)8 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)7 Paint (android.graphics.Paint)6