K
K
Karim Gataullin2017-09-14 11:59:57
ASP.NET
Karim Gataullin, 2017-09-14 11:59:57

Russian characters when generating pdf iTextSharp?

There is an asp.net mvc project, one function of which is to generate a PDF file with data from the database. The iTextSharp 5.5 nuget package was installed to resolve this issue. When coding, it became clear that the generator does not show the Cyrillic alphabet? After studying 1-2 Google pages for a solution to this problem, it became clear that the code like

BaseFont baseFont = BaseFont.CreateFont("c:/Windows/Fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
var font = new iTextSharp.text.Font(baseFont, 14, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

displays some krakozyabry and in the end does not solve the problem. How can it be solved?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Zhidkov, 2017-09-14
@bishounen01

using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
    Document doc = new Document(PageSize.A4, 30f, 30f, 30f, 30f);
    PdfWriter writer = PdfWriter.GetInstance(doc, ms);
    doc.Open();

    BaseFont baseFont = BaseFont.CreateFont("C:\\Windows\\Fonts\\arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

     //Заголовок 
     PdfContentByte cb = writer.DirectContent;
     cb.Rectangle(10f, 10f, doc.PageSize.Width - 20f, doc.PageSize.Height - 20f);
     cb.SetFontAndSize(baseFont, 13);
     cb.BeginText();
     cb.ShowTextAligned(
     PdfContentByte.ALIGN_CENTER, "Отчёт", 300f,  doc.PageSize.Height - 30f, 0);

     cb.EndText();
     cb.Stroke();
     doc.Close();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question