以下是代理 能正常替换原有pdf内容 输出之后如果是中文或者是 阿拉伯数字3就会乱码
英文则不行 。。
public static void main(String[] args) throws IOException,
COSVisitorException
{
Map maps = getMap();
PDDocument document = PDDocument
.load(new File("G:\demo.pdf"));
List pages = document.getDocumentCatalog().getAllPages();
boolean begin = false;
// int postion = -1;
LinkedList postions = new LinkedList<>(); // 用于存放需要clear 的 坐标
LinkedList cosStrings = new LinkedList<>();
StringBuilder test = new StringBuilder();
for (PDPage pdPage : pages)
{
PDStream pdStream = pdPage.getContents();
PDFStreamParser parser = new PDFStreamParser(pdStream);
parser.parse();
List tokens = parser.getTokens();
StringBuilder sb = new StringBuilder();
for (int i = 0, len = tokens.size(); i < len; i++)
{
Object object = tokens.get(i);
if (object instanceof PDFOperator)
{
PDFOperator operator = (PDFOperator) object;
// Tj和TJ 是PDF的运算符
if ("Tj".equals(operator.getOperation()))
{
COSString previous = (COSString) tokens.get(i - 1);
if (previous.getString().indexOf("(") != -1)
{
postions.addFirst(i - 1);
sb.append(previous.getString().replaceFirst("\\{",
""));
continue;
} else if (previous.getString().indexOf(")") != -1)
{
sb.append(previous.getString().replaceFirst("\\}",
""));
// 刷新缓冲区
for (int position : postions)
{
COSString optation = (COSString) tokens
.get(position);
optation.reset();
}
COSString optation = (COSString) tokens.get(i - 1);
optation.reset();
COSString replace = (COSString) tokens.get(postions
.getFirst());
String value = maps.get(sb.toString());
if (value != null)
{
System.out.println(value);
replace.append(value.getBytes("UTF-8"));
}
postions.clear();
sb = new StringBuilder();
continue;
} else if (!postions.isEmpty())
{
postions.addLast(i - 1);
}
}
else if ("TJ".equals(operator.getOperation()))
{
COSArray previous = (COSArray) tokens.get(i - 1);
for (int j = 0, lenj = previous.size(); j < lenj; j++)
{
Object arrayElement = previous.get(j);
if (arrayElement instanceof COSString)
{
COSString cosString = (COSString) arrayElement;
String cosValue=cosString.getString();
if (cosValue.indexOf("{") != -1)
{
System.out.println(i+cosString.getString());
cosStrings.addFirst(cosString);
}if (cosValue.indexOf("}") != -1)
{
System.out.println(i+cosString.getString());
cosStrings.addLast(cosString);
replace(maps, cosStrings);
continue ;
} else if (!cosStrings.isEmpty())
{
cosStrings.add(cosString);
}
}
}
}
}
}
// 修改流
PDStream updatePdStream = new PDStream(document);
// 输出流
OutputStream outputStream = updatePdStream.createOutputStream();
// 主题写流
ContentStreamWriter tokenWriter = new ContentStreamWriter(
outputStream);
tokenWriter.writeTokens(tokens);
// 设置修改流到当前 页
pdPage.setContents(updatePdStream);
}
document.save("G:\\demoFuse.pdf");
}
private static void replace(Map maps,
LinkedList cosStrings) throws IOException,
UnsupportedEncodingException
{
//解析key
String key= getKey(cosStrings);
System.out.println(key);
//获取值
String value= maps.get(key);
//填充值
if(value != null)
{
//字符编码都以测试过无效 包括value.getBytes("UTF-8") 之类的替换
cosStrings.get(i).append(value.getBytes());
}else{
cosStrings.getFirst().append(value.getBytes("UTF-8"));
}
}
//清空集合
cosStrings.clear();
}