还是没有解决问题,麻烦再帮指点一下:
1.测试文件:
copy_pdf_test.pdf (78.7 KB)
2.再次叙述下问题
将"fan" 替换成 “321312222222222222222222222222” 后 右侧文本被挤出
image.png (104.1 KB)
3.使用的代码
public class Test3 {
public static final Set<String> set = new HashSet();
public static void main(String[] args) {
// Open document
Document pdfDocument = new Document("D:\\word_test\\copy_pdf_test.pdf");
replace(pdfDocument,"fan","321312222222222222222222222222");
pdfDocument.save("D:\\word_test\\qq.pdf");
}
public static void replace( Document pdfDocument,String source,String target){
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(source);
TextReplaceOptions textReplaceOptions = new TextReplaceOptions();
textReplaceOptions.setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
textFragmentAbsorber.setTextReplaceOptions(textReplaceOptions);
// Accept the absorber for all pages of document
pdfDocument.getPages().accept(textFragmentAbsorber);
// Get the extracted text fragments into collection
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
// Loop through the fragments
for (TextFragment textFragment : textFragmentCollection) {
Position baselinePosition = textFragment.getBaselinePosition();
double xIndent = baselinePosition.getXIndent();
double yIndent = baselinePosition.getYIndent();
if(!set.contains(baselinePosition.getXIndent() + "-" + baselinePosition.getYIndent())){
set.add(xIndent + "-" + yIndent);
// Update text and other properties
textFragment.setText(target);
}
}
}
}