C\#Word 페이지 마다 배경 설정

18720 단어 C#Word배경 설정
Word 문서 에 배경 을 설정 할 때 보통 전체 문서 에 대해 통 일 된 배경 만 설정 할 수 있 습 니 다.일부 페이지 에 배경 을 따로 설정 하려 면 다른 방식 으로 이 루어 져 야 합 니 다.본 고 는 C\#프로그램 코드 를 통 해 어떻게 실현 하 는 지 보 여 준다.VB.NET 코드 를 참고 로 첨부 합 니 다.
사고:머 릿 말 에 모양 이나 그림 을 추가 하고 모양 이나 그림 을 평평 하 게 펴 서(즉,모양 이나 그림 크기 를 페이지 크기 로 설정 하 는 것)전체 페이지 에 보 냅 니 다.배경 을 추가 할 때 모양 을 추가 하고 모양 색 을 설정 하여 단색 배경 효과 로 설정 합 니 다.그림 추가 로 그림 배경 효 과 를 실현 합 니 다.
이번 프로그램 운영 환경 은 다음 과 같다.Net Framework 4.5.1
서로 다른 배경 을 설정 할 때 다음 과 같은 두 가지 상황 으로 나 뉜 다.
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
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
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
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
C\#Word 의 서로 다른 페이지 에 서로 다른 배경 을 설정 하 는 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 관련 C\#Word 에 배경 내용 을 설정 하 는 것 은 예전 의 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 많은 응원 부 탁 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기