Search code examples
c#algorithmpdfpdf-generationpdfsharp

Why is this page staying at zero? Is this a scope/hositing/reference/etc. issue?


Basically, I have a function

Action NewPageIfNecessary = () => {
    if ( curY > (page.Height - 20 ) ) 
    {
        D.AddPage();
        curY = 0;
        page = D.Pages[++pagenum];
    }
};

that I wrote so that my PDFsharp drawing program knows to create and move on to a new page if we're too far down the previous page. That is sandwiched between code like

PdfDocument D = new PdfDocument();
D.Info.Title = "Self-Assessment Results Summary";
D.AddPage();
int pagenum = 0;
PdfPage page = D.Pages[pagenum];
page.Orientation = PageOrientation.Portrait;

and

foreach ( var Survey in Surveys )
{
    NewPageIfNecessary();
    gfx.DrawString(Survey.Title, new XFont("Arial", 24), XBrushes.Black, 20, curY += 32);
    foreach ( var S in Survey.SectionAverages )
    {
        NewPageIfNecessary();
        gfx.DrawString(S.SectionTitle, new XFont("Arial", 20), XBrushes.Black, 30, curY += 30); 

    }
    foreach ( var S in Survey.ViewModel ) 
    {
        NewPageIfNecessary();
        gfx.DrawString(S.Title, new XFont("Arial", 20), XBrushes.Black, 30, curY += 30);
        foreach ( var SS in S.SubSections )

but the issue I'm noticing is that

page = D.Pages[++pagenum];

is not bringing me on to a new page. Any idea why?


Solution

  • You do not show the code that assigns gfx. Your page variable does not define where things are drawn. Free the gfx and create a new one for the new page.

    See also:
    https://stackoverflow.com/a/21143712/1015447
    https://stackoverflow.com/a/32488876/1015447