Combine two (or more) PDF's

后端 未结 17 1481
梦毁少年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: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 pdfs)
        {
            List lstDocuments = new List();
            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;
            }           
        }
    

提交回复
热议问题