News Category

Page Background

Page Background (8)

Watermarks can be added to Word documents to inform other people about the documents' ownership or status. Sometimes, you may want to get rid of an existing watermark in a Word document. This article will demonstrate how to remove watermarks from Word documents in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Remove Text or Image Watermarks from Word Documents in C# and VB.NET

You can remove the watermark of a Word document by setting the Document.Watermark property as null.

The following steps show you how to remove the watermark from a Word document:

  • Initialize an instance of Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Remove the watermark from the document by setting the Document.Watermark property as null.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;

namespace RemoveWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();
            //Load a Word document
            doc.LoadFromFile("Sample.docx");

            //Remove the watermark from the document
            doc.Watermark = null;

            //Save the result document
            doc.SaveToFile("RemoveWatermark.docx", FileFormat.Docx2013);
        }
    }
}

C#/VB.NET: Remove Text or Image Watermarks from Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Text watermark and image watermark are two kinds of watermarks in Word document. The text watermark always shows some additional but related information to the word context. While image watermark is used to make the Word document be more attractive. This section will demonstrate how to use Spire.Doc to add text watermark and image watermark to Word document in C#.

Add Image Watermark in C#:

using Spire.Doc;
namespace SetImageWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                //create a new instance of Document and load the document from file.
                Document doc = new Document();
                doc.LoadFromFile("Sample.docx", FileFormat.Docx2013);

                //create a new instance of the PictureWatermark and load the picture from file.
                PictureWatermark picture = new PictureWatermark();
                picture.Picture = System.Drawing.Image.FromFile("logo.png");

                //set the image watermark scaling and Washout property
                picture.Scaling = 20;
                picture.IsWashout = false;

                //add the picture watermark
                doc.Watermark = picture;

                //save the document to file
                doc.SaveToFile("ImageWatermark.docx", FileFormat.Docx2013);

            }
        }
    }
}

Add text watermark and image watermark to word document in C#

Add Text Watermark in C#::

using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace SetTextWatermark
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                //create a new instance of Document and load the document from file.
                Document doc = new Document();
                doc.LoadFromFile("Sample.docx", FileFormat.Docx2013);

                //create a new instance of the TextWatermark 
                TextWatermark txtWatermark = new TextWatermark();

                //set the text watermark with text string, font, color and layout.
                txtWatermark.Text = "Confidential";
                txtWatermark.FontSize = 45;
                txtWatermark.Color = Color.Green;
                txtWatermark.Layout = WatermarkLayout.Diagonal;

                //add the text watermark
                doc.Watermark = txtWatermark;

                //save the file.
                doc.SaveToFile("TextWatermark.docx", FileFormat.Docx2013);

            }
        }
    }
}

Add text watermark and image watermark to word document in C#

The default background of a Word document is white, and in the vast majority of cases, a simple white background is sufficient. However, if you are creating a resume, a broacher or other creative document that needs to be eye-catching, setting a unique background color or image may also be essential. This article will demonstrate how to programmatically add a background color or image to a Word document using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Add a Background Color to a Word Document

Adding a background color to a Word document is quite simple. You just need to set the background type as color and then choose a color as the background. The detailed steps are as follows.

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Set the background type as color using Document.Background.Type property.
  • Set a background color for the document using Document.Background.Color property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;

namespace ConvertWordToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load a sample Word document 
            document.LoadFromFile("Test.docx");

            //Set the background type as color
            document.Background.Type = BackgroundType.Color;

            //Set the background color
            document.Background.Color = Color.AliceBlue;

            //Save the document
            document.SaveToFile("PureColorBackground.docx", FileFormat.Docx);
        }
    }
}

C#/VB.NET: Add Background Color or Image to Word Documents

Add a Gradient Background to a Word Document

