【问题标题】:Why can't I append a PDF to another PDF with this code using iTextSharp?为什么我不能使用 iTextSharp 使用此代码将 PDF 附加到另一个 PDF?
【发布时间】:2020-02-08 08:50:45
【问题描述】:

我正在尝试使用 iTextSharp 将两个 PDF 文件的内容合并到一个新的 PDF 文件中。我以前在类似情况下使用过 PDFStamper 来完成此操作,但由于某种原因它这次无法正常工作。附加根本不起作用;该文件已创建,但到此代码块结束时大小保持为 0 字节。任何人都可以提供任何帮助,我们将不胜感激。

File.Create(session.getLocalDir() + newPdfFile);

// pasting content from original file to new file
PdfReader reader = new PdfReader(originalFile);
string pageSelection = "1-" + reader.NumberOfPages;
reader.SelectPages(pageSelection);
PdfStamper stamper = new PdfStamper(reader, new FileStream(newPdfFile, FileMode.Append, FileAccess.Write));
stamper.Close();
reader.Close();

// pasting content from temp file to new file
reader = new PdfReader(temp);
pageSelection = "1-" + reader.NumberOfPages;
reader.SelectPages(pageSelection);
stamper = new PdfStamper(reader, new FileStream(newPdfFile, FileMode.Append, FileAccess.Write));
stamper.Close();
reader.Close();

【问题讨论】:

  • 在调试器中单步执行时观察到了什么?
  • 就是这样,它不会抛出异常或以其他方式指示发生了任何错误。但是原始文件和临时文件都不会发生附加。
  • 不清楚为什么结果文件的长度为 0 字节,但是在您尝试时追加将无论如何都不起作用。您不能简单地连接两个 pdf 并希望结果是它们的合并。
  • 我不是在询问例外情况。我在问每行代码在执行时是否符合您的期望。如果您的期望只是程序不会崩溃或抛出异常,那么您的代码就可以工作!不过,我怀疑它们的局限性如此之大。每一行的期望应该是特定于该行的。
  • 不,它没有按照我的预期工作。就像我在原始帖子中提到的那样,我之前已经使用这种方法成功地附加到了 PDF 中。唯一的区别是我没有使用我明确创建的 PDF,就像我在上面的代码中所做的那样。也许这就是问题所在。但是有没有更好的方法可以将两个 PDF 合并为一个新的?

标签: c# visual-studio pdf itext append


【解决方案1】:

这里最简单的解决方案是在添加临时 pdf 文件时使用 MemoryStream 来保存它。经过一些研究,我发现正如 mkl 所暗示的那样, PdfStamper 类不适合此操作。在 iTextSharp 中,您可以使用另一种方式附加 2 个 PDF:

        MemoryStream stream = new MemoryStream();
        PdfCopyFields copy = new PdfCopyFields(stream);


        var ms1 = new MemoryStream(File.ReadAllBytes(file1Path));
        ms1.Position = 0;
        copy.AddDocument(new PdfReader(ms1));
        ms1.Dispose();

        var ms2 = new MemoryStream(File.ReadAllBytes(file2Path));
        ms2.Position = 0;
        copy.AddDocument(new PdfReader(ms2));
        ms2.Dispose();
        copy.Close();

生成的“流”变量包含组合的 PDF,可以使用 PdfStamper 写入文件。

如果可以选择切换到 .net 的 iText 7,则可以省略 PdfStamper,而可以使用 PdfDocument.copyPagesTo() 方法。

一个简单的例子(使用 iText 7 for .net):

    MemoryStream stream = new MemoryStream();
    PdfDocument outputDocument = new PdfDocument(new PdfWriter(stream));
    PdfDocument pdfSource = new PdfDocument(new PdfReader("c:\\firstInput.pdf"));

    pdfSource.CopyPagesTo(1, pdfSource.GetNumberOfPages(), outputDocument);
    pdfSource = new PdfDocument(new PdfReader("c:\\secondInput.pdf"));
    pdfSource.CopyPagesTo(1, pdfSource.GetNumberOfPages(), outputDocument);
    pdfSource.Close();
    outputDocument.Close();

    MemoryStream outputStream = new MemoryStream(stream.ToArray());
    outputDocument = new PdfDocument(new PdfReader(outputStream), new PdfWriter("c:\\result.pdf"));

第二次编辑了针对特定用例的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    相关资源
    最近更新 更多