2012-07-18 53 views
1

我嘗試解析html文件並生成pdf。我使用代碼ITextSharp:用西里爾文/國際詞語解析html

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

如何定義基本字體,如果我想使用西裏爾字母/國際字?

+0

看看這篇文章的幫助http://stackoverflow.com/questions/10329863/display-unicode-characters-in-converting-html-to-pdf – 2012-07-20 13:45:24

回答

4

你應該註冊字體

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

和修飾的頁面的身體

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

對於別人,誰就能在俄語閱讀,這article能有用

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 }; 
+0

謝謝尤里。但我想使用XMLWorker&XMLParser。 – PlushEngineCell 2012-08-30 06:01:18