Adding gradient background requires more steps. You need to set the background type as gradient, choose two colors, and then set shading variant and style. The detailed steps are as follows.

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Set the background type as gradient using Document.Background.Type property.
  • Get the background gradient using Document.Background.Gradient property.
  • Select two colors using BackgroundGradient.Color1 and BackgroundGradient.Color2 properties.
  • Set shading variant and style for the gradient using BackgroundGradient.ShadingVariant and BackgroundGradient. ShadingStyle properties.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using System.Drawing;
using Spire.Doc.Documents;

namespace ConvertWordToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document document = new Document();

            //Load a sample Word document 
            document.LoadFromFile("Test.docx");

            //Set the background type as gradient
            document.Background.Type = BackgroundType.Gradient;

            //Get the background gradient
            BackgroundGradient gradient = document.Background.Gradient;

            //Select two colors
            gradient.Color1 = Color.White;
            gradient.Color2 = Color.LightBlue;

            //Set shading variant and style for the gradient
            gradient.ShadingVariant = GradientShadingVariant.ShadingDown;
            gradient.ShadingStyle = GradientShadingStyle.Horizontal;

            //Save the document
            document.SaveToFile("AddGradientBackground.docx", FileFormat.Docx);

        }
    }
}

C#/VB.NET: Add Background Color or Image to Word Documents

Insert a Background Image to a Word Document

To insert a background image to a Word document, you need to set the background type as picture, and then insert a picture as the background. The detailed steps are as follows.

  • Create a Document instance.
  • Load a sample Word document using Document.LoadFromFile() method.
  • Set the background type as picture using Document.Background.Type property.
  • Set a background picture for the document using Document.Background.Picture property.
  • Save the result document using Document.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
namespace SetImageBackground
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                //Create a Document instance
                Document document = new Document();

                //Load a sample Word document 
                document.LoadFromFile("Test.docx");

                //Set the background type as picture
                document.Background.Type = BackgroundType.Picture;

                //Set background picture
                document.Background.Picture = Image.FromFile("background.jpg");

                //Save the document
                document.SaveToFile("AddBackgroundPicture.docx", FileFormat.Docx);
            }

        }
    }
}

C#/VB.NET: Add Background Color or Image to Word Documents

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

MS page borders options provide the choice to set the position for page border by margins measured from text or edge of page. Besides, it provides the option for custom page border by setting the style and color of top, bottom, left, right borders. We have introduced the method to set page borders and set whether page border surrounds header/footer or not using Spire.Doc. This article is going to introduce the solution provided by Spire.Doc to set the position and custom style for page borders in C#.

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

Step 1: Load the sample document with no page border.

            Document document = new Document();
            document.LoadFromFile("S.docx");
            Section section = document.Sections[0];

Step 2: Set the position of page borders and its space. Effects will show the difference between page border measured from text and page edge with the same space.

            section.PageSetup.PageBorderOffsetFrom = PageBorderOffsetFrom.PageEdge;
            section.PageSetup.Borders.Top.Space = 20;
            section.PageSetup.Borders.Bottom.Space = 30;
            section.PageSetup.Borders.Left.Space = 20;
            section.PageSetup.Borders.Right.Space =25;

Step 3: Set the style and color of the top, bottom, left, right page borders, namely custom page borders.

            section.PageSetup.Borders.Top.BorderType = BorderStyle.Double;
            section.PageSetup.Borders.Bottom.BorderType = BorderStyle.Engrave3D;
            section.PageSetup.Borders.Left.BorderType = BorderStyle.Double;
            section.PageSetup.Borders.Right.BorderType = BorderStyle.Double;

            section.PageSetup.Borders.Top.Color = Color.YellowGreen;
            section.PageSetup.Borders.Bottom.Color = Color.DeepSkyBlue;
            section.PageSetup.Borders.Left.Color = Color.DeepSkyBlue;
            section.PageSetup.Borders.Right.Color = Color.DeepSkyBlue;

