How to delete a page of a word document programmatically

Last post 01-09-2012, 1:30 AM by saikat123. 8 replies.
Sort Posts: Previous Next
  •  01-03-2012, 4:57 AM 352657

    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?
    Filed under: .Net;Apose.Words
     
  •  01-03-2012, 5:36 AM 352673 in reply to 352657

    Re: How to delete a page of a word document programmatically

    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,

    Awais Hafeez
    Support Developer
    Aspose Sialkot Team
    Aspose - Your File Format Experts

    Keep in touch! We're on Twitter and Facebook
     
  •  01-07-2012, 1:33 AM 353628 in reply to 352673

    Re: How to delete a page of a word document programmatically .NET

    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 .
     
  •  01-07-2012, 2:31 AM 353638 in reply to 353628

    Re: How to delete a page of a word document programmatically

    Hi Saikat,

    Thanks for your inquiry. You can find here as attachment to the Adam's post:

    Extract document content separeted by page breaks: 



    Best Regards,
    Imran Rafique
    Support Developer
    Aspose Sialkot Team
    http://www.aspose.com/
    Aspose - Your File Format Experts
    Keep in touch! We're on Twitter and Facebook
     
  •  01-07-2012, 2:45 AM 353642 in reply to 353628

    Re: How to delete a page of a word document programmatically

    Attachment: Present (inaccessible)
    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
     
  •  01-09-2012, 12:18 AM 353724 in reply to 353642

    Re: How to delete a page of a word document programmatically

    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 ?

     
  •  01-09-2012, 12:53 AM 353731 in reply to 353724

    Re: How to delete a page of a word document programmatically

    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:

    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
     
  •  01-09-2012, 12:56 AM 353732 in reply to 353724

    Re: How to delete a page of a word document programmatically

    Hi Saikat,

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


    Best Regards,
    Imran Rafique
    Support Developer
    Aspose Sialkot Team
    http://www.aspose.com/
    Aspose - Your File Format Experts
    Keep in touch! We're on Twitter and Facebook
     
  •  01-09-2012, 1:30 AM 353738 in reply to 353731

    Re: How to delete a page of a word document programmatically

    Thank You very much.

    It is working properly :)

     
View as RSS news feed in XML