合并 PDF c#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/73950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combine PDFs c#
提问by
How can I combine multiple PDFs into one PDF without a 3rd party component?
如何在没有第三方组件的情况下将多个 PDF 合并为一个 PDF?
回答by Jorge Ferreira
AFAIK C# has no built-in support for handling PDF so what you are asking can not be done without using a 3rd party component or a COTS library.
AFAIK C# 没有处理 PDF 的内置支持,因此如果不使用 3rd 方组件或 COTS 库,就无法完成您的要求。
Regarding libraries there is a myriad of possibilities. Just to point a few:
关于图书馆,有无数的可能性。简单说几点:
http://csharp-source.net/open-source/pdf-libraries
http://csharp-source.net/open-source/pdf-libraries
http://www.codeproject.com/KB/graphics/giospdfnetlibrary.aspx
http://www.codeproject.com/KB/graphics/giospdfnetlibrary.aspx
回答by vengafoo
The .NET Framework does not contain the ability to modify/create PDFs. You need a 3rd party component to accomplish what you are looking for.
.NET Framework 不包含修改/创建 PDF 的能力。您需要一个 3rd 方组件来完成您正在寻找的内容。
回答by Sergey Volegov
I don't think you can. Opensource component PDFSharp has that functionality, and a nice source code sample on file combining
我不认为你可以。开源组件 PDFSharp 具有该功能,以及一个很好的文件合并源代码示例
回答by Nathan Jones
As others have said, there is nothing built in to do that task. Use iTextSharpwith this example code.
正如其他人所说,没有内置任何东西来完成这项任务。在此示例代码中使用iTextSharp。
回答by MagicKat
Although it has already been said, you can't manipulate PDFs with the built-in libraries of the .NET Framework. I can however recommend iTextSharp, which is a .NET port of the Java iText. I have played around with it, and found it to be a very easy tool to use.
虽然已经说过,但您无法使用 .NET Framework 的内置库来操作 PDF。但是,我可以推荐iTextSharp,它是 Java iText 的 .NET 端口。我玩过它,发现它是一个非常容易使用的工具。
回答by Paul
ITextSharpis the way to go
ITextSharp是要走的路
回答by Sajitha Rathnayake
I don't think .NET Framework contains such like libraries. I used iTextsharp with c# to combine pdf files. I think iTextsharp is easyest way to do this. Here is the code I used.
我不认为 .NET Framework 包含这样的库。我使用 iTextsharp 和 c# 来组合 pdf 文件。我认为 iTextsharp 是最简单的方法。这是我使用的代码。
string[] lstFiles=new string[3];
lstFiles[0]=@"C:/pdf/1.pdf";
lstFiles[1]=@"C:/pdf/2.pdf";
lstFiles[2]=@"C:/pdf/3.pdf";
PdfReader reader = null;
Document sourceDocument = null;
PdfCopy pdfCopyProvider = null;
PdfImportedPage importedPage;
string outputPdfPath=@"C:/pdf/new.pdf";
sourceDocument = new Document();
pdfCopyProvider = new PdfCopy(sourceDocument, new System.IO.FileStream(outputPdfPath, System.IO.FileMode.Create));
//Open the output file
sourceDocument.Open();
try
{
//Loop through the files list
for (int f = 0; f < lstFiles.Length-1; f++)
{
int pages =get_pageCcount(lstFiles[f]);
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();
}
//At the end save the output file
sourceDocument.Close();
}
catch (Exception ex)
{
throw ex;
}
private int get_pageCcount(string file)
{
using (StreamReader sr = new StreamReader(File.OpenRead(file)))
{
Regex regex = new Regex(@"/Type\s*/Page[^s]");
MatchCollection matches = regex.Matches(sr.ReadToEnd());
return matches.Count;
}
}