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

Free Support Forum - aspose.com

Add an image to all pages

I want to add an image to all pages in document.
This image should be located in the top-right corner of printable area.
I use following function for this purpose.
shape.setRelativeVerticalPosition(RelativeVerticalPosition.MARGIN);
This function adds given image but the image is located in top-corner of document.
I think that adding operation ignores the margins of page. Some part of the image is in non-printable area for document. The printers could not print the image properly because some part of the image is in non-printable area.
Is my usage correct? How can I locate an image in top-right corner of printable area?


private void addAllPageShape2(Bitmap bitmap boolean behindText) throws Exception {

Shape shape = new Shape(document, ShapeType.IMAGE);
shape.getImageData().setImage(bitmap);
shape.setBehindText(behindText);

shape.setRelativeVerticalPosition(RelativeVerticalPosition.MARGIN);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN);
shape.setVerticalAlignment(VerticalAlignment.TOP);
shape.setHorizontalAlignment(HorizontalAlignment.RIGHT);

Paragraph dataMatrixPara = new Paragraph(document);
dataMatrixPara.appendChild(shape);

for (Section sect : document.getSections()) {
// 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.
try {
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_PRIMARY);
header.appendChild(dataMatrixPara.deepClone(true));
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(HEADER_FIRST);
header.appendChild(dataMatrixPara.deepClone(true));
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_EVEN);
header.appendChild(dataMatrixPara.deepClone(true));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Hi Ozan,

Thanks for your inquiry. Please use following code example to achieve your requirements. Hope this helps you.

private static void addAllPageShape(Document document, String imagepath, boolean behindText) throws Exception {

Shape shape = new Shape(document, ShapeType.IMAGE);

shape.getImageData().setImage(imagepath);

shape.setBehindText(behindText);

shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

shape.setLeft(document.getFirstSection().getPageSetup().getPageWidth() - shape.getImageData().getImageSize().getWidthPoints());

Paragraph dataMatrixPara = new Paragraph(document);

dataMatrixPara.appendChild(shape);

for (Section sect : document.getSections()) {

try {

HeaderFooter header1 = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.FOOTER_PRIMARY);

if (header1 == null)

{

// There is no header of the specified type in the current section, create it.

header1 = new HeaderFooter(document, HeaderFooterType.FOOTER_PRIMARY);

sect.getHeadersFooters().add(header1);

}

header1.appendChild(dataMatrixPara.deepClone(true));

HeaderFooter header2 = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_FIRST);

if (header2 == null)

{

header2 = new HeaderFooter(document, HeaderFooterType.HEADER_FIRST);

sect.getHeadersFooters().add(header2);

}

header2.appendChild(dataMatrixPara.deepClone(true));

HeaderFooter header3 = sect.getHeadersFooters().getByHeaderFooterType(HeaderFooterType.HEADER_EVEN);

if (header3 == null)

{

header3 = new HeaderFooter(document, HeaderFooterType.HEADER_EVEN);

sect.getHeadersFooters().add(header3);

}

header3.appendChild(dataMatrixPara.deepClone(true));

} catch (Exception e) {

e.printStackTrace();

}

}

}

Document doc = new Document(MyDir + "in.docx");

addAllPageShape(doc, MyDir + "in.png", false);

doc.save(MyDir + "Out.docx");

Hi,


I worked. Thanks for help.
But, I changed the example code like this:

shape.setLeft(document.getFirstSection().getPageSetup().getPageWidth()

-shape.getImageData().getImageSize().getWidthPoints()

-document.getFirstSection().getPageSetup().getLeftMargin());

u1:p</u1:p>

Best regards.

Hi Ozan,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.