Our users are asking for a replacement adjustment option where the new text expands to the left instead of the right. This usually comes up in tables. For example, in the screenshot below, say you want to replace that first “1” with “1-10”. With all the replacement options now, it will either cut off that text or push the rest of the table row to the right. It would be nice to have an option where the text is pushed to the left instead, as in the second screenshot.
Screenshot 2025-02-24 at 9.21.31 AM.png (49.5 KB)
Screenshot 2025-02-24 at 9.21.05 AM.png (50.3 KB)
Screenshot 2025-02-24 at 9.37.29 AM.png (25.8 KB)
BigTable_fixture.pdf (145.1 KB)
@kander91
Would you please also be able to provide us with some sample code that you have been using for text replacements? It would help us in analyzing the feasibility of this feature and proceed accordingly.
Sure, here’s the method that we use
private static Rectangle replaceTextInRect(Page page, Rectangle rect, String originalText, String replacementText) throws Exception {
//Find the original text. Starts by looking in the given rectangle then expands the rectangle if it doesn't find it
//there. Returns null if it can't find it anywhere. So this returns either null or a collection containing the
//original text.
TextFragmentCollection textFragmentCollection = findOriginalText(originalText, rect, page);
if (textFragmentCollection == null || textFragmentCollection.size() == 0) {
throw new Exception(String.format("Could not find the text that's supposed to be in this rectangle.\nText: %s\nPage: %s", originalText, page.getNumber()));
}
boolean replacementTextInserted = false;
Rectangle newRect = null;
for (int i=1; i <= textFragmentCollection.size(); i++) {
TextFragment textFragment = textFragmentCollection.get_Item(i);
try {
//Get any whitespace before and after the text in this fragment. We don't want to delete that when we
//change the text because it will mess up the spacing for the line.
String[] leadingAndTrailingWhiteSpace = getLeadingAndTrailingWhitespaces(textFragment.getText());
String leadingWhiteSpace = i == 1 ? leadingAndTrailingWhiteSpace[0] : "";
String trailingWhiteSpace = i == textFragmentCollection.size() ? leadingAndTrailingWhiteSpace[1] : "";
if (!textFragment.getText().isEmpty() && !replacementTextInserted) {
//This determines how text will be replaced. ShiftRestOfLine only affects the rest of the current line.
textFragment.getReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.ShiftRestOfLine);
textFragment.getReplaceOptions().setIgnoreParagraphs(true);
textFragment.setText(leadingWhiteSpace + replacementText + trailingWhiteSpace);
newRect = textFragment.getRectangle();
replacementTextInserted = true;
} else {
textFragmentCollection.get_Item(i).setText(leadingWhiteSpace + trailingWhiteSpace);
}
} catch (Exception e) {
System.out.println("may be missing font: " + textFragment.getTextState().getFont().getFontName());
throw e;
}
}
page.freeMemory();
return getReverseRotatedRectangle(page, newRect);
}
@kander91
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PDFJAVA-44764
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
Great, thanks so much for taking a look!