Как вставить таблицу в DOCX после определенной закладки в C# и .NET


Наш клиент прислал нам запрос с просьбой помочь ему с таким примером. У него есть документ .docx с несколькими закладками , он хочет вставить новую таблицу после определенной закладки. Давайте посмотрим, как это сделать.

Загрузите полученный файл: результат - вставка таблицы после bookmark.docx

Полный код

using SautinSoft.Document;
using SautinSoft.Document.Tables;
using System;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            // Get your free 100-day key here:   
            // https://sautinsoft.com/start-for-free/

            InsertTableAfterSpecificBookmark();
        }

        /// <summary>
        /// How to insert a table in DOCX document after a specific bookmark.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/from-customers-insert-table-in-docx-after-specific-bookmark-net-csharp-vb.php
        /// </remarks>		
        public static void InsertTableAfterSpecificBookmark()
        {
            // A one of our customers sent us a request to help him with such example. 
            // He has a .docx document with several bookmarks, he wants to insert 
            // a new table after the specific bookmark. Let's see how to make it.

            string inpFile = @"..\..\..\bookmarks.docx";
            string outFile = @"Result.docx";

            // 1. Load a document with bookmarks.
            DocumentCore dc = DocumentCore.Load(inpFile);

            // 2. Find a specific bookmark by name.
            // Our DOCX document contains 2 bookmarks: table1 and table2.
            Bookmark bookmark = dc.Bookmarks["table2"];

            // 3. Insert a table after the bookmark "table2".
            if (bookmark != null)
            {
                // Create a new simple table 2 x 3.
                Table table = CreateTable(dc, 2, 3, 100f);

                // Insert the table after the bookmark.
                bookmark.End.Content.End.Insert(table.Content);
            }

            // Let's save our document into DOCX format.
            dc.Save(outFile);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }

        public static Table CreateTable(DocumentCore dc, int rows, int columns, float widthMm)
        {
            // Let's create a plain table: 2x3, 100 mm of width.
            Table table = new Table(dc);
            double width = LengthUnitConverter.Convert(widthMm, LengthUnit.Millimeter, LengthUnit.Point);
            table.TableFormat.PreferredWidth = new TableWidth(width, TableWidthUnit.Point);
            table.TableFormat.Alignment = HorizontalAlignment.Center;

            int counter = 0;

            // Add rows.
            for (int r = 0; r < rows; r++)
            {
                TableRow row = new TableRow(dc);

                // Add columns.
                for (int c = 0; c < columns; c++)
                {
                    TableCell cell = new TableCell(dc);

                    // Set cell formatting and width.
                    cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dotted, Color.Black, 1.0);

                    // Set the same width for each column.
                    cell.CellFormat.PreferredWidth = new TableWidth(width / columns, TableWidthUnit.Point);

                    if (counter % 2 == 1)
                        cell.CellFormat.BackgroundColor = new Color("#358CCB");

                    row.Cells.Add(cell);

                    // Let's add a paragraph with text into the each column.
                    Paragraph p = new Paragraph(dc);
                    p.ParagraphFormat.Alignment = HorizontalAlignment.Center;
                    p.ParagraphFormat.SpaceBefore = LengthUnitConverter.Convert(3, LengthUnit.Millimeter, LengthUnit.Point);
                    p.ParagraphFormat.SpaceAfter = LengthUnitConverter.Convert(3, LengthUnit.Millimeter, LengthUnit.Point);

                    p.Content.Start.Insert(String.Format("{0}", (char)(counter + 'A')), new CharacterFormat() { FontName = "Arial", FontColor = new Color("#3399FF"), Size = 12.0 });
                    cell.Blocks.Add(p);
                    counter++;
                }
                table.Rows.Add(row);
            }
            return table;
        }
    }
}

Download

Imports SautinSoft.Document
Imports SautinSoft.Document.Tables
Imports System

Namespace Sample
    Friend Class Sample
        Shared Sub Main(ByVal args() As String)
            InsertTableAfterSpecificBookmark()
        End Sub
        ''' Get your free 100-day key here:   
        ''' https://sautinsoft.com/start-for-free/
        ''' <summary>
        ''' How to insert a table in DOCX document after a specific bookmark.
        ''' </summary>
        ''' <remarks>
        ''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/from-customers-insert-table-in-docx-after-specific-bookmark-net-csharp-vb.php
        ''' </remarks>		
        Public Shared Sub InsertTableAfterSpecificBookmark()
            ' A one of our customers sent us a request to help him with such example. 
            ' He has a .docx document with several bookmarks, he wants to insert 
            ' a new table after the specific bookmark. Let's see how to make it.

            Dim inpFile As String = "..\..\..\bookmarks.docx"
            Dim outFile As String = "Result.docx"

            ' 1. Load a document with bookmarks.
            Dim dc As DocumentCore = DocumentCore.Load(inpFile)

            ' 2. Find a specific bookmark by name.
            ' Our DOCX document contains 2 bookmarks: table1 and table2.
            Dim bookmark As Bookmark = dc.Bookmarks("table2")

            ' 3. Insert a table after the bookmark "table2".
            If bookmark IsNot Nothing Then
                ' Create a new simple table 2 x 3.
                Dim table As Table = CreateTable(dc, 2, 3, 100.0F)

                ' Insert the table after the bookmark.
                bookmark.End.Content.End.Insert(table.Content)
            End If

            ' Let's save our document into DOCX format.
            dc.Save(outFile)

            ' Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
        End Sub

        Public Shared Function CreateTable(ByVal dc As DocumentCore, ByVal rows As Integer, ByVal columns As Integer, ByVal widthMm As Single) As Table
            ' Let's create a plain table: 2x3, 100 mm of width.
            Dim table As New Table(dc)
            Dim width As Double = LengthUnitConverter.Convert(widthMm, LengthUnit.Millimeter, LengthUnit.Point)
            table.TableFormat.PreferredWidth = New TableWidth(width, TableWidthUnit.Point)
            table.TableFormat.Alignment = HorizontalAlignment.Center

            Dim counter As Integer = 0

            ' Add rows.
            For r As Integer = 0 To rows - 1
                Dim row As New TableRow(dc)

                ' Add columns.
                For c As Integer = 0 To columns - 1
                    Dim cell As New TableCell(dc)

                    ' Set cell formatting and width.
                    cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Dotted, Color.Black, 1.0)

                    ' Set the same width for each column.
                    cell.CellFormat.PreferredWidth = New TableWidth(width / columns, TableWidthUnit.Point)

                    If counter Mod 2 = 1 Then
                        cell.CellFormat.BackgroundColor = New Color("#358CCB")
                    End If

                    row.Cells.Add(cell)

                    ' Let's add a paragraph with text into the each column.
                    Dim p As New Paragraph(dc)
                    p.ParagraphFormat.Alignment = HorizontalAlignment.Center
                    p.ParagraphFormat.SpaceBefore = LengthUnitConverter.Convert(3, LengthUnit.Millimeter, LengthUnit.Point)
                    p.ParagraphFormat.SpaceAfter = LengthUnitConverter.Convert(3, LengthUnit.Millimeter, LengthUnit.Point)

                    p.Content.Start.Insert(String.Format("{0}", ChrW(counter + AscW("A"c))), New CharacterFormat() With {
                        .FontName = "Arial",
                        .FontColor = New Color("#3399FF"),
                        .Size = 12.0
                    })
                    cell.Blocks.Add(p)
                    counter += 1
                Next c
                table.Rows.Add(row)
            Next r
            Return table
        End Function
    End Class
End Namespace

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.com или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.