.NET
I’m trying to set a different margin for the first and last page. but when I move the node to last page and go to the previous node and set margins it doesn’t work according to my condition.
The document I have has the paragraph that is extended from the second last paragraph. so when I move to the last page and then previous node. Technically the remaining text is part of the previous node so the margins don’t apply.
Is there a way to make the paragraph on the last page a seperate node even though it is part of the previous page’s last node?
Please compress the following resources into ZIP format and attach the .zip file here for testing:
- Your simplified source Word document
- Aspose.Words for .NET 21.5 generated output DOCX file showing the undesired behavior (if any)
- Source code you used to generate above DOCX file
- Your expected DOCX file showing the desired output. You can create this expected document manually by using MS Word.
As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you more information.
Sample.zip (41.4 KB)
I’ve attached the resources
I’ve commented the code to which changes the margins for the first page because it didn’t work.
But for the last page it does work and what I wanted it to do is even if the starting sentence in the last page is the continuity of the last paragraph in second last page. I would want to change the margin & without inserting break. If it inserts break in certain scenarios where there are list of items. for eg. 1,2,3 it inserts a new line and a new nummber line which would obviously not have any content.
Is there a better approach to loop through the pages other than section by section?
It takes some time to do and it won’t be great in production.
Thanks
The following C# code of Aspose.Words for .NET will split template Word document into three sub-documents i.e a document containing only the first page, a document object containing only the middle pages and a document containing only the last page. The code will then configure page setup values (left, right, top, bottom margins) for these three subdocuments and finally merge them to form a final document:
Document doc = new Document("C:\\Temp\\sample\\template.docx");
int total_Pages = doc.PageCount;
Document first_Page = doc.ExtractPages(0, 1);
Document middle_Pages = doc.ExtractPages(1, total_Pages - 2);
Document last_Page = doc.ExtractPages(total_Pages - 1, 1);
PageSetup pageSetup_first_Page = first_Page.FirstSection.PageSetup;
pageSetup_first_Page.LeftMargin = pageSetup_first_Page.RightMargin = pageSetup_first_Page.TopMargin = pageSetup_first_Page.BottomMargin = 72 * 1;
foreach (Section section in middle_Pages.Sections)
{
PageSetup pageSetup = section.PageSetup;
pageSetup.LeftMargin = pageSetup.RightMargin = pageSetup.TopMargin = pageSetup.BottomMargin = 72 * 2;
}
PageSetup pageSetup_last_Page = last_Page.FirstSection.PageSetup;
pageSetup_last_Page.LeftMargin = pageSetup_last_Page.RightMargin = pageSetup_last_Page.TopMargin = pageSetup_last_Page.BottomMargin = 72 * 1;
Document final_Document = (Document)doc.Clone(false);
final_Document.RemoveAllChildren();
final_Document.AppendDocument(first_Page, ImportFormatMode.KeepSourceFormatting);
final_Document.AppendDocument(middle_Pages, ImportFormatMode.KeepSourceFormatting);
final_Document.AppendDocument(last_Page, ImportFormatMode.KeepSourceFormatting);
final_Document.Save("C:\\Temp\\sample\\21.5.docx");
@awais.hafeez
Thanks for the code.
While it does work good for the first page. It didn’t quite for the last page. In the generated document the content has the different margin ( which is what we need) but the content itself could fit in the second last page because there was enough space for the content that is in the last page.
Is there a way to do so?
Is there any efficient way (time wise) doing this. It takes roughly around five seconds and there are like many other computations I need to perform before this which would end up taking much longer.
Thank you
The C# code mentioned in my previous post took just 1,530 milliseconds on my end to produce following output DOCX document when using the latest (21.5) version of Aspose.Words for .NET.
- Attachment: output produced using 21.5 version of Aspose.Words.zip (12.5 KB)
Can you please elaborate (with the help of screenshots), what is the problem in above output DOCX? Also, please fix those problems manually by using MS Word and attach the fixed-version of above document here for our reference. We will then investigate the scenario further on our end and provide you more information.