Line break characters

Which constant from the ControlChar class is used in line break.

e.g. please find attached document.

I want to replace all line break with some characters.


Which Character is used for line break in the attached document. from the ControlChar?




Hi Kumar,

Thanks for your inquiry. You can use “ControlChar.LINE_BREAK” to replace all line break with some characters. Please follow up the link where my colleague described, how we can use IReplacingCallback to replace soft line break with paragraph breaks:

I hope this will help. In case of any ambiguity, please let me know.

Hi,

In the attachment attached originalSubDoc.doc

I have some line breaks but when i try to replace them with some thing e.g.

for(Paragraph para : (Iterable) doc.getChildNodes(NodeType.PARAGRAPH, true))
{
for (Run run : (Iterable) para.getChildNodes(NodeType.RUN, true))
{
String RunNodeText = run.getText();
RunNodeText = RunNodeText.replaceAll(ControlChar.LINE_BREAK, “#~#”);
}
}
return doc;

It dose not get replaced.

Do i have to replace the line break in the way u have given in link. ??

Hi,

I tried to using the link u provided.
I am using aspose word jar 4.0.2

I have implemented it in this way :

package com.zycus;

import java.util.ArrayList;
import java.util.regex.Pattern;

import com.aspose.words.ControlChar;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.Node;
import com.aspose.words.NodeType;
import com.aspose.words.Paragraph;
import com.aspose.words.ReplaceAction;
import com.aspose.words.ReplaceEvaluator;
import com.aspose.words.ReplaceEvaluatorArgs;
import com.aspose.words.Run;

public class TextReplaceTest
{
public static void main(String[] args)
{
try
{
Document doc = new Document(“D:/temp/originalSubDoc.doc”);

doc.getRange().replace(Pattern.compile(ControlChar.PARAGRAPH_BREAK), new LineBreakReplacer("#~#"), true);
doc.save(“D:/temp/tempDoc.doc”);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class LineBreakReplacer implements ReplaceEvaluator
{
String replaceWith;
public LineBreakReplacer(String test)
{
super();
System.out.println(“in constructor”);
replaceWith = test;
}
public int replace(Object arg0, ReplaceEvaluatorArgs e) throws Exception
{

// Get MatchNode, this is Run node where matched word starts
// The word can consist of more than one run,
// in this case MatchNode will represent only part of the word
Run matchRun = (Run) e.getMatchNode();
// We will store all parts of the word in the list
ArrayList mainTextNodes = new ArrayList();
// Get match word
String word = e.getMatchNode().getText();
// The run can contain whole match word or part of the word
// When run contains only part of the word IndexOf will return
// negative value
int index = matchRun.getText().lastIndexOf(word);
System.out.println(“word …” + word);
int wordLength = word.length();
while (index < 0)
index = matchRun.getText().lastIndexOf(word.substring(0, wordLength–));
// Match Run can contain text before match word, so we should split
// the Run
if (index > 0)
{
// Clone match run to preserve formating
Run beforeRun = (Run) matchRun.deepClone(true);
// Split text betwee two runs
beforeRun.setText(matchRun.getText().substring(0, index));
matchRun.setText(matchRun.getText().substring(index));
matchRun.getParentNode().insertBefore(beforeRun, matchRun);
}
// If the word is spanned to several Run nodes, we should find all
// runs that represent whole word
int currentIdx = word.length();
while (currentIdx > 0)
{
if (matchRun.getText().length() > currentIdx)
{
break;
}

else
{
mainTextNodes.add(matchRun);
currentIdx = currentIdx - matchRun.getText().length();
// If there is no next nodes, we should stop while loop
Node currentNode = matchRun.getNextSibling();
if (currentNode == null)
{
break;
}
else
{
// The next node could be not Run node (BookmarkStart or
// BookmarkEnd for instance)
// in this case we should move to the next node
while (currentNode != null)
{
if (currentNode.getNodeType() == NodeType.RUN)
{
matchRun = (Run) currentNode;
break;
}
currentNode = currentNode.getNextSibling();
}
}
}
}
// Now we should split the latest run in the sequence that
// represents whole word,
// if there is text after match word
if (matchRun != null)
{
if (matchRun.getText().length() == currentIdx)
{
mainTextNodes.add(matchRun);
}
else
{
Run lastRun = (Run) matchRun.deepClone(true);
lastRun.setText(matchRun.getText().substring(0, currentIdx));
matchRun.setText(matchRun.getText().substring(currentIdx));
matchRun.getParentNode().insertBefore(lastRun, matchRun);
mainTextNodes.add(lastRun);
}
}
// Move DocumentBuilder cursor to the run with line break ans insert
// a paragraph break.
DocumentBuilder builder = new DocumentBuilder((Document) e.getMatchNode().getDocument());
builder.moveTo((Node) mainTextNodes.get(0));
builder.writeln(replaceWith);
// Remove run(s) with line break
for (int i = 0; i < mainTextNodes.size(); i++)
{
Run currentRun = (Run) mainTextNodes.get(i);
System.out.println("=>" + currentRun + “<=”);
currentRun.remove();
}

return ReplaceAction.SKIP;

}

}

this is not replace the lline break/pagebreak.

I have tried it for both. on the attached document.
named : originalSubDoc.doc

this doc originalSubDoc is also renerated from aspose jar.

I want to replace all line break, page break, new line char with some text. say for e.g. “#~#”

If i run this code for replacing PARAGRAPH_BREAK the i get null pointer


java.lang.NullPointerException
at com.aspose.words.acc.Nn(FindReplace.java:162)
at com.aspose.words.Range.replace(Range.java:146)
at com.zycus.TextReplaceTest.main(TextReplaceTest.java:41)

Hi Kumar,

Thank you for inquiry. Please note that, pressing SHIFT+ENTER inside MS Word inserts a manual line break character. Its Aspose.Words equivalent is ControlChar.LineBreak field. In your provided Word document, I have insert line break which is working perfect with following code snippet.

Document doc = new Document("c:/temp/originalSubDoc.doc");
for (Run run : (Iterable<Run>)doc.getChildNodes(NodeType.RUN, true))
{
    if (run.getText().endsWith(ControlChar.LINE_BREAK))
        run.setText(run.getText().replace(ControlChar.LINE_BREAK, "--LineBreak--"));
}
doc.save("c:/temp/test096/tempDoc8.doc");

In case of any ambiguity, please let me know.

Hi Imran,

Please find attached originalSubDoc.doc in the reply.

when i run the same code which you have provided, it dose not work.

I am unable to replace line breaks.

Mycode:

public static Document replaceNewLineWithCustomChar(Document doc)
{
for (Paragraph para : (Iterable) doc.getChildNodes(NodeType.PARAGRAPH, true))
{
for (Run run : (Iterable) para.getChildNodes(NodeType.RUN, true))
{
String RunNodeText = run.getText();

System.out.println(“node text:” + RunNodeText);
System.out.println(“node type:” + run.getNodeType());

if (run.getText().endsWith(ControlChar.LINE_BREAK))
run.setText(run.getText().replace(ControlChar.LINE_BREAK, “–LineBreak–”));
}
}
return doc;
}

Hi Kumar,

Thanks for your inquiry.

We release a new version of Aspose.Words every month. Each new release contains many improvements, bug fixes and new features. So, we suggest you always use latest version of Aspose.Words. Moreover, I have attached input/output document along with slight change where I have inserted a line break manually by pressing SHIFT+ENTER inside provided Word document. Now you can observe "--LineBreak--" text within first line by executing replaceNewLineWithCustomChar method.

Hi Imran,

The document which u added out.doc dose not have any string as mentioned by you.

Could you please re check and share the code snippet to replace the any new line character with a string.

Thanks,

Kumar Sabnis
Software Engineer
Zycus Infotech Ltd.

Hi Kumar,


Thanks for the inquiry and sorry for delay. I’m afraid. I could not find the problem. I have attached snapshot for your reference. You can use the code snippet already mentioned above.