PDFsharp & MigraDoc Foundation

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

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Mon Feb 07, 2022 12:38 pm 
Offline

Joined: Mon Feb 07, 2022 12:21 pm
Posts: 2
Hi, i have problem with migradoc. I`am trying to create table by mixing PdfSharp and Migadoc.
My code:
Code:
private void GeneratePdf(ref HttpResponseMessage response, string recordId, Model model)
    {
   PdfDocument documentpdf = new PdfDocument();
    PdfPage page = documentpdf.AddPage();
    page.Width = XUnit.FromMillimeter(210);
    page.Height = XUnit.FromMillimeter(297);
    XGraphics gfx = XGraphics.FromPdfPage(page);
    gfx.MUH = PdfFontEncoding.Unicode;
    XFont fontX = new XFont(SomeFont, 20, XFontStyle.Underline);
    Font font = new Font(SomeFont, 11);

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

    //logo
    AddLogoWithTitle(page, gfx, fontX, SomeDate);

    MigraDoc.DocumentObjectModel.Tables.Table table = section.AddTable();
    table.Style = "Table";
    table.Format.Font = font;
    table.Format.Borders.Distance = "0.3cm";
    table.Borders.Bottom.Color = MigraDoc.DocumentObjectModel.Colors.DarkGray;
    table.KeepTogether = false;
    Column col1a = table.AddColumn("2cm");
    col1a.Format.Alignment = ParagraphAlignment.Right;
    Column col1aMid = table.AddColumn("2cm");
    Column col2a = table.AddColumn("2cm");
    col2a.Format.Alignment = ParagraphAlignment.Left;
    Column col1b = table.AddColumn("2cm");
    col1b.Format.Alignment = ParagraphAlignment.Right;
    Column col1bMid = table.AddColumn("2cm");
    Column col2b = table.AddColumn("2cm");
    col2b.Format.Alignment = ParagraphAlignment.Left;
    Row row = table.AddRow();
    row.HeadingFormat = true;
   
   //calling it multiple times with other values
   CreateSectionPartLabelWith3Merged("someString", model.property, table,row);
   
   DocumentRenderer docRenderer = new DocumentRenderer(doc);
    #region Generowanie pliku i renderowanie tabel
    docRenderer.PrepareDocument();
    docRenderer.RenderObject(gfx, 5, XUnit.FromCentimeter(4), "12cm", table);
}
private void CreateSectionPartLabelWith3Merged(string partLabel, string partValue, MigraDoc.DocumentObjectModel.Tables.Table table, Row row)
{
    row = table.AddRow();

    row.Cells[0].Format.Font.Bold = true;
    row.Cells[1].MergeRight = 4;
    row.Cells[0].Format.Font.Size = 11;
    row.Cells[0].AddParagraph(partLabel);
    row.Cells[1].AddParagraph(partValue);
}
private void AddLogoWithTitle(PdfPage page, XGraphics gfx, XFont fontX, string createDate)
{
    XRect rectTitle = new XRect(0, 45, page.Width, 70);
    gfx.DrawString("Some text" + createDate, fontX, XBrushes.Black, rectTitle, XStringFormats.Center);
    string path = "SomePath"
    XImage logoImage = XImage.FromFile(path);
    gfx.DrawImage(logoImage, 10, 10, 187, 40);
}


The problem is when i call function to add next row with section and table reach end of page, then my text doesn`t appear on the next page (next page doesn`t exist, just text out of page boundary). I was trying to change table.KeepTogether to false or setting KeepWith to some number but still same problem.


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 07, 2022 12:42 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
justUser wrote:
The problem is when i call function to add next row with section and table reach end of page, then my text doesn`t appear on the next page (next page doesn`t exist, just text out of page boundary). I was trying to change table.KeepTogether to false or setting KeepWith to some number but still same problem.
Here is a sample that creates a document page by page, allowing PDFsharp code to add content to each page:
viewtopic.php?p=9293#p9293

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Feb 07, 2022 1:38 pm 
Offline

Joined: Mon Feb 07, 2022 12:21 pm
Posts: 2
Ok, thanks for quick response, I added
Code:
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();

and call RenderDocument(); on this object but then i got exception: "DocumentRenderer" must be set before calling "PrepareDocumentRenderer"
I know that for you is soo simple, but i am new in this library and i dont know that correct flow.

EDIT: doesnt matter, i used only migradoc without pdfsharp


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 136 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