I need to append a Watermark to a PDF file, but I would like to avoid creating a new file. The following code will not work unless I give the outputstream a different name. How can the syntax and/or order here be adjusted to get the result I am after?
public class AppendHistoricalWatermark {
public AppendHistoricalWatermark(SmbFile smbfile){
System.out.println("AppendHistoricalWatermark: " + smbfile.getPath());
try{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(smbfile.getInputStream()), new PdfWriter(smbfile.getOutputStream()));
Document document = new Document(pdfDoc);
Rectangle pageSize;
PdfCanvas canvas;
int n = pdfDoc.getNumberOfPages();
System.out.println("AppendHistoricalWatermark: NumberOfPages: " + n);
for (int i = 1; i <= n; i++) {
PdfPage page = pdfDoc.getPage(i);
pageSize = page.getPageSize();
canvas = new PdfCanvas(page);
Paragraph p = new Paragraph("HISTORICAL").setFontSize(60);
canvas.saveState();
PdfExtGState gs1 = new PdfExtGState().setFillOpacity(0.2f);
canvas.setExtGState(gs1);
document.showTextAligned(p, pageSize.getWidth() / 2, pageSize.getHeight() / 2, pdfDoc.getPageNumber(page),
TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
canvas.restoreState();
}
pdfDoc.close();
}
catch(Exception e){
System.out.println("AppendHistoricalWatermark: " + e);
e.printStackTrace();
}
}
}