如何在iText7 / iText7.pdfhtml的行首修复CJK标点

时间:2019-01-08 14:44:37

标签: .net itext7

使用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

我的问题如下:

CJK punctuation problem in iText7/iText7.pdfhtml

非常感谢您的提前帮助!

1 个答案:

答案 0 :(得分:0)

我想问题出在IsSplitCharacter方法实现中。您未使用NOT_ENDING_CHARACTERS,而仅使用NOT_BEGIN_CHARACTERS

尽管由于缺少源字符串数据而无法复制样本(这意味着我尚未测试我的方法),但我认为应该采用以下方式实现该方法:

public bool IsSplitCharacter(GlyphLine text, int glyphPos)
{
    if (!text.Get(glyphPos).HasValidUnicode())
    {
        return false;
    }
    int charCode = text.Get(glyphPos).GetUnicode();

    if (NOT_ENDING_CHARACTERS.Contains((char)charCode))
    {
        return false;
    }

    // Look ahead for the next non-whitespace character and check it not to be in NOT_BEGIN_CHARACTERS list
    for (int i = 1; glyphPos + i < text.end; i++)
    {
        if (!text.Get(glyphPos + i).HasValidUnicode())
        {
            break;
        }
        if (!TextUtil.isSpaceOrWhitespace(text.Get(glyphPos + i)))
        {
            if (NOT_BEGIN_CHARACTERS.Contains(text.Get(glyphPos + i).GetUnicode()))
            {
                return false;
            }
            break;
        }
    }

    return new DefaultSplitCharacters().IsSplitCharacter(text, glyphPos);
}

请注意,您的实现效率不是很高,因此您应该用HashSets替换char数组,以加快Contains查找的速度,该查找现在相对于数组大小是线性的。同样,您应该在DefaultSplitCharacters类中将其作为字段中的一个字段创建一次并重用,而不是每次都在IsSplitCharacter中创建CJKSplitCharacters实例。