@[TOC](PDFBOX(2.0.25) 修改文本内容)
pdfbox文本内容修在网上找不到新版本的,老版本的还不兼容(无效)。经过几周的寻找终于找到了个能用的 PDFBOX替换文本(pdfbox版本2.0.24,虽然我自己也发现这个字符编码的没弄成,参考这个后终于搞好了😊太不容易了。 记录下 方便以后使用
@Test
public void pdf() throws Exception {
File file = ResourceUtils.getFile("classpath:5.pdf");
PDDocument pd = PDDocument.load(file);
Map<COSName, PDFont> oldfont = new HashMap<COSName, PDFont>();
COSName fontName = null;
PDType0Font targetfont= PDType0Font.load(pd, new File("C:\\Windows\\Fonts\\simfang.ttf"));
for (PDPage page : pd.getPages()) {
PDFStreamParser pdfsp = new PDFStreamParser(page);
pdfsp.parse();
List<Object> tokens = pdfsp.getTokens();
for (int j = 0; j < tokens.size(); j++) {
Object next = tokens.get( j );
if(next instanceof COSName) {
fontName= (COSName)next;
if(!oldfont.containsKey(fontName)) {
oldfont.put(fontName, page.getResources().getFont(fontName));
}
}else
if(next instanceof COSString) {
COSString previous = (COSString)next;
try(InputStream in = new ByteArrayInputStream(previous.getBytes())){
StringBuffer sb = new StringBuffer();
while (in.available()>0) {
int rc = oldfont.get(fontName).readCode(in);
sb.append(oldfont.get(fontName).toUnicode(rc));
}
sb.append("例");
System.out.println("--Tj----"+sb.toString());
previous.setValue(targetfont.encode(sb.toString()));
}
}else if(next instanceof COSArray) {
byte[] pstring = {};
int prej = 0;
COSArray previous =(COSArray)next;
for (int k = 0; k < previous.size(); k++) {
Object arrElement = previous.getObject( k );
if( arrElement instanceof COSString ){
COSString cosString =(COSString)arrElement;
if (j == prej) {
byte[] thisbyte = cosString.getBytes();
byte[] temp = new byte[pstring.length+thisbyte.length];
System.arraycopy(pstring, 0, temp, 0, pstring.length);
System.arraycopy(thisbyte, 0, temp, pstring.length, thisbyte.length);
pstring=temp;
} else {
prej = j;
pstring = cosString.getBytes();
}
}
}
try(InputStream in = new ByteArrayInputStream(pstring)){
StringBuffer sb = new StringBuffer();
while (in.available()>0) {
int rc = oldfont.get(fontName).readCode(in);
sb.append(oldfont.get(fontName).toUnicode(rc));
}
sb.append("例");
System.out.println("TJ----"+sb.toString());
COSString cosString2 = (COSString) previous.getObject(0);
cosString2.setValue(targetfont.encode(sb.toString()));
}
int total = previous.size()-1;
for (int k = total; k > 0; k--) {
previous.remove(k);
}
}
}
PDStream updatedStream = new PDStream(pd);
OutputStream out = updatedStream.createOutputStream();
ContentStreamWriter tokenWriter = new ContentStreamWriter(out);
tokenWriter.writeTokens(tokens);
out.close();
oldfont.forEach((k,v)->{
page.getResources().put(k, targetfont);
});
page.setContents(updatedStream);
}
pd.save("d:/1.pdf");
pd.close();
}
昨天的代码有问题,修改后 修改前 修改后
|