Spire.Doc for .NET

Add Footnote in word in WPF

2016-03-11 08:03:05 Written by Koohji

Footnote is a note at the bottom of a page that gives more detailed information about something on the page. In this section, we will demonstrate how to add footnote in word using Spire.Doc for WPF.

First, create a new project by choosing WPF Application in Visual Studio, adding a button in the Main Window. Add Spire.Doc.Wpf.dll as reference to your project, and then double click the button to start.

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

Document doc = new Document();
doc.LoadFromFile("Oscar.docx");

Step 2: Select the section and paragraph which needs adding footnote. In this example, we choose the first section and the fourth paragraph.

Section sec = doc.Sections[0];
Paragraph paragraph = sec.Paragraphs[3];

Step 3: Add footnote by the method of paragraph.AppendFootnote() and then add the footnote contents by using footnote.TextBody.AddParagraph().AppendText(). At last, set the font style, size and color.

Footnote fn = paragraph.AppendFootnote(FootnoteType.Footnote);
TextRange text = fn.TextBody.AddParagraph().AppendText("Oscar Wilde (film), a 1960 biographical film about Oscar Wilde");
text.CharacterFormat.FontName = "Calibri";
text.CharacterFormat.FontSize = 11;
text.CharacterFormat.TextColor = Color.Chocolate;

Step 4: Set format of the marker.

fn.MarkerCharacterFormat.FontName = "Arial";
fn.MarkerCharacterFormat.FontSize = 14;
fn.MarkerCharacterFormat.Bold = true;
fn.MarkerCharacterFormat.TextColor = Color.Brown;

Step 5: Save the document and launch the file.

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

Effective Screenshot:

Add Footnote in word in WPF

Add Footnote in word in WPF

Full codes:

[C#]
using System.Drawing;
using System.Windows;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace Add_Footnote_to_Word
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document doc = new Document();
            doc.LoadFromFile("Oscar.docx");
            Section sec = doc.Sections[0];
            Paragraph paragraph = sec.Paragraphs[3];

            Footnote fn = paragraph.AppendFootnote(FootnoteType.Footnote);
            TextRange text = fn.TextBody.AddParagraph().AppendText("Oscar Wilde (film), a 1960 biographical film about Oscar Wilde");
            text.CharacterFormat.FontName = "Calibri";
            text.CharacterFormat.FontSize = 11;
            text.CharacterFormat.TextColor = Color.Chocolate;

            fn.MarkerCharacterFormat.FontName = "Arial";
            fn.MarkerCharacterFormat.FontSize = 14;
            fn.MarkerCharacterFormat.Bold = true;
            fn.MarkerCharacterFormat.TextColor = Color.Brown;

            doc.SaveToFile("AddFootNote.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("AddFootNote.docx");

        }
    }
}
[VB.NET]
Imports System.Drawing
Imports System.Windows
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace Add_Footnote_to_Word
	''' 
	''' Interaction logic for MainWindow.xaml
	''' 
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			Dim doc As New Document()
			doc.LoadFromFile("Oscar.docx")
			Dim sec As Section = doc.Sections(0)
			Dim paragraph As Paragraph = sec.Paragraphs(3)

			Dim fn As Footnote = paragraph.AppendFootnote(FootnoteType.Footnote)
			Dim text As TextRange = fn.TextBody.AddParagraph().AppendText("Oscar Wilde (film), a 1960 biographical film about Oscar Wilde")
			text.CharacterFormat.FontName = "Calibri"
			text.CharacterFormat.FontSize = 11
			text.CharacterFormat.TextColor = Color.Chocolate

			fn.MarkerCharacterFormat.FontName = "Arial"
			fn.MarkerCharacterFormat.FontSize = 14
			fn.MarkerCharacterFormat.Bold = True
			fn.MarkerCharacterFormat.TextColor = Color.Brown

			doc.SaveToFile("AddFootNote.docx", FileFormat.Docx)
			System.Diagnostics.Process.Start("AddFootNote.docx")

		End Sub
	End Class
End Namespace

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");
}
page 23