iTextSharpH

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;


public class iTextSharpH
{
    /// <summary> 合并PDF </summary>
    /// <param name="fileList">PDF文件集合</param>
    /// <param name="outMergeFile">合并文件名</param>
    public static void MergePDFFiles(string[] fileList, string outMergeFile)
    {
        List<PdfReader> readerList = new List<PdfReader>();//记录合并PDF集合
        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 (!string.IsNullOrEmpty(fileList[i]))
            {
                PdfReader 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);
                }
                readerList.Add(reader);
            }
        }
        document.Close();
        //释放集合资源
        foreach (var rd in readerList)
        {
            rd.Close();
        }
    }
}

 

posted @ 2018-07-23 16:02  -Ward-  阅读(141)  评论(0编辑  收藏  举报