Click here to Skip to main content
15,888,195 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I would like to develop window application to generate the pdf file with the help of pdfsharp.dll file. While run the program i got an error like Object reference not set to an instance of an object
Here is my code
C#
 private void button1_Click(object sender, EventArgs e)
        {
            PdfDocument pdf = new PdfDocument();
            PdfPage pg = pdf.AddPage();
            XGraphics grp = XGraphics.FromPdfPage(pg);
            grp.MUH = PdfFontEncoding.Unicode;
            grp.MFEH = PdfFontEmbedding.Default;
            XFont ft = new XFont("Microsoft Sans Serif", 15, XFontStyle.Bold);
            grp.DrawString("Hazardous Situation or Incident Record", ft, XBrushes.LightGreen, new XRect(0, 0, pg.Width.Point, pg.Height.Point), XStringFormats.TopCenter);
            string pdffilename = "Hazardous situation(or)Incident Record";
            pdf.Save(pdffilename);
            Process.Start(pdffilename);
            this.Close();
            XPen pn = new XPen(XColors.Blue, 2);
            grp.DrawLine(pn, 2, 80, 612, 80);
}

I got an error at last line.
Posted
Updated 22-Dec-14 2:03am
v2
Comments
Kornfeld Eliyahu Peter 22-Dec-14 8:05am    
Why that this.close() is in the middle of nothing? Why it is there at all?
Divakard3 22-Dec-14 8:06am    
i would like close the windows form when click the generate pdf button.
Sinisa Hajnal 22-Dec-14 8:08am    
Call close AFTER all other methods are finished. That should probably be the last line.
BillWoodruff 22-Dec-14 8:07am    
Put a break-point on the first line of code in the Button Click EventHandler, then single-step through the code (F11): locate the source of the error, and then ask a question here including a description of the error, and where it occurred.
Maciej Los 22-Dec-14 8:14am    
My virtual 5!

it is because you are trying to draw a line after the form is closed. Fix that part and everything will go fine
 
Share this answer
 
First of all, read all comments to your question. Secondly, be careful with code context and do not use objects that without a context.

Try this:
C#
private void button1_Click(object sender, EventArgs e)
{
            PdfDocument pdf = new PdfDocument();
            PdfPage pg = pdf.AddPage();
            XGraphics grp = XGraphics.FromPdfPage(pg);
            grp.MUH = PdfFontEncoding.Unicode;
            grp.MFEH = PdfFontEmbedding.Default;
            XFont ft = new XFont("Microsoft Sans Serif", 15, XFontStyle.Bold);
            grp.DrawString("Hazardous Situation or Incident Record", ft, XBrushes.LightGreen, new XRect(0, 0, pg.Width.Point, pg.Height.Point), XStringFormats.TopCenter);
            string pdffilename = "Hazardous situation(or)Incident Record";
            XPen pn = new XPen(XColors.Blue, 2);
            grp.DrawLine(pn, 2, 80, 612, 80);
            pdf.Save(pdffilename);
            Process.Start(pdffilename);
            //this.Close();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900