1
Reply

Delete a particular page from a word document using C#

Jaideep Chandra

Jaideep Chandra

Jul 13 2014 6:38 AM
736
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();
 

Answers (1)