Blank page removal in a word document using microsoft word.interop

吃可爱长大的小学妹 提交于 2019-12-22 13:50:15

问题


I have created a word document which generates dynamic contents using word.interop. It has some page breaks used in between. what is the problem which I am facing is, this page breaks creates blank pages which I don't want to show to users.

In some cases I need those page breaks there in order to maintain the page layout, so I can't think about removing those page breaks. but what I want is another solution like, in case if there no contents in a particular page other than a page break, remove that page.

How can I accomplish this, please help.. Thanks in advance..


回答1:


Ahm quite easy: just look on the page and if the only paragraph there contains only a pagebreak - delete the page (or more accurate: save the empty pages to a list an delete in one stepat the end of the story): I cannot provide code for you, because there is no sample code.




回答2:


    private bool RemoveBlankPage()
    {
        Word.Application wordapp = null;
        Word.Document doc = null;
        Word.Paragraphs paragraphs=null;
        try
        {
            // Start Word APllication and set it be invisible
            wordapp = new Word.Application();
            wordapp.Visible = false;
            doc = wordapp.Documents.Open(wordPath);
            paragraphs = doc.Paragraphs;
            foreach (Word.Paragraph paragraph in paragraphs)
            {
                if (paragraph.Range.Text.Trim() == string.Empty)
                {
                    paragraph.Range.Select();
                    wordapp.Selection.Delete();
                }
            }

            // Save the document and close document
            doc.Save();
            ((Word._Document)doc).Close();

            // Quit the word application
            ((Word._Application)wordapp).Quit();

        }
        catch(Exception ex)
        {
            MessageBox.Show("Exception Occur, error message is: "+ex.Message);
            return false;
        }
        finally
        { 
            // Clean up the unmanaged Word COM resources by explicitly
            // call Marshal.FinalReleaseComObject on all accessor objects
            if (paragraphs != null)
            {
                Marshal.FinalReleaseComObject(paragraphs);
                paragraphs = null;
            }
            if (doc != null)
            {
                Marshal.FinalReleaseComObject(doc);
                doc = null;
            }
            if (wordapp != null)
            {
                Marshal.FinalReleaseComObject(wordapp);
                wordapp = null;
            }
        }

        return true;
    }

This link helped me complete my project

https://code.msdn.microsoft.com/office/How-to-remove-blank-pages-e200755d



来源:https://stackoverflow.com/questions/16710782/blank-page-removal-in-a-word-document-using-microsoft-word-interop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!