How to format text in pdf template with iText

好久不见. 提交于 2021-02-08 08:45:24

问题


I have created a pdf template with 'open office writer' and filled the fields with iText. In pseudo code like this:

PdfReader reader = new PdfReader("C:/temp/Template.pdf");
FileOutputStream fileOutputStream = new FileOutputStream("C:/temp/TemplateTest.pdf");
PdfStamper stamper = new PdfStamper(reader, fileOutputStream);
stamper.setFormFlattening(true);
stamper.getAcroFields().setField("description", "This is a important description.");

stamper.close();

Now I just want the word 'important' in the 'description' text bold. How can I handle this? Is it possible to change a given format in a pdf template with iText and for substrings as well?


回答1:


You've defined a Text Field in PDF along with a font that should be used for that text field, e.g. Helvetica. In this case, you can only use that font. You can't introduce another font, such as Helvetica-Bold.

What you're looking for is not a Text Field, but a Rich Text Field. In this case, you set a Rich Text value. This Rich Text value looks very much like HTML, but it's slightly different. You can consult ISO-32000-1 to find out what's different about it.

You can fill out a Rich Text Field with iText, but... only if you don't flatten the form. because if you want iText to render the special-flavored HTML, you need code to parse that HTML. Actually: that's what we do in XFA Worker, but that doesn't help you because you don't have an XFA form, you have an AcroForm.

Long story short: read chapter 8 of my book, and you'll find an example where I fill out fields using ColumnText and the field positions. If you don't have the book, take a look at the MovieAds example.



来源:https://stackoverflow.com/questions/16079523/how-to-format-text-in-pdf-template-with-itext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!