itextsharp汉字未显示在PDF中

时间:2018-09-02 12:46:16

标签: c# asp.net itext

我需要编码方面的帮助。我正在尝试将带有中文字符的HTML转换为PDF。我设法进行转换,但是我的汉字消失了

这是我的HTML文件,我将其转换为字符串,并已在td中使用Arial Unicode MS设置了字体

string HTMLTemplate = "<table border=0 cellspacing='0' cellpadding='3'><tr><td style='width:100%;font: 10px/1.5em Verdana, Arial Unicode MS, Helvetica, sans-serif;'>GIGI无合约 F&B‎</td></tr></table>"

这是我的代码

XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.Register("C:\\fonts\\ARIALUNI.TTF");

CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);

iTextSharp.text.Document doc = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER, 7, 7, 7, 7);

iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(FilePath, FileMode.Create));

iTextSharp.tool.xml.pipeline.html.HtmlPipelineContext htmlContext = new iTextSharp.tool.xml.pipeline.html.HtmlPipelineContext(cssAppliers);

htmlContext.SetTagFactory(iTextSharp.tool.xml.html.Tags.GetHtmlTagProcessorFactory());

//create a cssresolver to apply css
iTextSharp.tool.xml.pipeline.css.ICSSResolver cssResolver = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);

//Create and attach pipline, without pipline parser will not work on css
iTextSharp.tool.xml.IPipeline pipeline = new iTextSharp.tool.xml.pipeline.css.CssResolverPipeline(cssResolver, new iTextSharp.tool.xml.pipeline.html.HtmlPipeline(htmlContext, new iTextSharp.tool.xml.pipeline.end.PdfWriterPipeline(doc, writer)));

//Create XMLWorker and attach a parser to it
iTextSharp.tool.xml.XMLWorker worker = new iTextSharp.tool.xml.XMLWorker(pipeline, true);
iTextSharp.tool.xml.parser.XMLParser xmlParser = new iTextSharp.tool.xml.parser.XMLParser(worker);


//All is well open documnet and start writing.
doc.Open();

xmlParser.Parse(new StringReader(AP_TEMPLATE_HTML));
doc.NewPage();

doc.Close();

0 个答案:

没有答案