简体   繁体   English

使用iTextSharp将文本添加到内存流中的现有多页PDF文档

[英]Adding text to existing multipage PDF document in memorystream using iTextSharp

I am trying to add text to an existing PDF file using iTextSharp. 我正在尝试使用iTextSharp将文本添加到现有的PDF文件中。 I have been reading many posts, including the popular thread here . 我一直在阅读很多帖子,包括这里流行帖子

I have some differences: 我有一些不同之处:

  • My PDF are X pages long 我的PDF长X页
  • I want to keep everything in memory, and never have a file stored on my filesystem 我希望将所有内容保存在内存中,并且永远不会将文件存储在我的文件系统中

So I tried to modify the code, so it takes in a byte array and returns a byte array. 所以我尝试修改代码,因此它接受一个字节数组并返回一个字节数组。 I have come this far: 我走到这一步:

  • The code compiles and runs 代码编译并运行
  • My out byte array has a different length than my in byte array 我的out字节数组的长度与字节数组的长度不同

My problem: 我的问题:

  • I cannot see my added text when i later store the modified byte array and open it in my PDF reader 当我稍后存储修改后的字节数组并在我的PDF阅读器中打开它时,我看不到我添加的文本

I don't get why. 我不明白为什么。 From every StackOverflow post I have seen, I do the same. 从我看到的每个StackOverflow帖子中,我都这样做。 using the DirectContent , I use BeginText and write a text. 使用DirectContent ,我使用BeginText并写一个文本。 However, i cannot see it, no matter how I move the position around. 然而,无论我如何移动位置,我都看不到它。

Any idea what is missing from my code? 知道我的代码中缺少什么吗?

public static byte[] WriteIdOnPdf(byte[] inPDF, string str)
        {
            byte[] finalBytes;

            // open the reader
            using (PdfReader reader = new PdfReader(inPDF))
            {
                Rectangle size = reader.GetPageSizeWithRotation(1);
                using (Document document = new Document(size))
                {
                    // open the writer
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (PdfWriter writer = PdfWriter.GetInstance(document, ms))
                        {
                            document.Open();

                            for (var i = 1; i <= reader.NumberOfPages; i++)
                            {
                                document.NewPage();

                                var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                                var importedPage = writer.GetImportedPage(reader, i);

                                var contentByte = writer.DirectContent;
                                contentByte.BeginText();
                                contentByte.SetFontAndSize(baseFont, 18);

                                var multiLineString = "Hello,\r\nWorld!";

                                contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, multiLineString,100, 200, 0);


                                contentByte.EndText();
                                contentByte.AddTemplate(importedPage, 0, 0);
                            }

                            document.Close();
                            ms.Close();
                            writer.Close();
                            reader.Close();
                        }

                        finalBytes = ms.ToArray();
                    }

                }

            }

            return finalBytes;
        }

The code below shows off a full-working example of creating a PDF in memory and then performing a second pass, also in memory. 下面的代码展示了在内存中创建PDF然后在内存中执行第二次传递的完整示例。 It does what @mkl says and closes all iText parts before trying to grab the raw bytes from the stream. 在尝试从流中获取原始字节之前,它执行@mkl所说的并关闭所有iText部分。 It also uses GetOverContent() to draw "on top" of the previous pdf. 它还使用GetOverContent()绘制上一个pdf的“顶部”。 See the code comments for more details. 有关详细信息,请参阅代码注释。

//Bytes will hold our final PDFs
byte[] bytes;

//Create an in-memory PDF
using (var ms = new MemoryStream()) {
    using (var doc = new Document()) {
        using (var writer = PdfWriter.GetInstance(doc, ms)) {
            doc.Open();

            //Create a bunch of pages and add text, nothing special here
            for (var i = 1; i <= 10; i++) {
                doc.NewPage();
                doc.Add(new Paragraph(String.Format("First Pass - Page {0}", i)));
            }

            doc.Close();
        }
    }

    //Right before disposing of the MemoryStream grab all of the bytes
    bytes = ms.ToArray();
}

//Another in-memory PDF
using (var ms = new MemoryStream()) {
    //Bind a reader to the bytes that we created above
    using (var reader = new PdfReader(bytes)) {
        //Store our page count
        var pageCount = reader.NumberOfPages;

        //Bind a stamper to our reader
        using (var stamper = new PdfStamper(reader, ms)) {

            //Setup a font to use
            var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

            //Loop through each page
            for (var i = 1; i <= pageCount; i++) {
                //Get the raw PDF stream "on top" of the existing content
                var cb = stamper.GetOverContent(i);

                //Draw some text
                cb.BeginText();
                cb.SetFontAndSize(baseFont, 18);
                cb.ShowText(String.Format("Second Pass - Page {0}", i));
                cb.EndText();
            }
        }
    }

    //Once again, grab the bytes before closing things out
    bytes = ms.ToArray();
}

//Just to see the final results I'm writing these bytes to disk but you could do whatever
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");
System.IO.File.WriteAllBytes(testFile, bytes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM