Hi,
my ASPOSE PDF .NET version is 8.2, I am searching a single word/text in pdf and replacing this with multiline text, when I generate final PDF the multiline text is overlapping with existing text on that pdf, is there any way I can fix this? I want to rearrange the remaining text after I replace with multiline text, I downloaded the sample aspose.pdf for .NET project and executed method “rearrangecontentusingtextreplacement.run()”, the generated pdf file is no different than original and I also don’t understand what’s the meaning of this below code there
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("[Cname,companyname,Textbox,50]");
@saan123,
In the constructor of the TextFragmentAbsorber class, you need to define a text string which you are seeking to replace. It appears as you did not change the search text string, and get the same output PDF document. There are various ways to search and replace text in the PDF document. Please refer to this help topic: Replace Text in a PDF Document. Kindly send us your source PDF document along with the old and new text strings. We will investigate and share our findings with you.
I defined the text string which I am seeking to replace in TextFragmentAbsorber constructor. It replaces that with new text, no issue but when I place a long text it’s not going to next line rather going in a straight line cutting itself horizontally at page end, please check the input and output file attached.
below is the code
string ExtractedText = “High Performance Computing most generally refers to the practice of aggregating computing power in a way that delivers much higher performance than one could get out of a typical desktop computer or workstation in order to solve large problems in science, engineering, or business.”;
Document doc1 = new Document(“input_file.pdf”);
TextFragmentAbsorber textFragmentAbsorber1 = new TextFragmentAbsorber(“Text Only”);
doc1.Pages.Accept(textFragmentAbsorber1);
@saan123,
Please note, PDF is a fixed file format and the other elements do not move after the replacement of text strings. However, in your scenario, the text goes outside the boundary of the page and the line break character “\r\n” also does not work. We have logged an enhancement ticket under the ticket ID PDFNET-43416 in our bug tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.
We would like to update you that PDFNET-43416 was logged as a feature request to support line breaks while replacing text. It has not been scheduled yet owing to previously logged and critical tickets. It will be scheduled on its due turn which can take some more months. We will let you know as soon as the feature will be supported. We appreciate your patience and comprehension in this regard.
We are afraid this ticket has not been scheduled yet. We have recorded your concerns and will notify you as soon as it will be resolved. We appreciate your patience and comprehension in this regard.
The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan
Please make sure that you are using the latest version of the API because the issue was fixed internally. Furthermore, it is also possible that your PDF document has some complex structure and the API is not able to handle it as per expectations. In that case, we will need sample document and sample code snippet from your side with some more details about the issue. We will test and log the ticket dedicated for your case and share the ID with you.
for (final TextFragment fragment : textFragments)
{
String replaceText = StringUtil.getNonNullString(varResolver.processPlainText(fragment.getText()));
if (replaceText.compareTo(fragment.getText()) != 0)
{
try
{
updatePDFTextFragment(fragment, replaceText);
}
catch (Exception e)
{
_logger.logError("Failed to expand document variable for content");
}
}
}
private static void updatePDFTextFragment(final TextFragment fragment, final String replaceText)
{
// find the text state of the variable text
final TextSegmentCollection textSegments = fragment.getSegments();
final TextState textState = textSegments.size() > 2 ? textSegments.get_Item(3).getTextState() : null;
final TextState fragmentTextState = fragment.getTextState();
// apply the text state of the variable text to the expanded text
if (textState != null && fragmentTextState != null)
{
fragmentTextState.setFont(textState.getFont());
fragmentTextState.setFontSize(textState.getFontSize());
fragmentTextState.setFontStyle(textState.getFontStyle());
fragmentTextState.setCharacterSpacing(textState.getCharacterSpacing());
fragmentTextState.setLineSpacing(textState.getLineSpacing());
fragmentTextState.setHorizontalScaling(textState.getHorizontalScaling());
fragmentTextState.setHorizontalAlignment(textState.getHorizontalAlignment());
fragmentTextState.setSubscript(textState.isSubscript());
fragmentTextState.setSuperscript(textState.isSuperscript());
fragmentTextState.setUnderline(textState.isUnderline());
fragmentTextState.setWordSpacing(textState.getWordSpacing());
fragmentTextState.setRenderingMode(textState.getRenderingMode());
fragmentTextState.setStrikeOut(textState.getStrikeOut());
fragmentTextState.setInvisible(textState.isInvisible());
if (textState.getForegroundColor() != null)
fragmentTextState.setForegroundColor(textState.getForegroundColor());
if (textState.getBackgroundColor() != null)
fragmentTextState.setBackgroundColor(textState.getBackgroundColor());
if (textState.getStrokingColor() != null)
fragmentTextState.setStrokingColor(textState.getStrokingColor());
// if the text background/foreground color is same compute the background color
if (fragmentTextState.getBackgroundColor()!= null && fragmentTextState.getBackgroundColor().equals(fragmentTextState.getForegroundColor()))
{
final Color computedBgColor = getComplementaryColor(fragment.getTextState().getBackgroundColor());
fragmentTextState.setBackgroundColor(computedBgColor);
}
}
fragment.setText(replaceText);
}
Can you please also share how you are getting textFragments collection? Can you please share the missing part of the code to allow us to test the scenario in our environment and address it accordingly?
Hi @asad.ali, Here’s how we are getting textFragments collection:
final TextFragmentAbsorber fragmentAbsorber = new TextFragmentAbsorber(
Pattern.compile(
"\\{\\{([^{]*?)}}",Pattern.CASE_INSENSITIVE | Pattern.DOTALL)
);
fragmentAbsorber.getTextSearchOptions().setRegularExpressionUsed(true);
fragmentAbsorber.getTextSearchOptions().setSearchForTextRelatedGraphics(false);
fragmentAbsorber.getTextReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
fragmentAbsorber.getTextReplaceOptions().setRightAdjustment(document.getPages().get_Item(1).getPageInfo().getMargin().getRight());
fragmentAbsorber.getTextReplaceOptions().setLeftAdjustment(document.getPages().get_Item(1).getPageInfo().getMargin().getLeft());
document.getPages().accept(fragmentAbsorber);
final TextFragmentCollection textFragments = fragmentAbsorber.getTextFragments();
// reset fragment absorber else fragmentAbsorber will react to the text fragments updates
fragmentAbsorber.reset();
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-45200
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.
The recent issue has been registered for your case only. The message was posted via automatic notification service which mistakenly picked irrelevant user ID. We have rectified it now. We apologize for the confusion caused.