2011-12-23 2 views
4

В следующем коде китайский шрифт (содержащий текст html) не отображается в формате pdf. Я также пробовал стили и шрифты в этом методе. Помогите решить эту проблему. Спасибо заранее всем.Как сделать китайский шрифт в pdf с помощью iTextSharp?

public static bool GeneratedPDF(string strHTMLText, string filename, string action, string rpttype) 
    { 
     bool blnReturn = false; 

     string fontpath = HttpContext.Current.Server.MapPath("~/files/fonts/"); 
     string filepath = HttpContext.Current.Server.MapPath("~/files/pdf/"); 

     BaseFont customfont = BaseFont.CreateFont(fontpath + "simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 

     Font font = new Font(customfont, 12); 


     //List<iTextSharp.text.IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new System.IO.StringReader(strHTMLText), null); 


     iTextSharp.text.Document document = new iTextSharp.text.Document(); 

     if (rpttype.Trim().ToUpper() == "REPORT") 
      document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A2.Rotate()); 

     else if (rpttype.Trim().ToUpper() == "GRID") 
      document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4.Rotate()); 

     else 
      document = new iTextSharp.text.Document(iTextSharp.text.PageSize.LETTER); 

     iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(filepath + "\\" + filename, FileMode.Create)); 
     document.Open(); 

     iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet(); 

     styles.LoadTagStyle("body", "font-family", "verdana"); 

     styles.LoadStyle("body", "font-size", "5px"); 


     List<iTextSharp.text.IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new System.IO.StringReader(strHTMLText), styles); 
     for (int k = 0; k < htmlarraylist.Count; k++) 
     { 

      document.Add((IElement)htmlarraylist[k]); 

     } 

     iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph(); 

     p.InsertRange(0, htmlarraylist); 

     p.Font = font; // font does not work 
     document.Add(p); 

     document.Close(); 

     blnReturn = true; 

     return blnReturn; 
    } 

ответ

6

Вы установили StyleSheet неправильно. Это должно быть что-то вроде этого:

string html = @" 
<html><body>  
<p>使用iText做PDF文件輸出</p>  
<p>使用iText做PDF文件輸出</p>  
<p>使用iText做PDF文件輸出</p>  
<p>使用iText做PDF文件輸出</p>  
<p>使用iText做PDF文件輸出</p> 
</body></html> 
"; 
FontFactory.Register("c:/windows/fonts/ARIALUNI.TTF"); 
StyleSheet style = new StyleSheet(); 
style.LoadTagStyle("body", "face", "Arial Unicode MS"); 
style.LoadTagStyle("body", "encoding", BaseFont.IDENTITY_H); 
using (Document document = new Document()) { 
    PdfWriter writer = PdfWriter.GetInstance(
    document, Response.OutputStream 
); 
    document.Open(); 
    foreach(IElement element in HTMLWorker.ParseToList(
     new StringReader(html.ToString()), style)) 
    { 
    document.Add(element); 
    } 
} 

Вы должны изменить третий параметр LoadTagStyle() выше правильное название конкретного шрифта, который вы используете. Другими словами, замените «Arial Unicode MS» выше именем/значением шрифта. Или вы также можете использовать шрифт.

+0

Большое спасибо, mr.kuuijinbo. – cathrine

Смежные вопросы