【发布时间】:2019-01-08 14:44:36
【问题描述】:
我在使用 iText7/iText7.pdfhtml 将 HTML 字符串转换为 PDF 文件时遇到了这个问题。我发现了一些类似的问题,例如:
但是这些问题的解决方案是针对 iText5 的,我在我的应用程序中尝试了这些解决方案,但最终失败了。我的代码是这样的:
IList<IElement> elements = HtmlConverter.ConvertToElements(pdfHtmlString, properties);
Document document = new Document(pdfDoc);
CJKSplitCharacters splitCharacters = new CJKSplitCharacters();
document.SetFontProvider(fp);
document.SetSplitCharacters(splitCharacters);
document.SetProperty(Property.SPLIT_CHARACTERS, splitCharacters);
foreach (IElement e in elements)
{
try
{
document.Add((AreaBreak)e);
}
catch
{
document.Add((IBlockElement)e);
}
}
CJKSplitCharacters 的代码:
public class CJKSplitCharacters : ISplitCharacters
{
// line of text cannot start or end with this character
static char u2060 = '\u2060'; // - ZERO WIDTH NO BREAK SPACE
// a line of text cannot start with any following characters in NOT_BEGIN_CHARACTERS[]
static char[] NOT_BEGIN_CHARACTERS = new char[]{u30fb, u2022, uff65, u300d, uff09, u0021, u0025, u0029, u002c,
u002e, u003f, u005d, u007d, uff61, uff63, uff64, uff67, uff68, uff69, uff6a, uff6b, uff6c, uff6d, uff6e,
uff6f, uff70, uff9e, uff9f, u3001, u3002, uff0c, uff0e, uff1a, uff1b, uff1f, uff01, u309b, u309c, u30fd,
u30fe, u309d, u309e, u3005, u30fc, u2019, u201d, u3015, uff3d, uff5d, u3009, u300b, u300f, u3011, u00b0,
u2032, u2033, u2103, u00a2, uff05, u2030, u3041, u3043, u3045, u3047, u3049, u3063, u3083, u3085, u3087,
u308e, u30a1, u30a3, u30a5, u30a7, u30a9, u30c3, u30e3, u30e5, u30e7, u30ee, u30f5, u30f6, u2060};
// a line of text cannot end with any following characters in NOT_ENDING_CHARACTERS[]
static char[] NOT_ENDING_CHARACTERS = new char[]{u0024, u0028, u005b, u007b, u00a3, u00a5, u201c, u2018, u3008,
u300a, u300c, u300e, u3010, u3014, uff62, uff08, uff3b, uff5b, uffe5, uff04, u2060};
/// <summary>
///
/// </summary>
/// <param name="text"></param>
/// <param name="glyphPos"></param>
/// <returns></returns>
public bool IsSplitCharacter(GlyphLine text, int glyphPos)
{
if (!text.Get(glyphPos).HasValidUnicode())
{
return false;
}
int charCode = text.Get(glyphPos).GetUnicode();
if (NOT_BEGIN_CHARACTERS.Contains((char)charCode))
{
return false;
}
return new DefaultSplitCharacters().IsSplitCharacter(text, glyphPos);
}
我的源代码在这里:Source Code
我的问题如下:
非常感谢您提前提供的帮助!
【问题讨论】:
-
感谢您在 GitHub 上发布的可重现项目。未来提示:发布最小的可重现样本,包括您尝试转换的字符串/HTML。例如。对我来说,我不可能手动输入这些字形。