Hi,
When I’m trying to add simple text footer, watermark to PDF files, I found that the implementation somehow failed to work on some PDF. Could you help let me know the causes or any solution?
Thanks
Attached two pdf files: one works, the other failed.
failure.pdf (18.4 KB)
sample.pdf (3.0 KB)
the sample code is as follows:
public static void fn3() {
// Document pdfDocument = new Document(“success.pdf”);
Document pdfDocument = new Document(“failure.pdf”);
TextStamp textStamp = new TextStamp("------ ${$Simple $Sample} 1990-2020 ${Text Simple} ------");
textStamp.setBackground(true);
textStamp.setVerticalAlignment(VerticalAlignment.Bottom);
textStamp.setHorizontalAlignment(HorizontalAlignment.Center);
textStamp.getTextState().setFont(FontRepository.findFont("Arial"));
textStamp.getTextState().setFontSize(10.0F);
textStamp.getTextState().setForegroundColor(Color.getDarkGreen());
FormattedText formattedText = new FormattedText("This is WATERMARK !!", java.awt.Color.BLUE, FontStyle.Courier, EncodingType.Identity_h, true, 72.0F);
WatermarkArtifact artifact = new WatermarkArtifact();
artifact.setText(formattedText);
artifact.setArtifactHorizontalAlignment(HorizontalAlignment.Center);
artifact.setArtifactVerticalAlignment(VerticalAlignment.Center);
artifact.setRotation(45);
artifact.setOpacity(0.5);
artifact.setBackground(true);
for (Page page: pdfDocument.getPages()) {
page.addStamp(textStamp);
page.getArtifacts().add(artifact);
}
// Save updated PDF file
pdfDocument.save("output.pdf");
}