PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 8:18 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Jul 23, 2020 1:16 pm 
Offline

Joined: Thu Jul 23, 2020 1:01 pm
Posts: 2
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();
        }
    }
}


Top
 Profile  
Reply with quote  
PostPosted: Thu Jul 23, 2020 2:55 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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

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


Top
 Profile  
Reply with quote  
PostPosted: Fri Jul 24, 2020 8:14 am 
Offline

Joined: Thu Jul 23, 2020 1:01 pm
Posts: 2
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.


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

All times are UTC


Who is online

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