最近使用itext實作合并pdf并對合并后的pdf添加頁碼的操作,但是發現,部分頁碼為顯示,反復查閱,沒有結果,大家有沒有好的辦法
下面是合并PDF和添加頁碼的代碼:
public static InputStream myTest(List<InputStream> files) {
Document document = null;
InputStream bis = null;
ByteArrayOutputStream bos = null;
try {
Rectangle rect = new Rectangle(PageSize.A4);
document = new Document(rect);
bos = new ByteArrayOutputStream();
PdfCopy copy = new PdfCopy(document, bos);
document.open();
int pages = 0;// 當前頁碼
for (int i = 0; i < files.size(); i++) {
// 第幾個
PdfReader reader = new PdfReader(files.get(i));
PdfCopy.PageStamp stamp;// 插入頁碼所需 不要頁碼可洗掉
int n = reader.getNumberOfPages();
for (int j = 1; j <= n; j++) {
Rectangle pageSize = reader.getPageSize(1);
double width = pageSize.getWidth();
pages++;
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
stamp = copy.createPageStamp(page);// 插入頁碼所需 不要頁碼可洗掉
ColumnText.showTextAligned(stamp.getUnderContent(), Element.ALIGN_CENTER, new Phrase(addFont(String.format("第" + pages + "頁"))), (int)width*0.5f, 100f, 0f);// 插入頁碼所需
// 不要頁碼可洗掉
stamp.alterContents();// 插入頁碼所需 不要頁碼可洗掉
copy.addPage(page);
}
reader.close();
}
copy.close();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
document.close();
}
bis = new ByteArrayInputStream(bos.toByteArray());
return bis;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/83530.html
標籤:Java相關
上一篇:idea用maven創建專案報錯Unable to import maven project: See logs for details
