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);
}
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]);
}
}
}
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]);
}
}
}
Aggregations