Esempio n. 1
0
        /// <summary>
        /// set section pagesize orientation
        /// </summary>
        public static void SetOrientation(this SectionProperties section, PageOrientationValues orientation)
        {
            var pageSize = section.Descendants <PageSize>().FirstOrDefault();

            if (pageSize == null)
            {
                throw new Exception($"must set paper first");
            }
            if (pageSize.Orient.Value != orientation)
            {
                var w = pageSize.Width;
                var h = pageSize.Height;
                pageSize.Width  = h;
                pageSize.Height = w;
                pageSize.Orient = new EnumValue <PageOrientationValues>(orientation);

                var margin = section.Descendants <PageMargin>().FirstOrDefault();
                if (margin != null)
                {
                    var left   = margin.Left.Value;
                    var top    = margin.Top.Value;
                    var right  = margin.Right.Value;
                    var bottom = margin.Bottom.Value;

                    margin.Top    = (int)left;
                    margin.Bottom = (int)right;
                    margin.Left   = (uint)Max(0, bottom);
                    margin.Right  = (uint)Max(0, top);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Generate the required OpenXml element for handling page orientation.
        /// </summary>
        private static SectionProperties ChangePageOrientation(PageOrientationValues orientation)
        {
            PageSize pageSize = new PageSize()
            {
                Width = (UInt32Value)16838U, Height = (UInt32Value)11906U
            };

            if (orientation == PageOrientationValues.Portrait)
            {
                UInt32Value swap = pageSize.Width;
                pageSize.Width  = pageSize.Height;
                pageSize.Height = swap;
            }
            else
            {
                pageSize.Orient = orientation;
            }

            return(new SectionProperties(
                       pageSize,
                       new PageMargin()
            {
                Top = 1417, Right = (UInt32Value)1417U, Bottom = 1417, Left = (UInt32Value)1417U,
                Header = (UInt32Value)708U, Footer = (UInt32Value)708U, Gutter = (UInt32Value)0U
            },
                       new Columns()
            {
                Space = "708"
            },
                       new DocGrid()
            {
                LinePitch = 360
            }
                       ));
        }
Esempio n. 3
0
        /// <summary>
        /// create new empty doc
        /// </summary>
        public static void Create(string pathfilename,
                                  PaperType paperType = PaperType.A4,
                                  PageOrientationValues orientation = PageOrientationValues.Portrait,
                                  double marginLeftMM   = 0,
                                  double marginTopMM    = 0,
                                  double marginRightMM  = 0,
                                  double marginBottomMM = 0)
        {
            using (var doc = WordprocessingDocument.Create(pathfilename, WordprocessingDocumentType.Document))
            {
                var main = doc.AddMainDocumentPart();
                main.Document = new Document();

                var body = main.Document.AppendChild(new Body());

                /*var p = body.AppendChild(new Paragraph());
                 * var r = p.AppendChild(new Run());
                 * r.AppendChild(new Text(DateTime.Now.ToString()));*/

                var sect = body.AppendChild(new SectionProperties());
                sect.SetPaper(paperType);
                sect.SetOrientation(orientation);
                sect.SetMargin(marginLeftMM, marginTopMM, marginRightMM, marginBottomMM);

                doc.Save();
            }
        }
Esempio n. 4
0
 private void PageColumnsSetting(PageSizeValues pageSizeValue, PageOrientationValues pageOrientationValue)
 {
     // 当页面大小为A3、横向时,自动分栏
     if (pageSizeValue.Equals(PageSizeValues.A3) && pageOrientationValue.Equals(PageOrientationValues.Portrait))
     {
         this.columns = new GenerateColumns(false, 2).Create(
             new GenerateColumn("10220", "1292.5").Create(),//10586
             new GenerateColumn("10220").Create()
             );
     }
 }
Esempio n. 5
0
        private void PageSetting(PageSizeValues pageSizeValue, PageOrientationValues pageOrientationValue)
        {
            UInt32Value width  = 11906U;
            UInt32Value height = 16838U;

            UInt32Value left  = 1080U;
            UInt32Value right = 1080U;

            int top    = 1080;
            int bottom = 1080;


            switch (pageSizeValue)
            {
            case PageSizeValues.A4:
                width  = 11906U;
                height = 16838U;
                break;

            case PageSizeValues.A3:
                width  = 16783U;
                height = 23757U;
                break;
            }

            switch (pageOrientationValue)
            {
            case PageOrientationValues.Landscape:
                break;

            case PageOrientationValues.Portrait:
                UInt32Value sweep = width;
                width  = height;
                height = sweep;

                int top_sweep = top;
                top  = (int)left.Value;
                left = (uint)top_sweep;
                break;
            }

            pageSize.Width  = width;
            pageSize.Height = height;
            pageSize.Orient = pageOrientationValue;

            pageMargin.Top    = top;
            pageMargin.Bottom = bottom;
            pageMargin.Left   = left;
            pageMargin.Right  = right;
            pageMargin.Header = (UInt32Value)720U;
            pageMargin.Footer = (UInt32Value)720U;
            pageMargin.Gutter = (UInt32Value)0U;
        }
Esempio n. 6
0
    /// <summary>
    /// 設定紙張方向,需先設定紙張大小,否則無作用
    /// </summary>
    /// <param name="newOrientation">Landscape:橫向;Portrait:直向</param>
    protected OpenXmlHelper SetPageOrientation(PageOrientationValues newOrientation)
    {
        var sections = outDoc.MainDocumentPart.Document.Descendants <SectionProperties>();

        foreach (SectionProperties sectPr in sections)
        {
            bool pageOrientationChanged = false;

            PageSize pgSz = sectPr.Descendants <PageSize>().FirstOrDefault();
            if (pgSz != null)
            {
                if (pgSz.Orient == null)
                {
                    if (newOrientation != PageOrientationValues.Portrait)
                    {
                        pgSz.Orient            = new EnumValue <PageOrientationValues>(newOrientation);
                        pageOrientationChanged = true;
                    }
                }
                else
                {
                    if (pgSz.Orient.Value != newOrientation)
                    {
                        pgSz.Orient.Value      = newOrientation;
                        pageOrientationChanged = true;
                    }
                }
                if (pageOrientationChanged)
                {
                    var width  = pgSz.Width;
                    var height = pgSz.Height;
                    pgSz.Width  = height;
                    pgSz.Height = width;

                    PageMargin pgMar = sectPr.Descendants <PageMargin>().FirstOrDefault();
                    if (pgMar != null)
                    {
                        var top    = pgMar.Top.Value;
                        var bottom = pgMar.Bottom.Value;
                        var left   = pgMar.Left.Value;
                        var right  = pgMar.Right.Value;

                        pgMar.Top    = new Int32Value((int)left);
                        pgMar.Bottom = new Int32Value((int)right);
                        pgMar.Left   = new UInt32Value((uint)Math.Max(0, bottom));
                        pgMar.Right  = new UInt32Value((uint)Math.Max(0, top));
                    }
                }
            }
        }
        return(this);
    }
Esempio n. 7
0
        // Creates an SectionProperties instance and adds its children.
        public SectionProperties Create(PageSizeValues pageSizeValue, PageOrientationValues pageOrientationValue)
        {
            this.PageSetting(pageSizeValue, pageOrientationValue);
            this.PageColumnsSetting(pageSizeValue, pageOrientationValue);

            SectionProperties sectionProperties = new SectionProperties();

            sectionProperties.Append(
                pageSize,
                pageMargin,
                new GeneratePageBorders().Create(),
                columns,
                docGrid
                );

            return(sectionProperties);
        }
Esempio n. 8
0
        public static bool Compare(this PageSize pageSize, PageOrientationValues orientation)
        {
            PageOrientationValues pageOrientation;

            if (pageSize.Orient != null)
            {
                pageOrientation = pageSize.Orient.Value;
            }
            else if (pageSize.Width > pageSize.Height)
            {
                pageOrientation = PageOrientationValues.Landscape;
            }
            else
            {
                pageOrientation = PageOrientationValues.Portrait;
            }

            return(pageOrientation == orientation);
        }
Esempio n. 9
0
        /// <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);
        }
Esempio n. 10
0
        public void ObjectiveItem(string[] itemList, int columnNum, int itemNo, PageOrientationValues orientation)
        {
            //string[] itemList = { "A", "B", "C", "D", "E" };//答案选项
            //int columnNum = 6;//题目数
            int rowNum = columnNum % 9 > 0 ? (columnNum / 9) + 1 : columnNum / 9;
            //int itemNo = 1;//题号起始

            int         columnWidth = 1020; //单元格宽度
            UInt32Value rowHeight   = 340;  //单元格高度
            StringValue fontSize    = "18";



            Table table      = new Table();
            int   tableWidth = columnWidth * columnNum;

            switch (orientation)
            {
            case PageOrientationValues.Landscape:    //风景模式、横向

                columnNum  = itemList.Length + 1;
                tableWidth = columnWidth * columnNum;
                for (int i = 0; i < columnNum; i++)
                {
                    table.Append(new GenerateTableExamineeNoFrame().CreateRowObjectiveLandscape(itemNo, itemList, tableWidth, columnNum, rowHeight, fontSize));
                    itemNo++;
                }
                break;

            case PageOrientationValues.Portrait:    //肖像模式、竖向
                for (int i = 0; i < rowNum; i++)
                {
                    int colNum = 0;
                    if (columnNum > 9)
                    {
                        colNum = 9;
                    }
                    else
                    {
                        colNum = columnNum;
                    }

                    table.Append(new GenerateTableExamineeNoFrame().CreateRowObjectiveTitle(itemNo, tableWidth, colNum, rowHeight, fontSize));

                    foreach (string item in itemList)
                    {
                        table.Append(new GenerateTableExamineeNoFrame().CreateRowObjectiveItem(item, tableWidth, colNum, rowHeight, fontSize, JustificationValues.Center));
                    }
                    itemNo    += 9;
                    columnNum -= 9;
                }
                break;
            }

            table.Append(
                new GenerateTableProperties().Create(tableWidth),
                new GenerateTableGrids().Create(tableWidth, columnNum)
                );

            this.body.Append(new GenerateBreakLine().Create());
            this.body.Append(table);
            this.body.Append(new GenerateBreakLine().Create());
        }
Esempio n. 11
0
 /// <summary>
 /// 页面设置
 /// </summary>
 /// <param name="pageSizeValue"></param>
 /// <param name="pageOrientationValu"></param>
 public void SectionProperties(PageSizeValues pageSizeValue, PageOrientationValues pageOrientationValu)
 {
     this.body.Append(new GenerateSectionProperties().Create(pageSizeValue, pageOrientationValu));
 }
        /// <summary>
        /// Given a document name, set the print orientation for all the sections of the document.
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="newOrientation"></param>
        public static void SetPrintOrientation(string fileName, PageOrientationValues newOrientation)
        {
            using (var document = WordprocessingDocument.Open(fileName, true))
            {
                bool documentChanged = false;

                var docPart  = document.MainDocumentPart;
                var sections = docPart.Document.Descendants <SectionProperties>();

                foreach (SectionProperties sectPr in sections)
                {
                    bool pageOrientationChanged = false;

                    PageSize pgSz = sectPr.Descendants <PageSize>().FirstOrDefault();
                    if (pgSz != null)
                    {
                        // No Orient property? Create it now. Otherwise, just
                        // set its value. Assume that the default orientation
                        // is Portrait.
                        if (pgSz.Orient == null)
                        {
                            // Need to create the attribute. You do not need to
                            // create the Orient property if the property does not
                            // already exist, and you are setting it to Portrait.
                            // That is the default value.
                            if (newOrientation != PageOrientationValues.Portrait)
                            {
                                pageOrientationChanged = true;
                                documentChanged        = true;
                                pgSz.Orient            = new EnumValue <PageOrientationValues>(newOrientation);
                            }
                        }
                        else
                        {
                            // The Orient property exists, but its value
                            // is different than the new value.
                            if (pgSz.Orient.Value != newOrientation)
                            {
                                pgSz.Orient.Value      = newOrientation;
                                pageOrientationChanged = true;
                                documentChanged        = true;
                            }
                        }

                        if (pageOrientationChanged)
                        {
                            // Changing the orientation is not enough. You must also
                            // change the page size.
                            var width  = pgSz.Width;
                            var height = pgSz.Height;
                            pgSz.Width  = height;
                            pgSz.Height = width;

                            PageMargin pgMar = sectPr.Descendants <PageMargin>().FirstOrDefault();
                            if (pgMar != null)
                            {
                                // Rotate margins. Printer settings control how far you
                                // rotate when switching to landscape mode. Not having those
                                // settings, this code rotates 90 degrees. You could easily
                                // modify this behavior, or make it a parameter for the
                                // procedure.
                                var top    = pgMar.Top.Value;
                                var bottom = pgMar.Bottom.Value;
                                var left   = pgMar.Left.Value;
                                var right  = pgMar.Right.Value;

                                pgMar.Top    = new Int32Value((int)left);
                                pgMar.Bottom = new Int32Value((int)right);
                                pgMar.Left   = new UInt32Value((uint)Math.Max(0, bottom));
                                pgMar.Right  = new UInt32Value((uint)Math.Max(0, top));
                            }
                        }
                    }
                }

                if (documentChanged)
                {
                    docPart.Document.Save();
                }
            }
        }
Esempio n. 13
0
 public static DocumentFormat.OpenXml.Wordprocessing.PageOrientationValues ToOOxml(this PageOrientationValues value)
 {
     return((DocumentFormat.OpenXml.Wordprocessing.PageOrientationValues)(int) value);
 }
Esempio n. 14
0
        internal DocX SetOrientation(PageOrientation value)
        {
            bool documentChanged = false;
            PageOrientationValues orientationValue = Convert.ToPageOrientationValues(value);

            var sectionProperty = GetBodySectionProperty();

            bool pageOrientationChanged = false;

            PageSize pageSize = sectionProperty.GetOrCreate <PageSize>();

            // No Orient property? Create it now. Otherwise, just
            // set its value. Assume that the default orientation
            // is Portrait.
            if (pageSize.Orient == null)
            {
                // Need to create the attribute. You do not need to
                // create the Orient property if the property does not
                // already exist, and you are setting it to Portrait.
                // That is the default value.
                if (value != PageOrientation.Portrait)
                {
                    pageOrientationChanged = true;
                    documentChanged        = true;
                    pageSize.Orient        = new EnumValue <PageOrientationValues>(orientationValue);
                }
            }
            else
            {
                // The Orient property exists, but its value
                // is different than the new value.
                if (pageSize.Orient.Value != orientationValue)
                {
                    pageSize.Orient.Value  = orientationValue;
                    pageOrientationChanged = true;
                    documentChanged        = true;
                }
            }

            if (pageOrientationChanged)
            {
                // Changing the orientation is not enough. You must also
                // change the page size.
                var width  = pageSize.Width;
                var height = pageSize.Height;
                pageSize.Width  = height;
                pageSize.Height = width;

                PageMargin pageMargin = sectionProperty.Descendants <PageMargin>().FirstOrDefault();

                if (pageMargin != null)
                {
                    // Rotate margins. Printer settings control how far you
                    // rotate when switching to landscape mode. Not having those
                    // settings, this code rotates 90 degrees. You could easily
                    // modify this behavior, or make it a parameter for the
                    // procedure.
                    var top    = pageMargin.Top.Value;
                    var bottom = pageMargin.Bottom.Value;
                    var left   = pageMargin.Left.Value;
                    var right  = pageMargin.Right.Value;

                    pageMargin.Top    = new Int32Value((int)left);
                    pageMargin.Bottom = new Int32Value((int)right);
                    pageMargin.Left   = new UInt32Value((uint)System.Math.Max(0, bottom));
                    pageMargin.Right  = new UInt32Value((uint)System.Math.Max(0, top));
                }
            }

            if (documentChanged)
            {
                MainDocumentPart.Document.Save();
            }

            return(this);
        }
Esempio n. 15
0
        /// <summary>
        /// Generate the required OpenXml element for handling page orientation.
        /// </summary>
        private static SectionProperties ChangePageOrientation(PageOrientationValues orientation)
        {
            PageSize pageSize = new PageSize() { Width = (UInt32Value) 16838U, Height = (UInt32Value) 11906U };
            if (orientation == PageOrientationValues.Portrait)
            {
                UInt32Value swap = pageSize.Width;
                pageSize.Width = pageSize.Height;
                pageSize.Height = swap;
            }
            else
            {
                pageSize.Orient = orientation;
            }

            return new SectionProperties (
                pageSize,
                new PageMargin() {
                    Top = 1417, Right = (UInt32Value) 1417U, Bottom = 1417, Left = (UInt32Value) 1417U,
                    Header = (UInt32Value) 708U, Footer = (UInt32Value) 708U, Gutter = (UInt32Value) 0U
                },
                new Columns() { Space = "708" },
                new DocGrid() { LinePitch = 360 }
            );
        }