2012-10-11 10 views
1

我沒有給出問題的適當標題。 :)使用PDF文件生成新的PDF結果在白色的地方

任何我需要從現有的PDF文件拆分(獲取)頁面。我正在使用droidtext。

我的代碼是

try { 

     String path = Environment.getExternalStorageDirectory() 
       + "/test123.pdf"; 
        /*Read Existing PDF*/ 
     PdfReader reader = new PdfReader(path); 

     Document doc = new Document(); 
     doc.open(); 

     File outfile = new File(Environment.getExternalStorageDirectory() 
       + "/test_new.pdf"); 
     if (!outfile.exists()) 
      outfile.createNewFile(); 

     FileOutputStream decfos = new FileOutputStream(outfile); 

     Document document = new Document(); 
     PdfWriter writer = PdfWriter.getInstance(document, decfos); 
     document.open(); 
     /*Getting First page*/ 
     PdfImportedPage page = writer.getImportedPage(reader, 1); 

     Image instance = Image.getInstance(page); 

     document.add(instance); 
     document.close(); 

    } catch (DocumentException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

我要創建 「test123.pdf」 文件中的一個頁面的PDF。它正在創建新的PDF。

但問題是在新的PDF文件中有白色邊框。我怎樣才能刪除這些空白。 在原始PDF中沒有這樣的白色邊框。

enter image description here

編輯 我再舉一個嘗試follwing代碼。但它給空指針異常的copy.addPage(page);

String path = Environment.getExternalStorageDirectory() 
       + "/test123.pdf"; 
     PdfReader reader = new PdfReader(path); 

     PdfImportedPage page; 
     PdfSmartCopy.PageStamp stamp; 
     File outfile = new File(Environment.getExternalStorageDirectory() 
       + "/test_new.pdf"); 
     Document doc = new Document(); 
     if (!outfile.exists()) 
      outfile.createNewFile(); 

     FileOutputStream decfos = new FileOutputStream(outfile); 
     PdfSmartCopy copy = new PdfSmartCopy(doc, decfos); 
     page = copy.getImportedPage(reader, 5); 

     stamp = copy.createPageStamp(page); 

     stamp.alterContents(); 
     copy.addPage(page); 

回答

2

我投的問題了,因爲兩個原因:

  1. 你沒看過的官方文檔。請參閱Edit DirectContent of iTextSharp PdfSmartCopy class以瞭解爲何不閱讀文檔是錯誤的。
  2. 我是iText的原始開發人員,我不贊同使用DroidText。這不是官方的iText版本。我強烈建議不要使用它:http://lowagie.com/itext2請注意,我住在比利時,而且根據比利時法律,我擁有我製作的所有版權的道德權利。這包括iText 2.1.7。

至於你的問題:你正在創建格式爲A4的頁面。在這些頁面中添加未知大小的導入頁面。如果這些頁面的大小也是A4,則它們將適合。如果他們有不同的大小,他們不會。要麼他們會被裁剪,要麼他們會有不必要的利潤。

+1

感謝您的回覆。 –

+0

關於** 1 **'我正在尋找droidtext代碼來找到答案。我會閱讀這一章,你提到並嘗試解決方案'。 ** 2 **'我會記住這一點。' –

+1

您有兩種更好的選擇來分割PDF:使用PdfCopy(結合PdfReader.selectPages()或自己選擇頁面)並使用PdfStamper和PdfReader.selectPages() 。如果由於拆分操作而需要不同的PDF,前者會更好;如果我只需要一個PDF,我更喜歡後者:選擇原始文檔的頁面。 –