Kinetic letters iTextSharp

I used http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3 to create pdf files from my razor views, and it works fine, but I can not display cyrillic letters, such like č, ć. I tried everything and I can not get it to work.

I have to somehow tell HtmlWorker to use a different font:

using (var htmlViewReader = new StringReader(htmlText)) { using (var htmlWorker = new HTMLWorker(pdfDocument)) { htmlWorker.Parse(htmlViewReader); } } 

You can help?

EDIT:

I was missing one line

 styleSheet.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H); 

The rest was the same as the answer.

+4
source share
1 answer

If you change the Render StandardPdfRenderer method to the following snippet, it should work:

 public byte[] Render(string htmlText, string pageTitle) { byte[] renderedBuffer; using (var outputMemoryStream = new MemoryStream()) { using (var pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin)) { string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF"); iTextSharp.text.FontFactory.Register(arialuniTff); PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream); pdfWriter.CloseStream = false; pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle }; pdfDocument.Open(); using (var htmlViewReader = new StringReader(htmlText)) { using (var htmlWorker = new HTMLWorker(pdfDocument)) { var styleSheet = new iTextSharp.text.html.simpleparser.StyleSheet(); styleSheet.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS"); styleSheet.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H); htmlWorker.SetStyleSheet(styleSheet); htmlWorker.Parse(htmlViewReader); } } } renderedBuffer = new byte[outputMemoryStream.Position]; outputMemoryStream.Position = 0; outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length); } return renderedBuffer; } 
+4
source

Source: https://habr.com/ru/post/1413733/


All Articles