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,
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 Fábio,
Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.
create a standalone/runnable simple Java application that demonstrates the code (Aspose.Words code) you used to generate
your output document
Hi Tahir,
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.
create a standalone/runnable simple Java application that demonstrates the code (Aspose.Words code) you used to generate
your output document
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.