1. 程式人生 > >C# 給Word不同頁面設定不同背景

C# 給Word不同頁面設定不同背景

給Word文件設定背景時,通常只能針對整篇文件設定統一的背景,如果需要對某些頁面單獨設定背景,則需要通過另外的方式來實現。本文通過C# 程式程式碼演示如何來實現。並附VB.NET程式碼作參考。

思路:通過在頁首中新增形狀或者圖片,並將形狀或圖片平鋪(即設定形狀或圖片大小為頁面大小)到整個頁面。新增背景時,通過新增形狀並設定形狀顏色來設定成純色背景效果;通過新增圖片來實現圖片背景效果。

本次程式執行環境中包括:引入Spire.Doc.dll;.Net Framework 4.5.1

設定不同背景時,分以下2種情況:

1. 只需設定首頁背景和其他頁面不同

1.1 設定純色背景

【C#】

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

namespace DifferentBackground1
{
    class Program
    {
        static void Main(string[] args)
        {
            //載入Word測試文件
            Document doc = new Document();
            doc.LoadFromFile("測試.docx");

            //獲取第一節
            Section section = doc.Sections[0];

            //設定首頁頁首頁尾不同
            section.PageSetup.DifferentFirstPageHeaderFooter = true;

            //在首頁頁首新增形狀
            HeaderFooter firstpageheader = section.HeadersFooters.FirstPageFooter;//獲取首頁頁首
            firstpageheader.Paragraphs.Clear();//清除頁首預設的段落格式(因預設頁首格式中包含有一條橫線)
            Paragraph firstpara = firstpageheader.AddParagraph();//重新新增段落
            float width = section.PageSetup.PageSize.Width;//獲取頁面寬度、高度
            float height = section.PageSetup.PageSize.Height;
            ShapeObject shape = firstpara.AppendShape(width, height, ShapeType.Rectangle);//新增形狀
            shape.BehindText = true;//設定形狀襯於文字下方
            shape.HorizontalAlignment = ShapeHorizontalAlignment.Center;//設定對齊方式,鋪滿頁面
            shape.VerticalOrigin = VerticalOrigin.TopMarginArea;
            shape.FillColor = Color.LightBlue;//形狀顏色


            //在其他頁面的頁首中新增形狀
            HeaderFooter otherheader = section.HeadersFooters.Header;
            otherheader.Paragraphs.Clear();
            Paragraph otherpara = otherheader.AddParagraph();
            ShapeObject shape1 = otherpara.AppendShape(width, height, ShapeType.Rectangle);
            shape1.BehindText = true;
            shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center;
            shape1.VerticalOrigin = VerticalOrigin.TopMarginArea;
            shape1.FillColor = Color.Pink;


            //儲存文件
            doc.SaveToFile("ColorBackground1.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("ColorBackground1.docx");
        }
    }
}

 

 

 

【VB.NET】

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing

Namespace DifferentBackground1
    Class Program
        Private Shared Sub Main(args As String())
            '載入Word測試文件
            Dim doc As New Document()
            doc.LoadFromFile("測試.docx")

            '獲取第一節
            Dim section As Section = doc.Sections(0)

            '設定首頁頁首頁尾不同
            section.PageSetup.DifferentFirstPageHeaderFooter = True

            '在首頁頁首新增形狀
            Dim firstpageheader As HeaderFooter = section.HeadersFooters.FirstPageFooter
            '獲取首頁頁首
            firstpageheader.Paragraphs.Clear()
            '清除頁首預設的段落格式(因預設頁首格式中包含有一條橫線)
            Dim firstpara As Paragraph = firstpageheader.AddParagraph()
            '重新新增段落
            Dim width As Single = section.PageSetup.PageSize.Width
            '獲取頁面寬度、高度
            Dim height As Single = section.PageSetup.PageSize.Height
            Dim shape As ShapeObject = firstpara.AppendShape(width, height, ShapeType.Rectangle)
            '新增形狀
            shape.BehindText = True
            '設定形狀襯於文字下方
            shape.HorizontalAlignment = ShapeHorizontalAlignment.Center
            '設定對齊方式,鋪滿頁面
            shape.VerticalOrigin = VerticalOrigin.TopMarginArea
            shape.FillColor = Color.LightBlue
            '形狀顏色

