有没有一种方法可以使用interop.word避免带有分页符的空白页?

时间:2019-01-23 12:12:23

标签: c# office-interop

我已被分配去制作一个生成多页文档的C#应用​​程序。我一直在使用interop.word来生成我的页面。虽然页面生成可以,但是有时候我会得到一个空白的页面,在生成过程中似乎无法摆脱它。

我的页面长度不同;有些页面的页面充满文字,而有些页面的四分之一充满文字。 如何做到这一点,以免在生成的文档中出现不必要的空白页?

我尝试使用下面的函数。它所做的只是插入一个分页符。

    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: enter image description here

图片2: enter image description here

1 个答案:

答案 0 :(得分:0)

感谢Sinatr,我已经解决了我的问题。我忘了在生成新页面的过程中添加一个段落,导致页面为空。我自己的愚蠢让我惊讶...