Issue with watermarks on Word documents - invalid formatting

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!

@Nobl

It seems you’re experiencing issues with watermarking Word documents using the GroupDocs.Watermark library. Here are some suggestions to address the problems you’ve encountered:

  1. Invalid Watermark Formatting: The issues with incorrect font family, size, and positioning may stem from missing fonts on your system. Ensure that the font “DejaVu Sans” is installed and accessible in your environment. If the font is not available, the library may default to a different font, leading to unexpected results.

  2. File Size Reduction: The significant reduction in file size after applying the watermark could indicate that the watermarking process is altering the document’s structure or content. This behavior might be expected if the library optimizes the document during watermarking, but it’s worth investigating further if it leads to data loss.

  3. Locking Behavior: Regarding the locking mechanism, it appears that the lock is being applied to the entire document rather than just the watermark. This could be a limitation or a bug in the library. You might want to check the documentation or reach out to support for clarification on this behavior.

  4. NullPointerException: The exception you encountered when trying to remove the locking while keeping the watermark on the last page suggests that the opts.setPageNumbers(pages); line is necessary for the watermarking process. If you want to avoid locking the document, you may need to explore alternative methods to specify the page for the watermark without triggering the exception.

  5. FontStyle Issue: The inability to instantiate FontStyle due to its private constructor is indeed a limitation. You can use the integer constants directly when creating the Font object, as they represent the styles you need.

Here’s a modified version of your code snippet that includes a check for font availability and handles the locking issue:

try (FileInputStream fis = new FileInputStream(fileIn);
     Watermarker watermarker = new Watermarker(fis, loadOptions)) {
    // Check if the font is available
    if (!isFontAvailable("DejaVu Sans")) {
        System.out.println("Font 'DejaVu Sans' is not available. Please install it.");
        return;
    }

    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();
    opts.setPageNumbers(new int[]{watermarker.getDocumentInfo().getPageCount()}); // last page

    // Only apply locking if necessary
    // opts.setLocked(true);
    // opts.setLockType(WordProcessingLockType.ReadOnly);

    watermarker.add(watermark, opts);
    watermarker.save(fileOut);
} catch (Throwable t) {
    t.printStackTrace();
}

// Method to check if a font is available
private boolean isFontAvailable(String fontName) {
    // Implement font checking logic here
    return true; // Placeholder
}

Make sure to implement the isFontAvailable method to check for the font’s presence in your environment.

If these suggestions do not resolve your issues, consider reaching out to GroupDocs support for more tailored assistance.

Feel free to ask if you have more questions!

No, as I already mentioned, the font does exist and is not a problem. Applying the watermark on PDF and Powerpoint files works without issues, only Word documents have the formatting problem. The exception is another problem when turning of the locking when also using a certain page number.