Search in sources :

Example 1 with PDAnnotationRubberStamp

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

the class TestPDPageAnnotationsFiltering method initMock.

@Before
public void initMock() {
    COSDictionary mockedPageWithAnnotations = new COSDictionary();
    COSArray annotsDictionnary = new COSArray();
    annotsDictionnary.add(new PDAnnotationRubberStamp().getCOSObject());
    annotsDictionnary.add(new PDAnnotationSquare().getCOSObject());
    annotsDictionnary.add(new PDAnnotationLink().getCOSObject());
    mockedPageWithAnnotations.setItem(COSName.ANNOTS, annotsDictionnary);
    page = new PDPage(mockedPageWithAnnotations);
}
Also used : PDAnnotationRubberStamp(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationRubberStamp) PDAnnotationSquare(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquare) COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSArray(org.apache.pdfbox.cos.COSArray) PDAnnotationLink(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink) Before(org.junit.Before)

Example 2 with PDAnnotationRubberStamp

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

the class RubberStamp method main.

/**
 * This will print the documents data.
 *
 * @param args The command line arguments.
 *
 * @throws IOException If there is an error parsing the document.
 */
public static void main(String[] args) throws IOException {
    if (args.length != 2) {
        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 (PDPage page : document.getPages()) {
                List<PDAnnotation> annotations = page.getAnnotations();
                PDAnnotationRubberStamp rs = new PDAnnotationRubberStamp();
                rs.setName(PDAnnotationRubberStamp.NAME_TOP_SECRET);
                rs.setRectangle(new PDRectangle(100, 100));
                rs.setContents("A top secret note");
                annotations.add(rs);
            }
            document.save(args[1]);
        }
    }
}
Also used : PDAnnotationRubberStamp(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationRubberStamp) PDPage(org.apache.pdfbox.pdmodel.PDPage) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDAnnotation(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) IOException(java.io.IOException) File(java.io.File)

Example 3 with PDAnnotationRubberStamp

use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationRubberStamp 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

PDAnnotationRubberStamp (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationRubberStamp)3 File (java.io.File)2 IOException (java.io.IOException)2 COSDictionary (org.apache.pdfbox.cos.COSDictionary)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2 PDPage (org.apache.pdfbox.pdmodel.PDPage)2 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)2 PDAnnotation (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation)2 OutputStream (java.io.OutputStream)1 COSArray (org.apache.pdfbox.cos.COSArray)1 PDResources (org.apache.pdfbox.pdmodel.PDResources)1 PDFormXObject (org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject)1 PDImageXObject (org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject)1 PDAnnotationLink (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationLink)1 PDAnnotationSquare (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationSquare)1 PDAppearanceDictionary (org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary)1 PDAppearanceStream (org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)1 Before (org.junit.Before)1