Free Support Forum - aspose.com

Top Margin is reset when adding watermark

Hi,

I have a Word document whose Top Margin was reduced to fit a lot of content on a single page.

When I use the following code to add a watermark, the TopMargin from the original document is lost and now the document spans two pages.

I know I can explictily set the TopMargin in code; the problem is that I have been unable to capture the original TopMargin value from the template (in code). A hard coded TopMargin value is not acceptable. In other words I need to preserve whatever TopMargin value was set in ANY Word document where I need to apply a watermark. Apose Words seems to reset the TopMargin to the default.

Here is the code for adding the watermark...

String imageFile = "D:/temp/AsposeTest/watermark.jpg";

String inFileName = "D:/temp/AsposeTest/DotWithSmallTopMargin.dot";

String outFileName = "D:/temp/AsposeTest/DocWithWatermark.doc";

Document doc = new Document(inFileName);

DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);

// Insert a floating picture.

Shape shape = builder.insertImage(imageFile);

shape.setWrapType(WrapType.NONE);

shape.setBehindText(true);

// Position the image in the center of the page.

shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);

shape.setHorizontalAlignment(HorizontalAlignment.CENTER);

shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

shape.setVerticalAlignment(VerticalAlignment.CENTER);

doc.save(outFileName);

Attached are a test Word docment and watermark image.

Thank You

BTW... I downloaded the latest version of Aspose Words and I get the same behavior.

I have reduced the test code to narrow down the issue. All I need to do is call builder.moveToHeaderFooter() for the TopMargin value to be reset...

String inFileName = "D:/temp/AsposeTest/DotWithSmallTopMargin.dot";

String outFileName = "D:/temp/AsposeTest/minOut.doc";

Document doc = new Document(inFileName);

DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);

doc.save(outFileName);

Hi Michael,

Thanks for your inquiry and sorry for the delayed response.

Plese note that in your case when you insert a primary header in your document using Microsoft Word, the default height of header area would be 0.5" and Aspose.Words does the same. I think you should just specify 'zero' distance between the header and the top of the page by using the PageSetup.HeaderDistance property.

I hope, this helps.

Best regards,