Home > Back-end >  Merge itext PDF and add page number part of the page does not display
Merge itext PDF and add page number part of the page does not display

Time:09-28

Use itext implement merge PDF recently and to the operation of the combined PDF add page Numbers, but found that part of the page for display, check it over and over again, not as a result, everyone is there good way
Below is a combination of PDF and add the page number code:
Public static InputStream myTest (List Files) {
The Document Document=null;
InputStream bis=null;
ByteArrayOutputStream bos=null;
Try {
A Rectangle the rect=new Rectangle (PageSize. A4);
The document=new document (the rect);
Bos=new ByteArrayOutputStream ();
PdfCopy copy=new PdfCopy (document, bos);
document.open();
Int pages=0;//the current page number
for (int i=0; i //p1:
PdfReader reader=new PdfReader (files. Get (I));
PdfCopy. PageStamp stamp;//insert page Numbers don't required page can be deleted
Int n=reader. GetNumberOfPages ();
For (int j=1; J & lt;=n; J++) {
A Rectangle pageSize=reader. GetPageSize (1);
Double width=pageSize. GetWidth ();
Pages++;
Document. Brought ();
PdfImportedPage page=copy. GetImportedPage (reader, j);
Stamp=copy. CreatePageStamp (page);//insert page Numbers don't required page can be deleted
ColumnText. ShowTextAligned (stamp. GetUnderContent (), Element. ALIGN_CENTER, new Phrase (addFont (the String. Format (" first "+ pages +" page "))), (int) width * 0.5 f, 100 f, 0 f);//insert page number needed
//don't page can be deleted
Stamp. AlterContents ();//insert page Numbers don't required page can be deleted
Copy. The addPage (page);
}
Reader. The close ();
}
Copy. The close ();
} the catch (IOException e) {
e.printStackTrace();
} the catch (DocumentException e) {
e.printStackTrace();
} the finally {
Document. The close ();
}
The bis=new ByteArrayInputStream (bos toByteArray ());
Return the bis.
}
  • Related