ITextSharp v5.5.13.0 XMLWorker土耳其语字符问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ITextSharp v5.5.13.0 XMLWorker土耳其语字符问题相关的知识,希望对你有一定的参考价值。

我使用了iTextSharp,所有土耳其人都消失了。 html内联css属性也适用于表元素,但不适用于div元素。

我尝试了很多编码转换示例代码,但没有找到任何结果。

我的示例代码:

public static byte[] HtmlToPdfItextSharp(string HTMLCONTENTSTRING, List<string> cssFiles = null)
{
    using (var ms = new MemoryStream())
    {
        Document pdfDoc = new Document(PageSize.A4.Rotate(), 10, 10, 10, 10);

        BaseFont STF_Helvetica_Turkish = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, "CP1254", BaseFont.NOT_EMBEDDED);
        Font fontNormal = new Font(STF_Helvetica_Turkish, 12, Font.NORMAL, BaseColor.BLACK);


        string fontPath = Path.Combine(Path.Combine(Server.MapPath("~/App_Data/Pdf/arial.ttf")));
        XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontProvider.UseUnicode = true;
        fontProvider.Register(fontPath);
        CssAppliers ca = new CssAppliersImpl(fontProvider);

        var pdfWriter = PdfWriter.GetInstance(pdfDoc, ms);

        pdfDoc.Open();
        pdfWriter.DirectContent.SetFontAndSize(STF_Helvetica_Turkish, 12);
        pdfWriter.CloseStream = false;

        var htmlContext = new HtmlPipelineContext(null);

        htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

        ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
        cssFiles.ForEach(e => cssResolver.AddCssFile(e, true));

        var pp = new PdfWriterPipeline(pdfDoc, pdfWriter);
        IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, pp));

        XMLWorker worker = new XMLWorker(pipeline, true);
        XMLParser parser = new XMLParser(worker);


        parser.Parse(new MemoryStream(Encoding.UTF8.GetBytes(HTMLCONTENTSTRING)));

        pdfDoc.Close();

        return ms.GetBuffer();
    }
}
答案

我更新了我的代码并添加了样式表文件(使用字体系列:arial;)并且我解决了字符但是它需要很长时间我的新更新函数如:

public static byte[] HtmlToPdfItextSharp(string HTMLCONTENTSTRING, List<string> cssFiles = null)
{
    using (var ms= new MemoryStream())
    {
        Document pdfDoc = new Document(PageSize.A4.Rotate(), 10, 10, 7, 10);

        var pdfWriter = PdfWriter.GetInstance(pdfDoc, ms);
        pdfWriter.CloseStream = false;
        pdfDoc.Open();

        var htmlContext = new HtmlPipelineContext(null);

        htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

        ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
        cssFiles.ForEach(e => cssResolver.AddCssFile(e, true));

        var pp = new PdfWriterPipeline(pdfDoc, pdfWriter);
        IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, pp));

        XMLWorker worker = new XMLWorker(pipeline, true);
        XMLParser parser = new XMLParser(worker);
        parser.Parse(new MemoryStream(Encoding.UTF8.GetBytes(pHtmlIcerik)));

        pdfDoc.Close();
        return ms.ToArray();
    }
}

Css代码:

 body {
     font-family:Arial;
}
table{
    font-family:Arial;
}
td{
    font-family:Arial;
}

以上是关于ITextSharp v5.5.13.0 XMLWorker土耳其语字符问题的主要内容,如果未能解决你的问题,请参考以下文章

无法从程序集“itextsharp,版本=5.5.5.0,文化=中性,PublicKeyToken=8354ae6d2174ddca”加载类型“iTextSharp.text.html.HtmlPar

iTextSharp 设置文档横向(横向)A4

C#工具类:使用iTextSharp操作PDF文档

使用 iTextSharp 在系统中使用字体

c#中带有html的itextsharp [重复]

iTextSharp - 非常大的表内存泄漏