Adding watermark/textstamp NOT working on some PDF file

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");
}

@gyangptc

Can you please share what actual issue you faced? Does it throw some exception or output PDF has some issues?

Success one will have watermark and footer added;
Failure one had no error message or exception but just got a result pdf with NO watermark/footer added.

Thanks,

@gyangptc

Please change artifact.setBackground(true); to artifact.setBackground(false); in order to get this working for all the PDFs. The reason artifact.setBackground(true); is working for sample.pdf because it does not have any content at the location where watermark is rendering. In case setBackground is true, the watermark will be rendered behind the layer of the actual content.

Got it, thanks!