C#Interop Word插入横向页面

时间:2019-03-11 10:38:59

标签: c# ms-word office-interop landscape

我正在尝试将横向页面插入Word文档。但这会将所有页面更改为横向。如何在Word文档中仅插入一个横向页面?

我的代码:

object wdSectionBreakNextPage = 2;
        //object pageBreak = 7; //el 7 es el tipo. https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.word.wdbreaktype?view=word-pia
        rngDoc.Select();
        //rngDoc.InsertBreak(pageBreak);
        rngDoc.InsertBreak(wdSectionBreakNextPage);
        rngDoc.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
        rngDoc.PageSetup.DifferentFirstPageHeaderFooter = 0;
        Microsoft.Office.Interop.Word.Paragraph para = rngDoc.Paragraphs.Add();
        para.Range.InsertParagraphAfter();

1 个答案:

答案 0 :(得分:0)

插入分节符是一个很好的第一步-这是必需的。

问题出在这行,我认为正在指导Word将页面方向应用于整个文档。 (您实际上没有指定分配给rngDoc的内容,但是您描述的行为表明它是整个文档。)

rngDoc.PageSetup.Orientation = WdOrientation.wdOrientLandscape;

如果仅将其应用于新部分,则需要指定该部分。同样,我们不知道rngDoc.Select()实际在做什么,因为我们没有上下文。无论如何,选择都是无关紧要的,并且不需要该行。遵循以下原则:

object objWdSectionBreakNextPage = 2; //Don't confuse with the actual enum name!
//get the index number of the section where rngDoc is located
int nrSections = rngDoc.Sections[1].Index; 
rngDoc.InsertBreak(objWdSectionBreakNextPage);
//set rngDoc to the new section
rngDoc = Doc.Sections[nrSections + 1].Range;
rngDoc.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
rngDoc.PageSetup.DifferentFirstPageHeaderFooter = 0;
Microsoft.Office.Interop.Word.Paragraph para = rngDoc.Paragraphs.Add();
para.Range.InsertParagraphAfter();