npio操作Word怎样让页眉居中 求教啊啊啊啊啊啊啊啊啊啊
直接设置页面段落居中headerPara.Format.HorizontalAlignment = HorizontalAlignment.Center;//设置页眉居中,下面是引入的免费版spire.doc.dll到程序的c#例子,用npoi,方法应该类似:
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;
using Spire.Doc.Fields;
namespace AddHeaderAndFooter
{
class Program
{
static void Main(string[] args)
{
//创建一个Document类实例,添加section
Document document = new Document("test.docx");
Section sec = document.Sections[0];
//添加页眉、页脚
HeaderFooter header = sec.HeadersFooters.Header;
Paragraph headerPara = header.AddParagraph();
headerPara.Format.HorizontalAlignment = HorizontalAlignment.Center;//设置页眉居中
HeaderFooter footer = sec.HeadersFooters.Footer;
Paragraph footerPara = footer.AddParagraph();
footerPara.Format.HorizontalAlignment = HorizontalAlignment.Center;//设置页脚居中
//添加文本和图片到页眉,并设置文本格式
//DocPicture headerImage = headerPara.AppendPicture(Image.FromFile("g.png"));
TextRange TR = headerPara.AppendText("页眉文字");
TR.CharacterFormat.FontName = "Andalus";
TR.CharacterFormat.FontSize = 12;
TR.CharacterFormat.TextColor = Color.Green;
TR.CharacterFormat.Bold = false;
//添加文本到页脚,并设置格式
TR = footerPara.AppendText("Copyright © 2021 All Rights Reserved.");
TR.CharacterFormat.Bold = false;
TR.CharacterFormat.FontSize = 9;
//保存文档
document.SaveToFile("output.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("output.docx");
}
}
}
效果图: