How to add a small image to every page?

Hello,


We need to add a small image to the top left corner of every document page.
The important requirement is that document isn’t changed - all the pages stay the same - like adding a watermark.

I tried using headers but they move layout as they take a space.

Could you please provide us with a sample code?

Thank you!

Hi Andrey,


Thanks for your inquiry. Please use the code from the following article to add image Shape inside header/footer of document:
http://www.aspose.com/docs/display/wordsjava/How+to++Add+a+Watermark+to+a+Document

In case, the problem still remains, please attach following resources here for testing:

  1. Your input Word document you want to insert image in.
  2. Your output document having images inside header and which shows the undesired behavior.
  3. Your expected document displaying the images on top left corners of each page without having a layout problem.

As soon as you get these pieces of information ready for me, I’ll start investigation into your issue and provide you code to achieve this.

Best regards,

Hello, it is a good example, but I need to insert an image, not a text. How is it possible?

Hi Andrey,


Thanks for your inquiry. You can use the following simple code to achieve this:
Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);

docBuilder.moveToHeaderFooter(HeaderFooterType.HEADER_FIRST);
docBuilder.insertImage(“C:\Temp\Aspose.Words.png”);

docBuilder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
docBuilder.insertImage(“C:\Temp\Aspose.Words.png”);

docBuilder.moveToHeaderFooter(HeaderFooterType.HEADER_EVEN);
docBuilder.insertImage(“C:\Temp\Aspose.Words.png”);

doc.save(“C:\Temp\out.docx”);

I hope, this helps.

Best regards,

OK, it works - thank you.


Now I have another task - I need to add a separate page to the end of a document with some text.

Text should not inherit any previous formatting, so I guess it’d be better not to use DocumentBuilder.

What should I do?

OK, I did it on my own. :slight_smile:


It was really easy. But now I have a problem I can’t deal with.

I add watermarks with the following code:

// Add QR headers
for (Section section : document.getSections()) {
for (int headerType : Arrays.asList(HeaderFooterType.HEADER_PRIMARY, HeaderFooterType.HEADER_FIRST, HeaderFooterType.HEADER_EVEN)) {
if (headerType == HeaderFooterType.HEADER_FIRST && !section.getPageSetup().getDifferentFirstPageHeaderFooter())
continue;
if (headerType == HeaderFooterType.HEADER_EVEN && !section.getPageSetup().getOddAndEvenPagesHeaderFooter())
continue;

Shape qrShape = new Shape(document, ShapeType.IMAGE);
qrShape.getImageData().setImageBytes(qrImageBytes);
qrShape.setWrapType(WrapType.NONE);
qrShape.setWidth(qrShape.getImageData().getImageSize().getWidthPoints());
qrShape.setHeight(qrShape.getImageData().getImageSize().getHeightPoints());
qrShape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
qrShape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
qrShape.setLeft(QR_PORTRAIT_LEFT);
qrShape.setTop(QR_PORTRAIT_TOP);

Paragraph qrParagraph = new Paragraph(document);
qrParagraph.appendChild(qrShape);

HeaderFooter header = section.getHeadersFooters().getByHeaderFooterType(headerType);
if (header == null) {
header = new HeaderFooter(section.getDocument(), headerType);
section.getHeadersFooters().add(header);
}
header.appendChild(qrParagraph);
}
}

I assume, I wrote it right, didn’t I?

Anyway, I can’t place watermarks in front of text. setBehindText doesn’t help. What can I do?

Hi Andrey,


Thanks for your inquiry.

The problem occurs because watermark shape resides inside header footer story and main content is inside body story (please see Story class). If you insert a watermark using Microsoft Word 2013 in output document, you will observe the same behaviour. All content of document’s header/footer is always behind the main content of the document.

