none
Delete a particular page from a word document using C# RRS feed

  • Question

  • I want to delete a particular page from a word document. 
    Suppose I have a word document. i want to delete the 3rd page of that document using C#.
    How shall I do it?
    I tried this.. but i can only delete some lines of a page. But not the whole page of a document. As for example, here in my code I have deleted only 5 lines..but I was to delete the whole page. In textbox1, I am taking the value of the page no which I want to delete.

                object missing = System.Reflection.Missing.Value;
                object fileName = filePath;
                object readOnly = false;
                object isVisible = true;
    
                //Start Word and open a document.  
                Word._Application oWord;
                Word._Document oDoc1, oDoc2;
                oWord = new Word.Application();
                oWord.Visible = true;
    
                oDoc1 = oWord.Documents.Open(ref fileName, ref missing, ref readOnly,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref isVisible, ref missing,
                    ref missing, ref missing, ref missing);
    
                //Goto some specific page and insert a blank page or page break  
                int y = 0;
                int.TryParse(textBox1.Text.ToString(), out y);
                object gotoPage = Word.WdGoToItem.wdGoToPage;
                object gotoNext = Word.WdGoToDirection.wdGoToNext;
                object gotoCount = null;
                object gotoName = y;
    
                object unit = Word.WdUnits.wdLine;
               
                //var lineCount =File.ReadLines(filePath).Count();
                //The lines you want to delete... 
                
                        object count = 5;
                        object extend = Word.WdMovementType.wdExtend;
    
                        oWord.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName);
                        oWord.Selection.MoveDown(ref unit, ref count, ref extend);
                        oWord.Selection.TypeBackspace();

    Sunday, July 13, 2014 10:42 AM

Answers

  • Hi,

    To delete a specified page of a Word document with C#, you could resort to Document.GoTo method to make sure the position of the specified page, find its "Page" Bookmark and then delete the range of this bookmark.

    Here is a sample for your reference.

    private void DeleteSpecialPage(Word.Document doc, int i)
    {
        Word.Range rngD = doc.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, 3, Type.Missing);
        rngD.Bookmarks["\\Page"].Range.Delete();
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 14, 2014 8:46 AM
    Moderator

All replies

  • Hi,

    To delete a specified page of a Word document with C#, you could resort to Document.GoTo method to make sure the position of the specified page, find its "Page" Bookmark and then delete the range of this bookmark.

    Here is a sample for your reference.

    private void DeleteSpecialPage(Word.Document doc, int i)
    {
        Word.Range rngD = doc.GoTo(Word.WdGoToItem.wdGoToPage, Word.WdGoToDirection.wdGoToAbsolute, 3, Type.Missing);
        rngD.Bookmarks["\\Page"].Range.Delete();
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 14, 2014 8:46 AM
    Moderator
  • This is how you can delete a particular or multiple pages from a document in C#
    string filePath = @"c:\sample.docx";  
    string filePathOut = @"c:\output\result.docx";  
    RemoveOptions removeOptions = new RemoveOptions(new int[] { 3, 5 }); //3 and 5 are the page numbers 
    using (Merger merger = new Merger(filePath))  
    {  
        merger.RemovePages(removeOptions);  
        merger.Save(filePathOut);  
    }  
    You have to add GroupDocs.Merger for .NET DLL in order to run this piece of code.
    Monday, October 28, 2019 4:46 PM