Here is a copy of my code.
package com.sample.pdf;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.log4j.Logger;
import com.aspose.pdf.License;
import com.aspose.pdf.TextFragment;
import com.aspose.pdf.TextFragmentAbsorber;
import com.aspose.pdf.TextReplaceOptions;
import com.sample.pdf.json.DEIDRequest;
public class TextReplacementTest {
private static final Logger LOGGER = Logger.getLogger(TextReplacementTest.class.getName());
public static void main(final String[] args) throws Exception {
TestTextRedactionUsingAspose();
}
private static void TestTextRedactionUsingAspose() throws Exception {
com.aspose.pdf.Document document = new com.aspose.pdf.Document("H:/redaction/output/RedactTextv8.pdf");
String outUrl="H:/redaction/output/Test.pdf";
ArrayList<DEIDRequest> deidRequests = new ArrayList<DEIDRequest>();
TextRedaction1DLImpl rt = new TextRedaction1DLImpl();
DEIDRequest deidReq = new DEIDRequest();
deidReq.setMethod("ds_replace");
deidReq.setMatch("1");
deidReq.setText("medication");
deidReq.setPg("4");
deidReq.setReplaceTerm("testing medication");
deidRequests.add(deidReq);
rt.replaceText(document, deidRequests, outUrl, false);
}
public void replaceText(com.aspose.pdf.Document document, ArrayList<DEIDRequest> deidReqs, String outUrl, boolean isProposal) throws Exception {
FileInputStream fstream = new FileInputStream("H:\\deidProject\\deid_pdf_withAPDFL_04092018\\deid_pdf"
+ "\\pdf_service_dl_aspose\\Aspose.Total.Java.lic");
// Instantiate the License class
License license = new License();
// Set the license through the stream object
license.setLicense(fstream);
try {
for (int i = 0; i < deidReqs.size(); i++) {
DEIDRequest deidRequest = deidReqs.get(i);
LOGGER.info(" ID: " + deidReqs.get(i).getId());
LOGGER.info(" Size: " + deidReqs.size());
LOGGER.info("markTextForRedaction: Match>" + deidReqs.get(i).getMethod() + " Text>"
+ deidReqs.get(i).getText() + " PageNumber>" + deidReqs.get(i).getPg() + " Replace Text>"+ deidRequest.getReplaceTerm());
if ("ds_replace".equals(deidReqs.get(i).getMethod())) {
/* TextReplaceOptions textReplaceOptions = textFragmentAbsorber.getTextReplaceOptions();
if(textReplaceOptions != null)
{
textReplaceOptions.setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
}
textFragmentAbsorber.setTextReplaceOptions(textReplaceOptions);*/
String searchRegExp = deidRequest.getText();
searchRegExp = searchRegExp.replace("+", "\\+");
searchRegExp = searchRegExp.replace("(", "\\(");
searchRegExp = searchRegExp.replace(")", "\\)");
searchRegExp=searchRegExp.replace("+", "\\\\+");
String regexForText = searchRegExp.replaceAll(" ", "(\\\\s*|\\\\n)");
// TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(searchRegExp);
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber( regexForText );
TextReplaceOptions textReplaceOptions = textFragmentAbsorber.getTextReplaceOptions();
if(textReplaceOptions != null){
System.out.println("setting AdjustSpaceWidth");
textReplaceOptions.setReplaceAdjustmentAction(TextReplaceOptions.ReplaceAdjustment.WholeWordsHyphenation);
}
textFragmentAbsorber.setTextReplaceOptions(textReplaceOptions);
document.getPages().get_Item(Integer.parseInt(deidRequest.getPg())).accept(textFragmentAbsorber);
// Replace each TextFragment
Iterator<TextFragment> it = textFragmentAbsorber.getTextFragments().iterator();
while (it.hasNext()) {
TextFragment textFragment = it.next();
LOGGER.info("Text Found >"+ searchRegExp);
textFragment.setText(deidRequest.getReplaceTerm());
}
}
}
if (isProposal) {
// TODO
} else {
document.save(outUrl.replaceAll("%20", " "));
document.close();
}
} catch (Exception e) {
// LOGGER.info("Error markTextForRedaction: Match>" + deidReqs.getMatch() + "
// Text>" + deidReq.getText() + " PageNumber>"+deidReq.getPg() );
LOGGER.info(e.getMessage(), e);
}
}
}
package com.gcesolutions.pdf.json;
public class DEIDRequest {
public void setId(String id) {
this.id = id;
}
private String id;
/** The replace term. */
private String replaceTerm="";
/** The text. */
private String text="";
/** The method. */
private String method="";
/** The match. */
private String match="";
/** The di type. */
private String diType ="";
/** The pg. */
private String pg ="";
/**
* Gets the replace term.
*
* @return the replace term
*/
public String getReplaceTerm() {
return replaceTerm;
}
/**
* Sets the replace term.
*
* @param replaceTerm the new replace term
*/
public void setReplaceTerm(String replaceTerm) {
this.replaceTerm = replaceTerm;
}
/**
* Gets the text.
*
* @return the text
*/
public String getText() {
return text;
}
/**
* Sets the text.
*
* @param text the new text
*/
public void setText(String text) {
this.text = text;
}
/**
* Gets the method.
*
* @return the method
*/
public String getMethod() {
return method;
}
/**
* Sets the method.
*
* @param method the new method
*/
public void setMethod(String method) {
this.method = method;
}
/**
* Gets the match.
*
* @return the match
*/
public String getMatch() {
return match;
}
/**
* Sets the match.
*
* @param match the new match
*/
public void setMatch(String match) {
this.match = match;
}
/**
* Gets the di type.
*
* @return the di type
*/
public String getDiType() {
return diType;
}
/**
* Sets the di type.
*
* @param diType the new di type
*/
public void setDiType(String diType) {
this.diType = diType;
}
/**
* Gets the pg.
*
* @return the pg
*/
public String getPg() {
return pg;
}
/**
* Sets the pg.
*
* @param pg the new pg
*/
public void setPg(String pg) {
this.pg = pg;
}
public String getId() {
// TODO Auto-generated method stub
return id;
}
}
Thanks!