We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Deleting searched text

I am able to replace the text with new text. But, I want to search and delete the found text. As shown in examples I used following code


textFragment.getSegments().delete(1);

but other texts along the found text also gets deleted. 


Hi there,


We are sorry for the inconvenience. We will appreciate it if you please share your sample PDF document here, so we will look into it and will guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,
Please find the file in attachment. All I am trying to do is search a text "sample" and replace it with text form field, following is snippet of the code,



Page requiredPage = null;
Rectangle requiredPosition = null;

// Loop through the fragments
for (com.aspose.pdf.TextFragment textFragment : (Iterable) textFragmentCollection) {

requiredPage = textFragment.getPage();
requiredPosition=textFragment.getRectangle();



TextBoxField textBoxField1 = new TextBoxField(requiredPage, requiredPosition);
textBoxField1.setPartialName("textbox1");
textBoxField1.setValue(textFragment.getText());


pdfDocument.getForm().add(textBoxField1);
}

Hi there,

Thanks for sharing the source document and code. I have tested your scenario with shared document using Aspose.Pdf for Java 11.7.0 and managed to observe the reported issue. For further investigation, I have logged an issue in our issue tracking system as PDFJAVA-35974 and also linked your request to it. We will keep you updated via this thread regarding the issue status.

We are sorry for the inconvenience caused.

<span style=“font-size:10.0pt;font-family:“Arial”,“sans-serif”;mso-fareast-font-family:
Calibri;color:#333333;mso-ansi-language:EN-US;mso-fareast-language:EN-US;
mso-bidi-language:AR-SA”>Best Regards,

@dknock

Thanks for your patience.

Please use following code snippet, in order to search and delete the text from PDF document.

Page requiredPage = null;
Rectangle requiredPosition = null;
Document pdfDocument = new Document("D:/Downloads/input (8).pdf");
String replaceText = "sample";
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(replaceText);
textFragmentAbsorber.getTextReplaceOptions().setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.AdjustSpaceWidth);
textFragmentAbsorber.getTextReplaceOptions().setReplaceScope(TextReplaceOptions.Scope.REPLACE_ALL);

pdfDocument.getPages().accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();

for (TextFragment textFragment : (Iterable<TextFragment>) textFragmentCollection) {
    requiredPage = textFragment.getPage();
    requiredPosition = textFragment.getRectangle();

    requiredPosition.setLLX(requiredPosition.getLLX() + 1);
    TextBoxField textBoxField1 = new TextBoxField(requiredPage, requiredPosition);
    textBoxField1.setPartialName("textbox1");    
    textBoxField1.setValue(textFragment.getText());   
    pdfDocument.getForm().add(textBoxField1);
    String emptyText =  "";
    for (int i = 0; i < replaceText.length(); i++) {
       emptyText = emptyText +  " ";
    }
    
    textFragment.setText(emptyText);

}

pdfDocument.save(myDir+"Updated_Text.pdf"); 

In case of any further assistance, please feel free to let us know.