예제 #1
0
        public Table InsertTable(int index, int rowCount, int columnCount, TableLook tableLook = null)
        {
            XElement newTable = HelperFunctions.CreateTable(rowCount, columnCount, tableLook);

            Paragraph p = HelperFunctions.GetFirstParagraphEffectedByInsert(Document, index);

            if (p == null)
            {
                Xml.Elements().First().AddFirst(newTable);
            }

            else
            {
                XElement[] split = HelperFunctions.SplitParagraph(p, index - p.startIndex);

                p.Xml.ReplaceWith
                (
                    split[0],
                    newTable,
                    split[1]
                );
            }


            return(new Table(Document, newTable));
        }
예제 #2
0
        public virtual Table InsertTable(int rowCount, int columnCount, TableLook tableLook = null) //Dmitchern, changed to virtual, and overrided in Table.Cell
        {
            XElement newTable = HelperFunctions.CreateTable(rowCount, columnCount, tableLook);

            Xml.Add(newTable);

            return(new Table(Document, newTable));
        }
 internal static XElement CreateTable(int rowCount, int columnCount, TableLook tableLook)
 {
     int[] columnWidths = new int[columnCount];
     for (int i = 0; i < columnCount; i++)
     {
         columnWidths[i] = 2310;
     }
     return(CreateTable(rowCount, columnWidths, tableLook));
 }
예제 #4
0
        public virtual Table InsertTableBeforeSelf(int rowCount, int columnCount, TableLook tableLook = null)
        {
            XElement newTable = HelperFunctions.CreateTable(rowCount, columnCount, tableLook);

            Xml.AddBeforeSelf(newTable);
            XElement newlyInserted = Xml.ElementsBeforeSelf().Last();

            return(new Table(Document, newlyInserted));
        }
        internal static XElement CreateTable(int rowCount, int[] columnWidths, TableLook tableLook)
        {
            tableLook = tableLook ?? new TableLook();
            int      firstRow     = Convert.ToInt32(tableLook.FirstRow);
            int      lastRow      = Convert.ToInt32(tableLook.LastRow);
            int      firstColumn  = Convert.ToInt32(tableLook.FirstColumn);
            int      lastColumn   = Convert.ToInt32(tableLook.LastColumn);
            int      noHBand      = Convert.ToInt32(tableLook.NoHorizontalBanding);
            int      noVBand      = Convert.ToInt32(tableLook.NoVerticalBanding);
            int      tblLookValue = noVBand << 6 | noHBand << 5 | lastColumn << 4 | firstColumn << 3 | lastRow << 2 | firstRow << 1;
            XElement newTable     =
                new XElement
                (
                    XName.Get("tbl", DocX.w.NamespaceName),
                    new XElement
                    (
                        XName.Get("tblPr", DocX.w.NamespaceName),
                        new XElement(XName.Get("tblStyle", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), "TableGrid")),
                        new XElement(XName.Get("tblW", DocX.w.NamespaceName), new XAttribute(XName.Get("w", DocX.w.NamespaceName), "5000"), new XAttribute(XName.Get("type", DocX.w.NamespaceName), "auto")),
                        new XElement(XName.Get("tblLook", DocX.w.NamespaceName),
                                     new XAttribute(XName.Get("val", DocX.w.NamespaceName), tblLookValue.ToString("X4")),
                                     new XAttribute(XName.Get("firstRow", DocX.w.NamespaceName), firstRow),
                                     new XAttribute(XName.Get("lastRow", DocX.w.NamespaceName), lastRow),
                                     new XAttribute(XName.Get("firstColumn", DocX.w.NamespaceName), firstColumn),
                                     new XAttribute(XName.Get("lastColumn", DocX.w.NamespaceName), lastColumn),
                                     new XAttribute(XName.Get("noHBand", DocX.w.NamespaceName), noHBand),
                                     new XAttribute(XName.Get("noVBand", DocX.w.NamespaceName), noVBand))
                    )
                );

            XElement tableGrid = new XElement(XName.Get("tblGrid", DocX.w.NamespaceName));

            for (int i = 0; i < columnWidths.Length; i++)
            {
                tableGrid.Add(new XElement(XName.Get("gridCol", DocX.w.NamespaceName), new XAttribute(XName.Get("w", DocX.w.NamespaceName), XmlConvert.ToString(columnWidths[i]))));
            }

            newTable.Add(tableGrid);

            for (int i = 0; i < rowCount; i++)
            {
                XElement row = new XElement(XName.Get("tr", DocX.w.NamespaceName));

                for (int j = 0; j < columnWidths.Length; j++)
                {
                    XElement cell = CreateTableCell();
                    row.Add(cell);
                }

                newTable.Add(row);
            }
            return(newTable);
        }
