PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 6:38 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Tue Jun 27, 2023 7:16 pm 
Offline

Joined: Tue Jun 27, 2023 6:44 pm
Posts: 3
I've used PDF Sharp to implement a somewhat complex feature for a client. So, I have a PdfDocument object and now I have a new requirement to add a new page containing a header and text. In some cases the text will be too long to fit on one page, so it needs to continue to as many subsequent pages as is needed, each subsequent page will also need a page header showing something like "narrative continued ...". I would like the headers on the first and subsequent pages to be a fillable form, so that I can lay it out in Adobe Acrobat as a template ahead of time.

From my reading it sounds like using MigraDoc for this is the best choice as it will span the pages automatically, however I'm not sure about the header thing for each page. Am I able to use MigraDoc AddSection and AddParagraph for the automatic pagination in conjunction with a header on each automatically generated page? And will I have any issues attaching this new functionality to the end of my already-generated PDF that used PDF Sharp from the previous feature that was implemented?

Thanks for your help


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 28, 2023 6:46 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
Hi!

MigraDoc can be used to render the text.

MigraDoc can have a different first page header. So you can use an empty header on the first page and have "narrative continued..." as header for standard pages (all other pages).

This post shows how you can draw MigraDoc contents page by page using RenderPage:
viewtopic.php?f=8&t=3172
You do not need a callback for your purpose.
This allows to draw headers with PDFsharp while rendering the body text with MigraDoc.

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


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 28, 2023 5:08 pm 
Offline

Joined: Tue Jun 27, 2023 6:44 pm
Posts: 3
Thank you @TH-Soft

Yes, I didn't realize how simple it actually was. I assumed MigraDoc didn't have such rich support for headers. Here is what I ended up doing:

Code:
Document migraDocDocument = new Document();

migraDocDocument.Styles["Normal"].Font.Name = "Courier New";

var section = migraDocDocument.AddSection();
section.Headers.FirstPage.AddParagraph("First page header");
section.PageSetup.DifferentFirstPageHeaderFooter = true;
section.Headers.Primary.AddParagraph("This is my header");
section.PageSetup.PageFormat = PageFormat.Letter;
section.AddParagraph(
        "Lorem ipsum dolor sit amet"
    );

PdfDocumentRenderer pdfDocumentRenderer = new PdfDocumentRenderer();
pdfDocumentRenderer.Document = migraDocDocument;
pdfDocumentRenderer.RenderDocument();

using (MemoryStream memoryStream = new MemoryStream())
{
    pdfDocumentRenderer.PdfDocument.Save(memoryStream, false);

    // Open the PDF document from the memory stream with the desired PdfDocumentOpenMode
    using (var streamedPdfDocument = PdfReader.Open(memoryStream, PdfDocumentOpenMode.Import))
    {
        foreach (var current in streamedPdfDocument.Pages)
        {
            document.AddPage(current);
        }
    }
}


One question though, it looks like you can't embed a PDF in the Header without first rendering it as an image, is that correct?


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 29, 2023 7:45 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
choco wrote:
One question though, it looks like you can't embed a PDF in the Header without first rendering it as an image, is that correct?
I don't know. Did you try it?
PDFs can be shown in the body and I assume they also work in the header. It's the same code that draws the paragraphs after all, header or body.

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 10, 2023 9:05 pm 
Offline

Joined: Tue Jun 27, 2023 6:44 pm
Posts: 3
Well, I just saw that the API only allowed for AddImage or AddParagraph on the header so I wouldn't even know how to go about adding a PDF in there but we've found a solution that we like that doesn't involve embedding a PDF in the header, so while it may be possible I don't think that it is and I don't plan on researching it further at this point.

Thank you for your help though!


Top
 Profile  
Reply with quote  
PostPosted: Mon Jul 10, 2023 9:09 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 916
Location: CCAA
AddImage can be used with PDF files.

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


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

All times are UTC


Who is online

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