0

I'm working in some Annotation stuff with iText.

I tried to draw a Stamp Annotation with text on an existing PDF file. The Stamp Annotation did show on the PDF but when I moving the stamp (or resizing), the text (will all format) is disappear and can't gain it back anymore.

Here is my code

PdfReader reader = new PdfReader(SRC_FILE);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(DEST_FILE));
PdfWriter writer = stamper.getWriter();

BaseColor textColor = BaseColor.ORANGE;
BaseColor fillColor = BaseColor.GRAY;
Rectangle rectangle = new Rectangle(150, 250, 450, 320);

PdfAnnotation annotation = PdfAnnotation.createSquareCircle(writer, rectangle, null, true);
annotation.setColor(textColor);

PdfDictionary dict = new PdfDictionary();

PdfTemplate template = PdfTemplate.createTemplate(writer, rectangle.getWidth(), rectangle.getHeight());
template.setBoundingBox(rectangle);
template.setColorFill(fillColor);
//template.setColorStroke(textColor);
template.setLineWidth(4);
template.rectangle(rectangle.getLeft(), rectangle.getBottom(), rectangle.getWidth(), rectangle.getHeight());
template.closePathFillStroke();
template.setColorFill(textColor);

ColumnText columnText = new ColumnText(template);
columnText.setSimpleColumn(rectangle);
Paragraph p = new Paragraph("This is my test", new Font(Font.FontFamily.HELVETICA, 24, Font.BOLD));
p.setAlignment(Element.ALIGN_CENTER);
p.setLeading(50);
columnText.setAlignment(Element.ALIGN_CENTER);
columnText.addElement(p);
columnText.go();

writer.releaseTemplate(template);

dict.put(PdfName.N, template.getIndirectReference());
annotation.put(PdfName.AP, dict);
annotation.put(PdfName.F, new PdfNumber(4));

stamper.addAnnotation(annotation, 1);
stamper.close();
reader.close();

I attached the Screenshot this issue for your review too.

The Stamp render on PDF file

I did some research and if I change the line code

PdfAnnotation annotation = PdfAnnotation.createSquareCircle(writer, rectangle, null, true);

to

PdfAnnotation annotation= stamper.getWriter().createAnnotation(rectangle, PdfName.FREETEXT);

The text will still there when I moving the Stamp, but still disappear if I resize the Rectangle.

mkl
  • 90,588
  • 15
  • 125
  • 265
  • finally, I found a fix for this issue. Instead of using **PdfTemplate**, I used **PdfAppearance** and add more line code `anno.setAppearance(PdfName.N, template);` – Công Thức Phan Feb 07 '18 at 15:01
  • Please make your fix an actual answer and accept it. – mkl Feb 08 '18 at 15:36

0 Answers0