某些部分的OpenXML Landscape

时间:2019-01-22 18:50:29

标签: c# openxml wordprocessingml

如何将某些部分(我应该使用哪种类型的部分)设置为横向或纵向?

我试图创建具有以下部分属性的部分(请参见下面的代码),然后将该部分分别设置为横向或纵向。但是,当我使用此代码并创建中断段落时,该代码会在横向中生成空白页面。

    public static SectionProperties PageOrientationPortrait()
    {
        SectionProperties sectionProperties2 = new SectionProperties();
        PageSize pageSize = new PageSize()
        {
            Width = (UInt32Value)12240U,
            Height = (UInt32Value)15840U,
            Orient = PageOrientationValues.Portrait
        };
        PageMargin pageMargin = new PageMargin()
        {
            Top = 1440,
            Right = (UInt32Value)1440U,
            Bottom = 1440,
            Left = (UInt32Value)1440U,
            Header = (UInt32Value)720U,
            Footer = (UInt32Value)720U,
            Gutter = (UInt32Value)0U
        };
        Columns columns = new Columns() { Space = "720" };
        DocGrid docGrid = new DocGrid() { LinePitch = 360 };
        sectionProperties2.Append(pageSize, pageMargin, columns, docGrid);
        return sectionProperties2;
    }
    public static SectionProperties PageOrientationLandScape()
    {
        SectionProperties sectionProperties = new SectionProperties();
        PageSize pageSize = new PageSize()
        {
            Width = (UInt32Value)15840U,
            Height = (UInt32Value)12240U,
            Orient = PageOrientationValues.Landscape
        };
        PageMargin pageMargin = new PageMargin()
        {
            Top = 1440,
            Right = (UInt32Value)1440U,
            Bottom = 1440,
            Left = (UInt32Value)1440U,
            Header = (UInt32Value)720U,
            Footer = (UInt32Value)720U,
            Gutter = (UInt32Value)0U
        };
        Columns columns = new Columns() { Space = "720" };
        DocGrid docGrid = new DocGrid() { LinePitch = 360 };
        sectionProperties.Append(pageSize, pageMargin, columns, docGrid);
        return sectionProperties;
    }


    public static Paragraph GenerateSectionBreakParagraph()
    {
        Paragraph paragraph232 = new Paragraph();

        ParagraphProperties paragraphProperties220 = new ParagraphProperties();

        SectionProperties sectionProperties1 = new SectionProperties();
        SectionType sectionType1 = new SectionType() { Val = SectionMarkValues.NextPage };

        sectionProperties1.Append(sectionType1);

        paragraphProperties220.Append(sectionProperties1);

        paragraph232.Append(paragraphProperties220);

        return paragraph232;
    }

1 个答案:

答案 0 :(得分:0)

当您希望在WordProcessing文档中创建标准部分时,首先需要创建一个空的Paragraph元素和一个空的ParagraphProperties元素。然后,您可以创建具有所需属性的SectionProperties元素,如下所示:

    /// <summary>
    /// Will create a section properties
    /// </summary>
    /// <param name="orientation">The wanted orientation (landscape or portrai)</param>
    /// <returns>A section properties element</returns>
    public static SectionProperties CreateSectionProperties(PageOrientationValues orientation)
    {
        // create the section properties
        SectionProperties properties = new SectionProperties();
        // create the height and width
        UInt32Value height = orientation == (PageOrientationValues.Portrait) ? 16839U : 11907U;
        UInt32Value width = orientation != (PageOrientationValues.Portrait) ? 16839U : 11907U;
        // create the page size and insert the wanted orientation
        PageSize pageSize = new PageSize()
        {
            Width = width,
            Height = height,
            Code = (UInt16Value)9U,
            // insert the orientation
            Orient = orientation };
        // create the page margin
        PageMargin pageMargin = new PageMargin()
        {
            Top = 1417,
            Right = (UInt32Value)1417U,
            Bottom = 1417,
            Left = (UInt32Value)1417U,
            Header = (UInt32Value)708U,
            Footer = (UInt32Value)708U,
            Gutter = (UInt32Value)0U
        };
        Columns columns = new Columns() { Space = "720" };
        DocGrid docGrid = new DocGrid() { LinePitch = 360 };
        // appen the page size and margin
        properties.Append(pageSize, pageMargin, columns, docGrid);
        return properties;
    }