However, you can overcome this problem by manually inserting watermarks in each Page. You can achieve this by moving the cursor to the first Paragraph in each Page of your document and then making those Paragraphs as an anchor points for your watermarks. Please see the following code example:
public static void InsertWatermarkTextAtEachPage(Document doc, String watermarkText) throws Exception
{
DocumentBuilder builder = new DocumentBuilder(doc);
PageSetup ps = builder.getPageSetup();
NodeCollection paragraphs <font color="BLUE">=</font> doc<font color="BLUE"><b>.</b></font>getChildNodes<font color="BLUE"><b>(</b></font>NodeType<font color="BLUE"><b>.</b></font>PARAGRAPH<font color="BLUE"><b>,</b></font> true<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
LayoutCollector collector <font color="BLUE">=</font> <font color="RED"><b>new</b></font> LayoutCollector<font color="BLUE"><b>(</b></font>doc<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
Paragraph anchorPara <font color="BLUE">=</font> <font color="RED"><b>null</b></font><font color="BLUE"><b>;</b></font>

<font color="RED"><b>int</b></font> pageIndex <font color="BLUE">=</font> <font color="BROWN">1</font><font color="BLUE"><b>;</b></font>
<font color="RED"><b>for</b></font> <font color="BLUE"><b>(</b></font>Paragraph para <font color="BLUE">:</font> <font color="BLUE"><b>(</b></font>Iterable<font color="BLUE"><</font>Paragraph<font color="BLUE">></font><font color="BLUE"><b>)</b></font> paragraphs<font color="BLUE"><b>)</b></font>
<font color="BLUE"><b>{</b></font>
    <font color="RED"><b>if</b></font> <font color="BLUE"><b>(</b></font>collector<font color="BLUE"><b>.</b></font>getStartPageIndex<font color="BLUE"><b>(</b></font>para<font color="BLUE"><b>)</b></font> <font color="BLUE">=</font><font color="BLUE">=</font> pageIndex<font color="BLUE"><b>)</b></font>
    <font color="BLUE"><b>{</b></font>
        anchorPara <font color="BLUE">=</font> para<font color="BLUE"><b>;</b></font>

        Shape watermark <font color="BLUE">=</font> <font color="RED"><b>new</b></font> Shape<font color="BLUE"><b>(</b></font>doc<font color="BLUE"><b>,</b></font> ShapeType<font color="BLUE"><b>.</b></font>TEXT_PLAIN_TEXT<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

        watermark<font color="BLUE"><b>.</b></font>getTextPath<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>setText<font color="BLUE"><b>(</b></font>watermarkText<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        watermark<font color="BLUE"><b>.</b></font>getTextPath<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>setFontFamily<font color="BLUE"><b>(</b></font><font color="PURPLE">"Arial"</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

        watermark<font color="BLUE"><b>.</b></font>setWidth<font color="BLUE"><b>(</b></font><font color="BROWN">300</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        watermark<font color="BLUE"><b>.</b></font>setHeight<font color="BLUE"><b>(</b></font><font color="BROWN">70</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        watermark<font color="BLUE"><b>.</b></font>setLeft<font color="BLUE"><b>(</b></font><font color="BROWN">100</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        watermark<font color="BLUE"><b>.</b></font>setTop<font color="BLUE"><b>(</b></font><font color="BROWN">100</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

        watermark<font color="BLUE"><b>.</b></font>setRotation<font color="BLUE"><b>(</b></font><font color="BLUE">-</font><font color="BROWN">40</font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

        watermark<font color="BLUE"><b>.</b></font>getFill<font color="BLUE"><b>(</b></font><font color="BLUE"><b>)</b></font><font color="BLUE"><b>.</b></font>setColor<font color="BLUE"><b>(</b></font>Color<font color="BLUE"><b>.</b></font>GRAY<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>
        watermark<font color="BLUE"><b>.</b></font>setStrokeColor<font color="BLUE"><b>(</b></font>Color<font color="BLUE"><b>.</b></font>GRAY<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

        watermark<font color="BLUE"><b>.</b></font>setWrapType<font color="BLUE"><b>(</b></font>WrapType<font color="BLUE"><b>.</b></font>NONE<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

        anchorPara<font color="BLUE"><b>.</b></font>appendChild<font color="BLUE"><b>(</b></font>watermark<font color="BLUE"><b>)</b></font><font color="BLUE"><b>;</b></font>

        pageIndex<font color="BLUE"><font color="BLUE">+</font><font color="BLUE">+</font></font><font color="BLUE"><b>;</b></font>
    <font color="BLUE"><b>}</b></font>
<font color="BLUE"><b>}</b></font>

}

I hope, this helps.

Best regards,

Thank you for the detail answer.


Is it safe to use this strategy? I mean is it guaranteed that watermark will be added to EVERY page? What if the page is empty or consists of something different than paragraphs? Or what if some paragraph is so huge that it takes more than one page, so there is a page that has no paragraph started?

Hi Andrey,


Thanks for your inquiry. Yes, these are the limitations of this code but you can build on it and add more logic to it to be able to meet all scenarios. Please see LayoutCollector class members; for example it has a useful method “GetNumPagesSpanned” to determine how many pages a a particular node spans. Please let me know if I can be of any further assistance.

Best regards,