예제 #6
0
파일: Table.cs 프로젝트: WordDocX/DocX
        internal Table(DocX document, XElement xml)
            : base(document, xml)
        {
            autofit = AutoFit.ColumnWidth;
            this.Xml = xml;
            this.mainPart = document.mainPart;

            XElement properties = xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));

            XElement style = properties?.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
            if (style != null)
            {
                XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));

                if (val != null)
                {
                    String cleanValue = val.Value.Replace("-", string.Empty);
                    if (Enum.IsDefined(typeof(TableDesign), cleanValue))
                    {
                        design = (TableDesign)Enum.Parse(typeof(TableDesign), cleanValue);
                    }
                    else
                    {
                        design = TableDesign.Custom;
                    }
                }
                else
                    design = TableDesign.None;
            }

            else
                design = TableDesign.None;

            XElement tableLook = properties?.Element(XName.Get("tblLook", DocX.w.NamespaceName));
            if (tableLook != null)
            {
                TableLook = new TableLook
                {
                    FirstRow = tableLook.GetAttribute(XName.Get("firstRow", DocX.w.NamespaceName)) == "1",
                    LastRow = tableLook.GetAttribute(XName.Get("lastRow", DocX.w.NamespaceName)) == "1",
                    FirstColumn = tableLook.GetAttribute(XName.Get("firstColumn", DocX.w.NamespaceName)) == "1",
                    LastColumn = tableLook.GetAttribute(XName.Get("lastColumn", DocX.w.NamespaceName)) == "1",
                    NoHorizontalBanding = tableLook.GetAttribute(XName.Get("noHBand", DocX.w.NamespaceName)) == "1",
                    NoVerticalBanding = tableLook.GetAttribute(XName.Get("noVBand", DocX.w.NamespaceName)) == "1"
                };
            }
        }
예제 #7
0
파일: Table.cs 프로젝트: kozanid/DocX
        internal Table(DocX document, XElement xml)
            : base(document, xml)
        {
            autofit = AutoFit.ColumnWidth;
            this.Xml = xml;

            XElement properties = xml.Element(XName.Get("tblPr", DocX.w.NamespaceName));

            XElement style = properties.Element(XName.Get("tblStyle", DocX.w.NamespaceName));
            if (style != null)
            {
                XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName));

                if (val != null)
                {
                    try
                    {
                        design = (TableDesign)Enum.Parse(typeof(TableDesign), val.Value.Replace("-", string.Empty));
                    }

                    catch (Exception)
                    {
                        design = TableDesign.Custom;
                    }
                }
                else
                    design = TableDesign.None;
            }

            else
                design = TableDesign.None;

            XElement tableLook = properties.Element(XName.Get("tblLook", DocX.w.NamespaceName));
            if (tableLook != null)
            {
                TableLook = new TableLook();
                TableLook.FirstRow = tableLook.GetAttribute(XName.Get("firstRow", DocX.w.NamespaceName)) == "1";
                TableLook.LastRow = tableLook.GetAttribute(XName.Get("lastRow", DocX.w.NamespaceName)) == "1";
                TableLook.FirstColumn = tableLook.GetAttribute(XName.Get("firstColumn", DocX.w.NamespaceName)) == "1";
                TableLook.LastColumn = tableLook.GetAttribute(XName.Get("lastColumn", DocX.w.NamespaceName)) == "1";
                TableLook.NoHorizontalBanding = tableLook.GetAttribute(XName.Get("noHBand", DocX.w.NamespaceName)) == "1";
                TableLook.NoVerticalBanding = tableLook.GetAttribute(XName.Get("noVBand", DocX.w.NamespaceName)) == "1";
            }

        }
예제 #8
0
        public virtual Table InsertTableBeforeSelf(int rowCount, int columnCount, TableLook tableLook = null)
        {
            XElement newTable = HelperFunctions.CreateTable(rowCount, columnCount, tableLook);
            Xml.AddBeforeSelf(newTable);
            XElement newlyInserted = Xml.ElementsBeforeSelf().Last();

            return new Table(Document, newlyInserted);
        }
