Stack empty Error when using iTextSharp for converting HTML to PDF

空扰寡人 提交于 2019-12-23 04:52:27

问题


I want to export in pdf my htmlcode

var document = new Document();
            string filePath = Server.MapPath("~/generatedBook");
            string exceptionPath = Server.MapPath("~/exceptions");
            PdfWriter pdfWriter = PdfWriter.GetInstance(document,
            new FileStream(filePath + "/Book"+_IdSes+".pdf", FileMode.Create));
            pdfWriter.SetFullCompression();
            pdfWriter.StrictImageSequence = true;
            pdfWriter.SetLinearPageMode();


            try
            {
                document.SetPageSize(PageSize.A4.Rotate());
                document.SetPageSize(new Rectangle(792f, 612f)); // 11" x 8.5"

//here it it gives me exception stack is empty

List<iTextSharp.text.IElement> htmlarraylist = 
 iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new 
                                         StringReader(htmlcode.ToString()), st);

//htmlcode is of type StringBuilder

foreach (IElement t in htmlarraylist)
                {
                    document.Add((IElement)t);
                }


                document.Close();

Full Exception

System.InvalidOperationException: Stack empty.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.Stack`1.Pop()
   at iTextSharp.text.html.simpleparser.HTMLWorker.EndElement(String tag)
   at iTextSharp.text.xml.simpleparser.SimpleXMLParser.ProcessTag(Boolean start)
   at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader)
   at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Parse(ISimpleXMLDocHandler doc,          
   ISimpleXMLDocHandlerComment comment, TextReader r, Boolean html)
   at iTextSharp.text.html.simpleparser.HTMLWorker.Parse(TextReader reader)

Any idea? thanks in advance


回答1:


HTMLWorker is deprecated and won't be maintained. it's replaced with xml-worker.



来源:https://stackoverflow.com/questions/11823539/stack-empty-error-when-using-itextsharp-for-converting-html-to-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!