Spire.Doc
Friday, 04 May 2012 02:16

Add Page Borders for Word in C#, VB.NET

Written by support iceblue

Word Page Border is one part of page background to beautify document appearance. Solution in this guide introduces how to insert and format Word page border in C# and VB.NET.

MS Word enables users to decorate Word pages to have a better layout or appearance. All the formats of page borders can be customized, including style, color, width, margin etc. This guide demonstrates how to add page borders for Word document and format them in C# and VB.NET via Spire.Doc for .NET. The following screenshot presents Word with page borders after programming.

Word Page Borders

Spire.Doc for .NET offers Section class to allow you to create new section instance or manipulate existing section. Borders is one property of PageSetup of Section class. You can directly set BorderType property of Borders to add page borders and set Color, Space of Left/Right properties to format borders. The border types Spire.Doc for .NET offers include None, Single, Thick, Double, Hairline, Dot, DashLargeGap, DotDash, DotDotDash, Triple, ThinkThickSmallGap, ThinThinSampllGap, ThinThickThinSamllGap, ThinThickMediumGap, ThickThinMediumGap, ThickThickThinMediumGap, ThinThickLargeGap, ThickThinLargeGap, ThinThickThinLargeGap, Wave, DoubleWave, DashSamllGap, DashDotStroker, Emboss3D, Engrave3D, Outset, Inset, TwistedLines1, Cleared. In this example, the border type is set as DoubleWave.

Download and install Spire.Doc for .NET and follow the code:

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

namespace WordBorder
{
    class PageBorders
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\Microsoft Word 2013 Preview.docx");
            Section section = document.Sections[0];

            //Add Page Borders with Special Style and Color
            section.PageSetup.Borders.BorderType = BorderStyle.DoubleWave;
            section.PageSetup.Borders.Color = Color.LightSeaGreen;
            //Space between Border and Text
            section.PageSetup.Borders.Left.Space = 50;
            section.PageSetup.Borders.Right.Space = 50;

            //Save and Launch
            document.SaveToFile("PageBorders.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("PageBorders.docx");
        }
    }
}
[VB.NET]
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordBorder
    Friend Class PageBorders
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\Microsoft Word 2013 Preview.docx")
            Dim section As Section = document.Sections(0)

            'Add Page Borders with Special Style and Color
            section.PageSetup.Borders.BorderType = BorderStyle.DoubleWave
            section.PageSetup.Borders.Color = Color.LightSeaGreen
            'Space between Border and Text
            section.PageSetup.Borders.Left.Space = 50
            section.PageSetup.Borders.Right.Space = 50

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

Spire.Doc is a stand-alone component, enabling developers/programmers to generate, open, write, edit and save Word document in WPF, .NET and Silverlight without Microsoft Word installed on system.

Thursday, 26 April 2012 06:44

Remove Word Section in C#, VB.NET

Written by support iceblue

In Word document, users can add new sections or remove existing sections. This guide demonstrates how to remove Word section in C# and VB.NET.

Section, important part to form a Word document, can be easily added and removed. In MS Word, users can operation section by section break, adding new section by inserting a break and deleting existing section by removing section break. In this guide, a solution will be presented to remove Word section in C# and VB.NET via Spire.Doc for .NET.

Spire.Doc for .NET enables developers to access SectionCollection property of Document class to get specified section in Word document and then remove the unnecessary one or more. There are two methods to remove Word section.

Remove one Specified Section

Invoke Remove(Spire.Doc.Interface.IDocumentObject entity) method to remove specified object of document. In this solution, entity is the first section of document which has gotten at first.

[C#]
            Section section = document.Sections[0];
            document.Sections.Remove(section);
[VB.NET]
            Dim section As Section = document.Sections(0)
            document.Sections.Remove(section)

Invoke RemoveAt(int index) method to remove document object at the specified index from the collection.

[C#]
            document.Sections.RemoveAt(0);
[VB.NET]
	    document.Sections.RemoveAt(0)

Remove All Sections

Invoke Clear() method to clear all objects of document.

[C#]
            document.Sections.Clear();
[VB.NET]
            document.Sections.Clear()

Note: Because a Word document must include at least one section, so the document cannot be saved if all the sections are removed.

Spire.Doc, the professional stand-alone component to manipulate MS Word document without automation, enables developers to generate, read, write, modify Word document on their .NET, WPF and Silverlight application.

Wednesday, 25 April 2012 03:48

Extract Text from Word Document in C#, VB.NET

Written by support iceblue

Word text can be extracted from a document and save in other files (for example TXT file) for other usage. This guide will present a convenient solution to extract Word text in C# and VB.NET.

Generally speaking, a Word document must include a lot of contents, such as text, image, table etc. Some of these contents can be extracted and used in other documents or files. The following guide focuses on introducing how to extract text from Word document and save in a TXT file in C# and VB.NET via Spire.Doc for .NET. And the following screenshot presents parts of text extracted from Word.

Extract Word Text

Text is all saved in Paragraph of Section class Spire.Doc for .NET provides. Therefore, you must get section and paragraph of document firstly and then get text to extract. And the following steps present details about how to extract text.

Firstly, load document by invoking LoadFromFile method of Document class with parameter string fileName. Secondly, initialize a StringBuilder class instance for saving text which will be extracted next. Thirdly, use foreach statement to get each paragraph of each section in document and invoke AppendLine(Paragraph.Text) method of StringBuilder class to appends copy of all extracted string (text in all paragraphs) in the StringBuilder instance. Finally, invoke File.WriteAllText method with parameter string path, string contents to create a new file to save extracted text. Please use the code.

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using System.Text;
using System.IO;

namespace ExtractTextfromWord
{
    class ExtractText
    {
        static void Main(string[] args)
        {
            //Load Document
            Document document = new Document();
            document.LoadFromFile(@"E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx");

            //Initialzie StringBuilder Instance
            StringBuilder sb = new StringBuilder();

            //Extract Text from Word and Save to StringBuilder Instance
            foreach (Section section in document.Sections)
            {
                foreach (Paragraph paragraph in section.Paragraphs)
                {
                    sb.AppendLine(paragraph.Text);
                }
            }

            //Create a New TXT File to Save Extracted Text
            File.WriteAllText("Extract.txt", sb.ToString());
            System.Diagnostics.Process.Start("ExtractText.txt");
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Text
Imports System.IO

Namespace ExtractTextfromWord
    Friend Class ExtractText
        Shared Sub Main(ByVal args() As String)
            'Load Document
            Dim document As New Document()
            document.LoadFromFile("E:\Work\Documents\WordDocuments\Spire.Doc for .NET.docx")

            'Initialzie StringBuilder Instance
            Dim sb As New StringBuilder()

            'Extract Text from Word and Save to StringBuilder Instance
            For Each section As Section In document.Sections
                For Each paragraph As Paragraph In section.Paragraphs
                    sb.AppendLine(paragraph.Text)
                Next paragraph
            Next section

            'Create a New TXT File to Save Extracted Text
            File.WriteAllText("Extract.txt", sb.ToString())
            System.Diagnostics.Process.Start("ExtractText.txt")
        End Sub
    End Class
End Namespace

Spire.Doc, the professional stand-alone component to manipulate MS Word document without automation, enables developers to generate, read, write, modify Word document on their .NET, WPF and Silverlight application.