コード例 #1
0
        public Word.TableCell GenerateCell(string val, string w, int span = 0, bool bold = false, string sz = "12", string s = "style22", string f = "Courier New", Word.JustificationValues halign = Word.JustificationValues.Left)
        {
            Word.LeftMargin margin = new Word.LeftMargin {
                Type = new EnumValue <Word.TableWidthUnitValues>(Word.TableWidthUnitValues.Dxa), Width = new StringValue("10")
            };
            Word.TableCellWidth width = new Word.TableCellWidth {
                Type = new EnumValue <Word.TableWidthUnitValues>(Word.TableWidthUnitValues.Dxa), Width = w
            };
            Word.Shading shading = new Word.Shading {
                Fill = "auto", Val = new EnumValue <Word.ShadingPatternValues>(Word.ShadingPatternValues.Clear)
            };
            Word.TableCellMargin             cellmargin = new Word.TableCellMargin(margin);
            Word.VerticalTextAlignmentOnPage align      = new Word.VerticalTextAlignmentOnPage {
                Val = new EnumValue <Word.VerticalJustificationValues>(Word.VerticalJustificationValues.Center)
            };
            Word.GridSpan gspan = new Word.GridSpan {
                Val = span
            };

            Word.TableCellProperties props = new Word.TableCellProperties(width, shading, cellmargin, align, gspan);

            Word.Paragraph paragraph = GenerateParagraph(val, bold, sz, s, f, halign);

            Word.TableCell cell = new Word.TableCell(props, paragraph);
            return(cell);
        }
コード例 #2
0
        private Wordprocessing.Table doptable2(Wordprocessing.Table table)
        {
            for (int i = 1; i <= 5; i++)
            {
                //Первый столбец
                Wordprocessing.TableRow tableRow = new Wordprocessing.TableRow();
                Wordprocessing.TableCell tableCell1 = new Wordprocessing.TableCell();
                Wordprocessing.TableCellProperties tableCellProperties1 = new Wordprocessing.TableCellProperties();
                Wordprocessing.TableCellWidth tableCellWidth1 = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
                Wordprocessing.GridSpan gridSpan1 = new Wordprocessing.GridSpan() { Val = 3 };
                 Wordprocessing.VerticalMerge verticalMerge1;
                if (i==1)  verticalMerge1 = new Wordprocessing.VerticalMerge() { Val = Wordprocessing.MergedCellValues.Restart };
                else verticalMerge1 = new Wordprocessing.VerticalMerge();
                tableCellProperties1.Append(tableCellWidth1);
                tableCellProperties1.Append(gridSpan1);
                tableCellProperties1.Append(verticalMerge1);

                tableCell1.Append(tableCellProperties1);
                if (i == 1)
                {
                    for (int j=1; j<5; j++)
                      tableCell1.Append(retText(j, 1));
                }
                else tableCell1.Append(retText(i, 1));
                tableRow.Append(tableCell1);

                //Второй столбец
                Wordprocessing.TableCell tableCell2 = new Wordprocessing.TableCell();
                Wordprocessing.TableCellProperties tableCellProperties2 = new Wordprocessing.TableCellProperties();
                Wordprocessing.TableCellWidth tableCellWidth2 = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
                Wordprocessing.VerticalMerge verticalMerge2;
                if (i == 1) verticalMerge2 = new Wordprocessing.VerticalMerge() { Val = Wordprocessing.MergedCellValues.Restart };
                else verticalMerge2 = new Wordprocessing.VerticalMerge();
                tableCellProperties2.Append(tableCellWidth2);
                tableCellProperties2.Append(verticalMerge2);
                tableCell2.Append(tableCellProperties2);
                tableCell2.Append(retText(i, 2));
                tableRow.Append(tableCell2);

                for (int j = 0; j < 2; j++)
                {
                    Wordprocessing.TableCell tableCell3 = new Wordprocessing.TableCell();
                    Wordprocessing.TableCellProperties tableCellProperties3 = new Wordprocessing.TableCellProperties();
                    Wordprocessing.TableCellWidth tableCellWidth3 = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
                    tableCellProperties3.Append(tableCellWidth3);
                    tableCell3.Append(tableCellProperties3);
                    tableCell3.Append(retText(i, j + 3));
                    tableRow.Append(tableCell3);

                }

                table.Append(tableRow);

            }

            Wordprocessing.TableRow tableRowrlast = new Wordprocessing.TableRow();

            Wordprocessing.TableCell tableCellrlast = new Wordprocessing.TableCell();
            Wordprocessing.TableCellProperties tableCellPropertiesrlast = new Wordprocessing.TableCellProperties();
            Wordprocessing.TableCellWidth tableCellWidthrlast = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
            Wordprocessing.GridSpan gridSpanrlast = new Wordprocessing.GridSpan() { Val = 3 };
            Wordprocessing.VerticalMerge verticalMergerlast = new Wordprocessing.VerticalMerge();
            tableCellPropertiesrlast.Append(tableCellWidthrlast);
            tableCellPropertiesrlast.Append(gridSpanrlast);
            tableCellPropertiesrlast.Append(verticalMergerlast);
            tableCellrlast.Append(tableCellPropertiesrlast);
            tableCellrlast.Append(retText(-1, -1));

            Wordprocessing.TableCell tableCellrlast1 = new Wordprocessing.TableCell();
            Wordprocessing.TableCellProperties tableCellPropertiesrlast1 = new Wordprocessing.TableCellProperties();
            Wordprocessing.TableCellWidth tableCellWidthrlast1 = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
            Wordprocessing.GridSpan gridSpanrlast1 = new Wordprocessing.GridSpan() { Val = 3 };
            tableCellPropertiesrlast1.Append(tableCellWidthrlast1);
            tableCellPropertiesrlast1.Append(gridSpanrlast1);
            tableCellrlast1.Append(tableCellPropertiesrlast1);
            tableCellrlast1.Append(retText(0, 0));

            tableRowrlast.Append(tableCellrlast);
            tableRowrlast.Append(tableCellrlast1);
            table.Append(tableRowrlast);

            return table;
        }
