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

第一:生成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

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值