.NET 实现将多个PDF简单合并

方法引用了iTextSharp.dll

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

        /// <summary>
        /// 将多个PDF简单合并
        /// </summary>
        /// <param name="files">多个pdf文件路径</param>
        /// <param name="saveFileName">合并后的pdf名字</param>
        public static void MergePdf(List<string> files,string saveFileName)
        {
            Document doc = new Document();
            PdfCopy pdfcopy = new PdfCopy(doc, new FileStream(saveFileName, FileMode.Create));
            try
            {
                doc.Open();
                for (int i = 0; i < files.Count; i++)
                {
                    PdfReader reader = new PdfReader(files[i]);
                    int pageNum = reader.NumberOfPages;
                    for (int j = 1; j <= pageNum; j++)
                    {
                        PdfImportedPage importPage = pdfcopy.GetImportedPage(reader, j);
                        pdfcopy.AddPage(importPage);
                    }
                    reader.Close();
                }
                doc.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                doc.Close();
            }
        }
实现方法

 

posted @ 2022-12-27 17:00  官方小可爱  阅读(161)  评论(0编辑  收藏  举报