Spire.Doc
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");

        }
    }
}

Sometimes in one Word document developers need to add page numbers for different sections, for example cover, directory and content are in different sections. This article talks about how to add page numbers for different sections via Spire.Doc.

Here will import a test document within 3 sections inside as followed screenshot.

Add page numbers in different sections in Word document via Spire.Doc

Here are the detailed steps:

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

Document document = new Document("test.docx");

Step 2: Create footer for the first section and add page number inside.

HeaderFooter footer = document.Sections[0].HeadersFooters.Footer;
Paragraph footerParagraph = footer.AddParagraph();
footerParagraph.AppendField("page number", FieldType.FieldPage);
footerParagraph.AppendText(" of ");
footerParagraph.AppendField("number of pages", FieldType.FieldSectionPages);
footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;

Step 3: Restart page number of next section and set the starting page number to 1.

document.Sections[1].PageSetup.RestartPageNumbering = true;
document.Sections[1].PageSetup.PageStartingNumber = 1;

Step 4: Repeat step2 and Step3 for the rest sections, so change the code with for loop.

for (int i = 0; i < 3; i++)
            {
                HeaderFooter footer = document.Sections[i].HeadersFooters.Footer;
                Paragraph footerParagraph = footer.AddParagraph();
                footerParagraph.AppendField("page number", FieldType.FieldPage);
                footerParagraph.AppendText(" of ");
                footerParagraph.AppendField("number of pages", FieldType.FieldSectionPages);
                footerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right;

                if (i == 2)
                    break;
                else
                {
                    document.Sections[i + 1].PageSetup.RestartPageNumbering = true;
                    document.Sections[i + 1].PageSetup.PageStartingNumber = 1;
                }
            }

Step 5: Save and review.

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

Result screenshot:

Add page numbers in different sections in Word document via Spire.Doc

In the MS Word Header & Footer Tools options, we could choose "Different First Page" and "Different odd and even pages". The article "How to create different headers/footers for odd and even pages" introduces the method to set different odd and even pages using Spire.Doc. Spire.DOC also provides an easy and quick method to add different first page header & footer. This article is going to introduce the method to add different first page header & footer.

FYI, if you only need the first page header and footer, please just set the first page header & footer and leave the rest alone. In this way, your Word document will only have header & footer in the first page, which provides a simpler way to add a header only into the first page of a document than the method mentioned in the article "How to add a header only into the first page of a document".

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 contains text.

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

Step 2: Get the section and set the property true.

Section section = document.Sections[0];
section.PageSetup.DifferentFirstPageHeaderFooter = true;

Step 3: Set the first page header. Here we append a picture as the header.

Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();
paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right;           
DocPicture headerimage = paragraph1.AppendPicture(Image.FromFile("2.bmp"));

Step 4: Set the first page footer.

Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();
paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange FF = paragraph2.AppendText("First Page Footer");
FF.CharacterFormat.FontSize = 20;

Step 5: Set the other header & footer. If you only need the first page header & footer, don't set this.

Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange NH = paragraph3.AppendText("If you only need first page header, don't set this.");
NH.CharacterFormat.FontSize = 20;

Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();
paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Center;
TextRange NF = paragraph4.AppendText("If you only need first page footer, don't set this.");
NF.CharacterFormat.FontSize = 20;

Step 6: save the document and launch to see effects.

document.SaveToFile("R.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("R.docx");

Effects:

How to add different first page header & footer

How to add different first page header & footer

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("T.docx");

            Section section = document.Sections[0];
            section.PageSetup.DifferentFirstPageHeaderFooter = true;

            Paragraph paragraph1 = section.HeadersFooters.FirstPageHeader.AddParagraph();
            paragraph1.Format.HorizontalAlignment = HorizontalAlignment.Right;           
            DocPicture headerimage = paragraph1.AppendPicture(Image.FromFile("2.bmp"));

            Paragraph paragraph2 = section.HeadersFooters.FirstPageFooter.AddParagraph();
            paragraph2.Format.HorizontalAlignment = HorizontalAlignment.Center;            
            TextRange FF = paragraph2.AppendText("First Page Footer");
            FF.CharacterFormat.FontSize = 20;

            Paragraph paragraph3 = section.HeadersFooters.Header.AddParagraph();
            paragraph3.Format.HorizontalAlignment = HorizontalAlignment.Center;
            TextRange NH = paragraph3.AppendText("If you only need first page header, don't set this.");
            NH.CharacterFormat.FontSize = 20;

            Paragraph paragraph4 = section.HeadersFooters.Footer.AddParagraph();
            paragraph4.Format.HorizontalAlignment = HorizontalAlignment.Center;
            TextRange NF = paragraph4.AppendText("If you only need first page footer, don't set this.");
            NF.CharacterFormat.FontSize = 20;
           
            document.SaveToFile("R.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("R.docx");
        }
    }
}
Page 1 of 31