Hello Aspose Team,
No matter how I delete the textFragment
, the original text always remains. Why is this happening? Especially when I use textFragment.setText("Longer text Longer text Longer text Longer text")
, the previous text still exists after the replacement.
version: 24.11
TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation affects it, causing the original text to remain. However, I need it to adjust line length automatically.
How can I use each old textFragment.setText
to replace text and automatically adjust the line length?
// code snap
public ByteArrayOutputStream extractAndReplace(InputStream file, String from, String to) throws Exception {
List<String> list = new ArrayList<>();
Document doc = new Document(file);
ParagraphAbsorber absorber = new ParagraphAbsorber();
absorber.visit(doc);
for (PageMarkup markup : absorber.getPageMarkups()) {
for (MarkupSection section : markup.getSections()) {
int k = 0;
List<TextFragment> newTextFragments = new ArrayList<>();
for (MarkupParagraph paragraph : section.getParagraphs()) {
for (int i = 0; i < paragraph.getFragments().size(); i++) {
TextFragment fragment = paragraph.getFragments().get(i);
fragment.getReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
fragment.setText("");
fragment.getSegments().clear();
k++;
}
}
}
}
log.info("output list:{}", JSON.toJSONString(list));
ByteArrayOutputStream output = new ByteArrayOutputStream();
// SaveFormat.Pdf
PdfSaveOptions saveOptions = new PdfSaveOptions();
doc.save(output, saveOptions);
return output;
}
image.png (93.9 KB)
image.png (219.5 KB)
25024.pdf (76.2 KB)