【发布时间】:2013-07-08 10:54:45
【问题描述】:
我可以在 asp.net 中合并两个或多个 PDF 吗?我知道我可以使用互操作来处理 Word 和 Excel 文件。但是我可以合并 PDF 吗?
请提出任何建议或任何链接。
【问题讨论】:
-
看看这里dotnetspider.com/resources/…它可能对你有帮助
我可以在 asp.net 中合并两个或多个 PDF 吗?我知道我可以使用互操作来处理 Word 和 Excel 文件。但是我可以合并 PDF 吗?
请提出任何建议或任何链接。
【问题讨论】:
试试iTextSharp:
iTextSharp 是 iText 的 C# 端口,以及开源 Java 库 PDF 生成和操作。它可以用来创建PDF 从头开始文档,将 XML 转换为 PDF(使用额外的 XFA Worker DLL),填写交互式 PDF 表单,标记新内容 在现有的 PDF 文档上,拆分和合并现有的 PDF 文档, 还有更多。
Here's an article 了解如何操作。
【讨论】:
PdfWriter 用于创建新文档。要从现有源文档的集合中复制页面,您应该使用PdfCopy. Cf。本书iText in Action — 2nd Edition中的样本Concatenate.cs。
unethicalreading 属性不再可用。尝试将项目中使用的 iTextSharp 版本降级到 4.0.3,如 opensubscriber.com/message/… 所示 - 希望您不要使用比此版本更新的任何功能。最坏的情况是加密的 PDF,您检测到它们的异常并使用旧库。
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;
}
}
【讨论】: