使用的工具是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();
}
}
}
}