Hi,
I have encountered some issues when using Groupdocs.Watermark to stamp some files. This mainly affects Word documents as they appear to randomly generate false watermarks that are not positioned correctly, do not have the correct font family and size and an invalid bounding box. I cannot find a root cause for this issues as some documents do work and some don’t. I am using Java 17 and the Groupdocs.Watermark library version 25.9 (with a valid license) on Ubuntu 24.04.1 LTS (wsl).
This is the code I am using to generate the watermark on the last page of a Word document:
try (FileInputStream fis = new FileInputStream(fileIn);
Watermarker watermarker = new Watermarker(fis, loadOptions)) {
var font = new com.groupdocs.watermark.watermarks.Font("DejaVu Sans", 6.0f);
TextWatermark watermark = new TextWatermark("I am a watermark", font);
watermark.setForegroundColor(Color.getBlack());
watermark.setHorizontalAlignment(HorizontalAlignment.Right);
watermark.setVerticalAlignment(VerticalAlignment.Bottom);
watermark.getMargins().setBottom(3.175f);
watermark.getMargins().setRight(184.15f);
WordProcessingWatermarkPagesOptions opts = new WordProcessingWatermarkPagesOptions();
int[] pages = new int[] {watermarker.getDocumentInfo().getPageCount()}; // starts from 1
opts.setPageNumbers(pages);
opts.setLocked(true);
opts.setLockType(WordProcessingLockType.ReadOnly);
watermarker.add(watermark, opts);
watermarker.save(fileOut);
} catch (Throwable t) {
t.printStackTrace();
}
Here is a working example which works as expected:
Input: aspose-test.docx (13,7 KB)
Output: aspose-test-marked.docx (11,7 KB)
And here is the issue I am referring to which occurs on the same environment with another document:
Input: testdocx9089-k-marked-3.docx (51,3 KB)
Output: testdocx9089-k-marked-4.docx (21,9 KB)
Another example where the watermark seems to glitch:
Input: aspose-test-x.docx (27,2 KB)
Output: aspose-test-invalid.docx (16,5 KB)
(I have noticed that the file size is reduced drastically after applying the watermark. Is this expected or might this be a hint of this problem?)
On the invalid outputs you can see that the watermark is stuck on the bottom edge on a much bigger font than I have specified. Also, you can just see the upper part of the first letter of the watermark text:
image.png (350 Bytes)
I checked for missing fonts on the system and also tried to change the font size in the code but nothing seems to have an effect. So in general, I cannot see a pattern why some documents work and some don’t with the exact same code. The issue was also documented on some other environments (all using Linux) where the issue always occured. Please note that this only applies to Word documents, PDF and Powerpoint work as expected.
Could you please investigate and possible find a solution on what is causing the wrongful generation of the watermark?
–
Another issue I found while testing is the part with the locking. According to the documentation (Locking watermark in word processing document | GroupDocs), the lock should be applied on the watermark only but it has an effect on the whole document which is protected and read-only afterward. Am I reading the document wrong or is this not the expected behaviour? (On powerpoint, the locking seems to be applied on the watermark and not the file which is more reasonable.)
When trying to remove the locking at all, but keep the part where I place the watermark only on the last page, I get the following exception:
// See my code above; I just commented out this part:
// opts.setLocked(true);
// opts.setLockType(WordProcessingLockType.ReadOnly);*/
java.lang.NullPointerException: Cannot invoke "com.groupdocs.watermark.watermarks.TileOptions.getLineSpacing()" because "<parameter1>" is null
at com.groupdocs.watermark.internal.cc.a(Unknown Source)
at com.groupdocs.watermark.internal.cj.d(Unknown Source)
at com.groupdocs.watermark.internal.cA.a(Unknown Source)
at com.groupdocs.watermark.contents.WordProcessingContent.addWatermark(Unknown Source)
at com.groupdocs.watermark.contents.WordProcessingContent.add(Unknown Source)
at com.groupdocs.watermark.Watermarker.add(Unknown Source)
Currently, this forces me to apply a lock if I want to keep the watermark only on the last page. If I also remove the following line, it works without throwing an exception:
// opts.setPageNumbers(pages);
This is a second issue I found while testing. Please also review this problem since I want to be able to place the watermark on the last page only while not having the whole document locked.
–
On a side note: the Java version of the library offers a constructor for the Font class where I can provide the name, a path to the fonts folder, a size and style:
public Font(String fontFamilyName, String folderPath, float size, FontStyle style) {
Sadly, FontStyle cannot be instantiated as its constructor is private and the constants in the FontStyle class are of integer type, so I cannot use them on the Font constructor either:
public static final int Regular = 0;
public static final int Bold = 1;
public static final int Italic = 2;
public static final int Underline = 4;
public static final int Strikeout = 8;
This is more of an inconvenience to not be able to ets the style, but I just wanted to address it here as well.
Could you please look into those problems and help me find a solution? Thanks in advance!