            '在其他頁面的頁首中新增形狀
            Dim otherheader As HeaderFooter = section.HeadersFooters.Header
            otherheader.Paragraphs.Clear()
            Dim otherpara As Paragraph = otherheader.AddParagraph()
            Dim shape1 As ShapeObject = otherpara.AppendShape(width, height, ShapeType.Rectangle)
            shape1.BehindText = True
            shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center
            shape1.VerticalOrigin = VerticalOrigin.TopMarginArea
            shape1.FillColor = Color.Pink


            '儲存文件
            doc.SaveToFile("ColorBackground1.docx", FileFormat.Docx2013)
            System.Diagnostics.Process.Start("ColorBackground1.docx")
        End Sub
    End Class
End Namespace
View Code

 

1.2 設定圖片背景

【C#】

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

namespace DifferentBackground1
{
    class Program
    {
        static void Main(string[] args)
        {
             //載入Word測試文件
            Document doc = new Document();
            doc.LoadFromFile("測試.docx");

            //獲取第一節
            Section section = doc.Sections[0];

            //設定首頁頁首頁尾不同
            section.PageSetup.DifferentFirstPageHeaderFooter = true;

            HeaderFooter firstpageheader = section.HeadersFooters.FirstPageFooter;//獲取首頁頁首
            firstpageheader.Paragraphs.Clear();//清除頁首預設的段落格式(因預設頁首格式中包含有一條橫線)
            Paragraph firstpara = firstpageheader.AddParagraph();//重新新增段落

            //獲取頁面寬度、高度
            float width = section.PageSetup.PageSize.Width;
            float height = section.PageSetup.PageSize.Height; 
          
            //新增圖片到首頁頁首
            DocPicture pic0 = firstpara.AppendPicture("1.png");
            pic0.TextWrappingStyle = TextWrappingStyle.Behind;
            pic0.HorizontalAlignment = ShapeHorizontalAlignment.Center;
            pic0.VerticalOrigin = VerticalOrigin.TopMarginArea;
            pic0.Width = width;
            pic0.Height = height;


            //在其他頁面的頁首中新增圖片
            HeaderFooter otherheader = section.HeadersFooters.Header;
            otherheader.Paragraphs.Clear();
            Paragraph otherpara = otherheader.AddParagraph();
            DocPicture pic1 = otherpara.AppendPicture("2.png");
            pic1.TextWrappingStyle = TextWrappingStyle.Behind;
            pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center;
            pic1.VerticalOrigin = VerticalOrigin.TopMarginArea;
            pic1.Width = width;
            pic1.Height = height;


            //儲存文件
            doc.SaveToFile("ImageBackground1.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("ImageBackground1.docx");
        }
    }
}

 

 

【VB.NET】

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace DifferentBackground1
    Class Program
        Private Shared Sub Main(args As String())
            '載入Word測試文件
            Dim doc As New Document()
            doc.LoadFromFile("測試.docx")

            '獲取第一節
            Dim section As Section = doc.Sections(0)

            '設定首頁頁首頁尾不同
            section.PageSetup.DifferentFirstPageHeaderFooter = True

            Dim firstpageheader As HeaderFooter = section.HeadersFooters.FirstPageFooter
            '獲取首頁頁首
            firstpageheader.Paragraphs.Clear()
            '清除頁首預設的段落格式(因預設頁首格式中包含有一條橫線)
            Dim firstpara As Paragraph = firstpageheader.AddParagraph()
            '重新新增段落
            '獲取頁面寬度、高度
            Dim width As Single = section.PageSetup.PageSize.Width
            Dim height As Single = section.PageSetup.PageSize.Height

