itextSharp 不应用 html 样式属性
itextSharp not apply html style attributes
我正在使用 itextsharp 从 html 创建一个 pdf 文件。它工作得很好,但我在 HTML 中的样式属性有问题。要解析 html 我正在使用此代码。
cssResolver.AddCssFile(AppDomain.CurrentDomain.BaseDirectory+"/Content/css/style.css",true);
IPipeline pipeline =
new CssResolverPipeline(cssResolver,
new HtmlPipeline(htmlContext,
new PdfWriterPipeline(doc, writer)));
var worker = new XMLWorker(pipeline, true);
var p = new XMLParser(true, worker, Encoding.Unicode);
using (var sr = new StringReader(html))
{
p.Parse(sr);
}
所以它应用了我的 css 样式并且它们有效,但是我的 html 文件中有一些样式,我无法将它移动到 css 文件,因为它们是动态形成的.
<div id="VC" class="vc-scene-canvas-div" style="left: 15px; bottom: 15px; height: 517px; width: 244px;">
<img src="">
<img src="" style="left: 32px; bottom: 0px; height: 459px; width: 181px; z-index: 10;">
<img src="" style="left: 14px; bottom: 0px; height: 477px; width: 217px; z-index: 20;">
</div>
当我形成我的 pdf 时,我看到除 images.But 之外的所有内容,当我删除样式标签时一切正常。
是否可以同时在 css 和 html 中使用样式?也许我做错了什么?
你的问题是你使用的CSS属性,XMLWorker
不支持left,bottom, z-index, 仅部分 height
您可以从此 list
检查每个 CSS 属性的支持情况
我正在使用 itextsharp 从 html 创建一个 pdf 文件。它工作得很好,但我在 HTML 中的样式属性有问题。要解析 html 我正在使用此代码。
cssResolver.AddCssFile(AppDomain.CurrentDomain.BaseDirectory+"/Content/css/style.css",true);
IPipeline pipeline =
new CssResolverPipeline(cssResolver,
new HtmlPipeline(htmlContext,
new PdfWriterPipeline(doc, writer)));
var worker = new XMLWorker(pipeline, true);
var p = new XMLParser(true, worker, Encoding.Unicode);
using (var sr = new StringReader(html))
{
p.Parse(sr);
}
所以它应用了我的 css 样式并且它们有效,但是我的 html 文件中有一些样式,我无法将它移动到 css 文件,因为它们是动态形成的.
<div id="VC" class="vc-scene-canvas-div" style="left: 15px; bottom: 15px; height: 517px; width: 244px;">
<img src="">
<img src="" style="left: 32px; bottom: 0px; height: 459px; width: 181px; z-index: 10;">
<img src="" style="left: 14px; bottom: 0px; height: 477px; width: 217px; z-index: 20;">
</div>
当我形成我的 pdf 时,我看到除 images.But 之外的所有内容,当我删除样式标签时一切正常。 是否可以同时在 css 和 html 中使用样式?也许我做错了什么?
你的问题是你使用的CSS属性,XMLWorker
不支持left,bottom, z-index, 仅部分 height
您可以从此 list
检查每个 CSS 属性的支持情况