【问题标题】:itextSharp - html to pdf some turkish characters are missingitextSharp - html 到 pdf 缺少一些土耳其语字符
【发布时间】:2014-02-19 06:19:56
【问题描述】:

当我尝试从 HTML 生成 PDF 时,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 上打印所有土耳其语字符?

【问题讨论】:

标签: itextsharp


【解决方案1】:

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

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

    【讨论】:

      【解决方案3】:

      经过几天的研究,我遇到了同样的问题;

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

      每当你需要写一段带有特殊字符的文字时,就这样做吧;

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

      另外,itextsharp 可能需要它来改变字体;

      using Font = iTextSharp.text.Font; 
      

      它就像一个魅力:)

      【讨论】:

        【解决方案4】:

        我遇到了类似的问题,我无法让 CP1254 编码工作,但我找到了另一个适合我的解决方案。

        在 css 中添加“font-family: Arial;”并将其放在外部 div 标签上。

        .className{
           font-family: Arial;
        }
        
        <div class="className">
        ...
        </div>
        

        我在这里找到了这个答案:How to generate a valid PDF/A file using iText and XMLWorker (HTML to PDF/A process)

        花了很长时间才找到这个解决方案,但我发现它正在寻找显示土耳其字符的字体解决方案。

        【讨论】:

          猜你喜欢
          • 2010-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-18
          • 2015-01-07
          • 1970-01-01
          • 2018-08-03
          相关资源
          最近更新 更多