Tab 1
此 Demo 展示如何设置 Word 文档的页面大小、页眉和页脚,以及如何将图片设置为页眉/页脚的背景。
如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo。
如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。。
Tab 2
using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace DemoOnlineCode
{
class HeaderAndFooter
{
public void demoHeaderAndFooter(String docFile,
String headerImageFile = null,
String footerImageFile = null,
string pageSizeName = "A4",
String headerFontColor = null,
String footerFontColor = null)
{
Document document = new Document(docFile, FileFormat.Auto);
SizeF pageSize = (SizeF)typeof(PageSize).GetField(pageSizeName).GetValue(null);
PageSetup(document, pageSize);
SetHeader(document, ConvertStrToColor(headerFontColor), headerImageFile);
SetFooter(document,ConvertStrToColor(footerFontColor), footerImageFile);
document.SaveToFile("demo.doc", FileFormat.Doc);
}
private void PageSetup(Document document, SizeF pageSize)
{
for (int i = 0; i < document.Sections.Count; i++)
{
Section section = document.Sections[i];
section.PageSetup.PageSize = pageSize;
section.PageSetup.Margins.Top = (float)72.0;
section.PageSetup.Margins.Bottom = (float)72.0;
section.PageSetup.Margins.Left = (float)89.0;
section.PageSetup.Margins.Right = (float)89.0;
}
}
private void SetFooter(Document document,
Color fontColor,
string footerImageFile = null,
string footerText = "Spire.Doc",
string fontName = "Calibri",
float fontSize = (float)12.0,
HorizontalAlignment alignment = HorizontalAlignment.Left)
{
for (int i = 0; i < document.Sections.Count; i++)
{
Section section = document.Sections[i];
HeaderFooter footer = section.HeadersFooters.Footer;
Paragraph footerParagraph = footer.AddParagraph();
TextRange text = footerParagraph.AppendText(footerText);
text.CharacterFormat.FontName = fontName;
text.CharacterFormat.FontSize = fontSize;
text.CharacterFormat.TextColor = fontColor;
footerParagraph.Format.HorizontalAlignment = alignment;
//border
footerParagraph.Format.Borders.Top.BorderType
= (BorderStyle)Spire.Doc.Documents.BorderStyle.Hairline;
footerParagraph.Format.Borders.Top.Space = (float)0.03;
if (footerImageFile != null)
{
Byte[] imagedata = System.IO.File.ReadAllBytes(footerImageFile);
//insert picture to footer
DocPicture footerPicture = footerParagraph.AppendPicture(imagedata);
//footer picture layout
footerPicture.TextWrappingStyle = TextWrappingStyle.Behind;
footerPicture.HorizontalOrigin = HorizontalOrigin.Page;
footerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left;
footerPicture.VerticalOrigin = VerticalOrigin.Page;
footerPicture.VerticalAlignment = ShapeVerticalAlignment.Bottom;
}
//insert page number
footerParagraph = footer.AddParagraph();
footerParagraph.AppendField("page number", FieldType.FieldPage);
footerParagraph.AppendText("of");
footerParagraph.AppendField("number of pages", FieldType.FieldNumPages);
footerParagraph.Format.HorizontalAlignment
= (HorizontalAlignment)HorizontalAlignment.Right;
}
}
private void SetHeader(Document document,
Color fontColor,
string headerImageFile = null,
string headerText = "E-iceblue",
string fontName = "Calibri",
float fontSize = (float)12.0,
HorizontalAlignment alignment = HorizontalAlignment.Center)
{
for (int i = 0; i < document.Sections.Count; i++)
{
Section section = document.Sections[i];
HeaderFooter header = section.HeadersFooters.Header;
//insert picture and text to header
Paragraph headerParagraph = header.AddParagraph();
if (headerImageFile != null)
{
Byte[] imagedata = System.IO.File.ReadAllBytes(headerImageFile);
DocPicture headerPicture = headerParagraph.AppendPicture(imagedata);
//header picture layout - text wrapping
headerPicture.TextWrappingStyle = TextWrappingStyle.Behind;
//header picture layout - position
headerPicture.HorizontalOrigin = HorizontalOrigin.Page;
headerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left;
headerPicture.VerticalOrigin = VerticalOrigin.Page;
headerPicture.VerticalAlignment = ShapeVerticalAlignment.Top;
}
//header text
TextRange text = headerParagraph.AppendText(headerText);
text.CharacterFormat.FontName = fontName;
text.CharacterFormat.FontSize = Convert.ToSingle(fontSize);
text.CharacterFormat.TextColor = fontColor;
headerParagraph.Format.HorizontalAlignment = alignment;
//border
headerParagraph.Format.Borders.Bottom.BorderType
= (Spire.Doc.Documents.BorderStyle)Spire.Doc.Documents.BorderStyle.Hairline;
headerParagraph.Format.Borders.Bottom.Space = (float)0.03;
}
}
private Color ConvertStrToColor(string strColor)
{
if (String.IsNullOrWhiteSpace(strColor))
{
return Color.Empty;
}
else
{
return ColorTranslator.FromHtml("#" + strColor);
}
}
}
}
Tab 3
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports Spire.Doc.Fields
Namespace DemoOnlineCode
Class HeaderAndFooter
Public Sub demoHeaderAndFooter(docFile As [String],
Optional headerImageFile As [String] = Nothing,
Optional footerImageFile As [String] = Nothing,
Optional pageSizeName As String = "A4",
Optional headerFontColor As [String] = Nothing,
Optional footerFontColor As [String] = Nothing)
Dim document As New Document(docFile, FileFormat.Auto)
Dim pageSize As SizeF _
= CType(GetType(PageSize).GetField(pageSizeName).GetValue(Nothing), SizeF)
PageSetup(document, pageSize)
SetHeader(document, ConvertStrToColor(headerFontColor), headerImageFile)
SetFooter(document, ConvertStrToColor(footerFontColor), footerImageFile)
document.SaveToFile("demo.doc", FileFormat.Doc)
End Sub
Private Sub PageSetup(document As Document, pageSize As SizeF)
For i As Integer = 0 To document.Sections.Count - 1
Dim section As Section = document.Sections(i)
section.PageSetup.PageSize = pageSize
section.PageSetup.Margins.Top = CSng(72.0)
section.PageSetup.Margins.Bottom = CSng(72.0)
section.PageSetup.Margins.Left = CSng(89.0)
section.PageSetup.Margins.Right = CSng(89.0)
Next
End Sub
Private Sub SetFooter(document As Document,
fontColor As Color,
Optional footerImageFile As String = Nothing,
Optional footerText As String = "Spire.Doc",
Optional fontName As String = "Calibri",
Optional fontSize As Single = CSng(12.0), _
Optional alignment As HorizontalAlignment = HorizontalAlignment.Left)
For i As Integer = 0 To document.Sections.Count - 1
Dim section As Section = document.Sections(i)
Dim footer As HeaderFooter = section.HeadersFooters.Footer
Dim footerParagraph As Paragraph = footer.AddParagraph()
Dim text As TextRange = footerParagraph.AppendText(footerText)
text.CharacterFormat.FontName = fontName
text.CharacterFormat.FontSize = fontSize
text.CharacterFormat.TextColor = fontColor
footerParagraph.Format.HorizontalAlignment = alignment
'border
footerParagraph.Format.Borders.Top.BorderType _
= DirectCast(Spire.Doc.Documents.BorderStyle.Hairline, BorderStyle)
footerParagraph.Format.Borders.Top.Space = CSng(0.03)
If footerImageFile IsNot Nothing Then
Dim imagedata As [Byte]() = System.IO.File.ReadAllBytes(footerImageFile)
'insert picture to footer
Dim footerPicture As DocPicture = footerParagraph.AppendPicture(imagedata)
'footer picture layout
footerPicture.TextWrappingStyle = TextWrappingStyle.Behind
footerPicture.HorizontalOrigin = HorizontalOrigin.Page
footerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left
footerPicture.VerticalOrigin = VerticalOrigin.Page
footerPicture.VerticalAlignment = ShapeVerticalAlignment.Bottom
End If
'insert page number
footerParagraph = footer.AddParagraph()
footerParagraph.AppendField("page number", FieldType.FieldPage)
footerParagraph.AppendText("of")
footerParagraph.AppendField("number of pages", FieldType.FieldNumPages)
footerParagraph.Format.HorizontalAlignment _
= DirectCast(HorizontalAlignment.Right, HorizontalAlignment)
Next
End Sub
Private Sub SetHeader(document As Document,
fontColor As Color,
Optional headerImageFile As String = Nothing,
Optional headerText As String = "E-iceblue",
Optional fontName As String = "Calibri",
Optional fontSize As Single = CSng(12.0), _
Optional alignment As HorizontalAlignment = HorizontalAlignment.Center)
For i As Integer = 0 To document.Sections.Count - 1
Dim section As Section = document.Sections(i)
Dim header As HeaderFooter = section.HeadersFooters.Header
'insert picture and text to header
Dim headerParagraph As Paragraph = header.AddParagraph()
If headerImageFile IsNot Nothing Then
Dim imagedata As [Byte]() = System.IO.File.ReadAllBytes(headerImageFile)
Dim headerPicture As DocPicture = headerParagraph.AppendPicture(imagedata)
'header picture layout - text wrapping
headerPicture.TextWrappingStyle = TextWrappingStyle.Behind
'header picture layout - position
headerPicture.HorizontalOrigin = HorizontalOrigin.Page
headerPicture.HorizontalAlignment = ShapeHorizontalAlignment.Left
headerPicture.VerticalOrigin = VerticalOrigin.Page
headerPicture.VerticalAlignment = ShapeVerticalAlignment.Top
End If
'header text
Dim text As TextRange = headerParagraph.AppendText(headerText)
text.CharacterFormat.FontName = fontName
text.CharacterFormat.FontSize = Convert.ToSingle(fontSize)
text.CharacterFormat.TextColor = fontColor
headerParagraph.Format.HorizontalAlignment = alignment
'border
headerParagraph.Format.Borders.Bottom.BorderType _
= DirectCast(BorderStyle.Hairline, Spire.Doc.Documents.BorderStyle)
headerParagraph.Format.Borders.Bottom.Space = CSng(0.03)
Next
End Sub
Private Function ConvertStrToColor(strColor As String) As Color
If [String].IsNullOrWhiteSpace(strColor) Then
Return Color.Empty
Else
Return ColorTranslator.FromHtml("#" & strColor)
End If
End Function
End Class
End Namespace