通过C#从word文档中删除页面 [英] Delete a page from a word document by C#

查看:118
本文介绍了通过C#从word文档中删除页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除word文档中的任何页面。有什么办法吗?

我只能从页面中删除一些行。有没有办法计算页面中的行数?然后我可以删除整个页面..或者如果有任何其他方式删除页面,请帮助我。下面我给出了我已经完成的代码..它只能从页面中删除一些行。

在textbox1中,我取的是我要删除的页面号的值。

I want to delete any page from a word document. Is there any way to do it?
I can only delete some lines from the page. Is there any way to count the no of lines from the page? Then I can delete the whole page.. Or if there any other way of deleting the page, please help me then. Below I am giving the code that I have done.. which can only delete some lines from a page.
In textbox1, I am taking the value of the page no, which I want to delete.

private void button1_Click(object sender, EventArgs e)
        {
            string filePath = null;
            //First we need to create a new OpenFileDialog:
            OpenFileDialog fDialog = new OpenFileDialog();

            //The title of the OpenFileDialog window:
            fDialog.Title = "Open text/word File";

            //Here we allow to be opened only files with the .txt and .doc extension:
            fDialog.Filter = "Text Files|*.txt|Doc Files|*.docx";

            //we set the InitialDirectory property
            fDialog.InitialDirectory = @"C:\";

            if (fDialog.ShowDialog() == DialogResult.OK)
            {

                //MessageBox.Show(fDialog.FileName.ToString());
                filePath = fDialog.FileName.ToString();
            }

            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);

              
            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 = 1;
                    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();
              
        }

推荐答案

我们已经在您之前的问题中向您解释过没有这样的事情word文档中的页面。页面是任意行集合,仅当用户定义他们希望查看文档的显示设备的形状和大小时才存在。在实际文档中,page没有任何意义。
We have already explained to you in your previous questions that there is no such thing as a page in a word document. A page is an arbitrary collection of lines, that only exists when the user defines the shape and size of the dispaly device that they wish to view the document on. Within the actual document, "page" has no meaning.


正如我在问题评论中提到的那样,请不要重新发布!



这是您之前的问题和答案:使用C#从word文档中删除特定页面[ ^ ]
As i mentioned in the comment to the question, please do not repost!

Here is your previous question and answers: Delete a particular page from a word document using C#[^]


这篇关于通过C#从word文档中删除页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