2010-10-06 70 views
2

示例代碼:iTextSharp爲什麼重新調整文檔大小而不是合併文檔?

using System; 
using System.IO; 
using System.Collections.Generic; 
using System.Text; 
using iTextSharp.text; 
using iTextSharp.text.pdf; 

namespace MergePDFs 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      try 
      { 
       int f = 1; 
       // we create a reader for a certain document 
       PdfReader reader = new PdfReader(args[f]); 
       // we retrieve the total number of pagse 
       int n = reader.NumberOfPages; 
       Console.WriteLine("There are " + n + " pages in the original file."); 
       // step 1: creation of a document-object 
       Document document = new Document(reader.GetPageSizeWithRotation(1)); 
       Console.WriteLine("PS: " + reader.GetPageSizeWithRotation(1)); 
       // step 2: we create a writer that listens to the document 
       String destinationFile = args[0];//The first argument is the destination 
       PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create)); 
       // step 3: we open the document 
       document.Open(); 
       PdfContentByte cb = writer.DirectContent; 
       PdfImportedPage page; 
       int rotation; 
       // step 4: we add content 
       while (f < args.Length) 
       { 
        int i = 0; 
        while (i < n) 
        { 
         i++; 
         //document.SetPageSize(reader.GetPageSizeWithRotation(i)); 
         document.NewPage(); 
         page = writer.GetImportedPage(reader, i); 
         rotation = reader.GetPageRotation(i); 
         if (rotation == 90 || rotation == 270) 
         { 
          cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height); 
         } 
         else 
         { 
          cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0); 
         } 
         Console.WriteLine("Processed page " + i); 
        } 
        f++; 
        if (f < args.Length) 
        { 
         reader = new PdfReader(args[f]); 
         // we retrieve the total number of pages 
         n = reader.NumberOfPages; 
         Console.WriteLine("There are " + n + " pages in the original file."); 
        } 
       } 
       // step 5: we close the document 
       document.Close(); 
      } 
      catch (Exception e) 
      { 
       Console.Error.WriteLine(e.Message); 
       Console.Error.WriteLine(e.StackTrace); 
      } 
     } 
    } 
} 

我希望它可以輸出相同的尺寸,這兩個文件來(這在我的情況是相同的尺寸)的文件。不幸的是,這並沒有發生,並且在合併文檔周圍正在打印白色邊框。任何幫助將不勝感激。

回答

0

嘗試

cb.AddTemplate(page, 0, 0); 
1

這是完全可能的,原始的PDF文件有作物箱子除了他們的媒體盒。

如果您確實只是想將文件A & B中的頁面複製到文件C中,請使用PdfCopy。

http://itextpdf.com/examples/iia.php?id=123

String[] files = { MovieLinks1.RESULT, MovieHistory.RESULT }; 
    // step 1 
    Document document = new Document(); 
    // step 2 
    PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT)); 
    // step 3 
    document.open(); 
    // step 4 
    PdfReader reader; 
    int n; 
    // loop over the documents you want to concatenate 
    for (int i = 0; i < files.length; i++) { 
     reader = new PdfReader(files[i]); 
     // loop over the pages in that document 
     n = reader.getNumberOfPages(); 
     for (int page = 0; page < n;) { 
      copy.addPage(copy.getImportedPage(reader, ++page)); 
     } 
    } 
    // step 5 
    document.close(); 

該技術將保留註釋(包括場,但你真的應該使用PdfCopyFields如果你想圍繞正確遷移領域),但不是書籤。我不確定圖層...我是這麼認爲的。

哦,你可能想要在完成每一個後添加copy.freeReader(reader) ......特別是如果你打算循環使用它們中的很多。

相關問題