Page Background

Page Background (9)

Spire.Doc supports to insert the text and image watermark to the word document in C#. It also supports to remove the text and image watermark from the word document. This article will demonstrate how to remove the watermark from the word document by setting the watermark object as null.

Firstly, view the word document with Text and Image watermark:

Remove the text and image watermark from the word document in C#

Remove the text and image watermark from the word document in C#

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

Document doc = new Document();
doc.LoadFromFile("Sample.docx",FileFormat.Docx2010);

Step 2: Set the watermark as null to remove the text and image watermark.

doc.Watermark = null;

Step 3: Save the document to file.

doc.SaveToFile("RemoveWatermark.docx",FileFormat.Docx2010);

Effective screenshot after removing the watermark:

Remove the text and image watermark from the word document in C#

Full codes of removing the watermark from the word document:

Document doc = new Document();
doc.LoadFromFile("Sample.docx",FileFormat.Docx2010);

doc.Watermark = null;

doc.SaveToFile("RemoveWatermark.docx",FileFormat.Docx2010);

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#:

//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#::

//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#

Thursday, 06 April 2017 08:11

Set image background for word document in C#

Written by support iceblue

We have already demonstrated how to set gradient background for word document in C#. Besides the gradient background, Spire.Doc also supports to add image background for word document. This article will show you how to set image background for word document in C#.

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

Document document = new Document();
document.LoadFromFile("Sample.docx", FileFormat.Docx2010);

Step 2: Set the background type as picture.

document.Background.Type = BackgroundType.Picture;

Step 3: Load the image from file.

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

Step 4: Save the document to file.

document.SaveToFile("Result.docx", FileFormat.Docx2010);

Effective screenshot of adding image background for word document:

Set image background for word document in C#

Full codes of how to set image background for word document:

{
    Document document = new Document();
    document.LoadFromFile("Sample.docx", FileFormat.Docx2010);

    document.Background.Type = BackgroundType.Picture;
                     
    document.Background.Picture = Image.FromFile("background.jpg");
       
    document.SaveToFile("Result.docx", FileFormat.Docx2010);
}

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"); 
        }
    }
}
Wednesday, 01 July 2015 03:07

How to set gradient background for Word in C#

Written by support iceblue

Gradient can be used for the Word background to catch the eye. In MS Word, we could choose one color or two colors for the gradient, set the shading style and shading variant of the gradient. It's worthy of mention that Spire.Doc supports to set gradient background in C# with all the options mentioned above. This article is going to introduce the simple code to achieve the feature in C# with Spire.Doc.

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

Step 1: Load the sample document that only has text.

Document doc = new Document("Sample.docx");

Step 2: Set the background type as Gradient.

doc.Background.Type = BackgroundType.Gradient;
BackgroundGradient Test =doc.Background.Gradient;

Step 3: Set the first color and second color for Gradient.

Test.Color1 = Color.White;
Test.Color2 = Color.Green;

Step 4: Set the Shading style and Variant for the gradient.

Test.ShadingVariant = GradientShadingVariant.ShadingDown;
Test.ShadingStyle = GradientShadingStyle.Horizontal;

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

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

Sample:

How to set gradient background for Word in C#

Effects:

How to set gradient background for Word in C#

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 ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document("Sample.docx");

            doc.Background.Type = BackgroundType.Gradient;
            BackgroundGradient Test =doc.Background.Gradient;
            Test.Color1 = Color.White;
            Test.Color2 = Color.Green;
            Test.ShadingVariant = GradientShadingVariant.ShadingDown;
            Test.ShadingStyle = GradientShadingStyle.Horizontal;

            doc.SaveToFile("Result.docx", FileFormat.Docx);
            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);

        }
    }
}
Thursday, 16 January 2014 08:50

How to Set Word Paragraph Shading in C#

Written by Administrator

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#]
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);
        }
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.

Text watermark and image watermark are all watermarks in Word document. The difference lies in that 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 show you solutions to add both text watermark and image watermark via a .NET Word component in C#, VB.NET.

Spire.Doc for .NET, a reliable .NET Word component, enables you to generate, read, edit, convert even print your Word document in a fast speed. Using Spire.Doc for .NET, you can easily add both text watermark and image watermark in your Word document in C#, VB.NET. Please see the target Word document with image watermark as below picture:

Word Watermarks

Here, you can download Spire.Doc for .NET and install it in system. After adding Spire.Doc dll, you can perform adding Word watermark task by below soluctions.

Add Image Watermark in Word with C#, VB.NET

There are four lines of key code for you to add your image watermark. First we need to create a new instance of the Spire.Doc.PictureWatermark named picture. Then, load an image from system by calling the method: System.Drawing.Image FromFile(string filename). The third line enables you to set the image watermark scaling when adding to Word file. While in the last step, you can set the image that you just load to be Word watermark. Please see the code below:

[C#]
PictureWatermark picture = new PictureWatermark();
picture.Picture = System.Drawing.Image.FromFile(@"..\imagess.jpeg");
picture.Scaling = 250;
document.Watermark = picture;
[VB.NET]
Dim picture As PictureWatermark = New PictureWatermark
picture.Picture = System.Drawing.Image.FromFile("..\imagess.jpeg")
picture.Scaling = 250
document.Watermark = picture

Add Text Watermark in Word with C#, VB.NET

This solution is as easy as the solution above. After creating a new instance of Spire.Doc.TextWatermark class which is called txtWatermark, you can set the text which will be shown in Word document. Then, by setting the property TextWatermark.FontSize and TextWatermark.Layout, you can decide the text size and layout of your text watermark. Finally, set the text watermark to be the one you just defined.

[C#]
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.Text = "Microsoft";
txtWatermark.FontSize = 90;
txtWatermark.Layout = WatermarkLayout.Diagonal;
document.Watermark = txtWatermark;
[VB.NET]
Dim txtWatermark As TextWatermark = New TextWatermark
txtWatermark.Text = "Microsoft"
txtWatermark.FontSize = 90
txtWatermark.Layout = WatermarkLayout.Diagonal
document.Watermark = txtWatermark

Spire.Doc for .NET is specially designed for creating, reading, writing, manipulating Word documents in your .NET applications. With this .NET Word component, you can perform a wide range of tasks directly.