Unable to add watermark to the PDF on MacOS Catalina and Amazon Linux 2 operating systems

Hello Team, we are facing issue regarding watermark is not printed on the created PDF.

One example, we are converting word file to pdf using Aspose library. The pdf is generated successfully but without the watermark. The same code base works fine on the windows machine. We also tried to use the font families which are available on the mentioned operating systems but still no luck. We are working with java 8 and soon will be upgrading to Java 11.

Aspose dependencies in pom.xml:

com.aspose
aspose-cells
19.1


com.aspose
aspose-words
19.1
jdk16


com.aspose
aspose-pdf
18.12


com.aspose
aspose-slides
18.12
jdk16


com.aspose
aspose-diagram
18.12
jdk16


com.aspose
aspose-tasks
18.11
jdk17


com.aspose
aspose-barcode
18.12
jdk17


com.aspose
aspose-email
18.9
jdk16


com.aspose
aspose-imaging
18.11


com.aspose
aspose-note
19.1
jdk17


com.aspose
aspose-cad
18.3
jdk16

Following code is written for watermark printing:
private TextStamp getBodyTextStamp(String watermarkText) {
FormattedText formatText = new FormattedText(watermarkTextArray[0]);
TextStamp textStamp = new TextStamp(formatText);
textStamp.setRotateAngle((double)getWatermarkAngle());
textStamp.setHorizontalAlignment(getWatermarkHorizontalAlignment());
textStamp.setVerticalAlignment(getWatermarkVerticalAlignment());
textStamp.getTextState().setFont(FontRepository.findFont(getWatermarkFontName()));
textStamp.getTextState().setFontSize((float)getWatermarkFontSize());
textStamp.setBackground(false);
textStamp.getTextState().setForegroundColor(Color.fromRgb(java.awt.Color.decode(getWatermarkColor())));
if (isWatermarkOutlineOnly()) {
textStamp.setDraw(isWatermarkOutlineOnly());
textStamp.setOutlineOpacity(.getWatermarkOpacity());
textStamp.setOpacity(0.0D);
} else {
textStamp.setOpacity(.getWatermarkOpacity());
}

        return textStamp;
    } else {
        return null;
    }
}

private void appendStamp(Document pdfDocument, TextStamp textStamp) {
if (textStamp != null) {
PageCollection pageCollection = pdfDocument.getPages();
for(int i = 1; i <= pageCollection.size(); ++i) {
pageCollection.get_Item(i).addStamp(textStamp);
}

    }
}

Could you please help us to find the root cause?

Thank you!

@abhave

Could you kindly try to use latest version of the APIs e.g. Aspose.PDF for Java 20.7. Also, please try to install all windows fonts in your system where issue is occuring. In case it still persists, please share your sample PDF document with us so that we can test the scenario in our environment and address it accordingly.

Hello Asad,

I tried with 20.7 version but still no luck. Please find the attached pdf file to apply watermark on.Test Document to convert to pdf.PDF (58.1 KB)

The code -> FontRepository.findFont(getWatermarkFontName())
Returns the instance of the Font but the end result does not show watermark printed on PDF file.

Please help us out.

Thank you!

@abhave

We will surely investigate the issue that you are facing and share our feedback with you. However, as requested earlier, would you please confirm that all windows fonts are installed in the system where you are facing the issue?

Hello Asad,

I confirm that we have installed windows fonts but still facing the issue.

Thank you!

@abhave

We have logged an investigation ticket as PDFJAVA-39670 in our issue tracking system for further anlaysis against your case. We will investigate the reasons behind this issue and keep you informed with the status of ticket resolution. Please be patient and spare us some time.

We are sorry for the inconvenience.

Hello Team,

Could you please share the progress on this issue?

Thank you!

@abhave

The earlier logged ticket is not yet resolved. It will be investigated and resolved on first come first serve basis. We will inform you as soon as we have some definite updates regarding its resolution. Please give us some time.

We are sorry for the inconvenience.

@abhave

We have investigated the earlier logged ticket and found that you got this problem because the opacity parameter was 0.

textStamp.setOpacity(0.0D);

We tested this problem with the next code snipped and got PDF with a watermark.

  Document document = new Document();
        com.aspose.pdf.Page page = document.getPages().add();
        TextFragment textFragment = new TextFragment("");
        TextSegment seg1 = new TextSegment("Bold black text ");
        String text = "text ";
        for (int i = 0; i < 500; i++) {
            text += "text ";
        }
        seg1.setText(text);
        seg1.getTextState().setFontStyle((FontStyles.Bold));
        seg1.getTextState().setFontSize(16);
        textFragment.getSegments().add(seg1);
        page.getParagraphs().add(textFragment);

        FormattedText formatText = new FormattedText("This is watermark text!");
        TextStamp textStamp = new TextStamp(formatText);
        textStamp.setRotateAngle(45);
        textStamp.setHorizontalAlignment(HorizontalAlignment.Center);
        textStamp.setVerticalAlignment(VerticalAlignment.Center);
        textStamp.getTextState().setFont(FontRepository.findFont("Arial"));
        textStamp.getTextState().setFontSize((float) 24);
        textStamp.setBackground(false);
        textStamp.getTextState().setForegroundColor(Color.getRed());
        textStamp.setOpacity(100);
        PageCollection pageCollection = document.getPages();
        for (int i = 1; i <= pageCollection.size(); ++i) {
            pageCollection.get_Item(i).addStamp(textStamp);
        }

        document.save(myDir + "output.pdf");

Also, the code snippet was tested on macOS Catalina and macOS Big Sur, and all worked fine.