Free Support Forum - aspose.com

How to set diff left and right margin to each page

Hi ,

I want to set diff margin to each page.

set for page 1 leftmargin is 1.25’’ and rightmargin 0’‘

for page 2 leftmargin is 1.25’’ and rightmargin is 1.25’'

I have tried following code it is not working

I downloaded latest Aspose.Words for .Net i.e. 13.1

Dim doc As New Document(“D:\DocumentBuilderDemo.doc”)
Dim builder As New DocumentBuilder(doc)

builder.PageSetup.ClearFormatting()
builder.PageSetup.PaperSize = PaperSize.A4
builder.PageSetup.LeftMargin = (1.25 * 72)
builder.PageSetup.RightMargin = 0.1
builder.Writeln(“sklfhsoafhsulahfuilasduiwatujlbvisgusghui hugysughoheroyio hfuowytuiyhqgoih yfuiweyfuiwqhjoqwhwoheg oqwhg89wyg89 q9h 9uy9wgh ho ofhy9w8yguio ghwo uiyuihg iohgo sklfhsoafhsulahfuilasduiwatujlbvisgusghui hugysughoheroyio hfuowytuiyhqgoih yfuiweyfuiwqhjoqwhwoheg oqwhg89wyg89 q9h 9uy9wgh ho ofhy9w8yguio ghwo uiyuihg iohgo”)
doc.MailMerge.DeleteFields()
builder.MoveToDocumentEnd()
builder.InsertBreak(BreakType.PageBreak)
builder.PageSetup.ClearFormatting()
builder.PageSetup.PaperSize = PaperSize.A4
builder.PageSetup.LeftMargin = (1.25 * 72)
builder.PageSetup.RightMargin = (1.25 * 72)
builder.Writeln(“sklfhsoafhsulahfuilasduiwatujlbvisgusghui hugysughoheroyio hfuowytuiyhqgoih yfuiweyfuiwqhjoqwhwoheg oqwhg89wyg89 q9h 9uy9wgh ho ofhy9w8yguio ghwo uiyuihg iohgo sklfhsoafhsulahfuilasduiwatujlbvisgusghui hugysughoheroyio hfuowytuiyhqgoih yfuiweyfuiwqhjoqwhwoheg oqwhg89wyg89 q9h 9uy9wgh ho ofhy9w8yguio ghwo uiyuihg iohgo”)
doc.Save(“D:\abc.doc”, SaveFormat.Doc)

Hi Jeevan,


Thanks for your inquiry. There is no page concept in Microsoft Word documents; pages are created by Microsoft Word on the fly and unfortunately there is no direct method you can use to achieve what you need. The PageSetup class represents the page setup properties of a section. PageSetup object contains all the page setup attributes of a section (left margin, bottom margin, paper size, and so on) as properties.

In your case, to be able to keep different margin settings for the first page and second page in your document, you need to add two Sections in your document such that the first Section contains the content of your first page and the second Section contains the content of second page. Once this configuration is applied, please use the following code snippet to set margins.

Document.Sections property returns a collection that represents all sections in the document.


Document doc = new Document(“input.docx”);

PageSetup firstPageSetup = doc.Sections[0].PageSetup;<o:p></o:p>

PageSetup otherPagesSetup = doc.Sections[1].PageSetup;<o:p></o:p>

firstPageSetup.LeftMargin = ConvertUtil.MillimeterToPoint(20);<o:p></o:p>

otherPagesSetup.LeftMargin = ConvertUtil.MillimeterToPoint(50);<o:p></o:p>

doc.Save(“out.docx”);<o:p></o:p>


Hope this answers your query. Please let us know if you have any more queries.