检测字符串中的CJK字符(C#) [英] Detecting CJK characters in a string (C#)

查看:315
本文介绍了检测字符串中的CJK字符(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iTextSharp生成一系列PDF,使用Open Sans作为默认字体。有时,名称会插入到PDF的内容中。但是我的问题是我需要插入的一些名称包含CJK字符(存储在SQL Server中的nvarchar列中),据我所知,Open Sans目前不支持CJK字符。我需要继续使用Open Sans作为我的默认字体,所以理想情况下我想尝试检测从数据库中抓取的字符串中的CJK字符,并在打印出这些字符时切换到CJK字体。

I am using iTextSharp to generate a series of PDFs, using Open Sans as the default font. On occasion, names are inserted into the content of the PDFs. However my issue is that some of the names I need to insert contain CJK characters (stored in nvarchar columns in SQL Server), and as far as I know Open Sans does not support CJK characters at present. I need to keep using Open Sans as my default font, so ideally I would like to try and detect CJK characters in the strings being grabbed from the database and switch to a CJK font when printing out those characters.

正则表达式是最好的选择吗?遗憾的是,我无法找到任何有助于此的正则表达式。

Would a regex be the best bet for this? I haven't been able to find any regex patterns that would help with this unfortunately.

提前感谢您的帮助!

推荐答案

使用iTextSharp.text.pdf.FontSelector;

use iTextSharp.text.pdf.FontSelector;

iTextSharp.text.pdf.FontSelector selector = new iTextSharp.text.pdf.FontSelector();

// add 2 type of font to FontSelector
selector.AddFont(openSansfont);
selector.AddFont(chinesefont);


iTextSharp.text.Phrase phrase = selector.Process(yourTxt);

FontSelector将为您使用正确的字体!

FontSelector will use the correct font for you!

源文件FontSelector.cs的详细说明。

Detailed Description from source file FontSelector.cs.

选择包含正确呈现文本所需的字形的相应字体。按顺序检查字体,直到找到该字符。

Selects the appropriate fonts that contain the glyphs needed to render text correctly. The fonts are checked in order until the character is found.

我忘了它首先搜索的订单!请体验一下!
编辑:订单是从第一个addFont到最后一个addFont。

I forgot which order it search first!! please experience it!! the order is from the first addFont to the last addFont.

http://itextpdf.com/examples/iia.php?id=214

这篇关于检测字符串中的CJK字符(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