PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 12:02 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Table and page breaks
PostPosted: Mon Oct 09, 2023 2:43 pm 
Offline

Joined: Mon Oct 09, 2023 12:42 pm
Posts: 10
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 3484 times ]
Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 09, 2023 3:00 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 09, 2023 3:25 pm 
Offline

Joined: Mon Oct 09, 2023 12:42 pm
Posts: 10
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; )


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 09, 2023 5:29 pm 
Offline

Joined: Mon Oct 09, 2023 12:42 pm
Posts: 10
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();


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 140 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group