i am a beginner to implement itextsharp, as i try this static code but getting error "Object reference not set to an instance".
Loading
i am a beginner to implement itextsharp, as i try this static code but getting error "Object reference not set to an instance".
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Patricia Hernandez16 reputationPosted Nov 23, 2022, 8:25 AM EST
I think saw an article in W3schools explaining that but I am not sure where.
Aravind GovindarajRank #430 · 5.8K reputationPosted Nov 23, 2022, 9:22 AM EST
may i know which line you are getting this error ?
Garima BansalRank #1,555 · 1K reputationPosted Nov 23, 2022, 1:19 PM EST
Aravind Govindaraj
PdfWriter.GetInstance(doc, Response.OutputStream); // in lthis line getting error
Garima BansalRank #1,555 · 1K reputationPosted Nov 23, 2022, 2:20 PM EST
System.NullReferenceException' in itextsharp.dll Object reference not set to an instance of an object.
but i have already add itextsharp.dll reference,still getting error
PdfWriter PdfWriterr = PdfWriter.GetInstance(doc, ms); in this line
Garima BansalRank #1,555 · 1K reputationPosted Nov 23, 2022, 2:21 PM EST
protected void Unnamed1_Click(object sender, EventArgs e)
{
string HTMLContent = "
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + "Project Report.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(GetPDF(HTMLContent));
Response.End();
}
public byte[] GetPDF(string pHTML)
{
byte[] bPDF;
MemoryStream ms = new MemoryStream();
TextReader txtReader = new StringReader(pHTML);
// 1: create object of a itextsharp document class
Document doc = new Document(PageSize.A4, 25, 25, 25, 25);
// 2: we create a itextsharp pdfwriter that listens to the document and directs a XML-stream to a file
PdfWriter PdfWriterr = PdfWriter.GetInstance(doc, ms);
// 3: we create a worker parse the document
HTMLWorker htmlWorker = new HTMLWorker(doc);
// 4: we open document and start the worker on the document
doc.Open();
htmlWorker.StartDocument();
// 5: parse the html into the document
htmlWorker.Parse(txtReader);
// 6: close the document and the worker
htmlWorker.EndDocument();
htmlWorker.Close();
doc.Close();
bPDF = ms.ToArray();
return bPDF;
}
Aravind GovindarajRank #430 · 5.8K reputationPosted Nov 23, 2022, 2:54 PM EST
I assume, there may be some issue in the stream object. could you kindly check this link which may helps you to implement html to pdf
https://stackoverflow.com/questions/25164257/how-to-convert-html-to-pdf-using-itextsharp