2012-04-11 15 views
2

私は、iTextをさまざまなユーティリティに使用しています。私たちは、pdfファイルをマージして編集して成功を収めています。例えばPdf Merge/Overlap with iText

: INPUT: PDF#1(1ページ) PDF#2(1ページ)

OUTPUT: PDF#3(1ページ:今私は2 PDFページをオーバーラップする必要がありますこれは、 2つの入力ページが重複した結果です)

iTextの最新バージョンでこれを行うことができるかどうかわかりません。また、2つの入力PDFファイルの1つをPDF出力ファイルの背景として使用することを検討しています。

ありがとうございます。

答えて

3

実際はかなり簡単です。 PdfWriterオブジェクトにはGetImportedPage()というインスタンスメソッドがあり、PdfImportedPageオブジェクトを返します。このオブジェクトはPdfContentByteAddTemplate()メソッドに渡すことができます。

GetImportedPage()は、取得するオブジェクトとページ番号がPdfReaderです。 PdfWriterDirectContentプロパティのインスタンスからPdfContentByteを取得できます。

以下のコードは、iTextSharp 5.1.2.0をターゲットとした完全なC#2010 WinFormsアプリケーションで、これをすべてオフにしています。デスクトップ上に最初に2つのファイルを作成します。最初は赤色の背景色が赤色で、2番目のファイルは段落だけです。次に、これらの2つのファイルが重複して3番目の文書に結合されます。追加のコメントについては、コードを参照してください。

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

非常にnice good job .. –

関連する問題