【问题标题】:Merging two PDF documents into one [closed]将两个PDF文档合并为一个[关闭]
【发布时间】:2013-07-08 10:54:45
【问题描述】:

我可以在 asp.net 中合并两个或多个 PDF 吗?我知道我可以使用互操作来处理 Word 和 Excel 文件。但是我可以合并 PDF 吗?

请提出任何建议或任何链接。

【问题讨论】:

标签: c# asp.net pdf


【解决方案1】:

试试iTextSharp:

iTextSharp 是 iText 的 C# 端口,以及开源 Java 库 PDF 生成和操作。它可以用来创建PDF 从头开始文档,将 XML 转换为 PDF(使用额外的 XFA Worker DLL),填写交互式 PDF 表单,标记新内容 在现有的 PDF 文档上,拆分和合并现有的 PDF 文档, 还有更多。

Here's an article 了解如何操作。

【讨论】:

  • 我只能使用您建议的代码合并 PDF 吗?或者任何其他格式也可以合并。?或从 doc 到 pdf 或 .xls 到 pdf。?这些操作可以做吗?
  • 抱歉,iTextSharp 只能从 html、xml 或以编程方式创建 PDF。您可以使用 LibreOffice 使用此处描述的方法将 doc 转换为 PDF:kgsspot.blogspot.ca/2011/09/…
  • @Ankur 请注意,此合并例程不是 iText(Sharp) 的最佳合并代码。 PdfWriter 用于创建新文档。要从现有源文档的集合中复制页面,您应该使用PdfCopy. Cf。本书iText in Action — 2nd Edition中的样本Concatenate.cs
  • @Ryan Weir:对于某些 PDF,我收到此错误“PdfReader not opens with owner password”。请帮忙。
  • @Ankur:我刚刚检查过,您是对的,在较新版本的 iTextSharp 中,您在 stackoverflow.com/a/11991353/808532 中提到的 unethicalreading 属性不再可用。尝试将项目中使用的 iTextSharp 版本降级到 4.0.3,如 opensubscriber.com/message/… 所示 - 希望您不要使用比此版本更新的任何功能。最坏的情况是加密的 PDF,您检测到它们的异常并使用旧库。
【解决方案2】:
using System.Text.RegularExpressions;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
using iTextSharp.text;
//Call this method in main with parameter
public static void MergePages(string outputPdfPath, string[] lstFiles)
{
    PdfReader reader = null;
    Document sourceDocument = null;
    PdfCopy pdfCopyProvider = null;
    PdfImportedPage importedPage;
    sourceDocument = new Document();
    pdfCopyProvider = new PdfCopy(sourceDocument, 
    new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
    sourceDocument.Open();
    try
    {
        for (int f = 0; f < lstFiles.Length - 1; f++)
        {
            int pages = 1; 
            reader = new PdfReader(lstFiles[f]);
            //Add pages of current file
            for (int i = 1; i <= pages; i++)
            {
                importedPage = pdfCopyProvider.GetImportedPage(reader, i);
                pdfCopyProvider.AddPage(importedPage);
            }
            reader.Close();
        }
        sourceDocument.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-10
    • 2011-01-31
    • 1970-01-01
    • 2014-07-21
    • 2014-11-16
    • 1970-01-01
    • 2013-11-20
    • 2022-01-26
    相关资源
    最近更新 更多