@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