page 220

How to Insert Word Bookmark in WPF

2016-03-03 08:21:19 Written by Koohji

In Word, bookmark is used for positioning, it enables readers to return to the read or modified location quickly. Thus, it would be necessary for us to insert a bookmark when we are editing or reading a long word document, so that next time we can get back to the specific location in a short period.

This article will demonstrate how to insert word bookmark in WPF by using Spire.Doc for WPF.

At first, please download Spire.Doc and install it correctly, then add Spire.Doc.Wpf.dll and Spire.License.dll from the installation folder as reference.

This is the effective screenshot after inserting bookmark:

How to Insert Word Bookmark in WPF

Now, follow the Detail steps below:

Use namespace:

using System.Windows;
using Spire.Doc;

Step 1: Initialize a new instance of the Document class and load the word document from file.

Document document = new Document();
document.LoadFromFile("Stories.docx");

Step 2: Get its first section and insert bookmark starts from the end of the second paragraph and ends to the third paragraph.

Section section = document.Sections[0];
section.Paragraphs[1].AppendBookmarkStart("bookmark");
section.Paragraphs[2].AppendBookmarkEnd("bookmark");

Step 3: Save and launch the file.

document.SaveToFile("Bookmark.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Bookmark.docx");

Full codes:

using Spire.Doc;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document document = new Document();
            document.LoadFromFile("Stories.docx");

            Section section = document.Sections[0];
            section.Paragraphs[1].AppendBookmarkStart("bookmark");
            section.Paragraphs[2].AppendBookmarkEnd("bookmark");

            document.SaveToFile("Bookmark.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Bookmark.docx");
        }



    }
}

Create Word Table in WPF with C#

2016-03-03 08:15:39 Written by Koohji

A table is a great way to display data into group, frequently used in our financial reports, academic reports and any other documents that contain a group of similar data. In this article, I am fusing on how to insert a table in Word, fill the table with data and how to format the cells using Spire.Doc for WPF with C#.

Code Snippets

Step 1: Initialize a new instance of Document class, add a section to it.

Document doc = new Document();
Spire.Doc.Section s = doc.AddSection();

Step 2: Define the data that will be filled with table.

String[] Header ={"Vendor No","Vendor Name", "Address","City","State","Zip"};  
String[][] data = {
                      new String[]{"3501","Cacor Corporation","161 Southfield Rd","Southfield","OH","60093"},
                      new String[]{"3502","Underwater","50 N3rd Street","Indianapolis","IN","46208"},
                      new String[]{"3503","J.W. Luscher Mfg.","65 Addams Street","Berkely","MA","02779"},
                      new String[]{"3504","Scuba Professionals","3105 East Brace","Rancho Dominguez","CA","90221"},
                      new String[]{"3505","Divers' Supply Shop", "5208 University Dr","Macon","GA","20865"},
                      new String[]{"3506","Techniques","52 Dolphin Drive","Redwood City","CA","94065-1086"},
                  };

Step 3: Add a table with border to section, set the rows and columns number based on the data length.

Spire.Doc.Table table = s.AddTable(true);
table.ResetCells(data.Length + 1, Header.Length);

Step 4: Fill the table with the predefined data and format the cells.

Spire.Doc.TableRow FRow= table.Rows[0];
FRow.IsHeader = true;
FRow.Height = 23;
FRow.RowFormat.BackColor = System.Drawing.Color.DarkBlue;
for (int i = 0; i < Header.Length; i++)
{
    //Cell Alignment
    Paragraph p = FRow.Cells[i].AddParagraph();
    FRow.Cells[i].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
    p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
    //Fill Header data and Format the Cells
    TextRange TR = p.AppendText(Header[i]);
    TR.CharacterFormat.FontName = "Calibri";
    TR.CharacterFormat.FontSize = 12;
    TR.CharacterFormat.TextColor = System.Drawing.Color.White;
    TR.CharacterFormat.Bold = true;
}
for (int r = 0; r < data.Length; r++)
{
    TableRow DataRow = table.Rows[r + 1];
    //Row Height
    DataRow.Height = 15;
    //C Represents Column.
    for (int c = 0; c < data[r].Length; c++)
    {
        //Cell Alignment
        DataRow.Cells[c].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
        //Fill Data in Rows
        Paragraph p2 =DataRow.Cells[c].AddParagraph();
        TextRange TR2=p2.AppendText(data[r][c]);
        //Format Cells
        p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
        TR2.CharacterFormat.FontName = "Calibri";
        TR2.CharacterFormat.FontSize = 10;
        TR2.CharacterFormat.TextColor = System.Drawing.Color.Black;
    }
}

