We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Deleting Lines in Paragraph

I am using multiple paragraphs with multiple lines , i want to show only 5 lines in each paragraph and the rest line should be removed. the paragraphs are dynamic and the value is placed inside the paragraph section is also dynamic. how can i do this task?

Hi,


Thanks for your inquiry. We are checking with this scenario and will get back to you soon.

Best regards,

Hi,


Thanks for being patient. Please find attached a couple of helper classes and try executing the following code to achieve what you are looking for:

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

RenderedDocument layoutDoc = new RenderedDocument(doc);

ArrayList lines = new ArrayList();

foreach (RenderedPage page in layoutDoc.Pages)

{

foreach (RenderedColumn column in page.Columns)

{

foreach (RenderedLine line in column.Lines)

{

lines.Add(line);

}

}

}

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))

{

int counter = 1;

foreach (RenderedLine line in lines)

{

if (para == line.Paragraph)

{

if (counter > 5)

{

string pattern = line.Text;

pattern = pattern.Replace(ControlChar.CrLf, "");

if (line.Text.Contains(ControlChar.ParagraphBreak))

pattern = pattern.Remove(pattern.Length - 1);

Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);

para.Range.Replace(regex, new ReplaceEvaluatorFindAndHide(), true);

}

counter++;

}

}

}

doc.Save(MyDir + @“out.docx”);


public class ReplaceEvaluatorFindAndHide : IReplacingCallback<o:p></o:p>

{

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

Node currentNode = e.MatchNode;

if (e.MatchOffset > 0)

currentNode = SplitRun((Run)currentNode, e.MatchOffset);

ArrayList runs = new ArrayList();

int remainingLength = e.Match.Value.Length;

while (

(remainingLength > 0) &&

(currentNode != null) &&

(currentNode.GetText().Length <= remainingLength))

{

runs.Add(currentNode);

remainingLength = remainingLength - currentNode.GetText().Length;

do

{

currentNode = currentNode.NextSibling;

}

while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));

}

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

{

SplitRun((Run)currentNode, remainingLength);

runs.Add(currentNode);

}

foreach (Run run in runs)

run.Font.Hidden = true;

return ReplaceAction.Skip;

}

}

public static Run SplitRun(Run run, int position)

{

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

if (run.Text.Length > 0)

{

afterRun.Text = run.Text.Substring(position);

run.Text = run.Text.Substring(0, position);

run.ParentNode.InsertAfter(afterRun, run);

}

return afterRun;

}


I hope, this helps.

Best regards,