News Category

Program Guide for WPF

Program Guide for WPF (24)

It's well known that we can add a variety of bullet styles to paragraphs in Word, such as various symbols, pictures, fonts and numbers. Bullet makes these paragraphs to be a bulleted list, so that we can easily create a well-structured and professional document.

This article will demonstrate how to set bullet style for word paragraphs in WPF applications using Spire.Doc for WPF.

Below is the effective screenshot after setting bullet style:

How to Set Bullet Style for Word Paragraphs in WPF

Code snippets:

Use namespace:

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

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

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

Step 2: Create a bulleted list.

Get the first section, then apply bullet style to paragraphs from 5th to 13th and set bullet position.

Section sec = doc.Sections[0];
for (int i = 4; i < 13; i++)
{
    Paragraph paragraph = sec.Paragraphs[i];
    paragraph.ListFormat.ApplyBulletStyle();
    paragraph.ListFormat.CurrentListLevel.NumberPosition = -20;
}

In addition, we can also use following code to create a numbered list:

paragraph.ListFormat.ApplyNumberedStyle();

Step 3: Save and launch the file.

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

Full codes:

private void button1_Click(object sender, RoutedEventArgs e)
{
    //Load Document
    Document doc = new Document();
    doc.LoadFromFile("Instruction.docx");

    //Set Bullet Style
    Section sec = doc.Sections[0];
    for (int i = 4; i < 13; i++)
    {
    Paragraph paragraph = sec.Paragraphs[i];

    //Create a bulleted list
    paragraph.ListFormat.ApplyBulletStyle();
    
        //Create a numbered list
    // paragraph.ListFormat.ApplyNumberedStyle();

        paragraph.ListFormat.CurrentListLevel.NumberPosition = -20;
    }

    //Save and Launch
    doc.SaveToFile("Bullet.docx", FileFormat.Docx);
    System.Diagnostics.Process.Start("Bullet.docx");
}

Dropdown list in Word is one type of forms that restricts the data entry to an item in the predefined list. Spire.Doc supports to add old dropdown list form field (Legacy Forms) to Word document. This dropdown list is limited to 25 items and requires forms protection. The following section will demonstrate how to create a dropdown form filed in Word in a WPF application.

Code Snippet:

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: Add a paragraph to the section and append text.

Spire.Doc.Documents.Paragraph p = s.AddParagraph();
p.AppendText("Country:  ");

Step 3: Call Paragraph.AppendField() method to insert a dropdown list and call DropDownItems.Add() method to add items.

string fieldName = "DropDownList";
DropDownFormField list = p.AppendField(fieldName, FieldType.FieldFormDropDown) as DropDownFormField;
list.DropDownItems.Add("Italy");
list.DropDownItems.Add("France");
list.DropDownItems.Add("Germany");
list.DropDownItems.Add("Greece");
list.DropDownItems.Add("UK");

Step 4: Add protection to the document by setting the protection type as AllowOnlyFormFields and also setting a password. Note: If you choose not to use a password, anyone can change your editing restrictions.

doc.Protect(ProtectionType.AllowOnlyFormFields,"e-iceblue");

Step 5: Save and launch the file.

doc.SaveToFile("result.doc", FileFormat.Doc);
System.Diagnostics.Process.Start("result.doc");

Output:

How to Create Dropdown Form Field in Word in WPF

Full Code:

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

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Document doc = new Document();
            Spire.Doc.Section s = doc.AddSection();
            Spire.Doc.Documents.Paragraph p = s.AddParagraph();
            p.AppendText("Country:  ");

            ParagraphStyle style = new ParagraphStyle(doc);
            style.Name = "FontStyle";
            style.CharacterFormat.FontName = "Arial";
            style.CharacterFormat.FontSize = 12;
            doc.Styles.Add(style);
            p.ApplyStyle(style.Name);

            string fieldName = "DropDownList";
            DropDownFormField list = p.AppendField(fieldName, FieldType.FieldFormDropDown) as DropDownFormField;
            list.DropDownItems.Add("Italy");
            list.DropDownItems.Add("France");
            list.DropDownItems.Add("Germany");
            list.DropDownItems.Add("Greece");
            list.DropDownItems.Add("UK");

            doc.Protect(ProtectionType.AllowOnlyFormFields, "e-iceblue");
            doc.SaveToFile("result.doc", FileFormat.Doc);
            System.Diagnostics.Process.Start("result.doc");

        }
    }
}

A page border is a border that appears outside the margins on each page. Page borders are primarily for decoration so you can use any style, color, and line thickness you want. A good page border can make your page more appealing. In this section, we will demonstrate how to set up page borders in word using Spire.Doc for WPF.

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

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

Step 2: Add page borders and set up the format of borders. In this instance, we set the style of border to ThinThickLargeGap and set the color to yellow green.

