Hi !!
I need to change all the line break to paragraph break. I will not save the changes. I need this changes for doing some other processing.
Is there any solution in Java?
Regards.
Thanks for your request. Please try using the following code:
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
// Open document
Document doc = new Document("C:\\Temp\\in.docx");
doc.getRange().replace(new asposewobfuscated.rn(ControlChar.LineBreak), new LineBreakReplacer(), false);
doc.save("C:\\Temp\\out.docx");
public static class LineBreakReplacer implements IReplacingCallback
{
public int replacing(ReplacingArgs e) {
// 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.getMatch().getValue();
// 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);
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();
// Remove run(s) with line break
for(int i=0; i<mainTextNodes.size(); i++ )
{
Run currentRun = (Run)mainTextNodes.get(i);
currentRun.remove();
}
return ReplaceAction.SKIP;
}
}
Best regards,
Do you have a c# / .net Example ?
Where does asposewobfuscated come from ?
Hi Boas,
doc = new Document(“C:\Temp\in.docx”);
LineBreakReplacer(), false);
: IReplacingCallback
IReplacingCallback.Replacing(ReplacingArgs e)
this is Run node where matched word starts
can consist of more than one run,
case MatchNode will represent only part of the word
matchRun = (Run)e.MatchNode;
store all parts of the word in the list
mainTextNodes = new ArrayList();
word
word = e.Match.Value;
can contain whole match word or part of the word
contains only part of the word IndexOf will return negative value
index = matchRun.Text.LastIndexOf(word);
wordLength = word.Length;
(index < 0)
matchRun.Text.LastIndexOf(word.Substring(0, wordLength–));
can contain text before match word, so we should split the Run
(index > 0)
match run to preserve formating
beforeRun = (Run)matchRun.Clone(true);
text betwee two runs
(matchRun.Text.Substring(0, index));
matchRun.Text.Substring(index);
matchRun);
word is spanned to several Run nodes, we should find all runs that represent
whole word
currentIdx = word.Length;
(currentIdx > 0)
(matchRun.Text.Length > currentIdx)
matchRun.Text.Length;
there is no next nodes, we should stop while loop
currentNode = matchRun.NextSibling;
(currentNode == null)
The next node could be not Run node (BookmarkStart or BookmarkEnd for instance)
in this case we should move to the next node
(currentNode != null)
currentNode.NextSibling;
should split the latest run in the sequence that represents whole word,
is text after match word
(matchRun != null)
(matchRun.Text.Length == currentIdx)
lastRun = (Run)matchRun.Clone(true);
matchRun.Text.Substring(0, currentIdx);
matchRun.Text.Substring(currentIdx);
DocumentBuilder cursor to the run with line break ans insert a paragraph break.
builder = new DocumentBuilder((Document)e.MatchNode.Document);
currentRun = (Run)mainTextNodes[i];