Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » iText »

[iText-questions] adding an image to a push button field

John Sanda

2005-06-02


I have looked at several similar posts, but I have been unable to add an
image to a push button field. I have a PDF template with a push button
field called labelField. I have been unsuccessful in several attempts at
adding an image to the button. Here is latest code that I have tried:

// start code to add image to push button field

PdfDictionary buttonDict =
(PdfDictionary)PdfReader.getPdfObject(reader.getAcroForm().getRefByName("labelField"));
PdfArray rect =
(PdfArray)PdfReader.getPdfObject(buttonDict.get(PdfName.RECT));
ArrayList list = rect.getArrayList();
PdfRectangle pdfRect = new PdfRectangle(getFloatValue(list, 0),
getFloatValue(list, 1), getFloatValue(list, 2), getFloatValue(list, 3));

PdfWriter writer = stamper.getWriter();
PdfContentByte contentByte = writer.getDirectContent();

contentByte.moveTo(0, 0);

Image image = Image.getInstance(new
LabelGenerator().generateLabel(labelDataProvider));
PdfFormField field = PdfFormField.createPushButton(writer);
PdfAppearance appearance = contentByte.createAppearance(image.width(),
image.height());
appearance.addImage(image, image.width(), 0, 0, image.height(), 0, 0);

field.setWidget(new Rectangle(pdfRect.left(), pdfRect.bottom(),
pdfRect.top(), pdfRect.right()), PdfAnnotation.HIGHLIGHT_NONE);
field.setFieldName("labelField");
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "flatState",
appearance);

stamper.addAnnotation(field, 1);

// end code to add image to push button field

// helper method to extract float value
private float getFloatValue(List list, int index) {
   PdfNumber pdfNumber = (PdfNumber)list.get(index);
   return pdfNumber.floatValue();
}
// end helper method


I would greatly appreciate any insight into what I am doing wrong or if
someone can point me to some examples. Thanks in advance!

- John



********************************************************************************
Unauthorized interception of this communication could be a
violation of Federal and State Law. This communication and
any files transmitted with it are confidential and may contain
protected health information. This communication is solely
for the use of the person or entity to whom it was addressed.
If you are not the intended recipient, any use, distribution,
printing or acting in reliance on the contents of this message
is strictly prohibited. If you have received this message
in error, please notify the sender and destroy any and all copies.
Thank you.
*******************************************************************************




-------------------------------------------------------
This SF.Net email is sponsored by Yahoo.
Introducing Yahoo! Search Developer Network - Create apps using Yahoo!
Search APIs Find out how you can build Yahoo! directly into your own
Applications - visit http://developer.yahoo.net/?fr=offad-ysdn-ostg-q22005
_______________________________________________
iText-questions mailing list
iText-questions@(protected)
https://lists.sourceforge.net/lists/listinfo/itext-questions
©2008 junlu.com - Jax Systems, LLC, U.S.A.