我正在使用itextsharp 5.3.5和xml worker从我的html页面生成PDF。在我的html页面中,我有一个富文本编辑器,它使用div标签和字体标签生成html。请参阅我的字段生成的以下html片段。
<html xml:space="preserve" xmlns="http://www.w3.org/1999/xhtml">
<div>
this text is not <strong>working</strong>
</div>
<div>
<font color="#ff0000">text in Red now</font>.
</div>
<div>
<font face="Tahoma" size="5">a font thoma and size 5.</font>
</div>
<div>
<font face="Tahoma" size="5">
<font face="Arial" size="3">here is a link to </font>
<a href="http://www.google.co.uk/">
<font face="Arial" size="3">Google</font>
</a>.
</font>
</div>
</html>
我发现如果我使用html worker,那么它会在pdf中完美地转换上面的html。但不是xml工作者。它只是没有用。
我还发现,如果我更改了一些标签并更新了我的html,那么xml worker会用pdf转换它。
<p>this text is not <strong>working</strong></p>
<span style="color:#ff0000">text in Red now</span>.
<br />
<span style="font-family:Tahoma; font-size:5">a font thoma and size 5.</span>
<br/>
<span style="font-family:Tahoma; font-size:5">
<span style="font-family:Arial; font-size:3">here is a link to </span><a href="http://www.google.co.uk/">
<span style="font-family:Arial; font-size:3">Google</span>
</a>.
</span>
<br />
现在很明显我无法在运行时更改html标签,这将是一个很大的混乱。我的代码如下。
StringReader html = new StringReader(htmlString);
var xmlWorkerFontProvider = new XMLWorkerFontProvider();
var cssAppliers = new CssAppliersImpl(xmlWorkerFontProvider);
CssFilesImpl cssFiles = new CssFilesImpl();
cssFiles.Add(XMLWorkerHelper.GetInstance().GetDefaultCSS());
StyleAttrCSSResolver cssRevolver = new StyleAttrCSSResolver();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
PdfWriterPipeline pdfWriterPipeline = new PdfWriterPipeline(document, writer);
IPipeline pipeline = new CssResolverPipeline(cssRevolver, new HtmlPipeline(htmlContext, pdfWriterPipeline));
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser xmlParser = new XMLParser(worker);
xmlParser.Parse(html);
如果您可以指导我,我将非常感激,我如何使用xml worker转换上面的pdf格式。
如果您需要任何进一步的信息,请告诉我。