Page number issue on the Merged forms

Hi,
I have a business need to merge multiple legal forms in one document that we are using Aspose to create Word and PDF as combined forms docment. But the issue is page numbering shows one continuous e.g. if we have 3 forms each 4 pages then it should show page 1 of 4, page 2 of 4 for each forms instead it is showing age 1 of 12 – Page 2 of 12 … Page 12 of 12. What shoudl I do to make it show individual form numberings in a combined document. Following is the code as a reference:

public static Document MergeDocuments(ArrayList documentList)
{
    Document finalDocument = null;
    foreach(Document individualDocument in documentList)
    {
        // individualDocument.AcceptAllRevisions();
        if (finalDocument == null)
        {
            finalDocument = individualDocument;
        }
        else
        {
            bool b = individualDocument.Sections[0].HeadersFooters[HeaderFooterType.HeaderPrimary].IsLinkedToPrevious;
            finalDocument.AppendDocument(individualDocument, ImportFormatMode.KeepSourceFormatting);
        }
    }
    return finalDocument;
}

Please help me get this issue fixed.
Thanks,
Ramesh Pandey

Hi

Thanks for your request. Maybe you should insert total number of pages as plain text instead of inserting it as NUMPAGES field. You can gen number of pages in the particular document using Document.PageCount property:
https://reference.aspose.com/words/net/aspose.words/document/pagecount/
Also, if each your document consists of only one section, then you can use SECTIONPAGES field instead of NUMPAGES field.
Hope this helps.
Best regards.

Hi Alexey,
Thanks for the quick reply. Actually our Forms are built by templates merging with form fields. We don’t use the document builder to generate the document per se. How to use the Text object in Word (non pdf) code set, iseems like it is designed for PDF not word. Also folowinbg two lines

finalDocument.Sections[sectionCount].PageSetup.RestartPageNumbering = true;
finalDocument.Sections[sectionCount].PageSetup.PageStartingNumber = 1;

made this work up to half way. I mean, document parts(multiple form sections) are starting from 1 now however the issue is that these form pages are displaying as 1 of 16, Page 2 of 16 where forms are set of 4 with each four pages. Expected display is to render as Page 1 of 4, Page 2 of 4,… Page 4 of 4 and then the next form starts with again Page 1 of 4 etc…, it should not display as Page 1 of 16 where 16 is total number of pages in entire documents not those specific forms (page number for the forms vary).
I think I am closure to the resolution with above two lines but need your help in finalizing it to fix total page display.

Hi

Thank for your request. As I told you earlier, you should avoid using NUMPAGES field in your document. Please attach your template here for testing. I will modify it and sent it back to you.
Best regards,

Hi Alexey,
Thanks for the quick reply. I used yur approach of inserting page numbers in the footer using document builder but have two issues/questions. As you will see in the attached document in the footer tagged by Ramesh for page, I am able to generate the page numbers but not able to indent or format at the right place. Second issue - let us say after your suggestion I can format the page number in the footer section properly, how should I hide the original page number that is appearing in the footer apart from the docunebt builder generated pae number. Here is the code for your reference:

finalDocument.Sections[sectionCount].PageSetup.RestartPageNumbering = true;
finalDocument.Sections[sectionCount].PageSetup.PageStartingNumber = 1;
DocumentBuilder builder = new DocumentBuilder(finalDocument);
builder.MoveToSection(sectionCount);
builder.MoveToHeaderFooter(Aspose.Words.HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.CurrentSection.HeadersFooters.LinkToPrevious(false);
builder.Write("PageRamesh ");
builder.InsertField("PAGE", "");
builder.Write("ofRamesh ");
builder.InsertField("SECTIONPAGES", "");
finalDocument = builder.Document;

Thanks for your help.

Hi Alexey,
Attached is the document that has bunch of forms with separate templates and formats. In the footer you will see the names of the forms to differetiate one from other. My previous post has code too. Also in the footer you will see one page x of y with Ramesh tag (for testing purposes) and other other one Page x of y generated by the Aspose standard process (non Document Builder - Template merging process).
Thanks,
Ramesh Pandey

Hi Ramesh,

Thank you for additional information. But my suggestion was not using DocumentBuilder such way to insert page numbers into your document. I think, it is better to modify your template. You can insert bookmark, for example, where NUMPAGES field is located. Then you can use DocuemntBuilder to move to the bookmark and insert number of pages.
Is it acceptable for you to modify your template?
Best regards.

Thanks Alexey, I am looking into all the Templates for modifying for Num Pages. In the meantime I am instructed to fix the page number in PDF because in reality PDF is the one that is sent to the customers. I tried using following code bt it’s similar issue of where Page x of y - y field is always combined total not related to the section total. Just to clarify Each forms that is appended in one form needed their own umbering as we have been discussing and this combined document is the one being converted using Aspose PDF component. Following is the code

Text oText;

foreach(Aspose.Pdf.Section sec in pdfFile.Sections)
{
    // Get a section in the pdf object
    Aspose.Pdf.Section sec1 = sec;
    sec.StartingPageNumber = 1;
    sec.IsPageNumberRestarted = true;
    sec.IsNewPage = true;
    // Create a HeaderFooter object for the section
    Aspose.Pdf.HeaderFooter hf = new Aspose.Pdf.HeaderFooter(sec1);
    // Set the HeaderFooter object to odd and even footers
    sec1.OddFooter = sec1.EvenFooter = hf;
    // Add a text paragraph containing current page number of total number of pages
    oText = new Text(hf, "page $p of $P ");
    oText.IsFirstParagraph = true;
    oText.IsAlignedByWord = false;
    hf.Paragraphs.Add(oText);
}
return pdfFile;

Any help on this one is greatly appreciated. Thanks for all your help. I am assuming that sections in the PDF is appnded forms corresponsdingly.

Hi

Thanks for your request. I think you should modify your template as shown in the attached document. I inserted bookmark in the document footer, after filling the document with data I insert number of pages calculated using Document.PageCount into this bookmark.

// Create an empty docuemnt that will be used as master document
Document master = new Document();
master.RemoveAllChildren();
// Generate sub docuemnts in the loop
for (int docIdx = 0; docIdx <5; docIdx++)
{
    // Open docuemnt
    Document doc = new Document(@"Test168\in.doc");
    DocumentBuilder builder = new DocumentBuilder(doc);
    // Configure sections
    doc.FirstSection.PageSetup.RestartPageNumbering = true;
    doc.FirstSection.PageSetup.SectionStart = SectionStart.NewPage;
    // Here you should fill your template with data.
    // in my case i jusr inset few lines using DocumentBuilder
    for (int i = 0; i <80; i++)
        builder.Writeln("This is just test document");
    // Insert page numbers
    doc.Range.Bookmarks["pagenum"].Text = doc.PageCount.ToString();
    // Append sub documen tto the marster document
    master.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);
}
// Save output as PDF
master.SaveToPdf(@"Test168\out.pdf");

Hope this helps.
Best regards.

Thanks Alexey, I am getting

doc.Range.Bookmarks["pagenum"] as null at the following line:
doc.Range.Bookmarks["pagenum"].Text = doc.PageCount.ToString();

Please suggest me how to fix it.

Hi

Thanks for your inquiry. Please make sure you have inserted this bookmarks into your document.
Best regards.