I have a question:
In this document, I have many options A B C D, but they don’t on the each line
I want to put the options A B C D down the each line with C# program using Aspose Word
Source file: 000V2_test.docx (29.7 KB)
Result what I want: 000V2_test_result.docx (30.2 KB)
Thank you !
@quanghieumylo You can use Find/Replace functionality and regular expression to achieve this. For example see the following code:
Document doc = new Document(@"C:\Temp\in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
opt.UseSubstitutions = true;
doc.Range.Replace(new Regex(@"\s+([A-Z]\.)"), "&p$1", opt);
doc.Save(@"C:\Temp\out.docx");
Thank you, @alexey.noskov. You a help me a lot.
When I use your code, it appears empty lines.
Please help me, how to remove empty lines?
@quanghieumylo You can modify code like the following to remove empty paragraphs from the document:
Document doc = new Document(@"C:\Temp\in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
opt.UseSubstitutions = true;
doc.Range.Replace(new Regex(@"\s+([A-Z]\.)"), "&p$1", opt);
// Remove empty paragraphs
doc.GetChildNodes(NodeType.Paragraph, true).Cast<Paragraph>().Where(p => !p.HasChildNodes)
.ToList().ForEach(p => p.Remove());
doc.Save(@"C:\Temp\out.docx");
Thank a lot
Excuse me, but in the above code, the replacement A B C D part has lost formatting (color, underline, bold…).
How can I keep this format of the letters A B C D?
Thank!
@quanghieumylo You can use IReplacingCallback to implement custom logic when perform replace operation. Please try using the following code:
Document doc = new Document(@"C:\Temp\in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
opt.ReplacingCallback = new ReplaceEvaluatorFindAndInsertparagraphBreak();
doc.Range.Replace(new Regex(@"\s+([A-Z]\.)"), "", opt);
doc.Save(@"C:\Temp\out.docx");
internal class ReplaceEvaluatorFindAndInsertparagraphBreak : IReplacingCallback
{
/// <summary>
/// This method is called by the Aspose.Words find and replace engine for each match.
/// </summary>
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
Document doc = (Document)e.MatchNode.Document;
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.MatchNode;
// 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.MatchOffset > 0)
currentNode = SplitRun((Run)currentNode, e.MatchOffset);
// This array is used to store all nodes of the match for further deleting.
List<Run> runs = new List<Run>();
// Find all runs that contain parts of the match string.
int remainingLength = e.Match.Value.Length;
while (
remainingLength > 0 &&
currentNode != null &&
currentNode.GetText().Length <= remainingLength)
{
runs.Add((Run)currentNode);
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.NextSibling;
} while (currentNode != null && currentNode.NodeType != 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((Run)currentNode);
}
// Create DocumentBuilder to insert an image.
DocumentBuilder builder = new DocumentBuilder(doc);
// Get the Run with label.
Run targetRun = null;
foreach (Run run in runs)
{
// Remove runs with whitespaced before list label.
if (String.IsNullOrEmpty(run.Text.Trim()) && run.ParentNode.FirstChild == run)
run.Remove();
if (Regex.IsMatch(run.Text.Trim(), @"^(\s+)?[A-Z]"))
{
targetRun = run;
targetRun.Text = targetRun.Text.TrimStart();
break;
}
}
// Move builder to the first run.
builder.MoveTo(targetRun);
// do not insert paragraph break if the run is the first node in paragraph.
if (targetRun.ParentNode.FirstChild != targetRun)
builder.Writeln();
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
run.ParentNode.InsertAfter(afterRun, run);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring((0), (0) + (position));
return afterRun;
}
}
Thank bro! It is perfect
Sorry man, I used your code for this document, but something is wrong.
Cau 1_sua2.docx (13.8 KB)
image.png (35.2 KB)
Help me, please. Thanks!
@quanghieumylo Please try using FindReplaceDirection.Backward
instead of default forward direction:
FindReplaceOptions opt = new FindReplaceOptions();
opt.Direction = FindReplaceDirection.Backward;
opt.ReplacingCallback = new ReplaceEvaluatorFindAndInsertparagraphBreak();