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

Free Support Forum - aspose.com

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 {<o:p></o:p>

Document doc = context.getContent(Document.class);<o:p></o:p>

<o:p></o:p>

Map<String, Paragraph> wmGraphMap = new HashMap<>();<o:p></o:p>

Map<String, Paragraph> qrCodeGraphMap = new HashMap<>();<o:p></o:p>

HeaderFooter headerFooter = null;<o:p></o:p>

// Insert the watermark into all headers of each document section.<o:p></o:p>

for (Section sect : doc.getSections()) {<o:p></o:p>

PageSetup ps = sect.getPageSetup();<o:p></o:p>

String key = ps.getPageWidth() + “_” + ps.getPageHeight();<o:p></o:p>

if (wmBytes != null) {<o:p></o:p>

Paragraph watermarkPara = wmGraphMap.get(key);<o:p></o:p>

if (watermarkPara == null) {<o:p></o:p>

watermarkPara = new Paragraph(doc);<o:p></o:p>

watermarkPara.appendChild(generateShape(context, doc, ps, wmBytes, ImageType.WATERMARK));<o:p></o:p>

wmGraphMap.put(key, watermarkPara);<o:p></o:p>

}<o:p></o:p>

// There could be up to three different headers in each section,<o:p></o:p>

// since we want<o:p></o:p>

// the watermark to appear on all pages, insert into all headers.<o:p></o:p>

insertWatermarkIntoHeader(watermarkPara, sect, headerFooter, HeaderFooterType.HEADER_PRIMARY);<o:p></o:p>

insertWatermarkIntoHeader(watermarkPara, sect, headerFooter, HeaderFooterType.HEADER_FIRST);<o:p></o:p>

insertWatermarkIntoHeader(watermarkPara, sect, headerFooter, HeaderFooterType.HEADER_EVEN);<o:p></o:p>

}<o:p></o:p>

if (qrCodeBytes != null) {<o:p></o:p>

Paragraph watermarkPara = qrCodeGraphMap.get(key);<o:p></o:p>

if (watermarkPara == null) {<o:p></o:p>

watermarkPara = new Paragraph(doc);<o:p></o:p>

watermarkPara.appendChild(generateShape(context, doc, ps, qrCodeBytes, ImageType.QRCODE));<o:p></o:p>

qrCodeGraphMap.put(key, watermarkPara);<o:p></o:p>

}<o:p></o:p>

// There could be up to three different headers in each section,<o:p></o:p>

// since we want<o:p></o:p>

// the watermark to appear on all pages, insert into all headers.<o:p></o:p>

insertWatermarkIntoHeader(watermarkPara, sect, headerFooter, HeaderFooterType.HEADER_PRIMARY);<o:p></o:p>

insertWatermarkIntoHeader(watermarkPara, sect, headerFooter, HeaderFooterType.HEADER_FIRST);<o:p></o:p>

insertWatermarkIntoHeader(watermarkPara, sect, headerFooter, HeaderFooterType.HEADER_EVEN);<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>

doc.updatePageLayout();<o:p></o:p>

}<o:p></o:p>

private void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooter headerFooter, int headerType) throws Exception {<o:p></o:p>

HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);<o:p></o:p>

if (header == null) {<o:p></o:p>

if (headerFooter == null) {<o:p></o:p>

header = new HeaderFooter(sect.getDocument(), headerType);<o:p></o:p>

sect.getHeadersFooters().add(header);<o:p></o:p>

} else {<o:p></o:p>

header = (HeaderFooter) headerFooter.deepClone(true);<o:p></o:p>

}<o:p></o:p>

} else {<o:p></o:p>

headerFooter = (HeaderFooter) header.deepClone(true);<o:p></o:p>

}<o:p></o:p>

Paragraph gg = header.getLastParagraph();<o:p></o:p>

if (gg != null) {<o:p></o:p>

Paragraph newnode = (Paragraph) watermarkPara.deepClone(true);<o:p></o:p>

int count = newnode.getCount();<o:p></o:p>

if (count >= 1) {<o:p></o:p>

gg.appendChild(newnode.getFirstChild());<o:p></o:p>

}<o:p></o:p>

if (count >= 2) {<o:p></o:p>

gg.appendChild(newnode.getLastChild());<o:p></o:p>

}<o:p></o:p>

} else {<o:p></o:p>

header.appendChild(watermarkPara.deepClone(true));<o:p></o:p>

}<o:p></o:p>

}<o:p></o:p>


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);
<font color="GREEN"><i>// 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);

