B
B
Benchstyle2020-04-15 16:06:25
Unicode
Benchstyle, 2020-04-15 16:06:25

IText7 htmlpdf why Cyrillic is not displayed?

All health. There was a need to work with iText, converting a table from an html document to pdf. An example from the developers is taken as a basis, in general everything is ok, only the Cyrillic alphabet is stubbornly not displayed on the page. In html lang ="ru", charset="utf-8", in SetLang(new PdfString("ru-RU")) by default it was en-US, the description says language and locale, but it doesn't take off. I will be grateful for help.

using System.IO;
using iText.Html2pdf;
using iText.Html2pdf.Attach.Impl;
using iText.IO.Font;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout.Font;
using iText.Samples.Sandbox.Pdfhtml;

namespace iText.Samples.Sandbox.Pdfhtml
{
    public class CreateAccessiblePDF
    {
        public static readonly string SRC = @"C:\Templates\";
        public static readonly string DEST = @"C:\Templates\result.pdf";

        public static void Main(string[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            string htmlSource = SRC + "1.html";

            new CreateAccessiblePDF().ManipulatePdf(htmlSource, DEST);
        }

        public void ManipulatePdf(string src, string dest)
        {
            WriterProperties writerProperties = new WriterProperties();
            writerProperties.AddXmpMetadata();
            PdfWriter pdfWriter = new PdfWriter(dest, writerProperties);
            PdfDocument pdfDoc = new PdfDocument(pdfWriter);
            pdfDoc.GetCatalog().SetLang(new PdfString("ru-RU"));
            pdfDoc.SetTagged();
            pdfDoc.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
            ConverterProperties props = new ConverterProperties();
            FontProvider fontProvider = new FontProvider();
            fontProvider.AddStandardPdfFonts();
            fontProvider.AddDirectory(SRC);
            props.SetFontProvider(fontProvider);
            props.SetBaseUri(SRC);
            HtmlConverter.ConvertToPdf(new FileStream(src, FileMode.Open), pdfDoc, props);
            
            pdfDoc.Close();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Benchstyle, 2020-04-15
@Benchstyle

Instead of:
fontProvider.AddStandardPdfFonts();
Specified:

fontProvider.AddFont(FONT_FILENAME, PdfEncodings.IDENTITY_H);

Topic closed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question