How to add different watermark on add and even pages in the doc

Hello,

** We got a new issue recently. **

  1. How to add different watermark on add and even pages in the doc ? (attached “show 1”) **

  2. After adding a watermark in a doc, all page headers of the first section are lost. Attached the doc we tested (“TEST.doc”) and pictures (“show 2 & 3”).

Here is the code:

private void insertTextWatermark(DecorateContext context, byte[] qrCodeBytes, byte[] wmBytes) throws Exception
{
    Document doc = context.getContent(Document.class);

    Map < String, Paragraph > wmGraphMap = new HashMap < > ();
    Map < String, Paragraph > qrCodeGraphMap = new HashMap < > ();
    HeaderFooter headerFooter = null;
    // Insert the watermark into all headers of each document section.
    for (Section sect: doc.getSections())
    {
        PageSetup ps = sect.getPageSetup();
        String key = ps.getPageWidth() + "_" + ps.getPageHeight();
        if (wmBytes != null)
        {
            Paragraph watermarkPara = wmGraphMap.get(key);
            if (watermarkPara == null)
            {
                watermarkPara = new Paragraph(doc);
                watermarkPara.appendChild(generateShape(context, doc, ps, wmBytes, ImageType.WATERMARK));
                wmGraphMap.put(key, watermarkPara);
            }
            // 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, headerFooter, HeaderFooterType.HEADER_PRIMARY);
            insertWatermarkIntoHeader(watermarkPara, sect, headerFooter, HeaderFooterType.HEADER_FIRST);
            insertWatermarkIntoHeader(watermarkPara, sect, headerFooter, HeaderFooterType.HEADER_EVEN);
        }
        if (qrCodeBytes != null)
        {
            Paragraph watermarkPara = qrCodeGraphMap.get(key);
            if (watermarkPara == null)
            {
                watermarkPara = new Paragraph(doc);
                watermarkPara.appendChild(generateShape(context, doc, ps, qrCodeBytes, ImageType.QRCODE));
                qrCodeGraphMap.put(key, watermarkPara);
            }
            // 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, headerFooter, HeaderFooterType.HEADER_PRIMARY);
            insertWatermarkIntoHeader(watermarkPara, sect, headerFooter, HeaderFooterType.HEADER_FIRST);
            insertWatermarkIntoHeader(watermarkPara, sect, headerFooter, HeaderFooterType.HEADER_EVEN);
        }
    }
    doc.updatePageLayout();
}

private void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooter headerFooter, int headerType) throws Exception {
    HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
    if (header == null)
    {
        if (headerFooter == null)
        {
            header = new HeaderFooter(sect.getDocument(), headerType);
            sect.getHeadersFooters().add(header);
        }
        else
        {
            header = (HeaderFooter) headerFooter.deepClone(true);
        }
    }
    else
    {
        headerFooter = (HeaderFooter) header.deepClone(true);
    }
    Paragraph gg = header.getLastParagraph();
    if (gg != null)
    {
        Paragraph newnode = (Paragraph) watermarkPara.deepClone(true);
        int count = newnode.getCount();
        if (count >= 1)
        {
            gg.appendChild(newnode.getFirstChild());
        }
        if (count >= 2)
        {
            gg.appendChild(newnode.getLastChild());
        }
    }
    else
    {
        header.appendChild(watermarkPara.deepClone(true));
    }
}

Thanks a lot!
Sara

Hi Sara,

Thanks for your inquiry. You can use the following code to achieve this:

Document doc = new Document(getMyDir() + "test.doc");
insertWatermarkText(doc, "Odd Pages");
doc.save(getMyDir() + "out-java.docx");
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())
    {
        sect.getPageSetup().setDifferentFirstPageHeaderFooter(false);
        sect.getPageSetup().setOddAndEvenPagesHeaderFooter(true);

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


    Shape watermarkClone = (Shape) watermark.deepClone(true);
    watermarkClone.getTextPath().setText("Even Pages");

    Paragraph watermarkParaClone = (Paragraph) watermarkPara.deepClone(false);
    watermarkParaClone.removeAllChildren();
    watermarkParaClone.appendChild(watermarkClone);

    for (Section sect : doc.getSections())
    {
        insertWatermarkIntoHeader(watermarkParaClone, 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));
}

I hope, this helps.

Best regards,