2016-02-15 96 views
2

如何使用PDFBox 2.0查找并替换PDF文档中的文本,他们拉动了旧的示例,它的语法不再起作用,因此我在想如果它仍然有可能,最好的方法是去做。谢谢!PDFBox 2.0 RC3 - 查找和替换文本

+3

那个老例子实际上只有在非常简单的PDF工作并没有改变或者(更糟糕的)损坏更复杂的。 – mkl

回答

3

你可以尝试这样的:

public static PDDocument replaceText(PDDocument document, String searchString, String replacement) throws IOException { 
    if (Strings.isEmpty(searchString) || Strings.isEmpty(replacement)) { 
     return document; 
    } 
    PDPageTree pages = document.getDocumentCatalog().getPages(); 
    for (PDPage page : pages) { 
     PDFStreamParser parser = new PDFStreamParser(page); 
     parser.parse(); 
     List tokens = parser.getTokens(); 
     for (int j = 0; j < tokens.size(); j++) { 
      Object next = tokens.get(j); 
      if (next instanceof Operator) { 
       Operator op = (Operator) next; 
       //Tj and TJ are the two operators that display strings in a PDF 
       if (op.getName().equals("Tj")) { 
        // Tj takes one operator and that is the string to display so lets update that operator 
        COSString previous = (COSString) tokens.get(j - 1); 
        String string = previous.getString(); 
        string = string.replaceFirst(searchString, replacement); 
        previous.setValue(string.getBytes()); 
       } else if (op.getName().equals("TJ")) { 
        COSArray previous = (COSArray) tokens.get(j - 1); 
        for (int k = 0; k < previous.size(); k++) { 
         Object arrElement = previous.getObject(k); 
         if (arrElement instanceof COSString) { 
          COSString cosString = (COSString) arrElement; 
          String string = cosString.getString(); 
          string = StringUtils.replaceOnce(string, searchString, replacement); 
          cosString.setValue(string.getBytes()); 
         } 
        } 
       } 
      } 
     } 
     // now that the tokens are updated we will replace the page content stream. 
     PDStream updatedStream = new PDStream(document); 
     OutputStream out = updatedStream.createOutputStream(); 
     ContentStreamWriter tokenWriter = new ContentStreamWriter(out); 
     tokenWriter.writeTokens(tokens); 
     page.setContents(updatedStream); 
     out.close(); 
    } 
    return document; 
} 
+2

此代码仅适用于非常简单的PDF文件,不会更改或(更糟糕)损坏更复杂的文件。 – mkl

+2

https://pdfbox.apache.org/2.0/migration.html为什么要删除ReplaceText示例? –

+0

这在您提到的链接的最后一节中有解释:https://pdfbox.apache.org/2.0/migration.html#why-was-the-replacetext-example-removed 这主要是由于字符编码和字体问题。 – maxxyme

1

我花在想出了一个解决方案的时间和最终获取的Acrobat DC订阅,这样我可以为文本创建字段作为占位符是更换。在我的情况下,这些字段是用于客户信息和订单详细信息,因此它不是非常复杂的数据,但该文档充满了业务相关条件的页面,并且布局非常复杂。

然后我只是做了这个,这可能适合你。

private void update() throws InvalidPasswordException, IOException { 
    Map<String, String> map = new HashMap<>(); 
    map.put("fieldname", "value to update"); 
    File template = new File("template.pdf"); 
    PDDocument document = PDDocument.load(template); 
    List<PDField> fields = document.getDocumentCatalog().getAcroForm().getFields(); 
    for (PDField field : fields) { 
     for (Map.Entry<String, String> entry : map.entrySet()) { 
      if (entry.getKey().equals(field.getFullyQualifiedName())) { 
       field.setValue(entry.getValue()); 
       field.setReadOnly(true); 
      } 
     } 
    } 
    File out = new File("out.pdf"); 
    document.save(out); 
    document.close(); 
} 

因人而异

+1

使用AcroForm字段确实是应该如何完成PDF填充。但是你不需要Acrobat来创建字段,你也可以用PDFBox来做到这一点...(虽然没有好的GUI) – mkl

+0

Thx @mkl,我意识到可以使用pdfbox创建字段,但我可以没有弄清楚如何将它们放在文档中的确切位置。 –