我创建了一个 word 文档,它使用word.interop生成动态内容。它之间使用了一些分页符。我面临的问题是,此分页符会创建我不想向用户显示的空白页面。
在某些情况下,我需要那些分页符来维护页面布局,所以我不能考虑删除这些分页符。但我想要的是另一种解决方案,例如,如果特定页面中除了分页符之外没有任何内容,请删除该页面。
我怎样才能做到这一点,请帮助..提前谢谢..
我创建了一个 word 文档,它使用word.interop生成动态内容。它之间使用了一些分页符。我面临的问题是,此分页符会创建我不想向用户显示的空白页面。
在某些情况下,我需要那些分页符来维护页面布局,所以我不能考虑删除这些分页符。但我想要的是另一种解决方案,例如,如果特定页面中除了分页符之外没有任何内容,请删除该页面。
我怎样才能做到这一点,请帮助..提前谢谢..
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;
}
这个链接帮助我完成了我的项目
https://code.msdn.microsoft.com/office/How-to-remove-blank-pages-e200755d
Ahm 很简单:只需查看页面,如果唯一的段落仅包含分页符 - 删除页面(或更准确地说:将空页面保存到列表中,然后在故事结尾一步删除):我无法提供代码给你,因为没有示例代码。