Replace string for Special characters

Hi,

This issue is already solved ? Im using Aspose and i need to replace all ControlChar.LINE_BREAK for ControlChar.PARAGRAPH_BREAK and i cannot find a solution for this.

Best regards

Hi Fábio,

Thanks for your inquiry. If you want to replace line break with some text, please use following code example.

Document doc = new Document(MyDir + "in.docx");

doc.getRange().replace(ControlChar.LINE_BREAK, "line break", false, false);

doc.save(MyDir + "Out.docx");

Document doc = new Document(MyDir + “in.docx”);

// Retrieve all paragraphs in the document.

NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);

// Iterate through all paragraphs

for (Paragraph para : (Iterable)paragraphs)

{

// Check all runs in the paragraph for line breaks

for (Run run : (Iterable)para.getRuns())

{

if (run.getText().contains(ControlChar.LINE_BREAK))

run.setText(run.getText().replace(ControlChar.LINE_BREAK, ControlChar.PARAGRAPH_BREAK));

}

}

doc.save(MyDir + "Out.docx");


Hope this helps you. Please let us know if you have any more queries.

Hi Tahir,

That solution solved part of my problem, but now when i have:

result.getDocument().getText() - > "Description \r#{if (%{${test} == 1})}"

and i do:

final ConditionalReplaceCallback a = new ConditionalReplaceCallback(video);
doc.getRange().replace(Pattern.compile("#\\{if"), a, true);


Note: ConditionalReplaceCallback implements IReplacingCallback


when i check the resulting of applying the pattern to the document content

final String result= e.getMatchNode().getText()

the String result has "\r#{" instead of "#{" could you please tell why are the "\r" is appearing in the result ?

Thank you

Note:
If i have: result.getDocument().getText() - > "Description\r#{if (%{${test} == 1})}"
(without the space between "description" and the "\r" all works fine, but with your solution the space is appearing after changing Line Breaks for Paragraph Breaks. Thank you once again.

Hi Fábio,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.


  • Please attach your input Word document.
  • Please

    create a standalone/runnable simple Java application that demonstrates the code (Aspose.Words code) you used to generate
    your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.

Unfortunately,
it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to
reproduce the problem. As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi Tahir,


The only problem is when the method com.aspose.words.Range(Pattern pattern, IReplacingCallback handler, boolean isForward) receive a pattern like: Pattern.compile("#\{if") and the range of document in which the replace is being performed has special characters like \r the replaced value of applying the pattern has the \r included and it shouldn’t be there, the result should be “#{if” and not “\r#{if”. Did you still needing the documents to prove this ?

Thank you again

Hi Fábio,

Thanks for your inquiry. Please use the same approach shared at following documentation link to find and replace the text.
http://www.aspose.com/docs/display/wordsjava/How+to++Find+and+Highlight+Text

Please use isForward value as false in Range.Replace method. Please use following ReplaceEvaluator in your code. Hope this helps you.


public class ReplaceEvaluator implements IReplacingCallback

{

public int replacing(ReplacingArgs e) throws Exception {

// This is a Run node that contains either the beginning or the complete match.

Node currentNode = e.getMatchNode();

// The first (and may be the only) run can contain text before the match,

// in this case it is necessary to split the run.

if (e.getMatchOffset() > 0)

currentNode = splitRun((Run)currentNode, e.getMatchOffset());

ArrayList runs = new ArrayList();

// Find all runs that contain parts of the match string.

int remainingLength = e.getMatch().group().length();

while (

(remainingLength > 0) &&

(currentNode != null) &&

(currentNode.getText().length() <= remainingLength))

{

runs.add(currentNode);

remainingLength = remainingLength - currentNode.getText().length();

// Select the next Run node.

// Have to loop because there could be other nodes such as BookmarkStart etc.

do

{

currentNode = currentNode.getNextSibling();

}

while ((currentNode != null) && (currentNode.getNodeType() != NodeType.RUN));

}

// Split the last run that contains the match if there is any text left.

if ((currentNode != null) && (remainingLength > 0))

{

splitRun((Run)currentNode, remainingLength);

runs.add(currentNode);

}

DocumentBuilder builder = new DocumentBuilder((Document)currentNode.getDocument());

builder.moveTo((Run)runs.get(0));

builder.write("New text e.g #{if");

//Remove runs

for (Run run : (Iterable) runs)

{

run.remove();

}


// Signal to the replace engine to do nothing because we have already done all what we wanted.

return ReplaceAction.SKIP;

}

private Run splitRun(Run run, int position) throws Exception

{

Run afterRun = (Run)run.deepClone(true);

afterRun.setText(run.getText().substring(position));

run.setText(run.getText().substring((0), (0) + (position)));

run.getParentNode().insertAfter(afterRun, run);

return afterRun;

}

}


If you still face problem, we need following detail for investigation purposes.


  • Please attach your input Word document.
  • Please

    create a standalone/runnable simple Java application that demonstrates the code (Aspose.Words code) you used to generate
    your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like.


Hi,

I fix the problem, thank you for support.

Kind regards,
Fábio Antunes

Hi Fábio,

Thanks for your feedback. It is nice to hear from you that your issue has been resolved. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.