怎樣注冊公司流程山西seo優(yōu)化公司
- 優(yōu)化方法一:使用PdfSmartCopy類代替PdfCopy類。這個類可以在合并PDF文件時,檢測并消除重復(fù)的對象,從而減少內(nèi)存的占用。您可以參考以下代碼示例:
//創(chuàng)建一個Document對象
Document document = new Document();//創(chuàng)建一個PdfSmartCopy對象
PdfSmartCopy copy = new PdfSmartCopy(document, new FileOutputStream("output.pdf"));//打開Document對象
document.open();//創(chuàng)建一個PdfReader對象
PdfReader reader = null;//遍歷要合并的PDF文件
for (String file : files) {//加載PDF文件reader = new PdfReader(file);//獲取PDF文件的總頁數(shù)int n = reader.getNumberOfPages();//遍歷每一頁,添加到PdfSmartCopy對象中for (int page = 0; page < n;) {copy.addPage(copy.getImportedPage(reader, ++page));}
}//關(guān)閉PdfReader對象
reader.close();//關(guān)閉Document對象
document.close();//創(chuàng)建一個PdfStamper對象
PdfStamper stamper = new PdfStamper(new PdfReader("output.pdf"), new FileOutputStream("output.pdf"));//獲取總頁數(shù)
int pageCount = stamper.getReader().getNumberOfPages();//遍歷每一頁,添加頁碼
for (int i = 1; i <= pageCount; i++) {//獲取當(dāng)前頁PdfContentByte content = stamper.getOverContent(i);//設(shè)置字體和顏色content.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12);content.setRGBColorFill(0, 0, 0);//獲取當(dāng)前頁的寬度和高度Rectangle pageSize = stamper.getReader().getPageSize(i);float width = pageSize.getWidth();float height = pageSize.getHeight();//計算頁碼的位置float x = width / 2;float y = 10;//添加頁碼content.beginText();content.showTextAligned(PdfContentByte.ALIGN_CENTER, "第" + i + "頁,共" + pageCount + "頁", x, y, 0);content.endText();
}//關(guān)閉PdfStamper對象
stamper.close();
- 優(yōu)化方法二:使用PdfWriter類代替PdfCopy類。這個類可以在合并PDF文件時,直接將每一頁寫入到輸出流中,而不需要將整個文檔加載到內(nèi)存中??梢詤⒖家韵麓a示例:
//創(chuàng)建一個Document對象
Document document = new Document();//創(chuàng)建一個PdfWriter對象
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));//打開Document對象
document.open();//創(chuàng)建一個PdfImportedPage對象
PdfImportedPage page = null;//創(chuàng)建一個PdfReader對象
PdfReader reader = null;//遍歷要合并的PDF文件
for (String file : files) {//加載PDF文件reader = new PdfReader(file);//獲取PDF文件的總頁數(shù)int n = reader.getNumberOfPages();//遍歷每一頁,添加到PdfWriter對象中for (int i = 1; i <= n; i++) {//獲取當(dāng)前頁的寬度和高度Rectangle pageSize = reader.getPageSizeWithRotation(i);float width = pageSize.getWidth();float height = pageSize.getHeight();//設(shè)置Document對象的頁面大小document.setPageSize(pageSize);//創(chuàng)建一個新的頁面document.newPage();//導(dǎo)入當(dāng)前頁page = writer.getImportedPage(reader, i);//將當(dāng)前頁添加到PdfWriter對象中writer.addPageDictEntry(PdfName.ROTATE, pageSize.getRotationAsPageDictEntry());writer.addDirectImageSimple(page);writer.getCurrentPage().add(page);//創(chuàng)建一個PdfContentByte對象PdfContentByte content = writer.getDirectContent();//設(shè)置字體和顏色content.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12);content.setRGBColorFill(0, 0, 0);//計算頁碼的位置float x = width / 2;float y = 10;//添加頁碼content.beginText();content.showTextAligned(PdfContentByte.ALIGN_CENTER, "第" + i + "頁,共" + pageCount + "頁", x, y, 0);content.endText();}
}//關(guān)閉PdfReader對象
reader.close();//關(guān)閉Document對象
document.close();
- 優(yōu)化方法三:使用PdfReader類的partial和selectPages方法。這些方法可以在加載PDF文件時,只讀取需要的頁面,而不需要將整個文檔加載到內(nèi)存中??梢詤⒖家韵麓a示例:
//創(chuàng)建一個Document對象
Document document = new Document();//創(chuàng)建一個PdfCopy對象
PdfCopy copy = new PdfCopy(document, new FileOutputStream("output.pdf"));//打開Document對象
document.open();//創(chuàng)建一個PdfReader對象
PdfReader reader = null;//遍歷要合并的PDF文件
for (String file : files) {//設(shè)置partial為true,只讀取需要的頁面reader = new PdfReader(new RandomAccessFileOrArray(file), null);reader.consolidateNamedDestinations();reader.partial = true;//獲取PDF文件的總頁數(shù)int n = reader.getNumberOfPages();//創(chuàng)建一個List對象,存儲需要的頁面List<Integer> pages = new ArrayList<Integer>();//遍歷每一頁,添加到List對象中for (int i = 1; i <= n; i++) {pages.add(i);}//使用selectPages方法,只選擇需要的頁面reader.selectPages(pages);//將選擇的頁面添加到PdfCopy對象中for (int i = 0; i < pages.size(); ) {copy.addPage(copy.getImportedPage(reader, ++i));}
}//關(guān)閉PdfReader對象
reader.close();//關(guān)閉Document對象
document.close();//創(chuàng)建一個PdfStamper對象
PdfStamper stamper = new PdfStamper(new PdfReader("output.pdf"), new FileOutputStream("output.pdf"));//獲取總頁數(shù)
int pageCount = stamper.getReader().getNumberOfPages();//遍歷每一頁,添加頁碼
for (int i = 1; i <= pageCount; i++) {//獲取當(dāng)前頁PdfContentByte content = stamper.getOverContent(i);//設(shè)置字體和顏色content.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12);content.setRGBColorFill(0, 0, 0);//獲取當(dāng)前頁的寬度和高度Rectangle pageSize = stamper.getReader().getPageSize(i);float width = pageSize.getWidth();float height = pageSize.getHeight();//計算頁碼的位置float x = width / 2;float y = 10;//添加頁碼content.beginText();content.showTextAligned(PdfContentByte.ALIGN_CENTER, "第" + i + "頁,共" + pageCount + "頁", x, y, 0);content.endText();
}//關(guān)閉PdfStamper對象
stamper.close();
[java - How to avoid OutOfMemoryError when merging PDFs using iText? - Stack Overflow]
[java - How to merge pdf files without loading all the documents in memory? - Stack Overflow]
[java - How to merge pdf files without loading all the documents in memory? - Stack Overflow]