Section section = document.Sections[0];
section.PageSetup.Borders.BorderType = BorderStyle.ThinThickLargeGap;
section.PageSetup.Borders.Color = Color.YellowGreen;

Step 3: Set up the space between the borders and the text.

section.PageSetup.Borders.Left.Space = 50;
section.PageSetup.Borders.Right.Space = 50;
section.PageSetup.Borders.Top.Space = 50;
section.PageSetup.Borders.Bottom.Space = 50;

Step 4: Save the document and launch the file.

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

Effective Screenshots:

Set up page borders in word in WPF

Full codes:

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

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

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile("Emily.docx");
            Section section = document.Sections[0];

            //Add Page Borders with Special Style and Color
            section.PageSetup.Borders.BorderType = BorderStyle.ThinThickLargeGap;
            section.PageSetup.Borders.Color = Color.YellowGreen;
            //Space between Border and Text
            section.PageSetup.Borders.Left.Space = 50;
            section.PageSetup.Borders.Right.Space = 50;
            section.PageSetup.Borders.Top.Space = 50;
            section.PageSetup.Borders.Bottom.Space = 50;
            //Save and Launch
            document.SaveToFile("Dickinson.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("Dickinson.docx");
        }
    }
        
}
[VB.NET]
Imports System.Drawing
Imports System.Windows
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace Emily
	''' 
	''' 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)
			'Load Document
			Dim document As New Document()
			document.LoadFromFile("Emily.docx")
			Dim section As Section = document.Sections(0)

			'Add Page Borders with Special Style and Color
			section.PageSetup.Borders.BorderType = BorderStyle.ThinThickLargeGap
			section.PageSetup.Borders.Color = Color.YellowGreen
			'Space between Border and Text
			section.PageSetup.Borders.Left.Space = 50
			section.PageSetup.Borders.Right.Space = 50
			section.PageSetup.Borders.Top.Space = 50
			section.PageSetup.Borders.Bottom.Space = 50
			'Save and Launch
			document.SaveToFile("Dickinson.docx", FileFormat.Docx)
			System.Diagnostics.Process.Start("Dickinson.docx")
		End Sub
	End Class

Header or footer are pieces of text or images that appear at the top or bottom of every document page. You can add any information you want to the header and footer areas such as page numbers, author's name, current date and company logo.

This article will demonstrate how to insert text header and footer for word document in WPF by using Spire.Doc for WPF.

Below are the effective screenshots after inserting header and footer:

Insert Header and Footer for Word Document in WPF

Insert Header and Footer for Word Document in 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 to your project as reference.

Then follow the detail steps below:

Use namespace:

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

Insert Header

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

Document doc = new Document();
doc.LoadFromFile("Eiffel Tower.docx");

Step 2: Get its first section, then add a header paragraph for section one.

Section section = doc.Sections[0];
HeaderFooter header = section.HeadersFooters.Header;
Paragraph paragraph = header.AddParagraph();

Step 3: Append text for header paragraph and set text format.

TextRange text = paragraph.AppendText("Eiffel Tower introduction");
text.CharacterFormat.FontName = "Cambria";
text.CharacterFormat.FontSize = 12;
text.CharacterFormat.TextColor = Color.DarkGreen;

Step 4: Set Header Paragraph Format.

paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
paragraph.Format.Borders.Bottom.BorderType = BorderStyle.DashSmallGap;
paragraph.Format.Borders.Bottom.Space = 0.05f;
paragraph.Format.Borders.Bottom.Color = Color.DarkGray;

Insert Footer

Step 5: Add a Footer paragraph.

HeaderFooter footer = section.HeadersFooters.Footer;
Paragraph sparagraph = footer.AddParagraph();

Step 6: Append text for footer paragraph and set text format.

TextRange stext = sparagraph.AppendText("the tallest structure in Paris");
stext.CharacterFormat.FontName = "Calibri";
stext.CharacterFormat.FontSize = 12;
stext.CharacterFormat.TextColor = Color.DarkGreen;

Step 7: Set Footer Paragrah Format.

sparagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
sparagraph.Format.Borders.Top.BorderType = BorderStyle.Hairline;
sparagraph.Format.Borders.Top.Space = 0.15f;
sparagraph.Format.Borders.Color = Color.DarkGray;

Step 8: Save and launch the file.

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