<font color="GREEN"><i>// Create a new paragraph and append the watermark to this paragraph.

Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(watermark);

<font color="GREEN"><i>// Insert the watermark into all headers of each document section.

for (Section sect : doc.getSections())
{
sect.getPageSetup().setDifferentFirstPageHeaderFooter(false);
sect.getPageSetup().setOddAndEvenPagesHeaderFooter(true);

    <font color="GREEN"><i>// 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 <font color="BLUE" size="+1">=</font> <font color="BLUE" size="+1"><b>(</b></font>Shape<font color="BLUE" size="+1"><b>)</b></font> watermark<font color="BLUE" size="+1"><b>.</b></font>deepClone<font color="BLUE" size="+1"><b>(</b></font>true<font color="BLUE" size="+1"><b>)</b></font><font color="BLUE" size="+1"><b>;</b></font>
watermarkClone<font color="BLUE" size="+1"><b>.</b></font>getTextPath<font color="BLUE" size="+1"><b>(</b></font><font color="BLUE" size="+1"><b>)</b></font><font color="BLUE" size="+1"><b>.</b></font>setText<font color="BLUE" size="+1"><b>(</b></font><font color="PURPLE">"Even Pages"</font><font color="BLUE" size="+1"><b>)</b></font><font color="BLUE" size="+1"><b>;</b></font>

Paragraph watermarkParaClone <font color="BLUE" size="+1">=</font> <font color="BLUE" size="+1"><b>(</b></font>Paragraph<font color="BLUE" size="+1"><b>)</b></font> watermarkPara<font color="BLUE" size="+1"><b>.</b></font>deepClone<font color="BLUE" size="+1"><b>(</b></font>false<font color="BLUE" size="+1"><b>)</b></font><font color="BLUE" size="+1"><b>;</b></font>
watermarkParaClone<font color="BLUE" size="+1"><b>.</b></font>removeAllChildren<font color="BLUE" size="+1"><b>(</b></font><font color="BLUE" size="+1"><b>)</b></font><font color="BLUE" size="+1"><b>;</b></font>
watermarkParaClone<font color="BLUE" size="+1"><b>.</b></font>appendChild<font color="BLUE" size="+1"><b>(</b></font>watermarkClone<font color="BLUE" size="+1"><b>)</b></font><font color="BLUE" size="+1"><b>;</b></font>

<font color="RED"><b>for</b></font> <font color="BLUE" size="+1"><b>(</b></font>Section sect <font color="BLUE" size="+1">:</font> doc<font color="BLUE" size="+1"><b>.</b></font>getSections<font color="BLUE" size="+1"><b>(</b></font><font color="BLUE" size="+1"><b>)</b></font><font color="BLUE" size="+1"><b>)</b></font>
<font color="BLUE" size="+1"><b>{</b></font>
    insertWatermarkIntoHeader<font color="BLUE" size="+1"><b>(</b></font>watermarkParaClone<font color="BLUE" size="+1"><b>,</b></font> sect<font color="BLUE" size="+1"><b>,</b></font> HeaderFooterType<font color="BLUE" size="+1"><b>.</b></font>HEADER_EVEN<font color="BLUE" size="+1"><b>)</b></font><font color="BLUE" size="+1"><b>;</b></font>
<font color="BLUE" size="+1"><b>}</b></font>

}

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

<font color="RED"><b>if</b></font> <font color="BLUE" size="+1"><b>(</b></font>header <font color="BLUE" size="+1">=</font><font color="BLUE" size="+1">=</font> <font color="RED"><b>null</b></font><font color="BLUE" size="+1"><b>)</b></font>
<font color="BLUE" size="+1"><b>{</b></font>
    <font color="GREEN"><i>// There is no header of the specified type in the current section, create it.

header = new HeaderFooter(sect.getDocument(), headerType);
sect.getHeadersFooters().add(header);
}

<font color="GREEN"><i>// Insert a clone of the watermark into the header.

header.appendChild(watermarkPara.deepClone(true));
}

I hope, this helps.

Best regards,