在Java中使用iText替换占位符 [英] Replacing placeholders using iText in Java

查看:2181
本文介绍了在Java中使用iText替换占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含<%DATE_OF_BIRTH%>等占位符的PDF,我希望能够读取PDF并使用iText将PDF占位符值更改为文本。

I have a PDF that contains placeholders like <%DATE_OF_BIRTH%>, i want to be able to read in the PDF and change the PDF placeholder values to text using iText.

所以在PDF中读取,可能使用replaceString()方法并更改占位符然后生成新的PDF。

So read in PDF, use maybe a replaceString() method and change the placeholders then generate the new PDF.

这可能吗?

谢谢。

推荐答案

PDF中占位符的使用非常非常有限。从理论上说它可以完成,并且在某些情况下可以按照你的意思去做,但由于PDF不太了解结构,所以很难:

The use of placeholders in PDF is very, very limited. Theoretically it can be done and there are some instances where it would be feasible to do what you say, but because PDF doesn't know about structure very much, it's hard:


  • 简单地提取单词很困难,因此在许多情况下识别PDF中的占位符已经很困难了。

  • simply extracting words is difficult so recognising your placeholders in the PDF would already be difficult in many cases.

更换PDF中的文本是一场噩梦,因为PDF文件通常没有单词,行和段落的概念。因此,例如没有很好的文本重排。

Replacing text in PDF is a nightmare because PDF files generally don't have a concept of words, lines and paragraphs. Hence no nice reflow of text for example.

就像我说的那样,它理论上可以 在特殊条件下工作,但这不是一个很好的解决方案。

Like I said, it could theoretically work under special conditions, but it's not a very good solution.

什么是更好的方法取决于你的用例:

What would be a better approach depends on your use case:

1)对于某些表格,可以将完整的表格作为背景图像或PDF文件,然后生成您的文本作为该背景的叠加(填写空白可以这么说)正如指出Bruno和mlk在评论中,在这种情况下,您还可以查看使用可以动态填充的表单字段。

1) For some forms it may be acceptable to have the complete form as a background image or PDF file and then generate your text as an overlay to that background (filling in the blanks so to speak) As pointed out by Bruno and mlk in comments, in this case you can also look into using form fields which can be dynamically filled.

2)对于其他表单,最好让你的结构化格式(如XML或HTML)的模板,以该格式替换文本,然后将其转换为PDF格式。

2) For other forms it may be better to have your template in a structured format such as XML or HTML, do the text replacement in that format and then convert it into PDF.

这篇关于在Java中使用iText替换占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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