C# 有没有办法用 itextsharp 替换 PDF 文件中的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/795066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:29:24  来源:igfitidea点击:

Is there a way to replace a text in a PDF file with itextsharp?

c#itextsharp

提问by AndreMiranda

I'm using itextsharp to generate the PDFs, but I need to change some text dynamically. I know that it's possible to change if there's any AcroField, but my PDF doen's have any of it. It just has some pure texts and I need to change some of them.

我正在使用 itextsharp 生成 PDF,但我需要动态更改一些文本。我知道如果有任何 AcroField 是可以改变的,但我的 PDF 确实有任何一个。它只有一些纯文本,我需要更改其中的一些。

Does anyone know how to do it?

有谁知道怎么做?

采纳答案by John Bubriski

Actually, I have a blog post on how to do it! But like IanGilham said, it depends on whether you have control over the original PDF. The basic idea is you setup a form on the page and replace the form fields with the text you want. (You can style the form so it doesn't look like a form)

实际上,我有一篇关于如何做到这一点的博客文章!但就像 IanGilham 所说,这取决于您是否可以控制原始 PDF。基本思想是您在页面上设置一个表单并用您想要的文本替换表单字段。(您可以设置表单的样式,使其看起来不像表单)

If you don't have control over the PDF, let me know how to do it!

如果您无法控制 PDF,请告诉我该怎么做!

Here is a link to the full post:

这是完整帖子的链接:

Using a template to programmatically create PDFs with C# and iTextSharp

使用模板通过 C# 和 iTextSharp 以编程方式创建 PDF

回答by IanGilham

I haven't used itextsharp, but I have been using PDFNet SDKto explore the content of a large pile of PDFs for localisation over the last few weeks.

我没有使用 itextsharp,但在过去的几周里,我一直在使用PDFNet SDK来探索大量 PDF 的内容以进行本地化。

I would say that what you require is absolutely achievable, but how difficult it is will depend entirely on how much control you have over the quality of the files. In my case, the files can be constructed from any combination of images, text in any random order, tables, forms, paths, single pixel graphics and scanned pages, some of which are composed from hundreds of smaller images. Let's just say we're having fun with it.

我会说你需要的东西是绝对可以实现的,但它的难易程度完全取决于你对文件质量的控制程度。就我而言,文件可以由图像、任意随机顺序的文本、表格、表格、路径、单像素图形和扫描页面的任意组合构成,其中一些由数百个较小的图像组成。让我们说我们玩得很开心。

In the PDFTron way of doing things, you would have to implement a viewer (sample available), and add some code over a text selection. Given the complexities of the format, it may be necessary to implement a simple editor in a secondary dialog with the ability to expand the selection to the next line (or whatever other fundamental object is used to make up text). The string could then be edited and applied by copying the entire page of the document into a new page, replacing the selected elements with your new string. You would probably have to do some mathematics to get this to work well though, as just about everything in PDF is located on the page by means of an affine transform.

在 PDFTron 的做事方式中,您必须实现一个查看器(可用示例),并在文本选择上添加一些代码。鉴于格式的复杂性,可能有必要在辅助对话框中实现一个简单的编辑器,以便将选择扩展到下一行(或任何其他用于组成文本的基本对象)。然后可以通过将文档的整个页面复制到新页面中来编辑和应用该字符串,用您的新字符串替换所选元素。您可能需要做一些数学运算才能使其正常工作,因为 PDF 中的几乎所有内容都通过仿射变换位于页面上。

Good luck. I'm sure there are people on here with some experience of itextsharp and PDF in general.

祝你好运。我敢肯定,这里有人对 itextsharp 和 PDF 有一定的总体经验。

回答by kuujinbo

This question comes up from time to time on the mailing list. The same answer is given time and time again - NO. See this threadfor the officialanswer from the person who created iText.

这个问题不时出现在邮件列表中。一次又一次地给出相同的答案-。请参阅此线程以获取创建 iText 的人官方答案。

This question shouldbe a FAQ on the itextsharp tag wiki.

这个问题应该itextsharp 标签 wiki 上的常见问题解答