Free Support Forum - aspose.com

Page Count Off

I am trying to count the number of pages that will be generated in a word document then insert the current page and total page in a bar code along with additional data, that is not relevant (test data at this point). The bar code generates successfully but the page count is off, and does something odd when it gets to page 4. The final document output is 5 pages when just saving the document WITHOUT any bar codes. below is the logic I’m using to get a bar code in each header.

private static void AddPageBreakAndSectionHeader(Document template, string headerText)
{
var runs = template.GetChildNodes(NodeType.Run, true);
var builder = new DocumentBuilder(template);
var dataStringMatches = new Regex(""(.*?)"").Matches(headerText);
var subString = “00000”;

        if (dataStringMatches.Count > 0)
            subString = dataStringMatches[0].Groups[1].ToString();
        builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
        var newString = subString.Replace("{NUMPAGES}", template.PageCount.ToString()).Replace("{PAGE}", "1");
        ResizeAndPositionBarcode(builder.InsertImage(CreateBarcodeImage(template, newString)));

        foreach (var run in runs)
        {
            //Find the page number
            var collector = new LayoutCollector(template);
            var pageNumber = collector.GetStartPageIndex(run);
            
            //if next run is on the next page, add a page break
            var nextNode = run?.ParentNode?.NextSibling;
            if (nextNode == null) continue;
            var nextPageNumber = collector.GetStartPageIndex(nextNode);
            if (nextPageNumber <= pageNumber) continue;
            builder.MoveTo(run);
            builder.InsertBreak(BreakType.SectionBreakNewPage);
            builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

            //Modify header footer
            newString = subString.Replace("{NUMPAGES}", template.PageCount.ToString()).Replace("{PAGE}", nextPageNumber.ToString());
            ResizeAndPositionBarcode(builder.InsertImage(CreateBarcodeImage(template, newString)));
        }
    }

I have also attached the document that is generated with the bar code on the right side.

TestMerge (2).zip (28.6 KB)

@johnmann04

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

The link below has your requested resources for testing…

Thanks,
John

@johnmann04

We are investigating this issue and will get back to you soon.

1 Like

@johnmann04

We have worked over your query and noticed that you are creating barcode and then inserting the content into Word document. This increases the page count to 5 and barcode has value “4 of 4”.

Please use Range.Replace method after calling AddPageBreakAndSectionHeader as shown below to get the correct output.

doc.UpdatePageLayout();
AddPageBreakAndSectionHeader(doc, headerText);

var options = new FindReplaceOptions { ReplacingCallback = new CustomReplaceTextWithImage() };
doc.Range.Replace(new Regex(@"\{\[(.*?)\]\}"), "", options);

doc.Save(@"c:\temp\barcodeConsoleAppOutput.docx");

That fixed the last barcode on page five. However, that is there for testing that we can create a barcode on the page itself our focus is on the barcode that runs down the right margin. This one needs to be correct as it should be 1 of 5, 2 of 5, 3 of 5 and so on (we omit the “of” from the barcode in the right margin). The barcode in the right margin is most important to us that it have the correct page count.

Thanks!

@johnmann04

You are facing the expected behavior of Aspose.Words. Your document contains one Section and you are inserting the barcode image in the header of document. In this case, the same image will be displayed on all pages of document. You can check the behavior by inserting the same image into your input document’s header using MS Word.

In your case, we suggest you following solution.

  1. Get the page count of Word document using Document.PageCount property.
  2. Generate barcode images for all pages.
  3. Insert each image according to page number on each page and set its position according to your requirement.

To insert image on each page, please move the cursor to the first paragraph of page and insert the image. To get the paragraph’s page number, please use LayoutCollector.GetStartPageIndex. This method returns 1-based index of the page where node begins.

Hope this helps you.