一個PDF文件是由一系列的對象組成的,比如說一個PDF文件中的圖片是由鍵值對形式的字典組成的一個流對象,而這個流對象是由stream和endstream這兩個詞來界定的。
那麼,如果我們現在在不同的兩個頁面上,都有一個相同的圖片,我們需要避免重複的信息存儲,這樣一來,就可以達到優化和壓縮PDF文件的大小的目的。
我們來看看這兩個對象是怎麼做的:
- PdfCopy:它會存儲兩個不同的圖片流對象,在索引表中會引用兩次,因此雖然是一個相同的圖片,但是在不同的頁面上會分別顯示對應的圖片。
- PdfSmartCopy:經過它處理,會重建索引表,會把這個圖片流對象引用到不同的頁面上,而這個圖片對象只存儲了一份。
由此看來,PdfCopy處理的比較快,但是生成的PDF文件的大小會很大,很臃腫。而PdfSmartCopy處理文檔比較慢,也會消耗更多的內存,但是生成的PDF文件大小會很小。
因此,在實際使用中,爲了控制大小,一般會使用PdfSmartCopy來對PDF文件進行處理。
例如:
public static void compressPdf(byte[] pdf, OutputStream out) throws Exception {
PdfReader reader = null;
PdfSmartCopy copy = null;
Document document = null;
try {
document = new Document();
copy = new PdfSmartCopy(document, out);
reader = new PdfReader(pdf);
int n = reader.getNumberOfPages();
document.open();
for (int i=1; i<=n; i++) {
document.newPage();
PdfImportedPage imported = copy.getImportedPage(reader, i);
copy.addPage(imported);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (document != null)
document.close();
if (reader != null)
reader.close();
if (copy != null)
copy.close();
}
}