Search in sources :

Example 6 with PDAppearanceDictionary

use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary in project pdfbox by apache.

the class PDAbstractAppearanceHandler method getRolloverAppearance.

/**
 * Get the annotations rollover appearance.
 *
 * <p>
 * This will get the annotations rollover appearance. If this is not
 * existent an empty appearance entry will be created.
 *
 * @return the appearance entry representing the rollover appearance.
 */
PDAppearanceEntry getRolloverAppearance() {
    PDAppearanceDictionary appearanceDictionary = getAppearance();
    PDAppearanceEntry rolloverAppearanceEntry = appearanceDictionary.getRolloverAppearance();
    if (rolloverAppearanceEntry.isSubDictionary()) {
        // TODO replace with "document.getDocument().createCOSStream()"
        rolloverAppearanceEntry = new PDAppearanceEntry(new COSStream());
        appearanceDictionary.setRolloverAppearance(rolloverAppearanceEntry);
    }
    return rolloverAppearanceEntry;
}
Also used : COSStream(org.apache.pdfbox.cos.COSStream) PDAppearanceEntry(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry) PDAppearanceDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary)

Example 7 with PDAppearanceDictionary

use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary in project pdfbox by apache.

the class PDAbstractAppearanceHandler method getNormalAppearance.

/**
 * Get the annotations normal appearance.
 *
 * <p>
 * This will get the annotations normal appearance. If this is not existent
 * an empty appearance entry will be created.
 *
 * @return the appearance entry representing the normal appearance.
 */
private PDAppearanceEntry getNormalAppearance() {
    PDAppearanceDictionary appearanceDictionary = getAppearance();
    PDAppearanceEntry normalAappearanceEntry = appearanceDictionary.getNormalAppearance();
    if (normalAappearanceEntry.isSubDictionary()) {
        // TODO replace with "document.getDocument().createCOSStream()"
        normalAappearanceEntry = new PDAppearanceEntry(new COSStream());
        appearanceDictionary.setNormalAppearance(normalAappearanceEntry);
    }
    return normalAappearanceEntry;
}
Also used : COSStream(org.apache.pdfbox.cos.COSStream) PDAppearanceEntry(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry) PDAppearanceDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary)

Example 8 with PDAppearanceDictionary

use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary in project pdfbox by apache.

the class PDVisibleSigBuilder method createAppearanceDictionary.

@Override
public void createAppearanceDictionary(PDFormXObject holderForml, PDSignatureField signatureField) throws IOException {
    PDAppearanceDictionary appearance = new PDAppearanceDictionary();
    appearance.getCOSObject().setDirect(true);
    PDAppearanceStream appearanceStream = new PDAppearanceStream(holderForml.getCOSObject());
    appearance.setNormalAppearance(appearanceStream);
    signatureField.getWidgets().get(0).setAppearance(appearance);
    pdfStructure.setAppearanceDictionary(appearance);
    LOG.info("PDF appearance dictionary has been created");
}
Also used : PDAppearanceDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary) PDAppearanceStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)

Example 9 with PDAppearanceDictionary

use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary in project pdfbox by apache.

the class CreateVisibleSignature2 method createVisualSignatureTemplate.

