C# 组合PDF文件#

C# 组合PDF文件#,c#,pdf-generation,C#,Pdf Generation,如何在没有第三方组件的情况下将多个PDF合并成一个PDF?AFAIK C#没有处理PDF的内置支持,因此,如果没有第三方组件或COTS库,您的要求就无法实现 关于图书馆,有无数的可能性。仅举几个例子: NET Framework不包含修改/创建PDF的功能。你需要一个第三方组件来完成你想要的东西。我认为你做不到。 开源组件PDFSharp具有这一功能,正如其他人所说,它是一个不错的,没有内置的东西来完成这项任务。与此配合使用。尽管已经说过,但您不能使用.NET Framework的内置库操作

如何在没有第三方组件的情况下将多个PDF合并成一个PDF?

AFAIK C#没有处理PDF的内置支持,因此,如果没有第三方组件或COTS库,您的要求就无法实现

关于图书馆,有无数的可能性。仅举几个例子:


NET Framework不包含修改/创建PDF的功能。你需要一个第三方组件来完成你想要的东西。

我认为你做不到。
开源组件PDFSharp具有这一功能,正如其他人所说,它是一个不错的

,没有内置的东西来完成这项任务。与此配合使用。

尽管已经说过,但您不能使用.NET Framework的内置库操作PDF。不过,我可以推荐它,它是Java iText的.NET端口。我对它进行了研究,发现它是一个非常容易使用的工具。

是一种方法。

我不认为.NET Framework包含这样的库。我使用iTextsharp和c#组合pdf文件。我认为这是最简单的方法。这是我使用的代码

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;
    }
}
string[]lstFiles=新字符串[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;
PDF导入页面导入页面;
字符串outputPdfPath=@“C:/pdf/new.pdf”;
sourceDocument=新文档();
pdfCopyProvider=newpdfcopy(sourceDocument,new System.IO.FileStream(outputPdfPath,System.IO.FileMode.Create));
//打开输出文件
Open();
尝试
{
//循环浏览文件列表
对于(int f=0;f对于(int i=1;我认为应该有人注意到iTextSharp使用AGPL许可证,这要求您在发布源代码的同时适当记录iTextSharp的使用情况。不过,他们确实提供了商业许可证。请注意您如何使用它!这是正确的还是有用的?请接受回答谢谢!最后一句话在我需要的时候保存。它也很快!我知道这很旧,但我遵循了这个答案,发现PDFSharp仍然没有实现对更现代版本的PDF的支持…您可能需要跳转才能使其适用于所有PDF。在跳转之前,请查看以下内容: