You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何将iTextSharp.text.Document转换为字节数组?合并PDF后转换失败求助

Fixing the iTextSharp Document to Byte Array Conversion Issue

Hey there! I totally get the frustration—you’ve nailed merging two PDFs but hit a wall when trying to turn that iTextSharp.text.Document into a byte array. Let’s sort this out quickly.

The Core Issue

The Document object in iTextSharp is just an abstraction for building your PDF content—it doesn’t actually store the raw byte data itself. To get a byte array, you need to route the Document’s output to a MemoryStream first, then extract the bytes from that stream.

Step-by-Step Solution

Here’s how to adjust your code to capture the merged PDF as a byte array:

  • Use a MemoryStream as the output target for your PdfWriter
  • Execute your merge logic as you did before
  • Properly close the Document and Writer to ensure all data is flushed to the stream
  • Extract the byte array from the MemoryStream

Working Code Example

using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

public byte[] MergePdfsToByteArray(string pdfPath1, string pdfPath2)
{
    using (var outputStream = new MemoryStream())
    {
        // Initialize Document and bind it to the MemoryStream via PdfWriter
        var document = new Document();
        var writer = PdfWriter.GetInstance(document, outputStream);

        document.Open();

        // Add pages from first PDF
        using (var reader1 = new PdfReader(pdfPath1))
        {
            for (int pageNum = 1; pageNum <= reader1.NumberOfPages; pageNum++)
            {
                PdfImportedPage importedPage = writer.GetImportedPage(reader1, pageNum);
                document.Add(Image.GetInstance(importedPage));
            }
        }

        // Add pages from second PDF
        using (var reader2 = new PdfReader(pdfPath2))
        {
            for (int pageNum = 1; pageNum <= reader2.NumberOfPages; pageNum++)
            {
                PdfImportedPage importedPage = writer.GetImportedPage(reader2, pageNum);
                document.Add(Image.GetInstance(importedPage));
            }
        }

        // Critical: Close the document and writer to flush all content to the stream
        document.Close();
        writer.Close();

        // Convert the stream to a byte array
        return outputStream.ToArray();
    }
}

Key Notes

  • Always close the Document and Writer: If you skip this, the MemoryStream might be missing the final parts of the PDF (like the cross-reference table), leading to corrupted data.
  • Use using blocks: This ensures resources like PdfReader are properly disposed, preventing memory leaks.
  • Avoid accessing the stream before closing the Document: The Document holds some data in memory until it’s closed, so the stream won’t have the full PDF until then.

Give this a try—your merged PDF should now convert to a byte array without issues!

内容的提问来源于stack exchange,提问作者paras

火山引擎 最新活动