PDF合并

 

using System.IO;

using iTextSharp.text;
using iTextSharp.text.pdf;

 

 

 

 

/// <summary>
        /// PDF合并
        /// </summary>
        /// <param name="fileList">需要合并的PDF文件路径</param>
        /// <param name="outMergeFile">合并保存输出路径</param>
        private void mergePDFFiles(string[] fileList, string outMergeFile)
        {
            PdfReader reader;
            Document document = new Document();
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outMergeFile, FileMode.Create));
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage newPage;
            for (int i = 0; i < fileList.Length; i++)
            {
                if (!File.Exists(fileList[i]))
                    continue;

                reader = new PdfReader(fileList[i]);
                int iPageNum = reader.NumberOfPages;
                for (int j = 1; j <= iPageNum; j++)
                {
                    document.NewPage();
                    newPage = writer.GetImportedPage(reader, j);
                    cb.AddTemplate(newPage, 0, 0);
                }
            }
            document.Close();
        }

 

你可能感兴趣的:(pdf)