如何使用iText格式化pdf模板中的文本 [英] How to format text in pdf template with iText

查看:128
本文介绍了如何使用iText格式化pdf模板中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有'open office writer'的pdf模板,并用iText填充了这些字段.用这样的伪代码:

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

现在,我只想在说明"文本中将重要"一词加粗.我该如何处理?是否可以使用iText和子字符串在pdf模板中更改给定格式?

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?

推荐答案

您已经在PDF中定义了文本字段以及应用于该文本字段的字体,例如Helvetica.在这种情况下,您只能使用该字体.您不能引入其他字体,例如Helvetica-Bold.

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.

您要查找的不是文本字段,而是RTF文本字段.在这种情况下,您可以设置RTF值.此Rich Text值看起来非常类似于HTML,但略有不同.您可以咨询ISO-32000-1来了解它的不同之处.

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.

您可以使用iText填写RTF文本字段,但是...仅在不展平表单的情况下才可以.因为如果您希望iText呈现特殊样式的HTML,则需要代码来解析该HTML.实际上:这就是我们在XFA Worker中所做的事情,但这对您没有帮助,因为您没有XFA表单,而您拥有AcroForm.

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.

长话短说:阅读本书的第8章,您会发现一个示例,其中我使用ColumnText和字段位置填写字段.如果您没有这本书,请查看电影广告示例.

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.

这篇关于如何使用iText格式化pdf模板中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