如何修改jar包源码以及解决iText生成pdf时中文标点存在行首问题

一、修改jar包

1.下载 jar 包源码。如果无法获得源码,需要用反编译工具反编译。
itext源码地址:https://repo.itextsupport.com/ui/repos/tree/General/releases/com/itextpdf/itext7-core
2.找到 jar 中你想要修改的类,在自己的工程目录下,创建一个同该类一样的包(package),保持相同包结构。
在这里插入图片描述

在这里插入图片描述
3.把你要修改的类复制到该包中,以及需要引用的类需要都进行复制。此时,可以对该类进行修改并编译为.class文件
4.对修改后的类文件,在idea中可以点击build,选择对当前类进行编译,在当前target目录下找到编译后的修改的class文件,打开需要修改的jar包,找到需要修改类所在目录,将修改后的文件拖入进行覆盖,jar包修改完毕
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、解决iText生成pdf时中文标点行首问题

对于itext-2.1.7.jar,主要对DefaultSplitCharacter.class进行修改,该方法主要用来判断字符是否为可拆分字符,对中文标点进行添加。
对于使用了其他jar,像在itext-gae-4.2.0-1.jar中对itext-2.1.7进行了引用,需要修改当前itext-gae-4.2.0-1.jar中的DefaultSplitCharacter.class才会使其生效

/*
 * $Id: DefaultSplitCharacter.java 3427 2008-05-24 18:32:31Z xlv $
 *
 * Copyright 2008 Bruno Lowagie and Xavier Le Vourch
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the License.
 *
 * The Original Code is 'iText, a free JAVA-PDF library'.
 *
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
 * All Rights Reserved.
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
 *
 * Contributor(s): all the names of the contributors are added in the source code
 * where applicable.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
 * provisions of LGPL are applicable instead of those above.  If you wish to
 * allow use of your version of this file only under the terms of the LGPL
 * License and not to allow others to use your version of this file under
 * the MPL, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the LGPL.
 * If you do not delete the provisions above, a recipient may use your version
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the MPL as stated above or under the terms of the GNU
 * Library General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
 * details.
 *
 * If you didn't download this code from the following link, you should check if
 * you aren't using an obsolete version:
 * http://www.lowagie.com/iText/
 */

package com.lowagie.text.pdf;

import com.lowagie.text.SplitCharacter;


/**
 * The default class that is used to determine whether or not a character
 * is a split character. You can subclass this class to define your own
 * split characters.
 * @since	2.1.2
 */

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 BRACKET
	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 };

	/**
	 * Custom method to for SplitCharacter to handle Japanese characters. Returns
	 * <CODE>true</CODE> if the character can split a line. The splitting
	 * implementation is free to look ahead or look behind characters to make a
	 * decision.
	 *
	 * @param start   the lower limit of <CODE>cc</CODE> inclusive
	 * @param current the pointer to the character in <CODE>cc</CODE>
	 * @param end     the upper limit of <CODE>cc</CODE> exclusive
	 * @param cc      an array of characters at least <CODE>end</CODE> sized
	 * @param ck      an array of <CODE>PdfChunk</CODE>. The main use is to be able
	 *                to call {@link com.lowagie.text.pdf.PdfChunk#getUnicodeEquivalent(int)}. It may be
	 *                <CODE>null</CODE> or shorter than <CODE>end</CODE>. If
	 *                <CODE>null</CODE> no conversion takes place. If shorter than
	 *                <CODE>end</CODE> the last element is used
	 * @return <CODE>true</CODE> if the character(s) can split a line
	 */
	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://download.csdn.net/download/qq_40035821/86512675
采用其它方式生成pdf的,也可以在自己项目中找找有没有类似的类,可以借鉴修改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值