java替换pdf模板出现中文乱码问题

来源:互联网 发布:神马快递单打印软件 编辑:程序博客网 时间:2024/06/02 02:55

第一:生成pdf模板所用工具下载地址:http://download.csdn.net/download/luoxxib/8341745(需要一个积分)工具好使,已试过。

第二:用法,如果已有pdf模板就跳过,要自己制作pdf模板的,先在word文档中编辑好内容样式,然后选择打印的时候选择FoxIt PDF Printer(把上面的工具安装好就有这个选项了)

                                 

第三:java替换代码实现,有异常的catch一下,架包用pdfbox1.8.11版本(下载地址)http://search.maven.org/#search%7Cga%7C1%7Cpdfbox,

String flag = "$";PDDocument pdDocument = PDDocument.load(new File(sourceFile));for (Object obj : pdDocument.getDocumentCatalog().getAllPages()) {PDPage page = (PDPage) obj;PDFStreamParser parser = new PDFStreamParser(page.getContents().getStream());parser.parse();List<Object> tokens = parser.getTokens();int status = 0;String replaceKey = null;String s0 = toCNString("%", "utf-16");//判断中文替换标志String s1 = toCNString("}", "utf-16");//判断中文结尾标志for (int j = 0; j < tokens.size(); j++) {Object next = tokens.get(j);if (next instanceof PDFOperator) {PDFOperator op = (PDFOperator) next;/************** new **************/try {Object pre = tokens.get(j - 1);if (pre instanceof COSString) {COSString previousString = (COSString) pre;String string = previousString.getString();if (string.contains(flag)) {for (String key : chars.keySet()) {if (string.indexOf(key) < 0) {continue;}string = string.replace(key, chars.get(key));}previousString.reset();previousString.append(string.getBytes(encoding));}if (string.contains(s0)){replaceKey = string;status = 3;}if (status == 3) {if (!string.contains(s0)) {replaceKey += string;}previousString.reset();}if (replaceKey!=null && replaceKey.contains(s1)) {replaceKey=toRealCNString(replaceKey, "utf-16");System.out.println(replaceKey);for (String key : chars.keySet()) {if (replaceKey.indexOf(key) < 0) {continue;}replaceKey = replaceKey.replace(key, chars.get(key));}byte[] copy = getCNBytes(replaceKey,"utf-16");previousString.append(copy);status = 0;replaceKey = null;}} else if (pre instanceof COSArray) {COSArray previousArray = (COSArray) pre;for (int k = 0; k < previousArray.size(); k++) {Object arrElement = previousArray.getObject(k);if (arrElement instanceof COSString) {COSString cosString = (COSString) arrElement;String string = cosString.getString();if (string.contains(flag)) {replaceKey = string;status = 1;}if (status == 1) {if (!string.contains(flag)) {replaceKey += string;}cosString.reset();}if (replaceKey!=null && replaceKey.contains("}")) {System.out.println(replaceKey);for (String key : chars.keySet()) {if (replaceKey.indexOf(key) < 0) {continue;}replaceKey = replaceKey.replace(key, chars.get(key));}cosString.append(replaceKey.getBytes("ascii"));status = 0;replaceKey = null;}if (string.contains(s0)) {replaceKey = string;status = 2;}if (status == 2) {if (!string.contains(s0)) {replaceKey += string;}cosString.reset();}if (replaceKey!=null && replaceKey.contains(s1)) {replaceKey=toRealCNString(replaceKey, "utf-16");System.out.println(replaceKey);for (String key : chars.keySet()) {if (replaceKey.indexOf(key) < 0) {continue;}replaceKey = replaceKey.replace(key, chars.get(key));}byte[] copy = getCNBytes(replaceKey,"utf-16");cosString.append(copy);status = 0;replaceKey = null;}}}}} catch (Exception e2) {System.out.println(op.getOperation());continue;}}}PDStream updatedStream = new PDStream(pdDocument);OutputStream out = updatedStream.createOutputStream();ContentStreamWriter tokenWriter = new ContentStreamWriter(out);tokenWriter.writeTokens(tokens);page.setContents(updatedStream);pdDocument.save(destinationFile);}pdDocument.close();
private static String toRealCNString(String cn, String encoding) throws UnsupportedEncodingException {<span style="white-space:pre"></span>byte[] bytes = cn.getBytes("ISO-8859-1");<span style="white-space:pre"></span>return new String(bytes, encoding);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>private static String toCNString(String cn, String encoding) throws UnsupportedEncodingException {<span style="white-space:pre"></span>byte[] copy = getCNBytes(cn,encoding);<span style="white-space:pre"></span>return new String(copy, "ISO-8859-1");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>private static byte[] getCNBytes(String replaceKey, String encoding) throws UnsupportedEncodingException {<span style="white-space:pre"></span>byte[] bytes = replaceKey.getBytes(encoding);<span style="white-space:pre"></span>byte[] copy = new byte[bytes.length - 2];<span style="white-space:pre"></span>for (int i = 0; i < copy.length; ++i) {<span style="white-space:pre"></span>copy[i] = bytes[i + 2];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return copy;<span style="white-space:pre"></span>}


第四、补充一下pdfbox的api中介绍pdf结构,上面的遍历按照里面的结构来的http://pdfbox.apache.org/1.8/architecture.html

还是要多看多看。。。。。。。

1 0