3

When I am trying to generate PDF from HTML, some Turkish characters like ĞÜŞİÖÇ ğüşıöç are missing in PDF, I see a space in place of these characters but i want to print that character.

My code is:

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

How to print all Turkish characters on PDF?

Vinit Patel
  • 2,408
  • 5
  • 28
  • 53
  • possible duplicate of [iTextSharp 5 polish character](http://stackoverflow.com/questions/4902033/itextsharp-5-polish-character) – Chris Haas Jan 28 '14 at 13:37
  • possible duplicate of [itextSharp - htmlString to pdf Unicode are missing](http://stackoverflow.com/questions/21423993/itextsharp-htmlstring-to-pdf-unicode-are-missing) – Mitat Koyuncu Jul 28 '15 at 11:48

4 Answers4

7

I have finally find a solution for this problem, by this you can print all Turkish character.

    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));
Mitat Koyuncu
  • 928
  • 8
  • 20
Vinit Patel
  • 2,408
  • 5
  • 28
  • 53
3

Same my problem solved this code;

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();
       }
   } }
muratoner
  • 2,316
  • 3
  • 20
  • 32
1

I had the same prolem after a few days of reserach;

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

Eveytime you need to write a text having special characters, do it this way;

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

additionally, this may be needed for itextsharp to let font change;

using Font = iTextSharp.text.Font; 

it works like a charm :)

Monjen72
  • 11
  • 2
0

I had a similar problem and I couldn't get the CP1254 encoding to work but I found another solution which worked for me.

In the css just add "font-family: Arial;" and put it on the outer div tag.

.className{
   font-family: Arial;
}

<div class="className">
...
</div>

I found this answer here: How to generate a valid PDF/A file using iText and XMLWorker (HTML to PDF/A process)

It took a long time to find this solution but I found it searching for a font solution to display Turkish characters.