Step 5: Call AutoFitBebavior() method to make the table AutoFit to Window.

table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow);

Step 6: Save and launch the file.

doc.SaveToFile("WordTable.docx");
System.Diagnostics.Process.Start("WordTable.docx");

Output:

Create Word Table in WPF with C#

Full Code:

private void button1_Click(object sender, RoutedEventArgs e)
{
    Document doc = new Document();
    Spire.Doc.Section s = doc.AddSection();
    
    String[] Header ={"Vendor No","Vendor Name", "Address","City","State","Zip"};  
    String[][] data = {
                          new String[]{"3501","Cacor Corporation","161 Southfield Rd","Southfield","OH","60093"},
                          new String[]{"3502","Underwater","50 N3rd Street","Indianapolis","IN","46208"},
                          new String[]{"3503","J.W. Luscher Mfg.","65 Addams Street","Berkely","MA","02779"},
                          new String[]{"3504","Scuba Professionals","3105 East Brace","Rancho Dominguez","CA","90221"},
                          new String[]{"3505","Divers' Supply Shop", "5208 University Dr","Macon","GA","20865"},
                          new String[]{"3506","Techniques","52 Dolphin Drive","Redwood City","CA","94065-1086"},
                      };
    Spire.Doc.Table table = s.AddTable(true);
    table.ResetCells(data.Length + 1, Header.Length);   
    Spire.Doc.TableRow FRow= table.Rows[0];
    FRow.IsHeader = true;
    FRow.Height = 23;
    FRow.RowFormat.BackColor = System.Drawing.Color.DarkBlue;
    for (int i = 0; i < Header.Length; i++)
    {
        Paragraph p = FRow.Cells[i].AddParagraph();
        FRow.Cells[i].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
        p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
        TextRange TR = p.AppendText(Header[i]);
        TR.CharacterFormat.FontName = "Calibri";
        TR.CharacterFormat.FontSize = 12;
        TR.CharacterFormat.TextColor = System.Drawing.Color.White;
        TR.CharacterFormat.Bold = true;
    }
    for (int r = 0; r < data.Length; r++)
    {
        TableRow DataRow = table.Rows[r + 1];
        DataRow.Height = 15;
        for (int c = 0; c < data[r].Length; c++)
        {
            DataRow.Cells[c].CellFormat.VerticalAlignment = Spire.Doc.Documents.VerticalAlignment.Middle;
            Paragraph p2 =DataRow.Cells[c].AddParagraph();
            TextRange TR2=p2.AppendText(data[r][c]);
            p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
            TR2.CharacterFormat.FontName = "Calibri";
            TR2.CharacterFormat.FontSize = 10;
            TR2.CharacterFormat.TextColor = System.Drawing.Color.Black;
        }
    }
    table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow);
    doc.SaveToFile("WordTable.docx");
    System.Diagnostics.Process.Start("WordTable.docx");
}

With the help of Spire.Doc for WPF, developers can add word documents easily for their WPF applications. When we want to show opinions or additional information about words, phrases or paragraphs, we can add the comments to the word documents to give more information about the contents. Spire.Doc offers a class of Spire.Doc.Fields.Comments to enable developers to work with comments easily. This article will demonstrate how to add the comments to the word documents in C# for WPF applications.

Note: Before Start, please download the latest version of Spire.Doc and add Spire.Doc.Wpf.dll in the bin folder as the reference of Visual Studio.

Here comes to the steps of how to add comments to word documents in C#:

Step 1: Create a new word document and load the document from file.

Document document = new Document();
document.LoadFromFile("sample.docx");

Step 2: Get Paragraph to Insert Comment.

Section section = document.Sections[0];
Paragraph paragraph = section.Paragraphs[2];

Step 3: Insert the comment.

string str = "This is the first comment";
Comment comment = paragraph.AppendComment(str);
comment.Format.Author = "E-iceblue";
comment.Format.Initial = "CM";

Step 4: Save the document to file.

document.SaveToFile("Addcomment.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("Addcomment.docx");

Effective screenshot of adding the word comments in C#:

How to add comments to word documents in C# for WPF applications

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            {
                Document document = new Document();
                document.LoadFromFile("sample.docx");

                Section section = document.Sections[0];
                Paragraph paragraph = section.Paragraphs[2];

                string str = "This is the first comment";
                Comment comment = paragraph.AppendComment(str);
                comment.Format.Author = "E-iceblue";
                comment.Format.Initial = "CM";

                document.SaveToFile("Addcomment.docx", FileFormat.Docx);
                System.Diagnostics.Process.Start("Addcomment.docx");

            }



        }
    }
}
page 220