XMLWorker:找到无效的嵌套标记 div,预期的结束标记 img

XMLWorker: Invalid nested tag div found, expected closing tag img

提问人:NeoSketo 提问时间:6/1/2016 最后编辑:NeoSketo 更新时间:10/9/2018 访问量:22957

问:

我只是想使用 XMLWorker 将图像添加到我的 pdf 文档中。但出于某种原因,它想要一个关闭的 img 标签。但即使我放了一个,它仍然失败了。

C# 代码

        Byte[] bytes;
        using (var ms = new MemoryStream())
        {
            using (var doc = new Document(PageSize.A4.Rotate()))
            {
                using (var writer = PdfWriter.GetInstance(doc, ms))
                {

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

                    //Our HTML and CSS

                    string exampleHtml = @"<div style='position: relative; width: 100%'>" +

                                                "<img src='/Content/images/certificate.jpg'><img>" +

                                                "<div style='position: absolute; top: 100px; left: 100px; width: 800px; height: 550px; text-align: center;'>" +
                                                "<h1>" +
                                                "<img src='/Content/images/CertTitle.png' alt='CERTIFICATE PDF' style='width: 800px;'/>" +
                                                "</h1>" +


                                                "<p>" +
                                                "The School certifies that<br/>" +

                                                "<h2>FIRSTNAME LAST NAME TITLE</h2>" +

                                                "has participated in the class titled<br />" +

                                                "<h2>COURSCODE - COURSENAME</h2>" +


                                                "This activity was designated for # Credit(s)&trade;" +

                                                "</p>" +




                                                "<div style='float: left;  position: absolute; bottom: 50px;'>" +
                                                "<i>Date issued: DATE<br/></i><img src='Content/images/ceo-signature.jpg' style='border-bottom: solid #000 1px;'>" +
                                                "</div>" +
                                                "</div>" +

                                                "</div>";

                    using (var srHtml = new StringReader(exampleHtml))
                    {

                        //Parse the HTML
                        iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml); //right here is where it crashes ({"Invalid nested tag div found, expected closing tag img."} {"The document has no pages."}
                    }


                    doc.Close();
                }
            }

            bytes = ms.ToArray();
        }

        var testFile = HttpContext.Server.MapPath("~/Content/documents/UserCertificates/test.pdf"); ;
        System.IO.File.WriteAllBytes(testFile, bytes);

        Response.AddHeader("Content-Disposition", "inline; filename=test.pdf");
        return File(testFile, "application/pdf");

我从来没有使用过关闭img标签,所以我很好奇我在这里是否做错了什么?

C# XHTML PDF 生成 XmlWriter

评论

1赞 Kevin Brown 6/1/2016
XMLWorker 与 XML 一起使用。虽然 HTML 支持未闭合的标签(如“img”标签),但这不是有效的 XML。你必须确保使用方法来确保你的“HTML”是有效的。你的 “br” 标签没有什么不同,虽然它可能在 HTML 中作为 <br> 工作,但它会因无效的 XML 而中断。

答:

1赞 user5373973 6/1/2016 #1

据我所知,img标签是这样的< img src="" />

但你是这样写的< img src="" >< img>

1赞 Blubberguy22 6/1/2016 #2

您的图像标签未正确关闭。要正确执行此操作,您需要更改为 或 。<img src='...'><img><img src='...'/><img src='...'></img>

请参阅如何正确关闭标签 M. Rabe 的答案

评论

0赞 Deshani Tharaka 4/22/2021
<img src='...'></img>这对我不起作用