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..
Ahm非常简单:只需查看页面,如果其中唯一的段落仅包含一个分页符-删除页面(或更准确地说:将空页面保存到列表中,然后在故事的最后一步删除):我无法提供代码,因为没有示例代码。
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.