PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Table and page breaks
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4484
Page 1 of 1

Author:  christophe62 [ Mon Oct 09, 2023 2:43 pm ]
Post subject:  Table and page breaks

Hi!
I've been desperately looking for a working example for a week now

I've read that: "MigraDoc adds page breaks automatically and repeats the table header automatically."

But I haven't found any examples that demonstrate this.

This my currently sample :

Code:
namespace Share.PDF;

using PdfSharpCore.Pdf;
using PdfSharpCore.Drawing;

using MigraDocCore.DocumentObjectModel.Tables;
using MigraDocCore.DocumentObjectModel;

public static class TableMultiPage
{
    private static PdfDocument? document;

   
    public static byte[] GetPDF()
    {
        document = new();

        // Create new page
        PdfPage page = document.AddPage();
        XGraphics gfx = XGraphics.FromPdfPage(page);
        //XFont font = new("OpenSans-Regular", 20, XFontStyle.Regular, options);

        Document doc = new();
        Section sec = doc.AddSection();

        sec.PageSetup.TopMargin = Unit.FromCentimeter(5);
        sec.PageSetup.BottomMargin = Unit.FromCentimeter(5);

        Table table = CreateTableMultiPage( sec);

        //PdfDocumentRenderer pr = new();
        //pr.PdfDocument.
        // Create a renderer and prepare (=layout) the document
        MigraDocCore.Rendering.DocumentRenderer docRenderer = new(doc);
        docRenderer.PrepareDocument();

        // Render the paragraph. You can render tables or shapes the same way.
        docRenderer.RenderObject(gfx, XUnit.FromCentimeter(1), XUnit.FromCentimeter(1), "12cm", table);


        MemoryStream PdfStream = new();
        document.Save(PdfStream);

        return PdfStream.ToArray();
    }


    private static Table CreateTableMultiPage(Section sec)
    {
        Table table = new();

        table.Borders.Width = 0.75;
        table.KeepTogether = false;

        table.AddColumn(Unit.FromCentimeter(3));
        table.AddColumn(Unit.FromCentimeter(2));

        Row row = table.AddRow();
        row.Shading.Color = Colors.PaleGoldenrod;
        Cell cell = row.Cells[0];
        cell.AddParagraph("N.");
        cell = row.Cells[1];
        cell.AddParagraph("Info");

        for (int line = 0; line < 100; ++line)
        {
            row = table.AddRow();
            row.HeadingFormat = true;
            row.Borders.Top.Width = 1;

            cell = row.Cells[0];
            cell.AddParagraph(line.ToString());

            cell = row.Cells[1];
            cell.Format.Alignment = ParagraphAlignment.Center;
            cell.AddParagraph("blablablaa");
        }

        sec.Add(table);

        return table;
    }

}


I don't know how to get page breaks :(
Many thanks in advance !

Attachments:
Capture d'écran 2023-10-09 164046.png
Capture d'écran 2023-10-09 164046.png [ 31.31 KiB | Viewed 3761 times ]

Author:  TH-Soft [ Mon Oct 09, 2023 3:00 pm ]
Post subject:  Re: Table and page breaks

When using "RenderObject" you must create page breaks as you create the pages.

Use MigraDoc as intended and pagebreaks come free.
Sample code:
https://pdfsharp.net/wiki/HelloMigraDoc-sample.ashx
https://pdfsharp.net/wiki/Invoice-sample.ashx

Don't forget to set "row.HeadingFormat = true;" for the header rows.

Author:  christophe62 [ Mon Oct 09, 2023 3:25 pm ]
Post subject:  Re: Table and page breaks

Thank you for your help.
I have seen these pages (and so many others)
You're talking about this: MigraDocCore.DocumentObjectModel.PageBreak ?

But I can't find any samples to show me how to use it

(thank for the line : row.HeadingFormat = true; )

Author:  christophe62 [ Mon Oct 09, 2023 5:29 pm ]
Post subject:  Re: Table and page breaks

I admit that I had difficulty interpreting your first sentence ^^
I am very happy to have finally found thanks to your help

Here is the code to replace to help other people :
Code:
//// Create a renderer and prepare (=layout) the document
//DocumentRenderer docRenderer = new(doc);
//docRenderer.PrepareDocument();

//docRenderer.RenderObject(gfx, XUnit.FromCentimeter(1), XUnit.FromCentimeter(1), "12cm", table);

//MemoryStream PdfStream = new();
//document.Save(PdfStream);

PdfDocumentRenderer renderer = new(true);
renderer.Document = doc;

renderer.RenderDocument();

MemoryStream PdfStream = new();
renderer.PdfDocument.Save(PdfStream);



return PdfStream.ToArray();

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/