예제 #9
0
        internal static XElement CreateTable(int rowCount, int[] columnWidths, TableLook tableLook)
        {
            tableLook = tableLook ?? new TableLook();
            int firstRow = Convert.ToInt32(tableLook.FirstRow);
            int lastRow = Convert.ToInt32(tableLook.LastRow);
            int firstColumn = Convert.ToInt32(tableLook.FirstColumn);
            int lastColumn = Convert.ToInt32(tableLook.LastColumn);
            int noHBand = Convert.ToInt32(tableLook.NoHorizontalBanding);
            int noVBand = Convert.ToInt32(tableLook.NoVerticalBanding);
            int tblLookValue = noVBand << 6 | noHBand << 5 | lastColumn << 4 | firstColumn << 3 | lastRow << 2 | firstRow << 1;
            XElement newTable =
            new XElement
            (
                XName.Get("tbl", DocX.w.NamespaceName),
                new XElement
                (
                    XName.Get("tblPr", DocX.w.NamespaceName),
                        new XElement(XName.Get("tblStyle", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), "TableGrid")),
                        new XElement(XName.Get("tblW", DocX.w.NamespaceName), new XAttribute(XName.Get("w", DocX.w.NamespaceName), "5000"), new XAttribute(XName.Get("type", DocX.w.NamespaceName), "auto")),
                        new XElement(XName.Get("tblLook", DocX.w.NamespaceName),
                            new XAttribute(XName.Get("val", DocX.w.NamespaceName), tblLookValue.ToString("X4")),
                            new XAttribute(XName.Get("firstRow", DocX.w.NamespaceName), firstRow),
                            new XAttribute(XName.Get("lastRow", DocX.w.NamespaceName), lastRow),
                            new XAttribute(XName.Get("firstColumn", DocX.w.NamespaceName), firstColumn),
                            new XAttribute(XName.Get("lastColumn", DocX.w.NamespaceName), lastColumn),
                            new XAttribute(XName.Get("noHBand", DocX.w.NamespaceName), noHBand),
                            new XAttribute(XName.Get("noVBand", DocX.w.NamespaceName), noVBand))
                )
            );

            XElement tableGrid = new XElement(XName.Get("tblGrid", DocX.w.NamespaceName));
            for (int i = 0; i < columnWidths.Length; i++)
                tableGrid.Add(new XElement(XName.Get("gridCol", DocX.w.NamespaceName), new XAttribute(XName.Get("w", DocX.w.NamespaceName), XmlConvert.ToString(columnWidths[i]))));

            newTable.Add(tableGrid);

            for (int i = 0; i < rowCount; i++)
            {
                XElement row = new XElement(XName.Get("tr", DocX.w.NamespaceName));

                for (int j = 0; j < columnWidths.Length; j++)
                {
                    XElement cell = CreateTableCell();
                    row.Add(cell);
                }

                newTable.Add(row);
            }
            return newTable;
        }
예제 #10
0
 internal static XElement CreateTable(int rowCount, int columnCount, TableLook tableLook)
 {
     int[] columnWidths = new int[columnCount];
     for (int i = 0; i < columnCount; i++)
     {
         columnWidths[i] = 2310;
     }
     return CreateTable(rowCount, columnWidths, tableLook);
 }
