XMLWorker:找到无效的嵌套标签 div,应为结束标签 img
XMLWorker: Invalid nested tag div found, expected closing tag img
我只是想使用 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)™" +
"</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标签,所以我很好奇我这里是不是做错了什么?
据我所知img标签是这样的< img src="" />
但是你是这样写的< img src="" >< img>
您的图片标签未正确关闭。要正确执行此操作,您需要将 <img src='...'><img>
更改为 <img src='...'/>
或 <img src='...'></img>
.
见How to close tag properly answer by M. Rabe
我只是想使用 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)™" +
"</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标签,所以我很好奇我这里是不是做错了什么?
据我所知img标签是这样的< img src="" />
但是你是这样写的< img src="" >< img>
您的图片标签未正确关闭。要正确执行此操作,您需要将 <img src='...'><img>
更改为 <img src='...'/>
或 <img src='...'></img>
.
见How to close tag properly answer by M. Rabe