开发者

How can I change the font size of annotations in iTextPdf?

I'm programming a system that generates a PDF file containing user-submitted comments. I want to reduce the size of the comments, i.e. the font size of each annotation. I'm using the Java version of iTextPdf.

My code:

PdfAnnotation annotation = PdfAnnot开发者_开发问答ation.createText(pdfStamper.getWriter(),
    new Rectangle(x, valor, x+100f, valor+100f), "authors", 
    comentario.getComentario(), true, "Comment");
annotation.setColor(Color.ORANGE);

Can I reduce the font size like this?

annotation.setFontSize("2px");


I've never used iTextPdf, but its API says that the PdfAnnotation class has no setFontSize() method.

However, it does contain a setDefaultAppearanceString() method, which takes a PdfContentByte as an argument. And PdfContentByte has a setFontAndSize() method.


From what I can see in the API, I would say: no, it's not possible.

Maybe you can use the setAppearance method. On the iText website is an example which creates an PdfAppearance object that owns a setFontAndSize method.


You have two options.

  1. You can set the default font & size, as Lord Torgamus described.

  2. You can use the Rich Text value of the annotation, which you have to directly access using the methods PdfAnnotation inherited from PdfDictionary:

annotation.put(PdfName.RC, new PdfString( "<font size=\"whatever\">" + 
                                          comentario.getComentario() + 
                                          "</font>" ) );

Note that iText cannot render Rich Text field appearances (at least not yet), so you have to turn off appearance generation so the resulting PDF will ask the viewer to do it:

myStamper.setGenerateAppearances( false );

Yep, have to use a stamper.

It's probably just easier to go with Torg's suggestion... but mind the rabbit. It doesn't play well with others.

PS: Splash's setAppearance() trick would work, but then you'd have to draw the whole thing yourself... background, borders, and text layout (which is much easier thanks to ColumnText).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