for (int i = 0; i < document.getNumberOfPages(); i++)
{
PDPage page = document.getPage(i);
List 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
String imageFilename = args[2];
PDImageXObject ximage;
if( imageFilename.toLowerCase().endsWith( ".jpg" ) )
{
ximage = JPEGFactory.createFromStream(document, new FileInputStream(imageFilename));
}
else if (imageFilename.toLowerCase().endsWith(".tif") || imageFilename.toLowerCase().endsWith(".tiff"))
{
ximage = CCITTFactory.createFromRandomAccess(document, new RandomAccessFile(new File(imageFilename),"r"));
}
else if (imageFilename.toLowerCase().endsWith(".gif") ||
imageFilename.toLowerCase().endsWith(".bmp") ||
imageFilename.toLowerCase().endsWith(".png"))
{
BufferedImage bim = ImageIO.read(new File(imageFilename));
ximage = LosslessFactory.createFromImage(document, bim);
}
else
{
throw new IOException( "Image type not supported: " + imageFilename );
}
// 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
PDStream stream = new PDStream(document);
OutputStream os = stream.createOutputStream();
PDFormXObject form = new PDFormXObject(stream);
form.setResources(new PDResources());
form.setBBox(rect);
form.setFormType(1);
// adjust the image to the target rectangle and add it to the stream
drawXObject(ximage, form.getResources(), os, lowerLeftX, lowerLeftY, imgWidth, imgHeight);
os.close();
PDAppearanceStream myDic = new PDAppearanceStream(form.getCOSStream());
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);
}