c#interop Word插入横向页面

问题描述 投票:1回答:1

我正在尝试将横向页面插入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();
c# ms-word office-interop landscape
1个回答
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();
© www.soinside.com 2019 - 2024. All rights reserved.