Hi everyone,

I used a lightly modified example code  (RubberstampWithImage) to add
rubber stamp to a text field. it works fine except the resolution of image
seems to be too low.

See below,  if I use the image of same pixel width as the box size, the
image becomes very blur. But I increase the image, it only shows part of
it.

So how do I add a clear stamp (image) that fits into the RECT of the text
widget? Thanks.


Field FQN: broker_sig
Image File: broker_signature.png
Image Size: (170, 34)
Box Size (424.46, 16.25)
in inch (5.8952775, 0.22569445)
Box Position (135.035, 182.717)


public static void addStamp(PDDocument document, PDField selectedField,
String imageFilename) throws IOException {

PDPage page = selectedField.getWidgets().get(0).getPage();

PDRectangle rect = selectedField.getWidgets().get(0).getRectangle();
List<PDAnnotation> annotations = page.getAnnotations();
PDAnnotationRubberStamp rubberStamp = new PDAnnotationRubberStamp();

// create a PDXObjectImage with the given image file
PDImageXObject ximage = PDImageXObject.createFromFile(imageFilename,
document);

// define and set the target rectangle
int imgWidth = ximage.getImage().getWidth();
int imgHeight = ximage.getImage().getHeight();
System.out.println("Field FQN: " + selectedField.getFullyQualifiedName());
System.out.println("Image File: " + imageFilename);
System.out.println("Image Size: (" + imgWidth + ", " + imgHeight + ")");
System.out.println("Box Size (" + rect.getWidth() + ", " + rect.getHeight()
+ ")");
System.out.println("in inch (" + rect.getWidth()/72 + ", " +
rect.getHeight()/72 + ")");
System.out.println("Box Position (" + rect.getLowerLeftX() + ", " +
rect.getLowerLeftY() + ")");
// 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
OutputStream os = form.getStream().createOutputStream();
//center the object horizontally
float imageLeftX = rect.getLowerLeftX() + (rect.getWidth() - imgWidth)/2;
drawXObject(ximage, form.getResources(), os, imageLeftX,
rect.getLowerLeftY(), imgWidth, imgHeight);
os.close();

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

-- 
Best Wishes,
Jason

Reply via email to