コード例 #3
0
ファイル: TableHelper.cs プロジェクト: xw-fighting/DocxToPdf
        private void adjustTableColumnsWidth()
        {
            if (this.table == null)
            {
                return;
            }

            // Get table total width
            float totalWidth = -1f;
            bool  autoWidth  = false;

            Word.TableWidth tableWidth = this.styleHelper.GetAppliedElement <Word.TableWidth>(this.table);
            if (tableWidth != null && tableWidth.Type != null)
            {
                switch (tableWidth.Type.Value)
                {
                case Word.TableWidthUnitValues.Nil:
                case Word.TableWidthUnitValues.Auto:     // fits the contents
                default:
                    autoWidth = true;
                    break;

                case Word.TableWidthUnitValues.Dxa:
                    if (tableWidth.Width != null)
                    {
                        totalWidth = Tools.ConvertToPoint(tableWidth.Width.Value, Tools.SizeEnum.TwentiethsOfPoint, -1f);
                    }
                    break;

                case Word.TableWidthUnitValues.Pct:
                    if (tableWidth.Width != null)
                    {
                        totalWidth = this.styleHelper.PrintablePageWidth * Tools.Percentage(tableWidth.Width.Value);
                        //if (table.Parent.GetType() == typeof(Word.Body))
                        //    totalWidth = (float)((pdfDoc.PageSize.Width - pdfDoc.LeftMargin - pdfDoc.RightMargin) * percentage(tableWidth.Width.Value));
                        //else
                        //    totalWidth = this.getCellWidth(table.Parent as Word.TableCell) * percentage(tableWidth.Width.Value);
                    }
                    break;
                }
            }
            Console.WriteLine("Table total width: " + totalWidth);

            if (!autoWidth)
            {
                scaleTableColumnsWidth(ref this.tableColumnsWidth, totalWidth);
            }
            else
            {
                totalWidth = this.tableColumnsWidth.Sum();
            }

            for (int i = 0; i < this.RowLength; i++)
            {
                // Get all cells in this row
                List <TableHelperCell> cellsInRow = this.cells.FindAll(c => c.rowId == i);
                if (cellsInRow.Count <= 0)
                {
                    continue;
                }

                // Get if any gridBefore & gridAfter
                int             skipGridsBefore = 0, skipGridsAfter = 0;
                float           skipGridsBeforeWidth = 0f, skipGridsAfterWidth = 0f;
                TableHelperCell head = cellsInRow.FirstOrDefault(c => c.rowStart);
                if (head != null)
                {
                    if (head.row.TableRowProperties != null)
                    {
                        // w:gridBefore
                        var tmpGridBefore = head.row.TableRowProperties.Elements <Word.GridBefore>().FirstOrDefault();
                        if (tmpGridBefore != null && tmpGridBefore.Val != null)
                        {
                            skipGridsBefore = tmpGridBefore.Val.Value;
                        }

                        // w:wBefore
                        var tmpGridBeforeWidth = head.row.TableRowProperties.Elements <Word.WidthBeforeTableRow>().FirstOrDefault();
                        if (tmpGridBeforeWidth != null && tmpGridBeforeWidth.Width != null)
                        {
                            skipGridsBeforeWidth = Tools.ConvertToPoint(Convert.ToInt32(tmpGridBeforeWidth.Width.Value), Tools.SizeEnum.TwentiethsOfPoint, -1f);
                        }

                        // w:gridAfter
                        var tmpGridAfter = head.row.TableRowProperties.Elements <Word.GridAfter>().FirstOrDefault();
                        if (tmpGridAfter != null && tmpGridAfter.Val != null)
                        {
                            skipGridsAfter = tmpGridAfter.Val.Value;
                        }

                        // w:wAfter
                        var tmpGridAfterWidth = head.row.TableRowProperties.Elements <Word.WidthAfterTableRow>().FirstOrDefault();
                        if (tmpGridAfterWidth != null && tmpGridAfterWidth.Width != null)
                        {
                            skipGridsAfterWidth = Tools.ConvertToPoint(Convert.ToInt32(tmpGridAfterWidth.Width.Value), Tools.SizeEnum.TwentiethsOfPoint, -1f);
                        }
                    }
                }

                int j = 0;
                int edgeEnd = 0;

                // -------
                // gridBefore
                edgeEnd = skipGridsBefore;
                for (; j < edgeEnd; j++)
                {
                    // deduce specific columns width from required width
                    skipGridsBeforeWidth -= this.tableColumnsWidth[j];
                }
                if (skipGridsBeforeWidth > 0f)
                {
                    // if required width is larger than the total width of specific columns,
                    // the remaining required width adds to the last specific column
                    this.tableColumnsWidth[edgeEnd - 1] += skipGridsBeforeWidth;
                }

                // ------
                // cells
                while (j < (cellsInRow.Count - skipGridsAfter))
                {
                    float reqCellWidth            = 0f;
                    Word.TableCellWidth cellWidth = this.styleHelper.GetAppliedElement <Word.TableCellWidth>(cellsInRow[j].cell);
                    if (cellWidth != null && cellWidth.Type != null)
                    {
                        switch (cellWidth.Type.Value)
                        {
                        case Word.TableWidthUnitValues.Auto:
                            //// TODO: calculate the items width
                            //if (cellsInRow[j].elements.Count > 0)
                            //{
                            //    iTSText.IElement element = cellsInRow[j].elements[0];
                            //}
                            break;

                        case Word.TableWidthUnitValues.Nil:
                        default:
                            break;

                        case Word.TableWidthUnitValues.Dxa:
                            if (cellWidth.Width != null)
                            {
                                reqCellWidth = Tools.ConvertToPoint(cellWidth.Width.Value, Tools.SizeEnum.TwentiethsOfPoint, -1f);
                            }
                            break;

                        case Word.TableWidthUnitValues.Pct:
                            if (cellWidth.Width != null)
                            {
                                reqCellWidth = Tools.Percentage(cellWidth.Width.Value) * totalWidth;
                            }
                            break;
                        }
                    }

                    // check row span
                    int spanCount = 1;
                    if (cellsInRow[j].cell != null)
                    {
                        Word.TableCell tmpCell = cellsInRow[j].cell;
                        if (tmpCell.TableCellProperties != null)
                        {
                            Word.GridSpan span = tmpCell.TableCellProperties.Elements <Word.GridSpan>().FirstOrDefault();
                            spanCount = (span != null && span.Val != null) ? span.Val.Value : 1;
                        }
                    }

                    edgeEnd = j + spanCount;
                    for (; j < edgeEnd; j++)
                    {
                        // deduce specific columns width from required width
                        reqCellWidth -= this.tableColumnsWidth[j];
                    }
                    if (reqCellWidth > 0f)
                    {
                        // if required width is larger than the total width of specific columns,
                        // the remaining required width adds to the last specific column
                        this.tableColumnsWidth[edgeEnd - 1] += reqCellWidth;
                    }
                }

                // ------
                // gridAfter
                edgeEnd = j + skipGridsAfter;
                for (; j < edgeEnd; j++)
                {
                    // deduce specific columns width from required width
                    skipGridsAfterWidth -= this.tableColumnsWidth[j];
                }
                if (skipGridsAfterWidth > 0f)
                {
                    // if required width is larger than the total width of specific columns,
                    // the remaining required width adds to the last specific column
                    this.tableColumnsWidth[edgeEnd - 1] += skipGridsAfterWidth;
                }

                if (!autoWidth) // fixed table width, adjust width to fit in
                {
                    scaleTableColumnsWidth(ref this.tableColumnsWidth, totalWidth);
                }
                else // auto table width
                {
                    totalWidth = this.tableColumnsWidth.Sum();
                }
            }
        }
