Add unique combination of shapes list into each pages in word document

Hi,
My main objective is adding a collection of shapes into each pages of the document.
I have an inquiry related to the following thread
I have managed to add a collection of shapes in the end of the page by using the following code snippets.

DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();
Shape[] omrLine = new Shape[10];
for (int i = 0; i < 9; i++)
{
    omrLine[i] = new Shape(doc, ShapeType.Line);
    omrLine[i].Rotation = 90;
    omrLine[i].Top = -10;
    omrLine[i].Left = i * 10;
    omrLine[i].Width = 12;
    omrLine[i].StrokeColor = Color.Red;
    builder.InsertNode(omrLine[i]);
}

The above code snippet adds the shapes collection at the end of the document by using builder.MoveToDocumentEnd()
In order to loop through each pages of the document, i am able to use the following code

foreach (Section docSection in doc.Sections)
{
    builder.MoveToSection(x);
}

But the MoveToSection method only accepts an integer value. Is there a better way to loop through each pages and add the collection of shapes correctly ?
Thanks for your assistance,
hadi teo

Hi Hadi,

Thanks for your inquiry. May be you should just add a Bookmark at the end of each page where you want your Shape objects be placed. You can then use DocumentBuilder.MoveToBookmark method to move the cursor to a specific Bookmark and then insert Shape Node there. I hope, this helps.

Best regards,

Hi,
There is an issue with using bookmark. For your information, the final word document is obtained by using Aspose mail merge feature. So there is a possibility that a table can span across multiple pages.
Is there any other way to loop through each pages ?
Regards,
hadi teo

Hi,
Please review the requirements carefully. I hope that this can be achieved by manipulating the Word document itself.
Otherwise i will need to use alternative method which are defined below:

  1. Save the final DOC document without Shape into PDF. The final document will be saved into MemoryStream
  2. Subsequently i will read from the MemoryStream and add Shape marks in each individual pages.
  3. Finally save the PDF result into the disk.

The alternative method is not desirable for me as it involves saving the document into memory stream and additionally i will need to use Aspose PDF to manipulate it. The performance will be impacted as well. These process will be performed using multi-thread because multiple documents will be processed simultaneously.
Regards,
hadi teo

Hi,
I have attached my sample application. Refer to the “sample.zip” attachment.
In this sample, i have simulated my requirement by instantiating the word document template called ‘in.doc’. Subsequently i have populated the data and the result word document span across three pages due to the amount of the data.
If you noticed, in the last page, i have added the collection of shapes. My original intention is to add those shapes in each pages. But unfortunately i do not have the correct code to loop through each pages and add those shapes. Currently i can only use the following code to insert the shapes at the end of the document.

DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToDocumentEnd();

I hope my requirement is clear enough.
Regards,
hadi teo

Hi Hadi,

Thanks for the additional information. I have made some changes in your ‘Program.cs’ and attached here a modified version. Could you please run this program on your side and let me know if this produces the output you’re looking for?

Best regards,

Hi Awais,
This is a clever tricks. I didn’t expect that we can tag to the header/footer. I have tried your codes and it’s working correctly.
I really appreciate your assistance. Thank you for your help.
I may need to adjust and tweak the codes as the shapes are only displayed at even pages. For your information, we are printing the report in duplex format.
Regards,
hadi teo

Hi Hadi,

Thanks for your feedback on this. Please let us know any time you have any further queries. We’re always glad to help you.

Moreover, I would suggest you please take a look at the following articles on Headers/Footers:
https://reference.aspose.com/words/net/aspose.words/headerfooter/
https://reference.aspose.com/words/net/aspose.words/headerfootercollection/

Best regards,

Hi,
Previously you have suggested the solution by manipulating the Footer (FooterEven and FooterPrimary).
Apparently it’s not really solving my problem, because in the even pages, i still need to draw custom OMR shapes uniquely. By using ‘FooterEven’, the application will draw exactly the same contents which is not my requirement.
I would like to iterate again my main requirements: " I want to append unique Shapes collection into each individual pages ".

  1. Using Footer is not really solving my problem because the the similar content will be drawn in Footnote.
  2. I should be able to move to a specific page direcly and append the unique Shapes collection.

Regards,
hadi teo

Hi Hadi,

Thanks for your inquiry. Please note that Microsoft Word documents are flow documents and there is no ‘Page’ concept in Microsoft Word documents. Microsoft Word document consists of one or more Sections. Each Section has Body (main content) and three types of Header and Footers. Header and footer can be First Page (displayed only on the first page of the section), Primary (displayed on other pages of the document) and Even (displayed on even pages). I think, you should go for the solution you described here to achieve this. Please let me know if I can be of any further assistance.

Best regards,

Hi Hadi,

Thanks for your patience. I can think of a way you can anchor unique Shape combinations to the first Cell of First row in each Page that is produced during MailMerge. Anchor points are used to tie a graphic object such as a Shape to specific paragraphs in a Word document (These anchors don’t exist apart from these Shapes). Please find attached the sample code and let us know if this solution works for you.

Best regards,

Hi Awais,
Thanks very much for your assistance. I really appreciate it that you tried your best to provide alternative solutions. I will keep this solution in mind as alternative solution.
Regards,
hadi teo

Hi Hadi,

Sure, we will wait for your feedback on this. Please let us know any time you have any further queries. We’re always glad to help you.

Best regards,