@dipikawani
dipikawani:
Please let me know how to delete 4th page from attached file by using below statement
Thanks for your patience. Please use the following code example to remove the specific page from the document. Please get the code of “RenderedDocument” in Aspose.Words for .NET examples repository at GitHub.
This code example does the following steps.
- Finds the text of the first line of 4th and 5th pages.
- Bookmark the content of the 4th page.
- Remove the content of bookmark.
Hope this helps you.
int remove_page = 4;
Document doc = new Document(MyDir + "orignal(RefPage4).Docx");
// Create a new RenderedDocument class from a Document object.
RenderedDocument layoutDoc = new RenderedDocument(doc);
//Find the text of first line of page 4
RenderedPage page = layoutDoc.Pages[remove_page - 1];
LayoutCollection<LayoutEntity> lines = page.GetChildEntities(LayoutEntityType.Line, true);
String startTxt = lines[0].Text.Replace("¶\r\n", "");
//Find the text of first line of page 5
page = layoutDoc.Pages[remove_page];
lines = page.GetChildEntities(LayoutEntityType.Line, true);
String endTxt = lines[0].Text.Replace("¶\r\n", "");
//Find the text of page 4 and 5 and bookmark the content of page 4.
FindAndInsertBookmark start = new FindAndInsertBookmark("bookmark", true);
FindAndInsertBookmark end = new FindAndInsertBookmark("bookmark", false);
doc.Range.Replace(startTxt, "", new FindReplaceOptions() { ReplacingCallback = start });
doc.Range.Replace(endTxt, "", new FindReplaceOptions() { ReplacingCallback = end });
//Remove the content of 4th page
doc.Range.Bookmarks["bookmark"].Text = "";
doc.Save(MyDir + "output.docx");
public class FindAndInsertBookmark : IReplacingCallback
{
string bmname;
Boolean isStart;
DocumentBuilder builder;
public FindAndInsertBookmark(string bmname, Boolean isStart)
{
this.bmname = bmname;
this.isStart = isStart;
}
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
// This is a Run node that contains either the beginning or the complete match.
Node currentNode = e.MatchNode;
if (builder == null)
builder = new DocumentBuilder((Document)currentNode.Document);
// 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);
ArrayList runs = new ArrayList();
// 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(currentNode);
remainingLength = 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(currentNode);
}
if (isStart)
{
Run run = (Run)runs[0];
run.ParentNode.InsertBefore(new BookmarkStart(run.Document, bmname), run);
}
else
{
Run run = (Run)runs[0];
run.ParentNode.InsertBefore(new BookmarkEnd(run.Document, bmname), run);
}
// Signal to the replace engine to do nothing because we have already done all what we wanted.
return ReplaceAction.Skip;
}
/// <summary>
/// Splits text of the specified run into two runs.
/// Inserts the new run just after the specified run.
/// </summary>
private static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring(0, position);
run.ParentNode.InsertAfter(afterRun, run);
return afterRun;
}
}