更新时间:2022-06-24 08:29:19
根据mkl,我使用了PdfStamper:
According to mkl I used the PdfStamper:
private static void print(Sldocuments item, String header, String footer) {
try {
String ftpFilename = item.getId()+"_"+item.getDocumentname();
String newName= String.valueOf(item.getId())+".pdf";
String path = (Global.SHARED_FOLDER_DEVELOPER);
String smbUser = "**;"+"**" + ":" + "**";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(smbUser);
SmbFile sFile = new SmbFile(path+ftpFilename, auth);
InputStream in = sFile.getInputStream();
PdfReader reader = new PdfReader(in);
// Create output PDF
SmbFile sFileOut = new SmbFile(path+newName, auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFileOut);
PdfStamper stamper = new PdfStamper(reader, sfos);
// Loop over the pages and add a header to each page
int n = reader.getNumberOfPages();
for (int i = 1; i <= n; i++) {
//add header
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(PDF_PAGE_SIZE.getWidth()-(headerPositionX*2));
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(20);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.addCell(header);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell("");
table.writeSelectedRows(0, -1, headerPositionX, headerPositionY, stamper.getOverContent(i));
//add footer
PdfPTable tableFooter = new PdfPTable(2);
tableFooter.setTotalWidth(PDF_PAGE_SIZE.getWidth()-(footerPositionX*2));
tableFooter.setLockedWidth(true);
tableFooter.getDefaultCell().setFixedHeight(20);
tableFooter.getDefaultCell().setBorder(Rectangle.NO_BORDER);
tableFooter.addCell(footer);
tableFooter.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
tableFooter.addCell(String.format("pagina %d of %d", i, n));
tableFooter.writeSelectedRows(0, -1, footerPositionX, footerPositionY*4, stamper.getOverContent(i));
}
// Close the stamper
stamper.close();
reader.close();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}