2012-04-11 152 views
2

我已经使用了iText的一些各种实用工具,例如我们合并和成功编辑PDF文件。现在我需要重叠2个PDF页面:PDF合并/与iText重叠

比如: INPUT: PDF#1(共1页) PDF#2(共1页)

OUTPUT: PDF#3(共1页:这是2输入页重叠的结果)

我不知道是否有可能使用iText最新版本进行此操作。我也在考虑将2个输入PDF文件中的一个用作PDF输出文件的背景。

预先感谢您。

回答

3

这实际上很容易做到。 PdfWriter对象具有一个名为GetImportedPage()的实例方法,该方法返回一个PdfImportedPage对象。该对象可以传递给PdfContentByteAddTemplate()方法。

GetImportedPage()需要一个PdfReader对象和您想要获得的页码。您可以从PdfWriterDirectContent属性的实例中获得PdfContentByte

下面的代码是一个完整的工作C#2010 WinForms应用程序定位iTextSharp 5.1.2.0,显示这一切。它首先在桌面上创建两个文件,第一个只有一个纯色的红色背景,第二个只有一个段落。然后它将这两个文件重叠成第三个文档。请参阅代码以获取其他意见。

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

namespace WindowsFormsApplication1 { 
    public partial class Form1 : Form { 
     public Form1() { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) { 
      //Folder that we'll work from 
      string workingFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
      string pdf1 = Path.Combine(workingFolder, "pdf1.pdf");//PDF with solid red background color 
      string pdf2 = Path.Combine(workingFolder, "pdf2.pdf");//PDF with text 
      string pdf3 = Path.Combine(workingFolder, "pdf3.pdf");//Merged PDF 

      //Create a basic PDF filled with red, nothing special 
      using (FileStream fs = new FileStream(pdf1, FileMode.Create, FileAccess.Write, FileShare.None)) { 
       using (Document doc = new Document(PageSize.LETTER)) { 
        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) { 
         doc.Open(); 
         PdfContentByte cb = writer.DirectContent; 
         cb.SetColorFill(BaseColor.RED); 
         cb.Rectangle(0, 0, doc.PageSize.Width, doc.PageSize.Height); 
         cb.Fill(); 
         doc.Close(); 
        } 
       } 
      } 

      //Create a basic PDF with a single line of text, nothing special 
      using (FileStream fs = new FileStream(pdf2, FileMode.Create, FileAccess.Write, FileShare.None)) { 
       using (Document doc = new Document(PageSize.LETTER)) { 
        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) { 
         doc.Open(); 
         doc.Add(new Paragraph("This is a test")); 
         doc.Close(); 
        } 
       } 
      } 

      //Create a basic PDF 
      using (FileStream fs = new FileStream(pdf3, FileMode.Create, FileAccess.Write, FileShare.None)) { 
       using (Document doc = new Document(PageSize.LETTER)) { 
        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) { 
         doc.Open(); 

         //Get page 1 of the first file 
         PdfImportedPage imp1 = writer.GetImportedPage(new PdfReader(pdf1), 1); 
         //Get page 2 of the second file 
         PdfImportedPage imp2 = writer.GetImportedPage(new PdfReader(pdf2), 1); 
         //Add the first file to coordinates 0,0 
         writer.DirectContent.AddTemplate(imp1, 0, 0); 
         //Since we don't call NewPage the next call will operate on the same page 
         writer.DirectContent.AddTemplate(imp2, 0, 0); 
         doc.Close(); 
        } 
       } 
      } 

      this.Close(); 
     } 
    } 
} 
+0

很不错好工作.. – 2014-07-01 13:45:45