Free Support Forum - aspose.com

Margins (top, bottom, left, right) are not being output to DOCX

Hi,

When I save the Document I am working on as DOCX output the margin settings (top, left, bottom and right) are not being saved to the document.

Using the debugger, I check each section of the document and confirm that TopMargin, LeftMargin, BottomMargin and RightMargin are all set to 72. I then call doc.Save(docStream, SaveFormat.Docx) and save the document to a file (using a FileStream). However, when I look at the internals of the docx file I see that the w:sectPR section does not include the margin information. This is what is in the docx file:

	<w:sectPr>
		<w:pgMar w:header="720"
		         w:footer="720" />
		<w:cols w:space="720" />
	</w:sectPr>

I expected to also see:

w:bottom="1440"
w:top="1440"
w:right="1440"
w:left="1440"

but they were not there?

How can I ensure that the margin information is being written out to my docx file?

Thanks for any and all insights!

Marc

@Marc17,

Thanks for your inquiry. To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your input Word document
  • Aspose.Words generated output DOCX file showing the undesired behavior
  • Your expected DOCX Word document. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document by using Microsoft Word. Please also list the complete steps that you performed in MS Word to get the desired output
  • Please also create a standalone simple console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing. Please do not include Aspose.Words.dll files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

Thank you for the quick response. I have figured out what is going on and the issue is not with Aspose.Words. The problem is the way our code was processing the document that was generated by Aspose.Words. If there is no <w:sectPr> section then our code uses the default settings. If there is a <w:sectPr> section and there are no entries for the four Margins we are setting them to 0. We should be setting them to the default.

We did not expect there to be a <w:pgMar> section without the four margin entries. But the document generated by Aspose.Words works fine in Word 2016 so what you are generating is correct. We need to change our code so that if the four margin values are not specified in <w:pgMar> section that we set them to the default and not to 0.

I confirmed this by changing the margins in the document we are processing with Aspose.Words so that they were 73 instead of 72 (72 being the default). When the values are not the default they are output into the document generated by Aspose.Words and our code works fine.

So it seems that Aspose.Words has optimized the output to not output the margin values if they are the default values. And based on the behavior in Word 2016 this is correct. It was something that we had not correctly anticipated.

Thank you again for the quick response. This issue is in our code and not in Aspose.Words. Aspose.Words is working correctly.

Sincerely,
Marc Guyott

@Marc17,

Thanks for the details. It is great that you were able to resolve this issue on your end. Please let us know anytime you have any further queries.