2011-11-22 3 views
0

다른 pdf에 pdf를 추가하려고합니다. Document doc.Add (IElement)는 내가 아는 것이며 pdf를 추가하는 데 어려움이 있습니다. 이미지 추가를 시도했는데 효과가있었습니다. 내 문서에 PDF 파일을 추가하려면 어떻게합니까?문서 doc.Add (pdf)가 가능합니까?

iTextSharp.text.Image img; 
foreach (var buf in List) { 
    myDoc.NewPage(); 
    img = iTextSharp.text.Image.GetInstance(buf); 
    img.ScaleToFit(612f, 792f); 
    img.Alignment = iTextSharp.text.Image.ALIGN_CENTER | iTextSharp.text.Image.ALIGN_MIDDLE; 
    myDoc.Add(img); 
} 

답변

0

Document.Add()을 사용할 수는 없지만 여전히 쉽습니다. 먼저 소스 문서를 읽으려면 PdfReader 개체를 만들어야합니다. 그런 다음 대상 문서의 PdfWriter을 사용하고 가져올 각 페이지에서 GetImportedPage(reader, pageNumber) 메서드를 호출하십시오. 그러면 PdfWriter.DirectContent.AddTemplate()에 전달할 수있는 PdfImportedPage 개체가 생깁니다.

다음은이 모든 단계를 보여주는 iTextSharp 5.1.1.0을 대상으로하는 전체 C# 2010 WinForms 응용 프로그램입니다. 먼저 샘플 파일 (File1)을 만든 다음 두 번째 파일 (File2)을 만들고 첫 번째 파일을 추가합니다. 회전 된 페이지를 처리하는 방법과 같은 몇 가지 엣지 경우를 과시하기 위해 몇 가지 추가 작업을 수행합니다. 자세한 내용은 코드 주석을 참조하십시오.

using System; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 
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) 
     { 
      //The names of the two files that we'll create 
      string File1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File1.pdf"); 
      string File2 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "File2.pdf"); 

      //Create a test file to merge into the second file 
      using (FileStream fs = new FileStream(File1, FileMode.Create, FileAccess.Write, FileShare.None)) 
      { 
       //We'll create this one landscape to show some checks that need to be done later 
       using (Document doc = new Document(PageSize.TABLOID.Rotate())) 
       { 
        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) 
        { 
         doc.Open(); 
         doc.Add(new Paragraph("File 1")); 
         doc.Close(); 
        } 
       } 
      } 

      //Create our second file 
      using (FileStream fs = new FileStream(File2, FileMode.Create, FileAccess.Write, FileShare.None)) 
      { 
       //Create this one as a regular US Letter portrait sized document 
       using (Document doc = new Document(PageSize.LETTER)) 
       { 
        using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) 
        { 
         doc.Open(); 

         doc.Add(new Paragraph("File 2")); 

         //Create a PdfReader to read our first file 
         PdfReader r = new PdfReader(File1); 

         //Store the number of pages 
         int pageCount = r.NumberOfPages; 

         //Variables which will be set in the loop below 
         iTextSharp.text.Rectangle rect; 
         PdfImportedPage imp; 

         //Loop through each page in the source document, remember that page indexes start a one and not zero 
         for (int i = 1; i <= pageCount; i++) 
         { 
          //Get the page's dimension and rotation 
          rect = r.GetPageSizeWithRotation(i); 

          //Get the actual page 
          imp = writer.GetImportedPage(r, i); 

          //These two commands must happen in this order 
          //First change the "default page size" to match the current page's size 
          doc.SetPageSize(rect); 
          //then add a new blank page which we'll fill with our actual page 
          doc.NewPage(); 

          //If the document has been rotated one twist left or right (90 degrees) then we need to add it a little differently 
          if (rect.Rotation == 90 || rect.Rotation == 270) 
          { 
           //Add the page accounting for the rotation 
           writer.DirectContent.AddTemplate(imp, 0, -1, 1, 0, 0, rect.Height); 
          } 
          else 
          { 
           //Add the page normally 
           writer.DirectContent.AddTemplate(imp, 0, 0); 
          } 

         } 

         doc.Close(); 
        } 
       } 
      } 
      this.Close(); 
     } 
    } 
} 
관련 문제