Free Support Forum - aspose.com

Center image in Header

I am writing a Word document and want to place an image in the header which I want to be centered. I am using the following code and the image is placed in the header but is left justified not centered. How can I center the image?

// Write the header image to the header
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.getFont().setName("Arial");
builder.getFont().setSize(11.0);

String imageFileName = headerImage.getPath();
Shape shape = builder.insertImage(imageFileName);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
shape.setVerticalAlignment(VerticalAlignment.CENTER);

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Please try using the following code.

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);

builder.getFont().setName("Arial");

builder.getFont().setSize(11.0);

String imageFileName = "test.jpg";

Shape shape = builder.insertImage(imageFileName);

shape.setWrapType(WrapType.NONE);

shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

shape.setHorizontalAlignment(HorizontalAlignment.CENTER);

shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

shape.setVerticalAlignment(VerticalAlignment.CENTER);

I hope this helps.

Best regards.