Html to pdf some characters are missing (itextsharp)

孤人 提交于 2019-11-27 23:07:07
slayer35

Finaly I think I found the solution,I changed itextsharp source code a little in order to show turkish characters.(turkish character code is cp1254)

I add "public const string CP1254 = "Cp1254";" to [BaseFont.cs] in the source code.

After that I modify the [FactoryProperties.cs].I changed like this;

public Font GetFont(ChainedProperties props)
{
I don't write the whole code.I changed only code below;
------------Default itextsharp code------------------------------------------------------
  if (encoding == null)
                encoding = BaseFont.WINANSI;
            return fontImp.GetFont(face, encoding, true, size, style, color);
-------------modified code--------------------------------------------

            encoding = BaseFont.CP1254;
            return fontImp.GetFont("C:\\WINDOWS\\Fonts\\arial.ttf", encoding, true, size, style, color);
}

.After I compile new dll ,and missing characters are shown.

Murat

No need to change the source code.

Try this:

iTextSharp.text.pdf.BaseFont STF_Helvetica_Turkish = iTextSharp.text.pdf.BaseFont.CreateFont("Helvetica","Cp1254", iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);    

iTextSharp.text.Font fontNormal = new iTextSharp.text.Font(STF_Helvetica_Turkish, 12, iTextSharp.text.Font.NORMAL);

thank you very much all who posted the samples..

i use the below solution from codeproject , and there was the turkish char set problems due to font..

If you use htmlworker you should register font and pass to htmlworker

http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3

      StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
                styles.LoadTagStyle("h3", "size", "5");
                styles.LoadTagStyle("td", "size", ".6");
                FontFactory.Register("c:\\windows\\fonts\\arial.ttf", "Garamond");   // just give a path of arial.ttf 
                styles.LoadTagStyle("body", "face", "Garamond");
                styles.LoadTagStyle("body", "encoding", "Identity-H");
                styles.LoadTagStyle("body", "size", "12pt");
                using (var htmlViewReader = new StringReader(htmlText))
                {
                    using (var htmlWorker = new HTMLWorker(pdfDocument, null, styles))
                    {
                        htmlWorker.Parse(htmlViewReader);
                    }
                }

I am not familiar with the iTextSharp library; however, you seem to be converting the output of your gridview component to a string and reading from that string to construct your PDF document. You also have a strange conversion from UTF-8 to UTF-8 going on.

From what I can see (given that your GridView is outputting characters correctly) if you are outputting the characters to a string they would be represented as UTF-16 in memory. You probably need to pass this string directly into the PDF library (like how you pass the raw UTF-16 .NET string "İııŞŞşşĞĞğğ" as it is).

dungnguyen

You can use:

iTextSharp.text.pdf.BaseFont Vn_Helvetica = iTextSharp.text.pdf.BaseFont.CreateFont(@"C:\Windows\Fonts\arial.ttf", "Identity-H", iTextSharp.text.pdf.BaseFont.EMBEDDED);
iTextSharp.text.Font fontNormal = new iTextSharp.text.Font(Vn_Helvetica, 12, iTextSharp.text.Font.NORMAL);

For Turkish encoding

CultureInfo ci = new CultureInfo("tr-TR");
Encoding enc = Encoding.GetEncoding(ci.TextInfo.ANSICodePage);

If you're outputting HTML, try different DOCTYPE tags at the top of the page.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Note if using HTML you may need to HTMLEncode the characters.

Server.HTMLEncode()

HttpServerUtility.HtmlEncode()

xoraxbx
BaseFont bF = BaseFont.CreateFont("c:\\arial.ttf","windows-1254",true);
Font f = new Font(bF,12f,Font.NORMAL);
Chunk c = new Chunk();
c.Font = f;
c.Append("Turkish characters: ĞÜŞİÖÇ ğüşıöç");
document.Add(c);

In the first line, you may write these instead of "windows-1254". All works:

  • Cp1254
  • iso-8859-9
  • windows-1254

Don't change the source code of the iTextSharp. Define a new style:

        var styles = new StyleSheet();
        styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONTFAMILY, "tahoma");
        styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, "Identity-H");

and then pass it to the HTMLWorker.ParseToList method.

i have finally find a soultution 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));
Matt Stuvysant

I strongly suggest not to change itextsharp source code in order to solve this problem. Have a look at my other comment on the subject: https://stackoverflow.com/a/24587745/1138663

Fatih Çengel

I solved the problem. I can provide my the other solution type...

try
{
        BaseFont bf = BaseFont.CreateFont("c:\\windows\\fonts\\calibrib.ttf",
            BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        Document document = new Document(PageSize.A4, 25, 25, 30, 30);
        PdfWriter writer = PdfWriter.GetInstance(document, fs);

        Font f = new Font(bf, 12f, Font.NORMAL);
        // Open the document to enable you to write to the document
        document.Open();
        // Add a simple and wellknown phrase to the document
        for (int x = 0; x != 100; x++)
        {
            document.Add(new Paragraph("Paragraph - This is a test! ÇçĞğİıÖöŞşÜü",f));
        }

        // Close the document
        document.Close();          
}
catch(Exception)
{

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!