Step 4: Save the document and launch to see effects.

            document.SaveToFile("result.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("result.docx"); 

Effects:

Page border position measured from text:

How to set position and custom style for page borders

Page border position measured from page edge:

How to set position and custom style for page borders

Full codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace Mirror_Margin
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("S.docx");
            Section section = document.Sections[0];

            section.PageSetup.PageBorderOffsetFrom = PageBorderOffsetFrom.PageEdge;
            section.PageSetup.Borders.Top.Space = 20;
            section.PageSetup.Borders.Bottom.Space = 30;
            section.PageSetup.Borders.Left.Space = 20;
            section.PageSetup.Borders.Right.Space =25;
            
            section.PageSetup.Borders.Top.BorderType = BorderStyle.Double;
            section.PageSetup.Borders.Bottom.BorderType = BorderStyle.Engrave3D;
            section.PageSetup.Borders.Left.BorderType = BorderStyle.Double;
            section.PageSetup.Borders.Right.BorderType = BorderStyle.Double;

            section.PageSetup.Borders.Top.Color = Color.YellowGreen;
            section.PageSetup.Borders.Bottom.Color = Color.DeepSkyBlue;
            section.PageSetup.Borders.Left.Color = Color.DeepSkyBlue;
            section.PageSetup.Borders.Right.Color = Color.DeepSkyBlue;

            document.SaveToFile("result.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("result.docx"); 
        }
    }
}

Spire.Doc can help developers to create word table with data and format cells easily and it also supports to add text watermark into the word documents. This article will show you how to create a vertical table at one side of the word document, which looks like the vertical watermark in the word document.

Firstly, please check the effective screenshot of the vertical table at the right of the word document added by Spire.Doc:

How to create vertical table at one side of the word document

Here comes to the steps of how to create vertical table in C#.

Step 1: Create a new document and add a section to the document.

Document document = new Document();
Section section=document.AddSection();

Step 2: Add a table with rows and columns and set the text for the table.

Table table = section.AddTable();
table.ResetCells(1, 1);
TableCell cell = table.Rows[0].Cells[0];
table.Rows[0].Height = 150;
cell.AddParagraph().AppendText("Draft copy in vertical style");

Step 3: Set the TextDirection for the table to RightToLeftRotated.

cell.CellFormat.TextDirection = TextDirection.RightToLeftRotated;

Step 4: Set the table format.

table.TableFormat.WrapTextAround = true;
table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Page;
table.TableFormat.Positioning.HorizRelationTo = HorizontalRelation.Page;
table.TableFormat.Positioning.HorizPosition = section.PageSetup.PageSize.Width- table.Width;
table.TableFormat.Positioning.VertPosition = 200;

Step 5: Save the document to file.

document.SaveToFile("result.docx",FileFormat.docx2013);

Full codes in C#:

using Spire.Doc;
using Spire.Doc.Documents;

namespace CreateVerticalTable
{
    class Program
    {
        static void Main(string[] args)
        {

            Document document = new Document();
            Section section=document.AddSection();
            Table table = section.AddTable();
            table.ResetCells(1, 1);
            TableCell cell = table.Rows[0].Cells[0];
            table.Rows[0].Height = 150;
            cell.AddParagraph().AppendText("Draft copy in vertical style");
            cell.CellFormat.TextDirection = TextDirection.RightToLeftRotated;
            table.TableFormat.WrapTextAround = true;
            table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Page;
            table.TableFormat.Positioning.HorizRelationTo = HorizontalRelation.Page;
            table.TableFormat.Positioning.HorizPosition = section.PageSetup.PageSize.Width- table.Width;
            table.TableFormat.Positioning.VertPosition = 200;

            document.SaveToFile("result.docx",FileFormat.docx2013);

        }
    }
}

By shading words or paragraphs in a word document, we can emphasize the message and catch others' eyes easily. This article will show you how to set paragraph shading in C# with the help of Spire.Doc.

Firstly, download Spire.Doc and install on your system. The Spire.Doc installation is clean, professional and wrapped up in a MSI installer.

Then, adds Spire.Doc.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Doc\Bin\NET4.0\ Spire.Doc.dll".

Now it comes to the steps of how to set the background color for text or paragraph.

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

