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

Create PDF and add footer
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3739
Page 1 of 1

Author:  emaborsa [ Fri Mar 09, 2018 2:47 pm ]
Post subject:  Create PDF and add footer

Hi, I'm new in MigraDoc and I'm trying to create a PDF and to add a footer.
Following this example I created my Document which includes a table. Now the idea was to add a footer, I found this example, but I'm not able to combine the two things.
My problem is that by creating my table the class Document is used, for the footer a PDFDocument is required instead.
Any help?
Thanks.

Author:  TH-Soft [ Fri Mar 09, 2018 3:03 pm ]
Post subject:  Re: Create PDF and add footer

Hi!

Document is MigraDoc, PdfDocument is PDFsharp.

The MigraDoc samples are here:
http://pdfsharp.net/wiki/MigraDocSamples.ashx

The Invoice sample uses a footer:
http://pdfsharp.net/wiki/Invoice-sample.ashx

So does this sample:
http://pdfsharp.net/wiki/HelloMigraDoc-sample.ashx

Author:  emaborsa [ Mon Mar 12, 2018 7:52 am ]
Post subject:  Re: Create PDF and add footer

I ended up with this code, it works:

Code:
public static void Buil(Sitzung sitzung)
{
   Document document = BuildDocument(sitzung);

   DefineStyles(document);

   PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always)
   {
      Document = document
   };
   renderer.RenderDocument();

   PdfDocument pdfDocument = renderer.PdfDocument;
   PdfPage page = pdfDocument.Pages[0];
   AddFooter(page, userName);

   DocumentRenderer docRenderer = new DocumentRenderer(document);
   docRenderer.PrepareDocument();

   MemoryStream stream = new MemoryStream();
   pdfDocument.Save(stream);
}



public static void AddFooter(PdfPage page, string userName)
{
   XGraphics gfx = XGraphics.FromPdfPage(page);
   XRect rect = new XRect(new XPoint(), gfx.PageSize);
   rect.Inflate(-15, -20);

   XFont font = new XFont(Frutiger, 7, XFontStyle.Italic);
   XStringFormat format = new XStringFormat
   {
      Alignment = XStringAlignment.Near,
      LineAlignment = XLineAlignment.Far
   };
   gfx.DrawString($"Ausdruck von {userName} um {DateTime.Now.ToString("dd.MM.yyyy HH:mm")}", font, XBrushes.Black, rect, format);
}

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