您的位置:首页 > Web前端 > HTML

C#关于iTextSharp将html转换为pdf不支持中文问题

2015-11-20 18:00 441 查看
使用iTextSharp的XMLWorker来解析带html标签的文字
修改iTextSharp\iTextSharp\textFontFactoryImp.cs中的public virtual int RegisterDirectories()方法使其扫描自己所需要的中文字体

            {

                int count = 0;

                string PATH = "自己字体所在的路径";

                count += RegisterDirectory(PATH);

                return count;

            }

由于C:\Windows\Fonts字体文件很多 扫描需要时间就会长  只把自己需要的拿出来用就可以了

代码

                FontFactory.RegisterDirectories();

                Document document = new Document();

                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath(NewPdfPath), FileMode.Create));

                document.Open();

                TextReader stringReader = new StringReader(htmlText.ToString());

                XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, stringReader);

                document.Close();

            }

其中htmlText是一串有HTML标签的代码的字符串

更多疑问就看看(iText in Action, 2nd Edition)英文文字版  

一些简单的使用可以看看  (XMLWorker
Documentation)
http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: