iTextSharp合併筆記

有專案需求將產製出的PDF進行合併

故寫了一段合併用的範例Code

            Document document = new Document();
            PdfWriter PdfWriter = PdfWriter.GetInstance(document, new FileStream(outFilePath, FileMode.Create));
            document.Open();
            PdfContentByte PdfContentByte = PdfWriter.DirectContent;
            PdfImportedPage PdfImportedPage;

            var reader = new PdfReader[data.Count()];
            var i = 0;
            foreach (var item in data)
            {
                var PDFPath = Path + item.pdfname + ".pdf";
                reader[i] = new PdfReader(PDFPath);
                int iPageNum = reader[i].NumberOfPages;
                for(int j=1;j<=iPageNum;j++) 
                {
                    document.NewPage();
                    PdfImportedPage = PdfWriter.GetImportedPage(reader[i], j);
                    PdfContentByte.AddTemplate(PdfImportedPage, 0, 0); 
                }
                i++;

                if (i == data.Count())
                {
                    document.Close();
                    foreach (var v_item in reader)
                    {
                        v_item.Close();
                    }
                }
            }

 

 

 

以上內容,若有錯誤

煩請各路高手路過指正

謝謝!

<(_ _)>