简体   繁体   中英

iTextSharp System.NullReferenceException

I am trying to make a method to get the PDF bytes:

public static Byte[] HtmlToBytes(string htmlText)
    {
        Byte[] bytes;

        using (var ms = new MemoryStream())
        {
            using (var doc = new Document(PageSize.A4, 10, 10, 10, 10))
            {
                using (var writer = PdfWriter.GetInstance(doc, ms))
                {
                    writer.CloseStream = false;
                    doc.Open();
                    using (var msHtml = new MemoryStream(Encoding.UTF8.GetBytes(htmlText)))
                    {
                        XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, Encoding.UTF8);
                    }
                }
            }
            bytes = ms.ToArray();
        }

        return bytes;
    }

But it gives out a NullReferenceException at this part

XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, Encoding.UTF8);

On the string I am passing there is only plain html like a table and a tag.

Here`s the Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
iTextSharp.tool.xml.pipeline.html.HtmlPipeline.Close(IWorkerContext context, Tag t, ProcessObject po) +76
iTextSharp.tool.xml.XMLWorker.EndElement(String tag, String ns) +186
iTextSharp.tool.xml.parser.XMLParser.EndElement() +111
iTextSharp.tool.xml.parser.state.ClosingTagState.Process(Char character) +61
iTextSharp.tool.xml.parser.XMLParser.ParseWithReader(TextReader reader) +247
iTextSharp.tool.xml.parser.XMLParser.Parse(TextReader reader) +5
iTextSharp.tool.xml.XMLWorkerHelper.ParseXHtml(PdfWriter writer, Document doc, TextReader inp) +453
TCC.Globals.HtmlToBytes(String htmlText) in C:\Users\Felipe\Source\Workspaces\Workspace\SgLeitos\TCC\TCC\Helpers\Globals.cs:118
TCC.Controllers.RelatoriosController.Leitos(Nullable`1 id) in C:\Users\Felipe\Source\Workspaces\Workspace\SgLeitos\TCC\TCC\Controllers\RelatoriosController.cs:34
lambda_method(Closure , ControllerBase , Object[] ) +107
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157 ...

I have tried as fallows:

public static Byte[] HtmlToBytes(string htmlText)
    {
        Byte[] bytes;

        using (var ms = new MemoryStream())
        {
            using (var doc = new Document(PageSize.A4, 10, 10, 10, 10))
            {
                using (var writer = PdfWriter.GetInstance(doc, ms))
                {
                    writer.CloseStream = false;
                    doc.Open();
                    using (var msHtml = new MemoryStream(Encoding.UTF8.GetBytes(htmlText)))
                    {
                        XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, Encoding.UTF8);
                    }
                }
            }
            bytes = ms.ToArray();
        }

        return bytes;
    }

    private static void Main(string[] args)
    {
        var str = @"<!DOCTYPE html><html lang=""en"" xmlns=""http://www.w3.org/1999/xhtml""><head><meta charset=""utf-8"" /><title></title></head><body><table border=""1"" style=""width:100%""><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr></table></body></html>";
        var s = HtmlToBytes(str);
        var str2 = @"<table border=""1"" style=""width:100%""><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr></table>";
        s = HtmlToBytes(str2);
        var str3 = @"<tabl=""width:100%""><tr><td>Jill</td><td>Smith</td><td>50</td></tr><tr><td>Eve</td><td>Jackson</td><td>94</td></tr></table>";
        s = HtmlToBytes(str3); //NULL HERE with corrupted html
    }

so the possible answere is, your HTML is corrupted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM