Combine PDFs c#

前端 未结 7 719
忘了有多久
忘了有多久 2020-12-14 18:50

How can I combine multiple PDFs into one PDF without a 3rd party component?

相关标签:
7条回答
  • 2020-12-14 19:05

    I don't think you can. Opensource component PDFSharp has that functionality, and a nice source code sample on file combining

    0 讨论(0)
  • 2020-12-14 19:10

    As others have said, there is nothing built in to do that task. Use iTextSharp with this example code.

    0 讨论(0)
  • 2020-12-14 19:15

    ITextSharp is the way to go

    0 讨论(0)
  • 2020-12-14 19:19

    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.

    0 讨论(0)
  • 2020-12-14 19:21

    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.

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-14 19:26

    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.

    Regarding libraries there is a myriad of possibilities. Just to point a few:

    http://csharp-source.net/open-source/pdf-libraries

    http://www.codeproject.com/KB/graphics/giospdfnetlibrary.aspx

    http://www.pdftron.com/net/index.html

    0 讨论(0)
提交回复
热议问题