C# ITextSharp 编辑现有的 pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14639839/
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-10 12:31:57  来源:igfitidea点击:

ITextSharp edit an existing pdf

c#itextsharp

提问by Mina N

I want to add a text to an existing PDF file using iTextSharp, I found different ways but in all of them the writer and reader are separate pdf files. I want a way so I can open a pdf then write different things in different positions. right now I have this code, but it makes a new file.

我想使用 iTextSharp 将文本添加到现有的 PDF 文件中,我发现了不同的方法,但在所有这些方法中,作者和读者都是单独的 pdf 文件。我想要一种方法,这样我就可以打开 pdf 然后在不同的位置写不同的东西。现在我有这个代码,但它创建了一个新文件。

using (FileStream stream1 = File.Open(path, FileMode.OpenOrCreate))
      {
      BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
      PdfReader reader = new PdfReader("C:\26178DATA\pdf\holding.pdf");
      var pageSize = reader.GetPageSize(1);
      PdfStamper stamper = new PdfStamper(reader, stream1);
      iTextSharp.text.Font tmpFont = new iTextSharp.text.Font(bf, fontSize);
      PdfContentByte canvas = stamper.GetOverContent(1);
      Phrase ph = new Phrase(words[1], tmpFont);
      ph.Font = tmpFont;
      canvas.SetFontAndSize(bf, fontSize);
      ColumnText.ShowTextAligned(canvas, Element.ALIGN_LEFT, ph, iTextSharp.text.Utilities.MillimetersToPoints(x * 10), pageSize.GetTop(iTextSharp.text.Utilities.MillimetersToPoints(y * 10)), 0);
      stamper.Close();
            }

采纳答案by mkl

You want to add a text to an existing PDF file using iTextSharp, found different ways but in all of them the writer and reader are separate pdf files.

想使用 iTextSharp 将文本添加到现有的 PDF 文件中,找到了不同的方法,但在所有这些方法中,作者和读者是单独的 pdf 文件。

As the normal way in which iText(Sharp) manipulates a PDF using a PdfStamper, can involve major reorganization of existing PDF elements, iText does not edit a file in place. The other way, using append mode, would allow for editing in place; but such an option is not implemented. A big draw-back of in-place editing is that in case of some program failure, the file in question might remain in an intermediary, unusable state.

由于 iText(Sharp) 使用 PdfStamper 操作 PDF 的正常方式可能涉及对现有 PDF 元素的重大重组,因此 iText 不会就地编辑文件。另一种方式,使用追加模式,将允许就地编辑;但这样的选择没有实施。就地编辑的一个大缺点是,如果某些程序出现故障,有问题的文件可能仍处于中间、无法使用的状态。

That being said, you can save the new file to the path of the original file by first reading the file into memory completely and then starting to create the output with the same path. In case of your sample code that would imply at least moving the PdfReader constructor use before the creation of the output stream:

话虽如此,您可以通过首先将文件完全读入内存然后开始创建具有相同路径的输出来将新文件保存到原始文件的路径中。如果您的示例代码意味着至少在创建输出流之前移动 PdfReader 构造函数使用:

PdfReader reader = new PdfReader(path);
using (FileStream stream1 = File.Open(path, FileMode.OpenOrCreate))
{
    BaseFont bf = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    ...

Alternatively you could create the result file in memory, i.e. in a MemoryStream instead of a FileStream, and, when finished, write the contents of the memory stream to your source file path.

或者,您可以在内存中创建结果文件,即在 MemoryStream 而不是 FileStream 中,并在完成后将内存流的内容写入您的源文件路径。