Full codes:

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

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

            Section section = doc.Sections[0];
            HeaderFooter header = section.HeadersFooters.Header;
            Paragraph paragraph = header.AddParagraph();

            TextRange text = paragraph.AppendText("Eiffel Tower introduction");
            text.CharacterFormat.FontName = "Cambria";
            text.CharacterFormat.FontSize = 12;
            text.CharacterFormat.TextColor = Color.DarkGreen;

            paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
            paragraph.Format.Borders.Bottom.BorderType = BorderStyle.DashSmallGap;
            paragraph.Format.Borders.Bottom.Space = 0.05f;
            paragraph.Format.Borders.Bottom.Color = Color.DarkGray;

            HeaderFooter footer = section.HeadersFooters.Footer;
            Paragraph sparagraph = footer.AddParagraph();

            TextRange stext = sparagraph.AppendText("the tallest structure in Paris");
            stext.CharacterFormat.FontName = "Calibri";
            stext.CharacterFormat.FontSize = 12;
            stext.CharacterFormat.TextColor = Color.DarkGreen;

            sparagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right;
            sparagraph.Format.Borders.Top.BorderType = BorderStyle.Hairline;
            sparagraph.Format.Borders.Top.Space = 0.15f;
            sparagraph.Format.Borders.Color = Color.DarkGray;

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



    }
}
[VB.NET]
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
	Dim doc As New Document()
	doc.LoadFromFile("Eiffel Tower.docx")

	Dim section As Section = doc.Sections(0)
	Dim header As HeaderFooter = section.HeadersFooters.Header
	Dim paragraph As Paragraph = header.AddParagraph()

	Dim text As TextRange = paragraph.AppendText("Eiffel Tower introduction")
	text.CharacterFormat.FontName = "Cambria"
	text.CharacterFormat.FontSize = 12
	text.CharacterFormat.TextColor = Color.DarkGreen

	paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right
	paragraph.Format.Borders.Bottom.BorderType = BorderStyle.DashSmallGap
	paragraph.Format.Borders.Bottom.Space = 0.05F
	paragraph.Format.Borders.Bottom.Color = Color.DarkGray

	Dim footer As HeaderFooter = section.HeadersFooters.Footer
	Dim sparagraph As Paragraph = footer.AddParagraph()

	Dim stext As TextRange = sparagraph.AppendText("the tallest structure in Paris")
	stext.CharacterFormat.FontName = "Calibri"
	stext.CharacterFormat.FontSize = 12
	stext.CharacterFormat.TextColor = Color.DarkGreen

	sparagraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right
	sparagraph.Format.Borders.Top.BorderType = BorderStyle.Hairline
	sparagraph.Format.Borders.Top.Space = 0.15F
	sparagraph.Format.Borders.Color = Color.DarkGray

	doc.SaveToFile("HeaderFooter.docx")
	System.Diagnostics.Process.Start("HeaderFooter.docx")
End Sub

Add Footnote in word in WPF

2016-03-11 08:03:05 Written by support iceblue

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

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



    }
}

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");

            }



        }
    }
}

Word hyperlink gives a large amount of information of the original word and image. The text and image with hyperlinks can guides readers to a Web page, e-mail address, or the other files and documents. In this article, we’re focusing on how to insert hyperlink into the text in C# on WPF applications with the help of Spire.Doc for WPF.

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 code snippets:

Step 1: Create a word document and add a section and paragraph to it.

Document document = new Document();
Section Sec = document.AddSection();
Paragraph Para = Sec.AddParagraph();

Step 2: Set the styles for the text and hyperlinks on the paragraph.

ParagraphStyle txtStyle = new ParagraphStyle(document);
txtStyle.Name = "Style";
txtStyle.CharacterFormat.FontName = "Calibri";
txtStyle.CharacterFormat.FontSize = 16;
txtStyle.CharacterFormat.TextColor = System.Drawing.Color.RosyBrown;
document.Styles.Add(txtStyle);
ParagraphStyle hyperlinkstyle = new ParagraphStyle(document);
hyperlinkstyle.Name = "linkStyle";
hyperlinkstyle.CharacterFormat.FontName = "Calibri";
hyperlinkstyle.CharacterFormat.FontSize = 14;
document.Styles.Add(hyperlinkstyle);

Step 3: Add the text to the paragraph with style and link it to a web page.

Para = Sec.AddParagraph();
Para.AppendText("Home page");
Para.ApplyStyle(txtStyle.Name);
Para = Sec.AddParagraph();
Para.AppendHyperlink("HomePage", "www.e-iceblue.com",HyperlinkType.WebLink);
Para.ApplyStyle(hyperlinkstyle.Name);

Step 4: Add the text to the paragraph with style and link it to an email address.

Para = Sec.AddParagraph();
Para.AppendText("Contact US");
Para.ApplyStyle(txtStyle.Name);
Para = Sec.AddParagraph();
Para.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com",HyperlinkType.EMailLink);
Para.ApplyStyle(hyperlinkstyle.Name);

Step 5: Save the document to file and launch to preview it.

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

Effective screenshot of adding hyperlinks to the word document in C#:

