Introduction

This blog is mainly focused on SharePoint
I will use this to store and share all the tips and tricks about SharePoint and the related products
List of all posts

Generate PDF document with PDFSharp


Topic Generate PDF document with PDFSharp
OutputPDF Document
RefPDFSharp can be found @www.pdfsharp.net

Step Description
1 Create the pdf document
 PdfDocument pdfDocument = new PdfDocument();
2 Add Info and first page
 pdfDocument.Info.Title = "Sample PDF";
    pdfDocument.Info.Author = "Tzarad";
    pdfDocument.Info.Subject = "Quick start for pdf generation with pdfsharp";
    pdfDocument.Info.Keywords = "pdfsharp";
    PdfPage page = pdfDocument.AddPage();
3 Get the graphic environnement
    XGraphics gfx = XGraphics.FromPdfPage(page);
    gfx.MUH = PdfFontEncoding.Unicode;
    gfx.MFEH = PdfFontEmbedding.Default;
4 Define custom colors
    MigraDoc.DocumentObjectModel.Color ColorCustomLight = MigraDoc.DocumentObjectModel.Color.Parse("0xFF34BDE4");
    MigraDoc.DocumentObjectModel.Color ColorCustomDark = MigraDoc.DocumentObjectModel.Color.Parse("0xFF0055A0");
    MigraDoc.DocumentObjectModel.Color ColorCustomNormal = Colors.Black;
5 Define custom fonts
    MigraDoc.DocumentObjectModel.Font CustomLigthSmall = new MigraDoc.DocumentObjectModel.Font();
    CustomLigthSmall.Name = "Trebuchet MS";
    CustomLigthSmall.Size = 7.5;
    CustomLigthSmall.Color = ColorCustomLight;
    MigraDoc.DocumentObjectModel.Font CustomDarkSmall = new MigraDoc.DocumentObjectModel.Font();
    CustomDarkSmall.Name = "Trebuchet MS";
    CustomDarkSmall.Size = 7.5;
    CustomDarkSmall.Color = ColorCustomDark;
    MigraDoc.DocumentObjectModel.Font CustomStandart = new MigraDoc.DocumentObjectModel.Font();
    CustomStandart.Name = "Trebuchet MS";
    CustomStandart.Size = 12;
    CustomStandart.Color = ColorCustomNormal;
    MigraDoc.DocumentObjectModel.Font CustomLargeFont = new MigraDoc.DocumentObjectModel.Font();
    CustomLargeFont.Name = "Trebuchet MS";
    CustomLargeFont.Size = 16;
    CustomLargeFont.Color = ColorCustomNormal;
    MigraDoc.DocumentObjectModel.Font CustomStandartSmall = new MigraDoc.DocumentObjectModel.Font();
    CustomStandartSmall.Name = "Trebuchet MS";
    CustomStandartSmall.Size = 7.5;
    CustomStandartSmall.Color = ColorCustomNormal;
6 Create doc and section
    // You always need a MigraDoc document for rendering.
 Document doc = new Document();
    Section sectop = doc.AddSection();
7 Create paragraph, add text and image
    Paragraph paratopleft = sectop.AddParagraph();
    paratopleft.Format.Alignment = ParagraphAlignment.Left;
    paratopleft.AddImage(moduleImagesPath + "logo.jpg");
    paratopleft.Format.Font = CustomLigthSmall.Clone();
    paratopleft.AddLineBreak();
    paratopleft.AddFormattedText("Company name", TextFormat.Bold);
moduleImagesPath : return the path to the images
8 Render the page : prerender step
    PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always);
    pdfRenderer.Document = doc;
    pdfRenderer.PrepareRenderPages();
9 Render the page : render object
 pdfRenderer.DocumentRenderer.RenderObject(gfx, XUnit.FromPoint(40), XUnit.FromPoint(20), XUnit.FromPoint(200), paratopleft);   
10 Render the page : render and save to stream
 pdfRenderer.RenderDocument();
    string filename = "Delmo" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
    MemoryStream stream = new MemoryStream();
    pdfDocument.Save(stream); 

No comments:

Post a Comment

by Category