            '新增圖片到首頁頁首
            Dim pic0 As DocPicture = firstpara.AppendPicture("1.png")
            pic0.TextWrappingStyle = TextWrappingStyle.Behind
            pic0.HorizontalAlignment = ShapeHorizontalAlignment.Center
            pic0.VerticalOrigin = VerticalOrigin.TopMarginArea
            pic0.Width = width
            pic0.Height = height


            '在其他頁面的頁首中新增圖片
            Dim otherheader As HeaderFooter = section.HeadersFooters.Header
            otherheader.Paragraphs.Clear()
            Dim otherpara As Paragraph = otherheader.AddParagraph()
            Dim pic1 As DocPicture = otherpara.AppendPicture("2.png")
            pic1.TextWrappingStyle = TextWrappingStyle.Behind
            pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center
            pic1.VerticalOrigin = VerticalOrigin.TopMarginArea
            pic1.Width = width
            pic1.Height = height


            '儲存文件
            doc.SaveToFile("ImageBackground1.docx", FileFormat.Docx2013)
            System.Diagnostics.Process.Start("ImageBackground1.docx")
        End Sub
    End Class
End Namespace
View Code

 

2. 設定指定多個頁面背景不同

注:給多個頁面設定不同背景時,需要通過獲取不同節的頁首來設定,本次程式環境中所用源文件已設定了多個節,如需手動設定分節,可參考程式碼:

document.Sections[0].Paragraphs[0].InsertSectionBreak(SectionBreakType.NoBreak);

2.1 設定純色背景

【C#】

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

namespace DifferentBackground2
{
    class Program
    {
        static void Main(string[] args)
        {
            //載入Word文件
            Document doc = new Document();
            doc.LoadFromFile("測試.docx");

            //獲取第一節
            Section section1 = doc.Sections[0];

            //獲取頁面寬度、高度
            float width = section1.PageSetup.PageSize.Width;
            float height = section1.PageSetup.PageSize.Height;

            //新增圖片到頁首
            HeaderFooter header1 = section1.HeadersFooters.Header;
            header1.Paragraphs.Clear();
            Paragraph para1 = header1.AddParagraph();
            ShapeObject shape1 = para1.AppendShape(width, height, ShapeType.Rectangle);//新增形狀
            shape1.BehindText = true;//設定形狀襯於文字下方
            shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center;//設定對齊方式,鋪滿頁面
            shape1.VerticalOrigin = VerticalOrigin.TopMarginArea;
            shape1.FillColor = Color.LightSalmon;//形狀顏色

            //同理設定第二節頁首中的圖片
            Section section2 = doc.Sections[1];
            HeaderFooter header2 = section2.HeadersFooters.Header;
            header2.Paragraphs.Clear();
            Paragraph para2 = header2.AddParagraph();
            ShapeObject shape2 = para2.AppendShape(width, height, ShapeType.Rectangle);//新增形狀
            shape2.BehindText = true;//設定形狀襯於文字下方
            shape2.HorizontalAlignment = ShapeHorizontalAlignment.Center;//設定對齊方式,鋪滿頁面
            shape2.VerticalOrigin = VerticalOrigin.TopMarginArea;
            shape2.FillColor = Color.Moccasin;//形狀顏色

            //同理設定第三節中的頁首中的圖片
            Section section3 = doc.Sections[2];
            HeaderFooter header3 = section3.HeadersFooters.Header;
            header3.Paragraphs.Clear();
            Paragraph para3 = header3.AddParagraph();
            ShapeObject shape3 = para3.AppendShape(width, height, ShapeType.Rectangle);//新增形狀
            shape3.BehindText = true;//設定形狀襯於文字下方
            shape3.HorizontalAlignment = ShapeHorizontalAlignment.Center;//設定對齊方式,鋪滿頁面
            shape3.VerticalOrigin = VerticalOrigin.TopMarginArea;
            shape3.FillColor = Color.LawnGreen;//形狀顏色

            //儲存文件
            doc.SaveToFile("ColorBackground2.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("ColorBackground2.docx");
        }
    }
}

 

 

【VB.NET】

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Imports System.Drawing

Namespace DifferentBackground2
    Class Program
        Private Shared Sub Main(args As String())
            '載入Word文件
            Dim doc As New Document()
            doc.LoadFromFile("測試.docx")