How to create word hyperlinks in C# on WPF applications

Full codes:

using Spire.Doc;
using Spire.Doc.Documents;
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();
            Section Sec = document.AddSection();
            Paragraph Para = Sec.AddParagraph();

            ParagraphStyle txtStyle = new ParagraphStyle(document);
            txtStyle.Name = "Style";
            txtStyle.CharacterFormat.FontName = "Calibri";
            txtStyle.CharacterFormat.FontSize = 16;
            txtStyle.CharacterFormat.TextColor = System.Drawing.Color.RosyBrown;
            document.Styles.Add(txtStyle);
            ParagraphStyle hyperlinkstyle = new ParagraphStyle(document);
            hyperlinkstyle.Name = "linkStyle";
            hyperlinkstyle.CharacterFormat.FontName = "Calibri";
            hyperlinkstyle.CharacterFormat.FontSize = 14;
            document.Styles.Add(hyperlinkstyle);

            Para = Sec.AddParagraph();
            Para.AppendText("Home page");
            Para.ApplyStyle(txtStyle.Name);

            Para = Sec.AddParagraph();
            Para.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);
            Para.ApplyStyle(hyperlinkstyle.Name);

            Para = Sec.AddParagraph();
            Para.AppendText("Contact US");
            Para.ApplyStyle(txtStyle.Name);

            Para = Sec.AddParagraph();
            Para.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com", HyperlinkType.EMailLink);
            Para.ApplyStyle(hyperlinkstyle.Name);

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


    }
}

By default, the font color of a word document is black. To achieve a more distinctive visual effect, we can change it to red, green or any other colors we like.

This article describes how to change word font color with Spire.Doc for WPF in C#, VB.NET.

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.

Below is the screenshot of the original word document:

How to Change Word Font Color in WPF

Detail steps:

Use namespace:

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

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

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

Step 2: Get its first section and first paragraph (the Title), then set text color for paragraph 1.

Section section = document.Sections[0];
Paragraph p1 = section.Paragraphs[0];
ParagraphStyle s1 = new ParagraphStyle(document);
s1.Name = "TitleTextColor";
s1.CharacterFormat.TextColor = Color.RosyBrown;
document.Styles.Add(s1);
p1.ApplyStyle(s1.Name);

Step 3: Get the second paragraph and set text color for paragraph 2.

Paragraph p2 = section.Paragraphs[1];
ParagraphStyle s2 = new ParagraphStyle(document);
s2.Name = "BodyTextColor";
s2.CharacterFormat.TextColor = Color.DarkBlue;
document.Styles.Add(s2);
p2.ApplyStyle(s2.Name);

Step 4: Save and launch the file.

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

Output:

How to Change Word Font Color in WPF

Full codes:

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

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

            //Set Text Color for Paragraph 1(the Title)
            Section section = document.Sections[0];
            Paragraph p1 = section.Paragraphs[0];
            ParagraphStyle s1 = new ParagraphStyle(document);
            s1.Name = "TitleTextColor";
            s1.CharacterFormat.TextColor = Color.RosyBrown;
            document.Styles.Add(s1);
            p1.ApplyStyle(s1.Name);

            //Set Text Color for Paragraph 2
            Paragraph p2 = section.Paragraphs[1];
            ParagraphStyle s2 = new ParagraphStyle(document);
            s2.Name = "BodyTextColor";
            s2.CharacterFormat.TextColor = Color.DarkBlue;
            document.Styles.Add(s2);
            p2.ApplyStyle(s2.Name);

            //Save and Launch
            document.SaveToFile("FontColor.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("FontColor.docx");
        }



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

Namespace WpfApplication1
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub
		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			'Load Document
			Dim document As New Document()
			document.LoadFromFile("Story.docx")

			'Set Text Color for Paragraph 1(the Title)
			Dim section As Section = document.Sections(0)
			Dim p1 As Paragraph = section.Paragraphs(0)
			Dim s1 As New ParagraphStyle(document)
			s1.Name = "TitleTextColor"
			s1.CharacterFormat.TextColor = Color.RosyBrown
			document.Styles.Add(s1)
			p1.ApplyStyle(s1.Name)

			'Set Text Color for Paragraph 2
			Dim p2 As Paragraph = section.Paragraphs(1)
			Dim s2 As New ParagraphStyle(document)
			s2.Name = "BodyTextColor"
			s2.CharacterFormat.TextColor = Color.DarkBlue
			document.Styles.Add(s2)
			p2.ApplyStyle(s2.Name)

			'Save and Launch
			document.SaveToFile("FontColor.docx", FileFormat.Docx)
			System.Diagnostics.Process.Start("FontColor.docx")
		End Sub



	End Class
End Namespace
Page 1 of 2