PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Adding short or long text as new set of pages to PDF
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4454
Page 1 of 1

Author:  choco [ Tue Jun 27, 2023 7:16 pm ]
Post subject:  Adding short or long text as new set of pages to PDF

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

Author:  TH-Soft [ Wed Jun 28, 2023 6:46 am ]
Post subject:  Re: Adding short or long text as new set of pages to PDF

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.

Author:  choco [ Wed Jun 28, 2023 5:08 pm ]
Post subject:  Re: Adding short or long text as new set of pages to PDF

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?

Author:  TH-Soft [ Thu Jun 29, 2023 7:45 am ]
Post subject:  Re: Adding short or long text as new set of pages to PDF

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.

Author:  choco [ Mon Jul 10, 2023 9:05 pm ]
Post subject:  Re: Adding short or long text as new set of pages to PDF

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!

Author:  TH-Soft [ Mon Jul 10, 2023 9:09 pm ]
Post subject:  Re: Adding short or long text as new set of pages to PDF

AddImage can be used with PDF files.

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