We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

No watermark on page 1 of PDF

watermark-page1.zip (117.2 KB)

Hello Aspose,

I have a Word document, and I’m adding a watermark to it and then saving it to a PDF (with Words for Java, version 18.3). Sometimes the watermark does not appear on the first page.

Please find attached a zip file with a Word document, a main Java class that illustrates the problem, and an example output PDF with missing watermark on page 1.

Please first set the constants at the top of the main class WordToPdfMain.java before running it.

The class Watermarker.java is used to add the watermark. It is based on this example: https://docs.aspose.com/display/wordsjava/How+to++Add+a+Watermark+to+a+Document

Could you please give me a hint on how to get watermarks on all pages?

Thanks in advance!

I’ve tried to add watermarks to paragraphs instead of section headers, according to this post
https://forum.aspose.com/t/watermark-is-not-visible/48599/20

Document doc = ...; // The Word document
Shape watermark = makeWatermark(doc);

NodeCollection<Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
LayoutCollector collector = new LayoutCollector(doc);
Paragraph anchorPar;
int pageIndex = 1;

for (Paragraph par: paragraphs) {
    if (collector.getStartPageIndex(par) == pageIndex && par.getAncestor(NodeType.GROUP_SHAPE) == null) {
        anchorPar = par;
        anchorPar.appendChild(watermark.deepClone(true));
        pageIndex++;
    }
}

This does print a watermark on each page, including the first page, but the problem here is the positioning of the watermark. How do I position it at a fixed position from the top of the page? (with the code above, and my watermark as coded in the zip file, the watermark is added at the top of the first paragraph on a page; but the paragraph can be positioned at an arbitrary distance from the page top).

@helmersg,

We are working over your query and will get back to you soon.

@helmersg,

The following code inserts watermark on all pages (see awjava-18.4.pdf (100.1 KB)):

Document doc = new Document("D:\\Temp\\watermark-page1\\aspose-forum-post-watermark\\Aspose.Words.Java.IN.docx");
insertWatermarkText(doc, "Hello World");
doc.save("D:\\Temp\\watermark-page1\\aspose-forum-post-watermark\\awjava-18.4.pdf");

The function to add watermark to a document is as follows:

private static void insertWatermarkText(Document doc, String watermarkText) throws Exception {
    // Create a watermark shape. This will be a WordArt shape.
    // You are free to try other shape types as watermarks.
    Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);

    // Set up the text of the watermark.
    watermark.getTextPath().setText(watermarkText);
    watermark.getTextPath().setFontFamily("Arial");
    watermark.setWidth(500);
    watermark.setHeight(100);
    // Text will be directed from the bottom-left to the top-right corner.
    watermark.setRotation(-40);
    // Remove the following two lines if you need a solid black text.
    watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more Word-style watermark
    watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more Word-style watermark

    // Place the watermark in the page center.
    watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
    watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
    watermark.setWrapType(WrapType.NONE);
    watermark.setVerticalAlignment(VerticalAlignment.CENTER);
    watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);

    // Create a new paragraph and append the watermark to this paragraph.
    Paragraph watermarkPara = new Paragraph(doc);
    watermarkPara.appendChild(watermark);

    // Insert the watermark into all headers of each document section.
    for (Section sect : doc.getSections()) {
        // There could be up to three different headers in each section, since we want
        // the watermark to appear on all pages, insert into all headers.
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
        insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
    }
}

private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception {
    HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);

    if (header == null) {
        // There is no header of the specified type in the current section, create it.
        header = new HeaderFooter(sect.getDocument(), headerType);
        sect.getHeadersFooters().add(header);
    }

    // Insert a clone of the watermark into the header.
    header.appendChild(watermarkPara.deepClone(true));
}

Dear Awais,

thanks for the quick response, problem solved!

(The comments were already in my javacode “There could be up to three different headers in each section” still I inserted the watermark only in the HEADER_PRIMARY, stupid me…)

Kind regards,
Guido

@helmersg,

Thanks for your feedback. In case you have further inquiries or need any help, please let us know. We are always glad to help you.