Search in sources :

Example 1 with PDOptionalContentProperties

use of com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties 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 PDOptionalContentProperties

use of com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties 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 PDOptionalContentProperties

use of com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties in project PdfBox-Android by TomRoush.

the class TestLayerUtility method testLayerImport.

/**
 * Tests layer import.
 *
 * @throws Exception if an error occurs
 */
public void testLayerImport() throws Exception {
    File mainPDF = createMainPDF();
    File overlay1 = createOverlay1();
    File targetFile = new File(testResultsDir, "text-with-form-overlay.pdf");
    PDDocument targetDoc = PDDocument.load(mainPDF);
    PDDocument overlay1Doc = PDDocument.load(overlay1);
    try {
        LayerUtility layerUtil = new LayerUtility(targetDoc);
        PDFormXObject form = layerUtil.importPageAsForm(overlay1Doc, 0);
        PDPage targetPage = targetDoc.getPage(0);
        layerUtil.wrapInSaveRestore(targetPage);
        AffineTransform at = new AffineTransform();
        layerUtil.appendFormAsLayer(targetPage, form, at, "overlay");
        targetDoc.save(targetFile.getAbsolutePath());
    } finally {
        targetDoc.close();
        overlay1Doc.close();
    }
    PDDocument doc = PDDocument.load(targetFile);
    try {
        PDDocumentCatalog catalog = doc.getDocumentCatalog();
        // OCGs require PDF 1.5 or later
        assertEquals(1.5f, doc.getVersion());
        PDPage page = doc.getPage(0);
        PDOptionalContentGroup ocg = (PDOptionalContentGroup) page.getResources().getProperties(COSName.getPDFName("oc1"));
        assertNotNull(ocg);
        assertEquals("overlay", ocg.getName());
        PDOptionalContentProperties ocgs = catalog.getOCProperties();
        PDOptionalContentGroup overlay = ocgs.getGroup("overlay");
        assertEquals(ocg.getName(), overlay.getName());
    } finally {
        doc.close();
    }
}
Also used : PDPage(com.tom_roush.pdfbox.pdmodel.PDPage) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDOptionalContentGroup(com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentGroup) PDFormXObject(com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject) AffineTransform(com.tom_roush.harmony.awt.geom.AffineTransform) PDOptionalContentProperties(com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties) File(java.io.File) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)

Aggregations

PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)3 PDOptionalContentProperties (com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentProperties)3 PDOptionalContentGroup (com.tom_roush.pdfbox.pdmodel.graphics.optionalcontent.PDOptionalContentGroup)2 AffineTransform (com.tom_roush.harmony.awt.geom.AffineTransform)1 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)1 PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)1 PDPage (com.tom_roush.pdfbox.pdmodel.PDPage)1 PDPageContentStream (com.tom_roush.pdfbox.pdmodel.PDPageContentStream)1 PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)1 PDFormXObject (com.tom_roush.pdfbox.pdmodel.graphics.form.PDFormXObject)1 Matrix (com.tom_roush.pdfbox.util.Matrix)1 File (java.io.File)1