0
var paper =  _repo.VeryLatestPaper().Result;
List list = new List(List.ORDERED);
paper.Questions.ForEach(q => list.Add(q.Message));

var doc1 = new Document(); 

string path = "B:\\Test\\PDF";
PdfWriter writer = PdfWriter.GetInstance(doc1, new FileStream(path + "/Doc1.pdf", FileMode.Create));
writer.PageEvent = new PDFWriterEvents("This is a Test");


doc1.Open();

//new XMLParser().Parse(new StringReader(text));

//XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc1, new StringReader(text));

doc1.Add(list);

doc1.Close();

I am using the above code with the latest itextsharp to generate a pdf list. So far it's working very excellently, until when I want to convert each itextsharp list from html to at least plain text, or preferably formatted text with images if possible. Someone please assist me convert q.Message to pdf of plain text ready for rendering to pdf file with XMLWorker and this code here .

//new XMLParser().Parse(new StringReader(text));

//XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc1, new StringReader(text));

Please note that I am not getting the html from a file, it's from the database…

David Tansey
  • 5,813
  • 4
  • 35
  • 51
kabue7
  • 59
  • 2
  • 10
  • 1
    You can use other kinds of streams than just filestreams. You can load the HTML in memory an pass a MemoryStream, for instance, or maybe you can get a stream representation from the database field directly. [Related question about using `PdfWriter.GetInstance` with a memory stream](http://stackoverflow.com/questions/2815761/create-pdf-in-memory-instead-of-physical-file). – GolezTrol May 29 '15 at 22:35
  • hi Golez, let me heck it out... – kabue7 May 29 '15 at 22:57

1 Answers1

-1

It has ability to convert HTML file in to pdf.

It has already answered here. I am making some chages here.

Required namespace for conversions are:

using iTextSharp.text;
using iTextSharp.text.pdf;

and for conversion and download file :

//Create a byte array that will eventually hold our final PDF
            Byte[] bytes;

            //Boilerplate iTextSharp setup here
            //Create a stream that we can write to, in this case a MemoryStream
            using (var ms = new MemoryStream())
            {

                //Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
                using (var doc = new Document())
                {

                    //Create a writer that's bound to our PDF abstraction and our stream
                    using (var writer = PdfWriter.GetInstance(doc, ms))

                    {

                        //Open the document for writing
                        doc.Open();


                        string finalHtml = string.Empty;
                       // read your html by database or from a html file here and store it into finalHtml e.g. a string



                        //XMLWorker also reads from a TextReader and not directly from a string
                        using (var srHtml = new StringReader(finalHtml))
                        {

                            //Parse the HTML
                            iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
                        }



                        doc.Close();
                    }
                }

                //After all of the PDF "stuff" above is done and closed but **before** we
                //close the MemoryStream, grab all of the active bytes from the stream
                bytes = ms.ToArray();
            }





        //clear the response
            Response.Clear();
            MemoryStream mstream = new MemoryStream(bytes);
        //define response content type
            Response.ContentType = "application/pdf";
        //give the name of file of pdf and add in to header
            Response.AddHeader("content-disposition", "attachment;filename=invoice.pdf");
            Response.Buffer = true;
            mstream.WriteTo(Response.OutputStream);
            Response.End();

Try to put all css inline

Community
  • 1
  • 1
Nitin Singh
  • 160
  • 1
  • 8
  • Since you didn't make any changes to the code except removing a little bit this would probably be better as a comment that points to the [original answer that this was copied from](http://stackoverflow.com/a/25164258/231316) – Chris Haas Jun 01 '15 at 21:16
  • Ok @Chris Hass, will take care of that things now onwards. ☺. I had only added that response header as a pdf. – Nitin Singh Jun 02 '15 at 00:27