PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

setting font size results in System.NullReferenceException
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4158
Page 1 of 1

Author:  tomse [ Thu Jul 23, 2020 1:16 pm ]
Post subject:  setting font size results in System.NullReferenceException

Hello
I get a System.NullReferenceException at style.Font.Size = 20; (beneath [Title]), when trying out the following code.
What am I doing wrong?
The project is simplified down to as little as possible, and still get the error message.

VS2019 CE, PDFsharp-MigraDoc-gdi v1.50.5147 from nuget

(please make note I'm a novice coder)


Code:
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PdfTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void createPDF()
        {
            // Create a new PDF document
            Document document = new Document();
            Section section = document.AddSection();
            section.PageSetup.PageFormat = PageFormat.A4;
            document.Info.Title = "Test";
            document.Info.Author = "Test";
            document.Info.Subject = "Test";


            DefineStyles(document);

            Paragraph paragraph = section.Headers.Primary.AddParagraph();
            paragraph.Style = "Title";
            paragraph.AddText("HEADER Test Text");
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            paragraph = section.Headers.Primary.AddParagraph();
            paragraph.Style = "Footer";
            paragraph.AddText("Footer test text");
            paragraph.Format.Alignment = ParagraphAlignment.Left;

            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false);
            pdfRenderer.Document = document;
            pdfRenderer.RenderDocument();
            const string filename = "c:\\temp\\HelloWorld.pdf";
            pdfRenderer.PdfDocument.Save(filename);
            // ...and start a viewer.
            Process.Start(filename);
        }

        public static void DefineStyles(Document document)
        {

            Style style = document.Styles["Normal"];
            style.Font.Name = "Arial";
            style.Font.Size = 14;

            // Create a font

            style = document.Styles["Title"];
            style.Font.Size = 20;
            style.Font.Bold = true;

            style = document.Styles["DateField"];
            style.Font.Size = 10;


            style = document.Styles["Text"];
            style.Font.Bold = true;

            style = document.Styles["Footer"];
            style.Font.Size = 8;

            style = document.Styles["Heading1"];
            style.Font.Size = 30;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            createPDF();
        }
    }
}

Author:  TH-Soft [ Thu Jul 23, 2020 2:55 pm ]
Post subject:  Re: setting font size results in System.NullReferenceExcepti

tomse wrote:
Hello
I get a System.NullReferenceException at style.Font.Size = 20; (beneath [Title]), when trying out the following code.
What am I doing wrong?
The style does not exist. Create a style with the name "Title" or use an existing style like "Heading1".
See also:
http://pdfsharp.net/wiki/Invoice-sample ... e_Styles_1

Author:  tomse [ Fri Jul 24, 2020 8:14 am ]
Post subject:  Re: setting font size results in System.NullReferenceExcepti

Thanks for the info, I'll try that.

I used http://pdfsharp.net/wiki/HelloMigraDoc-sample.ashx
this as a reference. Which only use predefined (which I didn't know then) styles.

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