コード例 #4
0
 private Wordprocessing.Table doptable(Wordprocessing.Table table)
 {
     //Строка подпись декана
     Wordprocessing.TableRow tableRow2 = new Wordprocessing.TableRow() { RsidTableRowAddition = "00FF67BF", RsidTableRowProperties = "008E2483" };
     Wordprocessing.TableCell tableCell = new Wordprocessing.TableCell();
     Wordprocessing.TableCellProperties tableCellProperties = new Wordprocessing.TableCellProperties();
     Wordprocessing.TableCellWidth tableCellWidth = new Wordprocessing.TableCellWidth() { Width = "0", Type = Wordprocessing.TableWidthUnitValues.Auto };
     Wordprocessing.GridSpan gridSpan = new Wordprocessing.GridSpan() { Val = 6 };
     Wordprocessing.TableCellVerticalAlignment tableCellVerticalAlignment = new Wordprocessing.TableCellVerticalAlignment() { Val = Wordprocessing.TableVerticalAlignmentValues.Center };
     tableCellProperties.Append(tableCellWidth);
     tableCellProperties.Append(gridSpan);
     tableCellProperties.Append(tableCellVerticalAlignment);
     Wordprocessing.Paragraph paragraph = new Wordprocessing.Paragraph();
     Wordprocessing.ParagraphProperties parprop = new Wordprocessing.ParagraphProperties();
     Wordprocessing.SpacingBetweenLines spacingBetweenLines2 = new Wordprocessing.SpacingBetweenLines() { After = "0" };
     parprop.Append(spacingBetweenLines2);
     paragraph.Append(parprop);
     Wordprocessing.Run run = new Wordprocessing.Run();
     Wordprocessing.Text text = new Wordprocessing.Text();
     text.Text = "Подпись декана";
     run.Append(text);
     paragraph.Append(run);
     tableCell.Append(tableCellProperties);
     tableCell.Append(paragraph);
     tableRow2.Append(tableCell);
     table.Append(tableRow2);
     return table;
 }