How to delete a page of a word document programmatically

I am unable to delete a page from a section of a word document programmatically.Is it possible in aspose?

Hi Saikat,

Thanks for your inquiry. Please note that MS Word document is flow document and does not contain any information about its layout into lines and pages. So unfortunately, there is no way to delete a particular page from a document. However, if each page in your document is a separate section, then you can just remove the section e.g: doc.Sections[4].Remove();

Please let me know if I can be of any further assistance.

Best Regards,

Is it possible to remove all the content of the page until first page break occurs in word document Programmatically?

In this way i want to delete first page .

Hi Saikat,

Thanks for your inquiry. You can find here as attachment to the Adam’s post:
https://docs.aspose.com/words/java/working-with-watermark/

Extract document content separeted by page breaks:
https://blog.aspose.com/2007/04/16/extract-word-document-content-separated-by-page-breaks-using-csharp/

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,

Thanks for your Response.

This code has issue in

if (run.Text.Contains(ControlChar.PageBreak)) statement as it shows error

“String doesn’t contain definition for contains”

Is there any alternate solution for this statement ?

Hi,

Thanks for your inquiry. Most likely, this error occurs because you are using .net framework 1.1. As String.Contains method was added in .Net Framework 2.0, I would suggest you update your compiler to VisualStudio 2005 or higher. Alternatively, if upgrading isn’t an option you can also make use of String.IndexOf method:
https://docs.microsoft.com/en-us/dotnet/api/system.string.indexof

I hope, this will help.

Best Regards,

Hi Saikat,

Thanks for your inquiry. Your document may don’t have page break section, please share document for more details.

Thank You very much.

It is working properly :slight_smile:

The issues you have found earlier (filed as WORDSNET-2978) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(46)