0
votes

I'm rendering a PDF document with MigraDoc. Each section has one or more paragraph texts.

Currently this is how I create a document;

var document = new Document();
var pdfRenderer = new PdfDocumentRenderer(true);
pdfRenderer.Document = document; 

for(int i=0;i<10;i++){
    Section section = document.AddSection();
    section.PageSetup.PageFormat = PageFormat.A4;

    for(int j=0;j<5;j++) {
    var paragraphText = GetParaText(i,j); // some large text can span multiple pages
    section.AddParagraph(paragraphText);
    //Want page count per section? 
     // Section 1 -> 5 , Section 2 ->3 etc.
    // int count = CalculateCurrentPageCount(); //*EDIT*
   }
}
// Create the PDF document
pdfRenderer.RenderDocument();
pdfRenderer.Save(filename);

Edit : Currently i use the following code to get the page count.
But it takes a lot of time ,possibly every page is rendered twice.

 public int CalculateCurrentPageCount()
        {
            var tempDocument = document.Clone();
            tempDocument.BindToRenderer(null);     
            var pdfRenderer = new PdfDocumentRenderer(true);
            pdfRenderer.Document = tempDocument;
            pdfRenderer.RenderDocument();
            int count = pdfRenderer.PdfDocument.PageCount;
            Console.WriteLine("-- Count :" + count);
            return count;
        }

Some of the sections can span multiple pages depending on content added.

Is it possible to get/find how many pages (in PDF) it took for a Section to render?

Edit 2 : Is it possible to tag a section and find on which page it starts on?

2

2 Answers

1
votes

Thx for the help. I calculated it like this (i.e. To get the count in code...) :

First i tagged the section with a creation count of the section

newsection.Tag = num_sections_in_doc; //count changes every time i add a section

Then i used GetDocumentObjectsFromPage :

var x = new Dictionary<int, int>();
                int numpages = pdfRenderer.PdfDocument.PageCount;
                for (int idx = 0; idx < numpages; idx++)
                {
                    DocumentObject[] docObjects = pdfRenderer.DocumentRenderer.GetDocumentObjectsFromPage(idx + 1);
                    if (docObjects != null && docObjects.Length > 0)
                    {
                        Section section = docObjects[0].Section;
                        int sectionTag = -1;
                        if (section != null)
                            sectionTag = (int)section.Tag;
                        if (sectionTag >= 0)
                        {
                            // count a section only once
                            if (!x.ContainsKey(sectionTag))
                                x.Add(sectionTag, idx + 1);
                        }
                    }
                }

x.Keys are the sections.
and x.values are the start of each section.

0
votes

If you want to display the page count in the PDF, use paragraph.AddSectionPagesField().

See also:
https://stackoverflow.com/a/19499231/162529

To get the count in code: you can add a tag to any document object (e.g. to any paragraph) and then use docRenderer.GetDocumentObjectsFromPage(...) to query the objects for a specific page. This allows you to find out which section the objects on this page belong to.

Or create each section in a separate document and then combine them to one PDF using docRenderer.RenderPage(...) as shown here:
http://www.pdfsharp.net/wiki/MixMigraDocAndPdfSharp-sample.ashx
The sample scales pages down to thumbnail size - you would draw them 1:1, each on a new page.