예제 #11
0
파일: DocX.cs 프로젝트: geirsagberg/DocX-1
        /// <summary>
        /// Insert a new Table at the end of this document.
        /// </summary>
        /// <param name="columnCount">The number of columns to create.</param>
        /// <param name="rowCount">The number of rows to create.</param>
        /// <returns>A new Table.</returns>
        /// <example>
        /// Insert a new Table with 2 columns and 3 rows, at the end of a document.
        /// <code>
        /// // Create a document.
        /// using (DocX document = DocX.Create(@"C:\Example\Test.docx"))
        /// {
        ///     // Create a new Table with 2 columns and 3 rows.
        ///     Table newTable = document.InsertTable(2, 3);
        ///
        ///     // Set the design of this Table.
        ///     newTable.Design = TableDesign.LightShadingAccent2;
        ///
        ///     // Set the column names.
        ///     newTable.Rows[0].Cells[0].Paragraph.InsertText("Ice Cream", false);
        ///     newTable.Rows[0].Cells[1].Paragraph.InsertText("Price", false);
        ///
        ///     // Fill row 1
        ///     newTable.Rows[1].Cells[0].Paragraph.InsertText("Chocolate", false);
        ///     newTable.Rows[1].Cells[1].Paragraph.InsertText("€3:50", false);
        ///
        ///     // Fill row 2
        ///     newTable.Rows[2].Cells[0].Paragraph.InsertText("Vanilla", false);
        ///     newTable.Rows[2].Cells[1].Paragraph.InsertText("€3:00", false);
        ///
        ///     // Save all changes made to document b.
        ///     document.Save();
        /// }// Release this document from memory.
        /// </code>
        /// </example>
        public new Table InsertTable(int rowCount, int columnCount, TableLook tableLook = null)
        {
            if (rowCount < 1 || columnCount < 1)
                throw new ArgumentOutOfRangeException("Row and Column count must be greater than zero.");

            Table t = base.InsertTable(rowCount, columnCount, tableLook);
            t.mainPart = mainPart;
            return t;
        }
예제 #12
0
파일: DocX.cs 프로젝트: geirsagberg/DocX-1
        public Table AddTable(int rowCount, int columnCount, TableLook tableLook = null)
        {
            if (rowCount < 1 || columnCount < 1)
                throw new ArgumentOutOfRangeException("Row and Column count must be greater than zero.");

            Table t = new Table(this, HelperFunctions.CreateTable(rowCount, columnCount, tableLook));
            t.mainPart = mainPart;
            return t;
        }
예제 #13
0
        public Table InsertTable(int index, int rowCount, int columnCount, TableLook tableLook = null)
        {
            XElement newTable = HelperFunctions.CreateTable(rowCount, columnCount, tableLook);

            Paragraph p = HelperFunctions.GetFirstParagraphEffectedByInsert(Document, index);

            if (p == null)
                Xml.Elements().First().AddFirst(newTable);

            else
            {
                XElement[] split = HelperFunctions.SplitParagraph(p, index - p.startIndex);

                p.Xml.ReplaceWith
                (
                    split[0],
                    newTable,
                    split[1]
                );
            }

            return new Table(Document, newTable);
        }
예제 #14
0
        //Dmitchern, changed to virtual, and overrided in Table.Cell
        public virtual Table InsertTable(int rowCount, int columnCount, TableLook tableLook = null)
        {
            XElement newTable = HelperFunctions.CreateTable(rowCount, columnCount, tableLook);
            Xml.Add(newTable);

            return new Table(Document, newTable);
        }
예제 #15
0
 /// <summary>
 /// Insert a new Table into this document before this Paragraph.
 /// </summary>
 /// <param name="rowCount">The number of rows this Table should have.</param>
 /// <param name="columnCount">The number of columns this Table should have.</param>
 /// <returns>A new Table inserted before this Paragraph.</returns>
 /// <example>
 /// <code>
 /// // Create a new document.
 /// using (DocX document = DocX.Create(@"Test.docx"))
 /// {
 ///     //Insert a Paragraph into this document.
 ///     Paragraph p = document.InsertParagraph("Hello World", false);
 ///
 ///     // Insert a new Table before this Paragraph.
 ///     Table newTable = p.InsertTableBeforeSelf(2, 2);
 ///     newTable.Design = TableDesign.LightShadingAccent2;
 ///     newTable.Alignment = Alignment.center;
 ///
 ///     // Save all changes made to this document.
 ///     document.Save();
 /// }// Release this document from memory.
 /// </code>
 /// </example>
 public override Table InsertTableBeforeSelf(int rowCount, int columnCount, TableLook tableLook = null)
 {
     return base.InsertTableBeforeSelf(rowCount, columnCount, tableLook);
 }
예제 #16
0
파일: Table.cs 프로젝트: geirsagberg/DocX-1
 public override Table InsertTable(int rowCount, int columnCount, TableLook tableLook = null)
 {
     Table table = base.InsertTable(rowCount, columnCount, tableLook);
     InsertParagraph(); //Dmitchern, It is necessary to put paragraph in the end of the cell, without it MS-Word will say that the document is corrupted
     //IMPORTANT: It will be better to check all methods that work with adding anything to cells
     return table;
 }