Java使用IText生产PDF时,中文标点符号出现在行首的问题处理
2024-01-10 16:17:02
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 character
static 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 DOT
static final char u2022 = '\u2022'; // ? - BLACK SMALL CIRCLE (BULLET)
static final char uff65 = '\uff65'; // ・ - HALFWIDTH KATAKANA MIDDLE DOT
static final char u300d = '\u300d'; // 」 - RIGHT CORNER BRACKET
static final char uff09 = '\uff09'; // ) - FULLWIDTH RIGHT PARENTHESIS
static final char u0021 = '\u0021'; // ! - EXCLAMATION MARK
static final char u0025 = '\u0025'; // % - PERCENT SIGN
static final char u0029 = '\u0029'; // ) - RIGHT PARENTHESIS
static final char u002c = '\u002c'; // , - COMMA
static final char u002e = '\u002e'; // . - FULL STOP
static final char u003f = '\u003f'; // ? - QUESTION MARK
static final char u005d = '\u005d'; // ] - RIGHT SQUARE BRACKET
static final char u007d = '\u007d'; // } - RIGHT CURLY
static final char uff61 = '\uff61'; // 。 - HALFWIDTH IDEOGRAPHIC FULL STOP
static final char uff70 = '\uff70'; // ー - HALFWIDTH KATAKANA-HIRAGANA PROLONGED SOUND MARK
static final char uff9e = '\uff9e'; // ゙ - HALFWIDTH KATAKANA VOICED SOUND MARK
static final char uff9f = '\uff9f'; // ゚ - HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK
static final char u3001 = '\u3001'; // 、 - IDEOGRAPHIC COMMA
static final char u3002 = '\u3002'; // 。 - IDEOGRAPHIC FULL STOP
static final char uff0c = '\uff0c'; // , - FULLWIDTH COMMA
static final char uff0e = '\uff0e'; // . - FULLWIDTH FULL STOP
static final char uff1a = '\uff1a'; // : - FULLWIDTH COLON
static final char uff1b = '\uff1b'; // ; - FULLWIDTH SEMICOLON
static final char uff1f = '\uff1f'; // ? - FULLWIDTH QUESTION MARK
static final char uff01 = '\uff01'; // ! - FULLWIDTH EXCLAMATION MARK
static final char u309b = '\u309b'; // ゛ - KATAKANA-HIRAGANA VOICED SOUND MARK
static final char u309c = '\u309c'; // ゜ - KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK
static final char u30fd = '\u30fd'; // ヽ - KATAKANA ITERATION MARK
static final char u2019 = '\u2019'; // ’ - RIGHT SINGLE QUOTATION MARK
static final char u201d = '\u201d'; // ” - RIGHT DOUBLE QUOTATION MARK
static final char u3015 = '\u3015'; // 〕 - RIGHT TORTOISE SHELL BRACKET
static final char uff3d = '\uff3d'; // ] - FULLWIDTH RIGHT SQUARE BRACKET
static final char uff5d = '\uff5d'; // } - FULLWIDTH RIGHT CURLY BRACKET
static final char u3009 = '\u3009'; // 〉 - RIGHT ANGLE BRACKET
static final char u300b = '\u300b'; // 》 - RIGHT DOUBLE ANGLE BRACKET
static final char u300f = '\u300f'; // 』 - RIGHT WHITE CORNER BRACKET
static final char u3011 = '\u3011'; // 】 - RIGHT BLACK LENTICULAR BRACKET
static final char u00b0 = '\u00b0'; // ° - DEGREE SIGN
static final char u2032 = '\u2032'; // ′ - PRIME
static final char u2033 = '\u2033'; // ″ - DOUBLE PRIME
static 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 SIGN
static final char u0028 = '\u0028'; // ( - LEFT PARENTHESIS
static final char u005b = '\u005b'; // [ - LEFT SQUARE BRACKET
static final char u007b = '\u007b'; // { - LEFT CURLY BRACKET
static final char u00a3 = '\u00a3'; // £ - POUND SIGN
static final char u00a5 = '\u00a5'; // ¥ - YEN SIGN
static final char u201c = '\u201c'; // “ - LEFT DOUBLE QUOTATION MARK
static final char u2018 = '\u2018'; // ‘ - LEFT SINGLE QUOTATION MARK
static final char u300a = '\u300a'; // 《 - LEFT DOUBLE ANGLE BRACKET
static final char u3008 = '\u3008'; // 〈 - LEFT ANGLE BRACKET
static final char u300c = '\u300c'; // 「 - LEFT CORNER BRACKET
static final char u300e = '\u300e'; // 『 - LEFT WHITE CORNER BRACKET
static final char u3010 = '\u3010'; // 【 - LEFT BLACK LENTICULAR BRACKET
static final char u3014 = '\u3014'; // 〔 - LEFT TORTOISE SHELL BRACKET
static final char uff62 = '\uff62'; // 「 - HALFWIDTH LEFT CORNER BRACKET
static final char uff08 = '\uff08'; // ( - FULLWIDTH LEFT PARENTHESIS
static final char uff3b = '\uff3b'; // [ - FULLWIDTH LEFT SQUARE BRACKET
static final char uff5b = '\uff5b'; // { - FULLWIDTH LEFT CURLY BRACKET
static final char uffe5 = '\uffe5'; // ¥ - FULLWIDTH YEN SIGN
static final char uff04 = '\uff04'; // $ - FULLWIDTH DOLLAR SIGN
static 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 };
@Override
public 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]);
}
}
文章来源:https://blog.csdn.net/slx765633632/article/details/135501577
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!