关闭

ItextSharp合并多个PDF

123人阅读 评论(0) 收藏 举报
 /// <summary> 合併PDF檔(集合) </summary>
        /// <param name="fileList">欲合併PDF檔之集合(一筆以上)</param>
        /// <param name="outMergeFile">合併後的檔名</param>
        private void mergePDFFiles(string[] fileList,string mergeName)
        {
            string pdfPath = System.Web.HttpContext.Current.Server.MapPath("~/upfile/");
            string url = "http://" + System.Web.HttpContext.Current.Request.Url.Host + ":" + System.Web.HttpContext.Current.Request.Url.Port + "/upfile/";
            PdfReader reader;
            Document doc = new Document();
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(pdfPath + mergeName, FileMode.Create));
            try
            {
                doc.Open();
                PdfContentByte cb = writer.DirectContent;
                PdfImportedPage newPage;
                if (doc.IsOpen())
                {
                    for (int i = 0; i < fileList.Length; i++)
                    {
                        string savePath = pdfPath + fileList[i];
                        reader = new PdfReader(pdfPath + fileList[i]);
                        {
                            int iPageNum = reader.NumberOfPages;
                            for (int j = 1; j <= iPageNum; j++)
                            {
                                newPage = writer.GetImportedPage(reader, j);
                                Rectangle r = reader.GetPageSize(j);
                                doc.SetPageSize(r);
                                doc.NewPage();
                                cb.AddTemplate(newPage, 0, 0);
                            }
                        }
                    }
                }
                doc.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
        }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:50994次
    • 积分:1077
    • 等级:
    • 排名:千里之外
    • 原创:56篇
    • 转载:37篇
    • 译文:0篇
    • 评论:3条