C# 有没有办法使用interop.word避免出现分页符的空白页?
我被指派制作一个生成多页文档的C应用程序。我一直在使用interop.word生成我的页面。虽然页面生成是可以的,但有时我会得到一个在生成过程中似乎无法摆脱的空白页面 我的书页长短不一;有些人的页面充满了文字,有些人的页面四分之一充满了文字。 我怎样才能使生成的文档中不会出现不需要的空白页 我试过使用下面的函数。它所做的只是插入一个分页符C# 有没有办法使用interop.word避免出现分页符的空白页?,c#,office-interop,C#,Office Interop,我被指派制作一个生成多页文档的C应用程序。我一直在使用interop.word生成我的页面。虽然页面生成是可以的,但有时我会得到一个在生成过程中似乎无法摆脱的空白页面 我的书页长短不一;有些人的页面充满了文字,有些人的页面四分之一充满了文字。 我怎样才能使生成的文档中不会出现不需要的空白页 我试过使用下面的函数。它所做的只是插入一个分页符 private void newPage(Word._Document doc) { doc.Words.Last.Inser
private void newPage(Word._Document doc)
{
doc.Words.Last.InsertBreak(Word.WdBreakType.wdPageBreak);
}
生成新页面的示例如下:
else if (iter == "Bedrijfsinformatie")
{
createParagraphIntro(oDoc, oMissing, paragraph, 16, "Cambria", 0, 0, Word.WdParagraphAlignment.wdAlignParagraphLeft, true, false, true);
createParagraphIntro(oDoc, oMissing, paragraph1, 9, "Cambria", 1, 0, Word.WdParagraphAlignment.wdAlignParagraphLeft, false, false, true);
createParagraphIntro(oDoc, oMissing, paragraph2, 9, "Cambria", 1, 0, Word.WdParagraphAlignment.wdAlignParagraphLeft, false, false, true);
createParagraphIntro(oDoc, oMissing, paragraph3, 9, "Cambria", 1, 0, Word.WdParagraphAlignment.wdAlignParagraphLeft, false, false, true);
createParagraphIntro(oDoc, oMissing, paragraph4, 9, "Cambria", 1, 0, Word.WdParagraphAlignment.wdAlignParagraphLeft, false, false, true);
createParagraphIntro(oDoc, oMissing, paragraph5, 9, "Cambria", 1, 0, Word.WdParagraphAlignment.wdAlignParagraphLeft, false, false, true);
createParagraphIntro(oDoc, oMissing, paragraph6, 9, "Cambria", 1, 0, Word.WdParagraphAlignment.wdAlignParagraphLeft, false, false, true);
createParagraphIntro(oDoc, oMissing, paragraph7, 9, "Cambria", 6, 0, Word.WdParagraphAlignment.wdAlignParagraphLeft, false, false, true);
//newPage(oDoc);
}
以及createParagraphIntro方法:
private void createParagraphIntro(Word._Document oDoc, object oMissing, string paragraphcontent, int fontsize, string fontname, int SpaceAfter,int spaceBefore, Word.WdParagraphAlignment alignment, bool bold, bool header,bool insertparagraph)
{
Word.Paragraph oPara;
oPara = oDoc.Content.Paragraphs.Add(ref oMissing);
object oStyleName = Word.WdBuiltinStyle.wdStyleHeading1;
object oStyleName1 = Word.WdBuiltinStyle.wdStyleNormal;
// to set the heading
if (header == true)
oPara.Range.set_Style(ref oStyleName);
else
oPara.Range.set_Style(ref oStyleName1);
// to make the text bold
if (bold == true)
oPara.Range.Font.Bold = 1;
else
oPara.Range.Font.Bold = 0;
oPara.Range.Text = paragraphcontent;
oPara.Format.SpaceBefore = spaceBefore * 10;
oPara.Format.SpaceAfter = SpaceAfter*10; // spacing after paragraph.
oPara.Range.Font.Size = fontsize;
oPara.Range.Font.Name = fontname;
oPara.Range.ParagraphFormat.Alignment = alignment;
oPara.Range.InsertParagraphAfter();
}
我希望我的应用程序生成一个没有空白页的word文档。目前情况并非如此
我希望任何人都能帮我解决这个问题
编辑:
别介意语言,我是荷兰人
图1:
图2:
多亏了Sinatr,我解决了我的问题。我忘了在生成新页面时添加段落,导致页面为空。我自己的愚蠢让我吃惊…你能打开生成的文档并调查为什么会有空白页吗?怎么了?是不是多了两行分页符?是否是带有分页符的空段落尝试将分页符添加到非空段落中?我已仔细检查,并且没有双分页符。每一页填好后,我称之为分页符。再说一遍,为什么会有空白页?这些页面的内容是什么样子的?在word中打开它,启用特殊字符,观察,查看上一页,查看下一页,猜测。。作为一种解决方法,你可以在事后将其视为后处理。第一个图像表示文档,如您所见,第三个页面是空的,您可以看到分页符,而我的代码中没有。生成代码如图2所示。对于页面Prijslijst,您没有向页面添加任何内容,也许您忘记了?还在给newPage打电话。为什么?仅当页面上有内容时才调用newPage。