Combine two (or more) PDF's

后端 未结 17 1338
梦毁少年i
梦毁少年i 2020-12-04 07:26

Background: I need to provide a weekly report package for my sales staff. This package contains several (5-10) crystal reports.

Problem:

相关标签:
17条回答
  • 2020-12-04 07:32

    There's some good answers here already, but I thought I might mention that pdftk might be useful for this task. Instead of producing one PDF directly, you could produce each PDF you need and then combine them together as a post-process with pdftk. This could even be done from within your program using a system() or ShellExecute() call.

    0 讨论(0)
  • 2020-12-04 07:33

    You could try pdf-shuffler gtk-apps.org

    0 讨论(0)
  • 2020-12-04 07:34

    This is something that I figured out, and wanted to share with you.

    Here you can join multiple Pdfs in one (following the input list order)

        public static byte[] MergePdf(List<byte[]> pdfs)
        {
            List<PdfSharp.Pdf.PdfDocument> lstDocuments = new List<PdfSharp.Pdf.PdfDocument>();
            foreach (var pdf in pdfs)
            {
                lstDocuments.Add(PdfReader.Open(new MemoryStream(pdf), PdfDocumentOpenMode.Import));
            }
    
            using (PdfSharp.Pdf.PdfDocument outPdf = new PdfSharp.Pdf.PdfDocument())
            { 
                for(int i = 1; i<= lstDocuments.Count; i++)
                {
                    foreach(PdfSharp.Pdf.PdfPage page in lstDocuments[i-1].Pages)
                    {
                        outPdf.AddPage(page);
                    }
                }
    
                MemoryStream stream = new MemoryStream();
                outPdf.Save(stream, false);
                byte[] bytes = stream.ToArray();
    
                return bytes;
            }           
        }
    
    0 讨论(0)
  • 2020-12-04 07:35

    Following method gets a List of byte array which is PDF byte array and then returns a byte array.

    using ...;
    using PdfSharp.Pdf;
    using PdfSharp.Pdf.IO;
    
    public static class PdfHelper
    {
        public static byte[] PdfConcat(List<byte[]> lstPdfBytes)
        {
            byte[] res;
    
            using (var outPdf = new PdfDocument())
            {
                foreach (var pdf in lstPdfBytes)
                {
                    using (var pdfStream = new MemoryStream(pdf))
                    using (var pdfDoc = PdfReader.Open(pdfStream, PdfDocumentOpenMode.Import))
                        for (var i = 0; i < pdfDoc.PageCount; i++)
                            outPdf.AddPage(pdfDoc.Pages[i]);
                }
    
                using (var memoryStreamOut = new MemoryStream())
                {
                    outPdf.Save(memoryStreamOut, false);
    
                    res = Stream2Bytes(memoryStreamOut);
                }
            }
    
            return res;
        }
    
        public static void DownloadAsPdfFile(string fileName, byte[] content)
        {
            var ms = new MemoryStream(content);
    
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/pdf";
            HttpContext.Current.Response.AddHeader("content-disposition", $"attachment;filename={fileName}.pdf");
            HttpContext.Current.Response.Buffer = true;
            ms.WriteTo(HttpContext.Current.Response.OutputStream);
            HttpContext.Current.Response.End();
        }
    
        private static byte[] Stream2Bytes(Stream input)
        {
            var buffer = new byte[input.Length];
            using (var ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                    ms.Write(buffer, 0, read);
    
                return ms.ToArray();
            }
        }
    }
    

    So, the result of PdfHelper.PdfConcat method is passed to PdfHelper.DownloadAsPdfFile method.

    PS: A NuGet package named [PdfSharp][1] need to be installed. So in the Package Manage Console window type:

    Install-Package PdfSharp

    0 讨论(0)
  • 2020-12-04 07:41

    I used iTextsharp with c# to combine pdf files. This 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-04 07:43

    PDFsharp seems to allow merging multiple PDF documents into one.

    And the same is also possible with ITextSharp.

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