在创建具有特定方向的截面属性元素时,重要的是相应地调整PageSize元素的高度和宽度。否则,该部分中的页面将无法正确显示(如果您使用纵向和纵向显示横向部分,则该部分将显示为纵向)。

完成创建sectionProperty之后,我们要做的就是将SectionProperty附加到空的ParagraphProperty上,然后将ParagraphProperties附加到该段落上。

使用此功能,我们可以使用以下控制台程序创建一个具有不同方向部分的Word文档:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;

namespace ChangeDocVariable
{
    class Program
    {
        static void Main(string[] args)
        {
            using(WordprocessingDocument doc = WordprocessingDocument.Create(@"path to document", WordprocessingDocumentType.Document))
            {
                // Create the maindocument part
                MainDocumentPart maindDocomentPart = doc.AddMainDocumentPart();
                // add the document
                Document document = maindDocomentPart.Document = new Document();
                // add the bdoy
                Body body = document.Body = new Body();

                // insert a set of sections
                // To insert a section we need to add a paragraph
                // which contains paragaph properties
                // which holds the section properties
                Paragraph firstSection = new Paragraph();
                ParagraphProperties firstSectionProperties = new ParagraphProperties();
                firstSectionProperties.Append(CreateSectionProperties(PageOrientationValues.Portrait));
                firstSection.Append(firstSectionProperties);

                Paragraph secondSection = new Paragraph();
                ParagraphProperties secondSectionProperties = new ParagraphProperties();
                secondSectionProperties.Append(CreateSectionProperties(PageOrientationValues.Landscape));
                secondSection.Append(secondSectionProperties);

                Paragraph thirdSection = new Paragraph();
                ParagraphProperties thirdSectionProperties = new ParagraphProperties();
                thirdSectionProperties.Append(CreateSectionProperties(PageOrientationValues.Portrait));
                thirdSection.Append(thirdSectionProperties);

                body.Append(firstSection, secondSection, thirdSection);

                // for the last section we can directly add a section properties
                body.Append(CreateSectionProperties(PageOrientationValues.Landscape));
            }
        }

        /// <summary>
        /// Will create a section properties
        /// </summary>
        /// <param name="orientation">The wanted orientation (landscape or portrai)</param>
        /// <returns>A section properties element</returns>
        public static SectionProperties CreateSectionProperties(PageOrientationValues orientation)
        {
            // create the section properties
            SectionProperties properties = new SectionProperties();
            // create the height and width
            UInt32Value height = orientation == (PageOrientationValues.Portrait) ? 16839U : 11907U;
            UInt32Value width = orientation != (PageOrientationValues.Portrait) ? 16839U : 11907U;
            // create the page size and insert the wanted orientation
            PageSize pageSize = new PageSize()
            {
                Width = width,
                Height = height,
                Code = (UInt16Value)9U,
                // insert the orientation
                Orient = orientation };
            // create the page margin
            PageMargin pageMargin = new PageMargin()
            {
                Top = 1417,
                Right = (UInt32Value)1417U,
                Bottom = 1417,
                Left = (UInt32Value)1417U,
                Header = (UInt32Value)708U,
                Footer = (UInt32Value)708U,
                Gutter = (UInt32Value)0U
            };
            Columns columns = new Columns() { Space = "720" };
            DocGrid docGrid = new DocGrid() { LinePitch = 360 };
            // appen the page size and margin
            properties.Append(pageSize, pageMargin, columns, docGrid);
            return properties;
        }
    }
}

这将创建一个包含四个部分的文档。可以将最后一个SectionProrties直接添加到文档本身。