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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Aravind GovindarajPosted Nov 23, 2022, 7:54 PM
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
Garima BansalPosted Nov 23, 2022, 7:21 PM
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;
}
Garima BansalPosted Nov 23, 2022, 7:20 PM
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 BansalPosted Nov 23, 2022, 6:19 PM
Aravind Govindaraj
PdfWriter.GetInstance(doc, Response.OutputStream); // in lthis line getting error
Aravind GovindarajPosted Nov 23, 2022, 2:22 PM
may i know which line you are getting this error ?
Patricia HernandezPosted Nov 23, 2022, 1:25 PM
I think saw an article in W3schools explaining that but I am not sure where.