PDFsharp & MigraDoc Foundation

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

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: Tue Sep 28, 2021 8:41 am 
Offline

Joined: Tue Sep 28, 2021 8:33 am
Posts: 4
Using PDFSharp, I would like to create one PDF-document for each distinct value in the property of the List I am looping through.

Grouping my collection, and creating a List:

Code:
var listName = collectionName
    .GroupBy(p => new { p.propertyName })
    .ToList();


Trying to execute my PDFsharp-code for every propertyName in listName:

Code:
foreach (var trip in paidTrip) {
    // Getting just the name string from the specific propertyName key
    string[] remove = { "{", "}", "propertyName", "=" };
    string pnString = trip.Key.ToString();
    foreach (string item in remove) {
        pnString = pnString.Replace(item, string.Empty);
}


Right here is where I believe I drop the ball; how can I bring each name with me to their distinct PDF-document? I am missing that connection. So, underneath this, I start creating my PDF-document(s):

Code:
   
    // Continued
    System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
    PdfDocument doc = new();
    PdfPage page = doc.AddPage();
    XGraphics gfx = XGraphics.FromPdfPage(page);
    // Adding fonts and brushes...
   
    // Adding some text, to check If I am able to grab the names of propertyName (which I am - but jut the first, once)...
    gfx.DrawString(pnString, new XFont("Arial", 40, XFontStyle.Bold), myGreen, new XPoint(5, 250));

    // And then, saving the PDF-document
    doc.Save("C:\My\File\Path\Test.pdf");


But as I said, this just saves one PDF for the first name found. I believe it is trying to save one file per name found, but it can't, because the file has already been created with the file name specified.

So my question is: how can I make sure that each name found in the foreach loop is brought with me to when I create the PDFs, and save one PDF-document for each of them?


Top
 Profile  
Reply with quote  
PostPosted: Tue Sep 28, 2021 8:18 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
Answered on SO:
https://stackoverflow.com/a/69360036/162529

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


Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 30, 2021 12:06 pm 
Offline

Joined: Tue Sep 28, 2021 8:33 am
Posts: 4
Thanks alot!


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: Bing [Bot], Google [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