多张PDF文件合并成一个PDF文件

来源:互联网 发布:js数组去重代码 编辑:程序博客网 时间:2024/05/24 06:45

做pdf报表的时候经常会遇到用户要求单张生成的报表要求能一下打印,生成一张报表,实现起来比较麻烦,这里介绍一种合并Pdf报表的实现办法。就是用户选择全部打印的时候,在后台先单张生成报表,然后再合并成一张报表。这里用到了pdfkit.dll,可惜这个dll是付费的,免费版本会在生成的pdf上加上一行很细小的字。

程序很简单,编程语言是C#:

private void Page_Load(object sender, System.EventArgs e)
  {

  //第一个Pdf文件
   FileStream  file1 = new FileStream(@"C:/temp/01.pdf",FileMode.Open, FileAccess.Read );
   pdf.PDF.Document source1 = new TallComponents.PDF.Document( new BinaryReader( file1 ) );

  //第二个Pdf文件 

FileStream  file2 = new FileStream(@"C:/temp/02.pdf",FileMode.Open, FileAccess.Read );
   pdf.PDF.Document source2 = new TallComponents.PDF.Document( new BinaryReader( file2 ) );

   pdf.PDF.Document target = new pdf.PDF.Document();

   // append pages from the existing documents
   target.Pages.Append( source1.Pages[0] );   
   target.Pages.Append( source2.Pages[0] );
   // stream the new PDF document directly to the browser

//生成一个合并后的Pdf
   Response.ContentType = "application/pdf";
   target.Write( new BinaryWriter( Response.OutputStream ) );

 }

Acrobat的Professional版本带了一个插件PDFMAKERAPILib,提供了一个

接口:CreatePDFFromMultipleFiles(ref System.Array ary)似乎也能实现这种功能,

但是这个传入的参数一直提示不正确,没研究明白,请高手研究一下吧。