Hi,
Currently we are using Aspose.Words 10.2.0.0 Version.
Please find the attached documents which i have used during coding.
Input File: Margin.docx
Output File: Margin1_Output.docx
I have taken the margin settings from Input file and assign to out put file, but margins are not getting applied.
I have written the following code for saving the word document using Aspose.Words.
Utility.VerifyLicense();
Document doc = new Document(@"D:\Margin.docx");
DocumentBuilder dBuilder = new DocumentBuilder(doc);
Aspose.Words.PageSetup oPageSetup = dBuilder.PageSetup;
double lhsMargin = oPageSetup.LeftMargin;
double rhsMargin = oPageSetup.RightMargin;
double distfromHeader = oPageSetup.HeaderDistance;
double distfromFooter = oPageSetup.FooterDistance;
double topMargin = oPageSetup.TopMargin;
double bottomMargin = oPageSetup.BottomMargin;
double gutter = oPageSetup.Gutter;
HtmlSaveOptions sav = new HtmlSaveOptions(SaveFormat.Html);
doc.Save(@"D:\Margin.html", sav);
string htmlStr = ReadHTMLFile(@"D:\Margin.html");
Document doc1 = new Document();
DocumentBuilder db = new DocumentBuilder(doc1);
db.InsertHtml(htmlStr);
Aspose.Words.PageSetup oPageSetup1 = db.PageSetup;
oPageSetup1.LeftMargin = lhsMargin;
oPageSetup1.RightMargin = rhsMargin;
oPageSetup1.TopMargin = topMargin;
oPageSetup1.BottomMargin = bottomMargin;
oPageSetup1.HeaderDistance = distfromHeader;
oPageSetup1.FooterDistance = distfromFooter;
oPageSetup1.Gutter = gutter;
doc1.Save(@"D:\Margin1.docx", SaveFormat.Docx);
Response.Write("Done");
Thanks in advance…
Hi Seenu,
Thanks for your inquiry. I’m working over your query and will update you soon.
Best Regards,
Hi Seenu,
Thanks for your inquiry. Page margin setting are being applied properly. There is some paragraph indent setting in your source document, which also needs to be considered. I have amended your code using latest version of Aspose.Words i.e. v11.5.0 as below, for paragraph formatting. Secondly we suggest you to use latest Aspose.Words API to avoid any potential issue.
Document doc = new Document(MyDir + "Margin.docx");
DocumentBuilder dBuilder = new DocumentBuilder(doc);
Aspose.Words.PageSetup oPageSetup = dBuilder.PageSetup;
// paragraph indent setting
double lindent = dBuilder.ParagraphFormat.LeftIndent;
double rindent = dBuilder.ParagraphFormat.RightIndent;
double lhsMargin = oPageSetup.LeftMargin;
double rhsMargin = oPageSetup.RightMargin;
double distfromHeader = oPageSetup.HeaderDistance;
double distfromFooter = oPageSetup.FooterDistance;
double topMargin = oPageSetup.TopMargin;
double bottomMargin = oPageSetup.BottomMargin;
double gutter = oPageSetup.Gutter;
HtmlSaveOptions sav = new HtmlSaveOptions(SaveFormat.Html);
doc.Save(MyDir + "Margin.html", sav);
string htmlStr = File.ReadAllText(MyDir + "Margin.html");
Document doc1 = new Document();
DocumentBuilder db = new DocumentBuilder(doc1);
db.InsertHtml(htmlStr);
Aspose.Words.PageSetup oPageSetup1 = db.PageSetup;
oPageSetup1.LeftMargin = lhsMargin;
oPageSetup1.RightMargin = rhsMargin;
oPageSetup1.TopMargin = topMargin;
oPageSetup1.BottomMargin = bottomMargin;
oPageSetup1.HeaderDistance = distfromHeader;
oPageSetup1.FooterDistance = distfromFooter;
oPageSetup1.Gutter = gutter;
// Retrieve all paragraphs in the document.
NodeCollection paragraphs = doc1.GetChildNodes(NodeType.Paragraph, true);
// Iterate through all paragraphs
foreach(Paragraph para in paragraphs)
{
// Apply paragraph indent settings.
para.ParagraphFormat.LeftIndent = lindent;
para.ParagraphFormat.RightIndent = rindent;
}
doc1.Save(MyDir + "Margin1.docx", SaveFormat.Docx);
Please feel free to contact us for any further assistance.
Best Regards,