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中沒有這樣的白色邊框。
編輯 我再舉一個嘗試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);
感謝您的回覆。 –
關於** 1 **'我正在尋找droidtext代碼來找到答案。我會閱讀這一章,你提到並嘗試解決方案'。 ** 2 **'我會記住這一點。' –
您有兩種更好的選擇來分割PDF:使用PdfCopy(結合PdfReader.selectPages()或自己選擇頁面)並使用PdfStamper和PdfReader.selectPages() 。如果由於拆分操作而需要不同的PDF,前者會更好;如果我只需要一個PDF,我更喜歡後者:選擇原始文檔的頁面。 –