            '獲取第一節
            Dim section1 As Section = doc.Sections(0)

            '獲取頁面寬度、高度
            Dim width As Single = section1.PageSetup.PageSize.Width
            Dim height As Single = section1.PageSetup.PageSize.Height

            '新增圖片到頁首
            Dim header1 As HeaderFooter = section1.HeadersFooters.Header
            header1.Paragraphs.Clear()
            Dim para1 As Paragraph = header1.AddParagraph()
            Dim shape1 As ShapeObject = para1.AppendShape(width, height, ShapeType.Rectangle)
            '新增形狀
            shape1.BehindText = True
            '設定形狀襯於文字下方
            shape1.HorizontalAlignment = ShapeHorizontalAlignment.Center
            '設定對齊方式,鋪滿頁面
            shape1.VerticalOrigin = VerticalOrigin.TopMarginArea
            shape1.FillColor = Color.LightSalmon
            '形狀顏色
            '同理設定第二節頁首中的圖片
            Dim section2 As Section = doc.Sections(1)
            Dim header2 As HeaderFooter = section2.HeadersFooters.Header
            header2.Paragraphs.Clear()
            Dim para2 As Paragraph = header2.AddParagraph()
            Dim shape2 As ShapeObject = para2.AppendShape(width, height, ShapeType.Rectangle)
            '新增形狀
            shape2.BehindText = True
            '設定形狀襯於文字下方
            shape2.HorizontalAlignment = ShapeHorizontalAlignment.Center
            '設定對齊方式,鋪滿頁面
            shape2.VerticalOrigin = VerticalOrigin.TopMarginArea
            shape2.FillColor = Color.Moccasin
            '形狀顏色
            '同理設定第三節中的頁首中的圖片
            Dim section3 As Section = doc.Sections(2)
            Dim header3 As HeaderFooter = section3.HeadersFooters.Header
            header3.Paragraphs.Clear()
            Dim para3 As Paragraph = header3.AddParagraph()
            Dim shape3 As ShapeObject = para3.AppendShape(width, height, ShapeType.Rectangle)
            '新增形狀
            shape3.BehindText = True
            '設定形狀襯於文字下方
            shape3.HorizontalAlignment = ShapeHorizontalAlignment.Center
            '設定對齊方式,鋪滿頁面
            shape3.VerticalOrigin = VerticalOrigin.TopMarginArea
            shape3.FillColor = Color.LawnGreen
            '形狀顏色
            '儲存文件
            doc.SaveToFile("ColorBackground2.docx", FileFormat.Docx2013)
            System.Diagnostics.Process.Start("ColorBackground2.docx")
        End Sub
    End Class
End Namespace
View Code

 

2.2 設定圖片背景

【C#】

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

namespace DifferentBackground2
{
    class Program
    {
        static void Main(string[] args)
        {
            //載入Word文件
            Document doc = new Document();
            doc.LoadFromFile("測試.docx");

            //獲取第一節
            Section section1 = doc.Sections[0];
            //新增圖片到頁首
            HeaderFooter header1 = section1.HeadersFooters.Header;
            header1.Paragraphs.Clear();
            Paragraph para1 = header1.AddParagraph();
            DocPicture pic1 = para1.AppendPicture("1.png");
            pic1.TextWrappingStyle = TextWrappingStyle.Behind;
            pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center;
            pic1.VerticalOrigin = VerticalOrigin.TopMarginArea;
            float width = section1.PageSetup.PageSize.Width;
            float height = section1.PageSetup.PageSize.Height;
            pic1.Width = width;
            pic1.Height = height;

            //同理設定第二節頁首中的圖片
            Section section2 = doc.Sections[1];
            HeaderFooter header2 = section2.HeadersFooters.Header;
            header2.Paragraphs.Clear();
            Paragraph para2 = header2.AddParagraph();
            DocPicture pic2 = para2.AppendPicture("2.png");
            pic2.TextWrappingStyle = TextWrappingStyle.Behind;
            pic2.HorizontalAlignment = ShapeHorizontalAlignment.Center;
            pic2.VerticalOrigin = VerticalOrigin.TopMarginArea; 
            pic2.Width = width;
            pic2.Height = height;

            //同理設定第三節中的頁首中的圖片
            Section section3 = doc.Sections[2];
            HeaderFooter header3 = section3.HeadersFooters.Header;
            header3.Paragraphs.Clear();
            Paragraph para3 = header3.AddParagraph();
            DocPicture pic3 = para3.AppendPicture("3.png");
            pic3.TextWrappingStyle = TextWrappingStyle.Behind;
            pic3.HorizontalAlignment = ShapeHorizontalAlignment.Center;
            pic3.VerticalOrigin = VerticalOrigin.TopMarginArea;
            pic3.Width = width;
            pic3.Height = height;

            //儲存文件
            doc.SaveToFile("ImageBackground2.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("ImageBackground2.docx");
        }
    }
}

 

 

【VB.NET】

Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields

Namespace DifferentBackground2
    Class Program
        Private Shared Sub Main(args As String())
            '載入Word文件
            Dim doc As New Document()
            doc.LoadFromFile("測試.docx")

