Java使用IText生成PDF时,中文标点符号出现在行首的问题处理
使用itext 5进行html转成pdf时,标点符号出现在某一行的开头 但这种情况下显然不符合中文书写的规则,主要问题出在itext中的DefaultSplitCharacter类,该方法主要用来判断字符是否为可拆分字符。
itext 版本:5.5.13
解决办法:
1、可以通过修改源码的方式,可以参考
如何修改jar包源码以及解决iText生成pdf时中文标点存在行首问题
2、如果不想修改源码,也可以在自己项目中创建与itext中DefaultSplitCharacter相同的包路径,并在该包下重新DefaultSplitCharacter类,能这样处理的原因,与类的加载顺序有关。
package com.itextpdf.text.pdf;import com.itextpdf.text.SplitCharacter;public class DefaultSplitCharacter implements SplitCharacter {/*** An instance of the default SplitCharacter.*/public static final SplitCharacter DEFAULT = new DefaultSplitCharacter();// line of text cannot start or end with this characterstatic final char u2060 = '\u2060'; // - ZERO WIDTH NO BREAK SPACE// a line of text cannot start with any following characters in// NOT_BEGIN_CHARACTERS[]static final char u30fb = '\u30fb'; // ・ - KATAKANA MIDDLE DOTstatic final char u2022 = '\u2022'; // • - BLACK SMALL CIRCLE (BULLET)static final char uff65 = '\uff65'; // ・ - HALFWIDTH KATAKANA MIDDLE DOTstatic final char u300d = '\u300d'; // 」 - RIGHT CORNER BRACKETstatic final char uff09 = '\uff09'; // ) - FULLWIDTH RIGHT PARENTHESISstatic final char u0021 = '\u0021'; // ! - EXCLAMATION MARKstatic final char u0025 = '\u0025'; // % - PERCENT SIGNstatic final char u0029 = '\u0029'; // ) - RIGHT PARENTHESISstatic final char u002c = '\u002c'; // , - COMMAstatic final char u002e = '\u002e'; // . - FULL STOPstatic final char u003f = '\u003f'; // ? - QUESTION MARKstatic final char u005d = '\u005d'; // ] - RIGHT SQUARE BRACKETstatic final char u007d = '\u007d'; // } - RIGHT CURLYstatic final char uff61 = '\uff61'; // 。 - HALFWIDTH IDEOGRAPHIC FULL STOPstatic final char uff70 = '\uff70'; // ー - HALFWIDTH KATAKANA-HIRAGANA PROLONGED SOUND MARKstatic final char uff9e = '\uff9e'; // ゙ - HALFWIDTH KATAKANA VOICED SOUND MARKstatic final char uff9f = '\uff9f'; // ゚ - HALFWIDTH KATAKANA SEMI-VOICED SOUND MARKstatic final char u3001 = '\u3001'; // 、 - IDEOGRAPHIC COMMAstatic final char u3002 = '\u3002'; // 。 - IDEOGRAPHIC FULL STOPstatic final char uff0c = '\uff0c'; // , - FULLWIDTH COMMAstatic final char uff0e = '\uff0e'; // . - FULLWIDTH FULL STOPstatic final char uff1a = '\uff1a'; // : - FULLWIDTH COLONstatic final char uff1b = '\uff1b'; // ; - FULLWIDTH SEMICOLONstatic final char uff1f = '\uff1f'; // ? - FULLWIDTH QUESTION MARKstatic final char uff01 = '\uff01'; // ! - FULLWIDTH EXCLAMATION MARKstatic final char u309b = '\u309b'; // ゛ - KATAKANA-HIRAGANA VOICED SOUND MARKstatic final char u309c = '\u309c'; // ゜ - KATAKANA-HIRAGANA SEMI-VOICED SOUND MARKstatic final char u30fd = '\u30fd'; // ヽ - KATAKANA ITERATION MARKstatic final char u2019 = '\u2019'; // ’ - RIGHT SINGLE QUOTATION MARKstatic final char u201d = '\u201d'; // ” - RIGHT DOUBLE QUOTATION MARKstatic final char u3015 = '\u3015'; // 〕 - RIGHT TORTOISE SHELL BRACKETstatic final char uff3d = '\uff3d'; // ] - FULLWIDTH RIGHT SQUARE BRACKETstatic final char uff5d = '\uff5d'; // } - FULLWIDTH RIGHT CURLY BRACKETstatic final char u3009 = '\u3009'; // 〉 - RIGHT ANGLE BRACKETstatic final char u300b = '\u300b'; // 》 - RIGHT DOUBLE ANGLE BRACKETstatic final char u300f = '\u300f'; // 』 - RIGHT WHITE CORNER BRACKETstatic final char u3011 = '\u3011'; // 】 - RIGHT BLACK LENTICULAR BRACKETstatic final char u00b0 = '\u00b0'; // ° - DEGREE SIGNstatic final char u2032 = '\u2032'; // ′ - PRIMEstatic final char u2033 = '\u2033'; // ″ - DOUBLE PRIMEstatic final char[] NOT_BEGIN_CHARACTERS = new char[] { u30fb, u2022, uff65, u300d, uff09, u0021, u0025, u0029,u002c, u002e, u003f, u005d, u007d, uff61, uff70, uff9e, uff9f, u3001, u3002, uff0c, uff0e, uff1a, uff1b,uff1f, uff01, u309b, u309c, u30fd, u2019, u201d, u3015, uff3d, uff5d, u3009, u300b, u300f, u3011, u00b0,u2032, u2033, u2060 };// a line of text cannot end with any following characters in// NOT_ENDING_CHARACTERS[]static final char u0024 = '\u0024'; // $ - DOLLAR SIGNstatic final char u0028 = '\u0028'; // ( - LEFT PARENTHESISstatic final char u005b = '\u005b'; // [ - LEFT SQUARE BRACKETstatic final char u007b = '\u007b'; // { - LEFT CURLY BRACKETstatic final char u00a3 = '\u00a3'; // £ - POUND SIGNstatic final char u00a5 = '\u00a5'; // ¥ - YEN SIGNstatic final char u201c = '\u201c'; // “ - LEFT DOUBLE QUOTATION MARKstatic final char u2018 = '\u2018'; // ‘ - LEFT SINGLE QUOTATION MARKstatic final char u300a = '\u300a'; // 《 - LEFT DOUBLE ANGLE BRACKETstatic final char u3008 = '\u3008'; // 〈 - LEFT ANGLE BRACKETstatic final char u300c = '\u300c'; // 「 - LEFT CORNER BRACKETstatic final char u300e = '\u300e'; // 『 - LEFT WHITE CORNER BRACKETstatic final char u3010 = '\u3010'; // 【 - LEFT BLACK LENTICULAR BRACKETstatic final char u3014 = '\u3014'; // 〔 - LEFT TORTOISE SHELL BRACKETstatic final char uff62 = '\uff62'; // 「 - HALFWIDTH LEFT CORNER BRACKETstatic final char uff08 = '\uff08'; // ( - FULLWIDTH LEFT PARENTHESISstatic final char uff3b = '\uff3b'; // [ - FULLWIDTH LEFT SQUARE BRACKETstatic final char uff5b = '\uff5b'; // { - FULLWIDTH LEFT CURLY BRACKETstatic final char uffe5 = '\uffe5'; // ¥ - FULLWIDTH YEN SIGNstatic final char uff04 = '\uff04'; // $ - FULLWIDTH DOLLAR SIGNstatic final 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 };@Overridepublic boolean isSplitCharacter(int start, int current, int end, char[] cc, PdfChunk[] ck) {// Note: If you don't add an try/catch and there is an issue with// isSplitCharacter(), iText silently fails and// you have no idea there was a problem.try {char c = getCharacter(current, cc, ck);int next = current + 1;if (next < cc.length) {char charNext = getCharacter(next, cc, ck);for (char not_begin_character : NOT_BEGIN_CHARACTERS) {if (charNext == not_begin_character) {return false;}}}for (char not_ending_character : NOT_ENDING_CHARACTERS) {if (c == not_ending_character) {return false;}}if (c <= ' ' || c == '-' || c == '\u2010') {return true;}if (c < 0x2002)return false;return ((c >= 0x2002 && c <= 0x200b)|| (c >= 0x2e80 && c < 0xd7a0)|| (c >= 0xf900 && c < 0xfb00)|| (c >= 0xfe30 && c < 0xfe50)|| (c >= 0xff61 && c < 0xffa0));} catch (Exception ex) {ex.printStackTrace();}return true;}/*** Returns a character int the array (Note: modified from the iText default* version with the addition null check of '|| ck[Math.min(position, ck.length -* 1)] == null'.** @param position position in the array* @param ck chunk array* @param cc the character array that has to be checked* @return the character*/protected char getCharacter(int position, char[] cc, PdfChunk[] ck) {if (ck == null || ck[Math.min(position, ck.length - 1)] == null) {return cc[position];}return (char) ck[Math.min(position, ck.length - 1)].getUnicodeEquivalent(cc[position]);}
}