简体   繁体   中英

Chinese characters not displayed using iTextSharp

I've been using itextsharp to generate pdf from my database data. My database data contains the text, the x and y coordinate and the page number. Here's how I do it.

 string oldFile = Server.MapPath(string.Format("~/App_Data/Documents/Templates/{0}", documentModel.Filename));

        // open the reader
        PdfReader reader = new PdfReader(oldFile);
        Rectangle size = reader.GetPageSizeWithRotation(1);
        iTextSharp.text.Document document = new iTextSharp.text.Document(size);

        PdfWriter writer = PdfWriter.GetInstance(document, _memoryStream);
        writer.CloseStream = false;
        document.Open();

        PdfContentByte cb;
        PdfImportedPage page;
        BaseFont bf;


foreach (var pageNumber in pages)
        {
            document.NewPage();

            cb = writer.DirectContent;
            // create the new page and add it to the pdf
            page = writer.GetImportedPage(reader, pageNumber);
            cb.AddTemplate(page, 0, 0);
            bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            cb.SetColorFill(BaseColor.DARK_GRAY);
            cb.SetFontAndSize(bf, 9);

            var items = documentModel.FieldContents.Where(a => a.Page == pageNumber).ToList();

            foreach (var item in items)
            {
                cb.BeginText();
                cb.ShowTextAligned(item.Alignment, item.Text ?? string.Empty, item.X, item.Y, item.Rotation);
                cb.EndText();
            }

        }

        ////////////////////////////////////////////////////////////////////

        document.Close();
        writer.Close();
        reader.Close();

        byte[] byteInfo = _memoryStream.ToArray();
        _memoryStream.Write(byteInfo, 0, byteInfo.Length);
        _memoryStream.Position = 0;

        RedirectToAction("Index");
        return new FileStreamResult(_memoryStream, "application/pdf");

I've been using this for a while now. But I want to support chinese characters also. I tried inputting chinese characters, but it is not being displayed. I changed it back to english characters and it works. Any ideas? Thanks!

There are different errors in your code.

As mkl already explained, the Type 1 Helvetica font you're using isn't embedded and doesn't know anything about Chinese glyphs. You need to use a font that knows how to draw Chinese characters. Furthermore CP1252 is the encoding for the Latin alphabet , so you shouldn't expect it to know anything about Chinese.

You're also creating code that is illegal (and that results in invalid PDF syntax):

cb.BeginText();
cb.ShowTextAligned(...);
cb.EndText();

This is a text object for which no font and size is defined. You are using the ´setFontAndSize()´ method in graphics state (where it doesn't belong) instead of in text state (where it's required). The fact that this code runs, tells me that you're using an old iTextSharp version. You should upgrade.

The solution to your problem is to find examples about adding Chinese text with iText . Then create a ´Phrase´ and position it using ColumnText.showTextAligned(); . By using ColumnText , you avoid the problem you now experience with the low-level approach: you're using beginText() and endText() without reading the PDF specification. ColumnText will do all this difficult work under the hood.

In case the Java examples confuse you, please visit this page where you'll find links to the C# port of the corresponding examples.

You can load you local Chinese font like this

BaseFont baseFont = BaseFont.CreateFont(
                "C:\\Windows\\Fonts\\simhei.ttf",
                BaseFont.IDENTITY_H,
                BaseFont.EMBEDDED); 
Font font = new Font(baseFont);

And then use the "font" for further texting.

And you can reference the common used Chinese local fonts as below

            {"yahei", "C:\\WINDOWS\\FONTS\\msyh.ttf"},
            {"fangsong", "C:\\WINDOWS\\FONTS\\simfang.ttf"},
            {"heiti" ,"C:\\Windows\\Fonts\\simhei.ttf"},
            {"kaiti" ,"C:\\Windows\\Fonts\\simkai.ttf"},
            {"lishu" ,"C:\\Windows\\Fonts\\SIMLI.TTF"},
            {"youyuan" ,"C:\\Windows\\Fonts\\SIMYOU.TTF"},
            {"songti" ,"C:\\Windows\\Fonts\\simsun.ttc,0"},
            {"xinsongti" ,"C:\\Windows\\Fonts\\simsun.ttc,1"}

Hope it helps you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM