ITextSharp:用西里尔/国际词语解析HTML

时间:2012-07-18 08:10:55

标签: c#-4.0 itextsharp cp1251

我尝试解析html文件并生成pdf。我用代码

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();

如何定义基本字体,如果我想使用西里尔/国际字词?

2 个答案:

答案 0 :(得分:4)

你应该注册字体

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

并修改了网页的正文

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

对于可以用俄语阅读的人来说,article可能很有用

答案 1 :(得分:1)

我提出以下变体

//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 };