ITextSharp: parse html with cyrillic/international words

萝らか妹 提交于 2020-01-06 02:17:08

问题


I try to parse html file and to generate pdf. I use code

document.Open();
HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(true);
IPipeline pipeline =
    new CssResolverPipeline(cssResolver,
        new HtmlPipeline(htmlContext,
                new PdfWriterPipeline(document, writer)));


XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser p = new XMLParser(true, worker, Encoding.Unicode);

p.Parse((TextReader)File.OpenText(@"Template.html"));
document.Close();

How can I define base font, If i'd like use cyrillic/international words?


回答1:


You should register font

string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
FontFactory.Register(arialuniTff);

and modifed page's body

<body face='Arial' encoding='koi8-r' >
...
</body >

For somebody, who can read in russian, this article can be useful




回答2:


I propose the following variant

//connect the font
            String FONT_LOCATION = Server.MapPath("~/fonts/arial.ttf");
            BaseFont baseFont = BaseFont.CreateFont(FONT_LOCATION, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, iTextSharp.text.Font.DEFAULTSIZE, iTextSharp.text.Font.NORMAL);
            //connected

PdfPCell cell1 = new PdfPCell(new Phrase(lblN, font)) { HorizontalAlignment = 1, VerticalAlignment= 1 };


来源:https://stackoverflow.com/questions/11536942/itextsharp-parse-html-with-cyrillic-international-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!