[C#]
Document document = new Document();
document.LoadFromFile(@"..\..\Sample.docx");

Step 2: Get a paragraph.

[C#]
Paragraph paragaph = document.Sections[0].Paragraphs[0];

Step 3: Shading the paragraph or the selected words.

[C#]
//Set background color for the paragraph
paragaph.Format.BackColor = Color.Yellow;

//Set background color for the selected text of paragraph
paragaph = document.Sections[0].Paragraphs[1];
TextSelection selection= paragaph.Find("Blues",true,false);
TextRange range = selection.GetAsOneRange();
range.CharacterFormat.TextBackgroundColor = Color.Yellow;

Step 4: Save the document to file.

[C#]
document.SaveToFile("sample.docx",FileFormat.Docx);

Effected Screenshot:

Set Word Paragraph Shading

Full codes:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace SetWordParagh
{
    class Program
    {
       static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile(@"..\..\Sample.docx");
            Paragraph paragaph = document.Sections[0].Paragraphs[0];

            //Set background color for the paragraph
            paragaph.Format.BackColor = Color.Yellow;

            //Set background color for the selected text of paragraph
            paragaph = document.Sections[0].Paragraphs[1];
            TextSelection selection= paragaph.Find("Blues",true,false);
            TextRange range = selection.GetAsOneRange();
            range.CharacterFormat.TextBackgroundColor = Color.Yellow;

            document.SaveToFile("sample.docx",FileFormat.Docx);
        }
        }
    }

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.

Watermarks are text or images displayed fadedly or in gray color in the background of a Word document. They can be used to declare confidentiality, copyright, or other attributes of the document, or just as decorations to make the document more attractive. This article shows an easy way to insert watermarks in Word documents with the help of Spire.Doc for .NET, including text watermarks and image watermarks.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Insert a Text Watermark in a Word Document

The detailed steps are as follows:

  • Create an object of Document class.
  • Load a Word document from disk using Document.LoadFromFile() method.
  • Insert a text watermark in the document using custom method InsertTextWatermark().
  • Save the document using Doucment.SaveToFile() method.
  • C#
  • VB.NET
using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace InsertImageWatermark
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of Document class
            Document document = new Document();

            //Load a Word document from disk
            document.LoadFromFile(@"D:\Samples\Sample.docx");

            //Insert a text watermark
            InsertTextWatermark(document.Sections[0]);

            //Save the document
            document.SaveToFile("InsertTextWatermark.docx", FileFormat.Docx);
        }
        private static void InsertTextWatermark(Section section)
        {
            TextWatermark txtWatermark = new TextWatermark();
            txtWatermark.Text = "DO NOT COPY";
            txtWatermark.FontSize = 50;
            txtWatermark.Color = Color.Blue;
            txtWatermark.Layout = WatermarkLayout.Diagonal;
            section.Document.Watermark = txtWatermark;

        }
    }
}

C#/VB.NET: Insert Watermarks in Word

Insert an Image Watermark in a Word Document

The detailed steps are as follows:

  • Create an object of Document class.
  • Load a Word document from disk using Document.LoadFromFile() method.
  • Insert an image watermark in the document using custom method InsertImageWatermark().
  • Save the document using Document.SaveToFile() method.
  • C#
  • VB.NET
using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace InsertWatermark
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create an object of Document class
            Document document = new Document();

            //Load a Word document from disk
            document.LoadFromFile(@"D:\Samples\Sample.docx");

            //Insert an image watermark
            InsertImageWatermark(document);

            //Save the document
            document.SaveToFile("InsertImageWatermark.docx", FileFormat.Docx);        
        }
        private static void InsertImageWatermark(Document document)
        {
            PictureWatermark picture = new PictureWatermark();
            picture.Picture = Image.FromFile(@"D:\Samples\Watermark.png");
            picture.Scaling = 200;
            picture.IsWashout = false;
            document.Watermark = picture;
        }
    }
}

C#/VB.NET: Insert Watermarks in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.