            '獲取第一節
            Dim section1 As Section = doc.Sections(0)
            '新增圖片到頁首
            Dim header1 As HeaderFooter = section1.HeadersFooters.Header
            header1.Paragraphs.Clear()
            Dim para1 As Paragraph = header1.AddParagraph()
            Dim pic1 As DocPicture = para1.AppendPicture("1.png")
            pic1.TextWrappingStyle = TextWrappingStyle.Behind
            pic1.HorizontalAlignment = ShapeHorizontalAlignment.Center
            pic1.VerticalOrigin = VerticalOrigin.TopMarginArea
            Dim width As Single = section1.PageSetup.PageSize.Width
            Dim height As Single = section1.PageSetup.PageSize.Height
            pic1.Width = width
            pic1.Height = height

            '同理設定第二節頁首中的圖片
            Dim section2 As Section = doc.Sections(1)
            Dim header2 As HeaderFooter = section2.HeadersFooters.Header
            header2.Paragraphs.Clear()
            Dim para2 As Paragraph = header2.AddParagraph()
            Dim pic2 As DocPicture = para2.AppendPicture("2.png")
            pic2.TextWrappingStyle = TextWrappingStyle.Behind
            pic2.HorizontalAlignment = ShapeHorizontalAlignment.Center
            pic2.VerticalOrigin = VerticalOrigin.TopMarginArea
            pic2.Width = width
            pic2.Height = height

            '同理設定第三節中的頁首中的圖片
            Dim section3 As Section = doc.Sections(2)
            Dim header3 As HeaderFooter = section3.HeadersFooters.Header
            header3.Paragraphs.Clear()
            Dim para3 As Paragraph = header3.AddParagraph()
            Dim pic3 As DocPicture = para3.AppendPicture("3.png")
            pic3.TextWrappingStyle = TextWrappingStyle.Behind
            pic3.HorizontalAlignment = ShapeHorizontalAlignment.Center
            pic3.VerticalOrigin = VerticalOrigin.TopMarginArea
            pic3.Width = width
            pic3.Height = height

            '儲存文件
            doc.SaveToFile("ImageBackground2.docx", FileFormat.Docx2013)
            System.Diagnostics.Process.Start("ImageBackground2.docx")
        End Sub
    End Class
End Namespace
View Code