Hi
Saikat,
Thanks for your inquiry. In case your Word document has an explicit page break to separate content on first page from the remaining pages, I can offer you a way that will help you in achieving what you are looking for. Please try using the following code snippet:
Document doc = new
Document(@"c:\test\input.docx");
Node startNode =
doc.FirstSection.Body.FirstParagraph.Runs[0];
Node endNode = null;
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph
para in paragraphs)
{
// Check all runs in the paragraph for the
first page breaks.
foreach (Run
run in para.Runs)
{
if
(run.Text.Contains(ControlChar.PageBreak))
{
endNode = run;
break;
}
}
}
if (startNode != null && endNode != null)
{
RemoveSequence(startNode,
endNode);
startNode.Remove();
endNode.Remove();
}
doc.Save(@"c:\test\out.docx");
---------------------------------------------------------------------------------------------------------------------
public void RemoveSequence(Node
start, Node end)
{
Node curNode =
start.NextPreOrder(start.Document);
while (curNode != null && !curNode.Equals(end))
{
//Move to
next node
Node
nextNode = curNode.NextPreOrder(start.Document);
//Check
whether current contains end node
if
(curNode.IsComposite)
{
CompositeNode
curComposite = (CompositeNode)curNode;
if
(!curComposite.GetChildNodes(NodeType.Any, true).Contains(end) &&
!curComposite.GetChildNodes(NodeType.Any, true).Contains(start))
{
nextNode = curNode.NextSibling;
curNode.Remove();
}
}
else
{
curNode.Remove();
}
curNode = nextNode;
}
}
Moreover, I have attached test documents here for you to play with.
I hope, this will help.
Best Regards,
Awais Hafeez
Support Developer
Aspose Sialkot Team
Aspose - Your File Format Experts
Keep in touch! We're on
Twitter and
Facebook