Search in sources :

Example 1 with PDDocumentCatalog

use of com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog in project PdfBox-Android by TomRoush.

the class LayerUtility method appendFormAsLayer.

/**
 * Places the given form over the existing content of the indicated page (like an overlay).
 * The form is enveloped in a marked content section to indicate that it's part of an
 * optional content group (OCG), here used as a layer. This optional group is returned and
 * can be enabled and disabled through methods on {@link PDOptionalContentProperties}.
 * <p>
 * You may want to call {@link #wrapInSaveRestore(PDPage) wrapInSaveRestore(PDPage)} before calling this method to make
 * sure that the graphics state is reset.
 *
 * @param targetPage the target page
 * @param form the form to place
 * @param transform the transformation matrix that controls the placement of your form. You'll
 * need this if your page has a crop box different than the media box, or if these have negative
 * coordinates, or if you want to scale or adjust your form.
 * @param layerName the name for the layer/OCG to produce
 * @return the optional content group that was generated for the form usage
 * @throws IOException if an I/O error occurs
 */
public PDOptionalContentGroup appendFormAsLayer(PDPage targetPage, PDFormXObject form, AffineTransform transform, String layerName) throws IOException {
    PDDocumentCatalog catalog = targetDoc.getDocumentCatalog();
    PDOptionalContentProperties ocprops = catalog.getOCProperties();
    if (ocprops == null) {
        ocprops = new PDOptionalContentProperties();
        catalog.setOCProperties(ocprops);
    }
    if (ocprops.hasGroup(layerName)) {
        throw new IllegalArgumentException("Optional group (layer) already exists: " + layerName);
    }
    PDRectangle cropBox = targetPage.getCropBox();
    if ((cropBox.getLowerLeftX() < 0 || cropBox.getLowerLeftY() < 0) && transform.isIdentity()) {
        // PDFBOX-4044
        Log.w("PdfBox-Android", "Negative cropBox " + cropBox + " and identity transform may make your form invisible");
    }
    PDOptionalContentGroup layer = new PDOptionalContentGroup(layerName);
    ocprops.addGroup(layer);
    PDPageContentStream contentStream = new PDPageContentStream(targetDoc, targetPage, AppendMode.APPEND, !DEBUG);
    contentStream.beginMarkedContent(COSName.OC, layer);
    contentStream.saveGraphicsState();
    contentStream.transform(new Matrix(transform));
    contentStream.drawForm(form);
    contentStream.restoreGraphicsState();
    contentStream.endMarkedContent();
    contentStream.close();
    return layer;
}
Also used : Matrix(com.tom_roush.pdfbox.util.Matrix) PDOptionalContentGroup(com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentGroup) PDOptionalContentProperties(com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle) PDPageContentStream(com.tom_roush.pdfbox.pdmodel.PDPageContentStream) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)

Example 2 with PDDocumentCatalog

use of com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog in project PdfBox-Android by TomRoush.

the class LayerUtility method importOcProperties.

/**
 * Imports OCProperties from source document to target document so hidden layers can still be
 * hidden after import.
 *
 * @param sourceDoc The source PDF document that contains the /OCProperties to be copied.
 * @throws IOException If an I/O error occurs.
 */
private void importOcProperties(PDDocument srcDoc) throws IOException {
    PDDocumentCatalog srcCatalog = srcDoc.getDocumentCatalog();
    PDOptionalContentProperties srcOCProperties = srcCatalog.getOCProperties();
    if (srcOCProperties == null) {
        return;
    }
    PDDocumentCatalog dstCatalog = targetDoc.getDocumentCatalog();
    PDOptionalContentProperties dstOCProperties = dstCatalog.getOCProperties();
    if (dstOCProperties == null) {
        dstCatalog.setOCProperties(new PDOptionalContentProperties((COSDictionary) cloner.cloneForNewDocument(srcOCProperties)));
    } else {
        cloner.cloneMerge(srcOCProperties, dstOCProperties);
    }
}
Also used : COSDictionary(com.tom_roush.pdfbox.cos.COSDictionary) PDOptionalContentProperties(com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)

Example 3 with PDDocumentCatalog

use of com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog 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 4 with PDDocumentCatalog

use of com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog 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 5 with PDDocumentCatalog

use of com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog in project PdfBox-Android by TomRoush.

the class TestEmbeddedFiles method testNullEmbeddedFile.

@Test
public void testNullEmbeddedFile() throws IOException {
    PDEmbeddedFile embeddedFile = null;
    boolean ok = false;
    try {
        PDDocument doc = PDDocument.load(getClass().getResourceAsStream("/pdfbox/com/tom_roush/pdfbox/pdmodel/common/null_PDComplexFileSpecification.pdf"));
        PDDocumentCatalog catalog = doc.getDocumentCatalog();
        PDDocumentNameDictionary names = catalog.getNames();
        assertEquals("expected two files", 2, names.getEmbeddedFiles().getNames().size());
        PDEmbeddedFilesNameTreeNode embeddedFiles = names.getEmbeddedFiles();
        PDComplexFileSpecification spec = embeddedFiles.getNames().get("non-existent-file.docx");
        if (spec != null) {
            embeddedFile = spec.getEmbeddedFile();
            ok = true;
        }
        // now test for actual attachment
        spec = embeddedFiles.getNames().get("My first attachment");
        assertNotNull("one attachment actually exists", spec);
        assertEquals("existing file length", 17660, spec.getEmbeddedFile().getLength());
        spec = embeddedFiles.getNames().get("non-existent-file.docx");
    } catch (NullPointerException e) {
        assertNotNull("null pointer exception", null);
    }
    assertTrue("Was able to get file without exception", ok);
    assertNull("EmbeddedFile was correctly null", embeddedFile);
}
Also used : PDEmbeddedFilesNameTreeNode(com.tom_roush.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode) PDEmbeddedFile(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDComplexFileSpecification(com.tom_roush.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog) PDDocumentNameDictionary(com.tom_roush.pdfbox.pdmodel.PDDocumentNameDictionary) Test(org.junit.Test)

Aggregations

PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)14 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)10 Test (org.junit.Test)6 PDDocumentNameDictionary (com.tom_roush.pdfbox.pdmodel.PDDocumentNameDictionary)5 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)5 File (java.io.File)5 PDEmbeddedFilesNameTreeNode (com.tom_roush.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode)4 PDComplexFileSpecification (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification)4 PDEmbeddedFile (com.tom_roush.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile)4 IOException (java.io.IOException)4 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)3 PDResources (com.tom_roush.pdfbox.pdmodel.PDResources)3 PDOptionalContentProperties (com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties)3 COSName (com.tom_roush.pdfbox.cos.COSName)2 COSObjectable (com.tom_roush.pdfbox.pdmodel.common.COSObjectable)2 PDNumberTreeNode (com.tom_roush.pdfbox.pdmodel.common.PDNumberTreeNode)2 PDOptionalContentGroup (com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentGroup)2 PDAnnotation (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotation)2 PDPageDestination (com.tom_roush.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination)2 PDAcroForm (com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm)2