// create a template PDF document with empty signature and return it as a stream.
private InputStream createVisualSignatureTemplate(PDDocument srcDoc, int pageNum, PDRectangle rect) throws IOException {
    try (PDDocument doc = new PDDocument()) {
        PDPage page = new PDPage(srcDoc.getPage(pageNum).getMediaBox());
        doc.addPage(page);
        PDAcroForm acroForm = new PDAcroForm(doc);
        doc.getDocumentCatalog().setAcroForm(acroForm);
        PDSignatureField signatureField = new PDSignatureField(acroForm);
        PDAnnotationWidget widget = signatureField.getWidgets().get(0);
        List<PDField> acroFormFields = acroForm.getFields();
        acroForm.setSignaturesExist(true);
        acroForm.setAppendOnly(true);
        acroForm.getCOSObject().setDirect(true);
        acroFormFields.add(signatureField);
        widget.setRectangle(rect);
        // from PDVisualSigBuilder.createHolderForm()
        PDStream stream = new PDStream(doc);
        PDFormXObject form = new PDFormXObject(stream);
        PDResources res = new PDResources();
        form.setResources(res);
        form.setFormType(1);
        PDRectangle bbox = new PDRectangle(rect.getWidth(), rect.getHeight());
        float height = bbox.getHeight();
        Matrix initialScale = null;
        switch(srcDoc.getPage(pageNum).getRotation()) {
            case 90:
                form.setMatrix(AffineTransform.getQuadrantRotateInstance(1));
                initialScale = Matrix.getScaleInstance(bbox.getWidth() / bbox.getHeight(), bbox.getHeight() / bbox.getWidth());
                height = bbox.getWidth();
                break;
            case 180:
                form.setMatrix(AffineTransform.getQuadrantRotateInstance(2));
                break;
            case 270:
                form.setMatrix(AffineTransform.getQuadrantRotateInstance(3));
                initialScale = Matrix.getScaleInstance(bbox.getWidth() / bbox.getHeight(), bbox.getHeight() / bbox.getWidth());
                height = bbox.getWidth();
                break;
            case 0:
            default:
                break;
        }
        form.setBBox(bbox);
        PDFont font = PDType1Font.HELVETICA_BOLD;
        // from PDVisualSigBuilder.createAppearanceDictionary()
        PDAppearanceDictionary appearance = new PDAppearanceDictionary();
        appearance.getCOSObject().setDirect(true);
        PDAppearanceStream appearanceStream = new PDAppearanceStream(form.getCOSObject());
        appearance.setNormalAppearance(appearanceStream);
        widget.setAppearance(appearance);
        try (PDPageContentStream cs = new PDPageContentStream(doc, appearanceStream)) {
            // why does scale have no effect when done in the form matrix???
            if (initialScale != null) {
                cs.transform(initialScale);
            }
            // show background (just for debugging, to see the rect size + position)
            cs.setNonStrokingColor(Color.yellow);
            cs.addRect(-5000, -5000, 10000, 10000);
            cs.fill();
            // show background image
            // save and restore graphics if the image is too large and needs to be scaled
            cs.saveGraphicsState();
            cs.transform(Matrix.getScaleInstance(0.25f, 0.25f));
            PDImageXObject img = PDImageXObject.createFromFileByExtension(imageFile, doc);
            cs.drawImage(img, 0, 0);
            cs.restoreGraphicsState();
            // show text
            float fontSize = 10;
            float leading = fontSize * 1.5f;
            cs.beginText();
            cs.setFont(font, fontSize);
            cs.setNonStrokingColor(Color.black);
            cs.newLineAtOffset(fontSize, height - leading);
            cs.setLeading(leading);
            cs.showText("(Signature very wide line 1)");
            cs.newLine();
            cs.showText("(Signature very wide line 2)");
            cs.newLine();
            cs.showText("(Signature very wide line 3)");
            cs.endText();
        }
        // no need to set annotations and /P entry
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        doc.save(baos);
        return new ByteArrayInputStream(baos.toByteArray());
    }
}
Also used : PDFont(org.apache.pdfbox.pdmodel.font.PDFont) PDField(org.apache.pdfbox.pdmodel.interactive.form.PDField) PDPage(org.apache.pdfbox.pdmodel.PDPage) PDAppearanceStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDResources(org.apache.pdfbox.pdmodel.PDResources) PDAcroForm(org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm) PDStream(org.apache.pdfbox.pdmodel.common.PDStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PDImageXObject(org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject) Matrix(org.apache.pdfbox.util.Matrix) PDAppearanceDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary) ByteArrayInputStream(java.io.ByteArrayInputStream) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDAnnotationWidget(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDFormXObject(org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) PDPageContentStream(org.apache.pdfbox.pdmodel.PDPageContentStream) PDSignatureField(org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField)

Example 10 with PDAppearanceDictionary

use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary in project pdfbox by apache.

the class RubberStampWithImage method doIt.

/**
 * Add a rubber stamp with an jpg image to every page of the given document.
 * @param args the command line arguments
 * @throws IOException an exception is thrown if something went wrong
 */
public void doIt(String[] args) throws IOException {
    if (args.length != 3) {
        usage();
    } else {
        try (PDDocument document = PDDocument.load(new File(args[0]))) {
            if (document.isEncrypted()) {
                throw new IOException("Encrypted documents are not supported for this example");
            }
            for (int i = 0; i < document.getNumberOfPages(); i++) {
                PDPage page = document.getPage(i);
                List<PDAnnotation> annotations = page.getAnnotations();
                PDAnnotationRubberStamp rubberStamp = new PDAnnotationRubberStamp();
                rubberStamp.setName(PDAnnotationRubberStamp.NAME_TOP_SECRET);
                rubberStamp.setRectangle(new PDRectangle(200, 100));
                rubberStamp.setContents("A top secret note");
                // create a PDXObjectImage with the given image file
                // if you already have the image in a BufferedImage,
                // call LosslessFactory.createFromImage() instead
                PDImageXObject ximage = PDImageXObject.createFromFile(args[2], document);
                // define and set the target rectangle
                int lowerLeftX = 250;
                int lowerLeftY = 550;
                int formWidth = 150;
                int formHeight = 25;
                int imgWidth = 50;
                int imgHeight = 25;
                PDRectangle rect = new PDRectangle();
                rect.setLowerLeftX(lowerLeftX);
                rect.setLowerLeftY(lowerLeftY);
                rect.setUpperRightX(lowerLeftX + formWidth);
                rect.setUpperRightY(lowerLeftY + formHeight);
                // Create a PDFormXObject
                PDFormXObject form = new PDFormXObject(document);
                form.setResources(new PDResources());
                form.setBBox(rect);
                form.setFormType(1);
                // adjust the image to the target rectangle and add it to the stream
                try (OutputStream os = form.getStream().createOutputStream()) {
                    drawXObject(ximage, form.getResources(), os, lowerLeftX, lowerLeftY, imgWidth, imgHeight);
                }
                PDAppearanceStream myDic = new PDAppearanceStream(form.getCOSObject());
                PDAppearanceDictionary appearance = new PDAppearanceDictionary(new COSDictionary());
                appearance.setNormalAppearance(myDic);
                rubberStamp.setAppearance(appearance);
                rubberStamp.setRectangle(rect);
                // add the new RubberStamp to the document
                annotations.add(rubberStamp);
            }
            document.save(args[1]);
        }
    }
}
Also used : PDPage(org.apache.pdfbox.pdmodel.PDPage) COSDictionary(org.apache.pdfbox.cos.COSDictionary) PDAppearanceStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDAnnotation(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation) OutputStream(java.io.OutputStream) PDResources(org.apache.pdfbox.pdmodel.PDResources) IOException(java.io.IOException) PDAnnotationRubberStamp(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationRubberStamp) PDImageXObject(org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject) PDAppearanceDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDFormXObject(org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) File(java.io.File)

Aggregations

PDAppearanceDictionary (org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary)11 PDAppearanceEntry (org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry)5 PDAppearanceStream (org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)4 COSStream (org.apache.pdfbox.cos.COSStream)3 PDResources (org.apache.pdfbox.pdmodel.PDResources)3 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)3 PDAnnotationWidget (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)3 COSDictionary (org.apache.pdfbox.cos.COSDictionary)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2 PDPage (org.apache.pdfbox.pdmodel.PDPage)2 PDFormXObject (org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject)2 PDImageXObject (org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject)2 Matrix (org.apache.pdfbox.util.Matrix)2 Point2D (java.awt.geom.Point2D)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 COSName (org.apache.pdfbox.cos.COSName)1