Header/ Footer Text and Image

Hello there,
I would like to get some code snippets for the following scenarios as i had problems in positioning using the (x,y) postions

  1. Insert header/Footer text to all pages with (x,y) positions
  2. Insert Header/footer text to a specified page with (x,y) positions
  3. Insert page numbers to header/footer with (x,y) positions
  4. Insert page numbers starting from a specific page
  5. Insert logo in header/footer with (x,y) position

@srinivasr, the code example given below shows how to insert text and page numbers at (x,y) position via using textboxes. Also you can insert a logo image in a similar way:

    public static void main(String[] args) throws Exception {
        License lic = new License();
        lic.setLicense("Aspose.Words.Java.lic");

        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        /* Insert textbox in the header */
        int textBoxX = 200;
        int textBoxY = 10;
        double textBoxWidth = 200.0;
        double textBoxHeight = 50.0;
        String text = "This text is absolutely positioned";

        Shape textBox = CreateTextBoxWithText(doc, textBoxX, textBoxY, textBoxWidth, textBoxHeight, text);
        textBox.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
        textBox.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

        builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
        builder.getCurrentParagraph().appendChild(textBox);

        /* Insert a logo image */
        int imageX = 200;
        // Negative number becase the position is calculated from the bottom margin.
        int imageY = -20;
        int imageWidth = 50;
        int imageHeight = 50;

        String logoFile = "logo.jpg";
        builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
        BufferedImage qrImage = ImageIO.read(new File(logoFile));
        builder.insertImage(qrImage,
                RelativeHorizontalPosition.MARGIN, imageX,
                RelativeVerticalPosition.BOTTOM_MARGIN, imageY,
                imageWidth, imageHeight,
                WrapType.NONE);

        /* Insert a textbox with the page number */
        int pgTextBoxX = 400;
        int pgTextBoxY = 0;
        double pgTextBoxWidth = 20.0;
        double pgTextBoxHeight = 20.0;

        Shape pageNumberShape = CreateTextBoxWithPageNumber(doc, pgTextBoxX, pgTextBoxY, pgTextBoxWidth, pgTextBoxHeight);
        pageNumberShape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
        pageNumberShape.setRelativeVerticalPosition(RelativeVerticalPosition.BOTTOM_MARGIN);

        builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
        builder.insertParagraph().appendChild(pageNumberShape);

        doc.save("doc1.docx");
    }

    private static Shape CreateTextBoxWithText(Document doc, int x, int y, double width, double height, String text) throws Exception {
        // Create a paragraph containing the text.
        Paragraph paragraph = new Paragraph(doc);
        Run run = new Run(doc, text);
        paragraph.appendChild(run);

        // Append the paragraph to the textbox
        Shape textBox = CreateTextBox(doc, x, y, width, height);
        textBox.appendChild(paragraph);

        return textBox;
    }

    private static Shape CreateTextBoxWithPageNumber(Document doc, int x, int y, double width, double height) throws Exception {
        Paragraph paragraph = new Paragraph(doc);
        paragraph.appendField("PAGE", "");

        Shape textBox = CreateTextBox(doc, x, y, width, height);
        textBox.appendChild(paragraph);

        return textBox;
    }

    private static Shape CreateTextBox(Document doc, int x, int y, double width, double height) throws Exception {
        Shape textBox = new Shape(doc, ShapeType.TEXT_BOX);

        textBox.setLeft(x);
        textBox.setTop(y);

        textBox.setWidth(width);
        textBox.setHeight(height);

        // Remove the border around the textbox.
        textBox.setStroked(false);

        textBox.setWrapType(WrapType.NONE);

        return textBox;
    }

doc1.docx (32.9 KB)




Here are the links to our documentation with more code examples:

DocumentBuilder.insertImage
DocumentBuilder.insertShape

Shape.setRelativeHorizontalPosition
RelativeHorizontalPosition

Shape.setRelativeVerticalPosition
RelativeVerticalPosition

DocumentBuilder.insertField

Hi i have few doubts in this document i tried to add header and footer as per the above code but i cannot add header and footer for some pages for some reasons. I also attach the document here doc.docx (135.6 KB)

@srinivasr The problem occurs because in your document there is opaque content that overlaps header and footer. That is why the added header is not visible.

Is there any solution to this? can we insert header by any other means? How can i make the headers and footers visible?

@srinivasr In MS Word, header/footer content is always under the main body content. As a workaround, you can emulate header/footer by repeating content in the main body of your document and put this content in front of the main content. However, this will not be header/footer actually and it will be difficult to handle such content upon editing document.

Document doc = new Document("C:\\Temp\\in.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

Iterable<Paragraph> paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
LayoutCollector collector = new LayoutCollector(doc);

int pageIndex = 1;
for (Paragraph para : paragraphs)
{
    if (collector.getStartPageIndex(para) == pageIndex)
    {
        /* Insert textbox in the header */
        int textBoxX = 200;
        int textBoxY = 10;
        double textBoxWidth = 200.0;
        double textBoxHeight = 50.0;
        String text = "This text is absolutely positioned";

        Shape textBox = CreateTextBoxWithText(doc, textBoxX, textBoxY, textBoxWidth, textBoxHeight, text);
        textBox.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
        textBox.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

        para.prependChild(textBox);

        /* Insert a logo image */
        int imageX = 200;
        // Negative number because the position is calculated from the bottom margin.
        int imageY = -50;
        int imageWidth = 50;
        int imageHeight = 50;

        String logoFile = "C:\\Temp\\logo.jpg";
        builder.moveTo(para.getFirstChild());
        BufferedImage qrImage = ImageIO.read(new File(logoFile));
        builder.insertImage(qrImage,
                RelativeHorizontalPosition.MARGIN, imageX,
                RelativeVerticalPosition.BOTTOM_MARGIN, imageY,
                imageWidth, imageHeight,
                WrapType.NONE);

        pageIndex++;
    }
}

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