2014-01-28 54 views
0

当我尝试从HTML生成PDF时,一些土耳其字符如ĞÜŞİÖÇğüşıöç在PDF中缺失,我看到一个空格代替这些字符,但我想打印该字符。itextSharp - html to pdf一些土耳其字符丢失

我的代码是:

public virtual void print pdf(string html, int id) 
{ 
    String htmlText = html.ToString(); 
    Document document = new Document(); 
    string filePath = HostingEnvironment.MapPath("~/Content/Pdf/"); 
    PdfWriter.GetInstance(document, new FileStream(filePath + "\\pdf-"+id+".pdf", FileMode.Create)); 
    document.Open(); 
    iTextSharp.text.html.simpleparser.HTMLWorker hw = 
        new iTextSharp.text.html.simpleparser.HTMLWorker(document); 

    hw.Parse(new StringReader(htmlText)); 
    document.Close(); 
} 

如何打印PDF所有土耳其字符?

+0

[iTextSharp的5抛光字符]的可能重复(http://stackoverflow.com/questions/4902033/itextsharp-5-polish-character) –

+0

可能重复[itextSharp-htmlString to pdf Unicode are missing](http://stackoverflow.com/questions/21423993/itextsharp-htmlstring-to-pdf-unicode-are-missing) –

回答

4

我终于找到了解决这个问题的办法,通过这个你可以打印所有的土耳其字符。

String htmlText = html.ToString(); 
    Document document = new Document(); 
    string filePath = HostingEnvironment.MapPath("~/Content/Pdf/"); 
    PdfWriter.GetInstance(document, new FileStream(filePath + "\\pdf-"+Name+".pdf", FileMode.Create)); 
    document.Open(); 

    iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document); 
    FontFactory.Register(Path.Combine(_webHelper.MapPath("~/App_Data/Pdf/arial.ttf")), "Garamond"); // just give a path of arial.ttf 
    StyleSheet css = new StyleSheet(); 
    css.LoadTagStyle("body", "face", "Garamond"); 
    css.LoadTagStyle("body", "encoding", "Identity-H"); 
    css.LoadTagStyle("body", "size", "12pt"); 

    hw.SetStyleSheet(css); 

    hw.Parse(new StringReader(htmlText)); 
2

同样我的问题解决了这段代码;

var pathUpload = Server.MapPath($"~/Test.pdf"); 
using (var fs = System.IO.File.Create(pathUpload)) 
{ 
    using (var doc = new Document(PageSize.A4, 0f, 0f, 10f, 10f)) 
    { 
     using (var writer = PdfWriter.GetInstance(doc, fs)) 
     { 
      doc.Open(); 
      BaseFont baseFont = BaseFont.CreateFont("C:\\Windows\\Fonts\\Arial.ttf", "windows-1254", true); 
      Font fontNormal = new Font(baseFont, 24, Font.NORMAL); 

      var p = new Paragraph("Test paragrapgh İÇşıĞğŞçöÖ", fontNormal); 
      doc.Add(p); 
      doc.Close(); 
     } 
    } } 
1

经过几天的研究,我有同样的问题;

BaseFont myFont = BaseFont.CreateFont(@"C:\windows\fonts\arial.ttf", "windows-1254", BaseFont.EMBEDDED); 
Font fontNormal = new Font(myFont); 

Eveytime你需要写一个有特殊字符的文本,这样做;

doc.Add(new Paragraph("İıĞğŞşÜüÖöŞşÇç", fontNormal));  // a new paragraph 
results.Add(new ListItem("İıĞğŞşÜüÖöŞşÇç", fontNormal)); // a new list item 

另外,这可能是itextsharp需要让字体更改;

using Font = iTextSharp.text.Font; 

它就像一个魅力:)