PDF合并

使用的工具是itextsharp  免费的

下载链接:链接: https://pan.baidu.com/s/1nIpcRoJ1EOylYr9oCXF6ZQ 提取码: cb3a 复制这段内容后打开百度网盘手机App,操作更方便哦

代码:过程都在代码里写了,需要自己理解,不懂得可以问我

using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfReader pdfReader = null;
            PdfStamper pdfStamper = null;
            List<PdfReader> readList = new List<PdfReader>();
            Document doc = new Document();
            PdfWriter write = null;
            //数组示例,我这里使用一个字符串然后用;分割地址
            string files = "C:\\Users\\Administrator\\Desktop\\test1.pdf;C:\\Users\\Administrator\\Desktop\\test2.pdf";
            //得到地址数组
            string[] fileList = files.Split(';');
            string newFile = "C:\\Users\\Administrator\\Desktop\\combinePdf.pdf";

            try
            {
                //创建新的pdf实例,就是创建一个新的pdf
                write = PdfWriter.GetInstance(doc, new FileStream(newFile, FileMode.Create, FileAccess.Write, FileShare.None));
                doc.Open();
                PdfContentByte cb = write.DirectContent;
                
                PdfImportedPage newPage;
                //遍历地址数组
                for(int i = 0; i < fileList.Length; i++)
                {
                    //读取每个pdf
                    pdfReader = new PdfReader(fileList[i]);
                    //得到pdf的页数
                    int pageNum = pdfReader.NumberOfPages;
                    for(int j = 1; j <= pageNum; j++)
                    {
                        //创建新的一页
                        doc.NewPage();
                        //将读取的pdf第j页传到新的页
                        newPage = write.GetImportedPage(pdfReader, j);
                        //添加到新pdf里
                        cb.AddTemplate(newPage, 0, 0);
                    }
                    readList.Add(pdfReader);

                }
            }
            catch(Exception e)
            {

            }
            finally
            {
                //注意关闭和清空
                foreach(var rd in readList)
                {
                    rd.Dispose();
                }
                doc.Close();
                pdfReader.Close();
                pdfStamper.Close();
                
            }

            

        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值