我试图用页眉中的页码创建一个word文档。我使用SimpleField插入页码和页数。 但是,与文本不同(如下面的图片所示),这些字段不受RunProperties中定义的颜色和字体大小的影响。
如何格式化SimpleField?
谢谢
private static void CreateReport(string filename)
{
using (var mem = new MemoryStream())
{
using (var wordDocument = WordprocessingDocument.Create(mem,WordprocessingDocumentType.Document,true))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
// Create the document structure
mainPart.Document = new Document();
mainPart.Document.Body = new Body();
//Create paragraph for header
var paragraph = new Paragraph();
var run = paragraph.AppendChild(new Run());
run.Append(new RunProperties()
{
Bold = new Bold(),FontSize = new FontSize() { Val = "48" },Color = new Color() { Val = "FF0000" /*red*/ }
}
);
run.Append(new Text() { Text = "Page:" });
run.Append(new SimpleField() { Instruction = @"PAGE" });
run.Append(new Text() { Text = "/" });
run.Append(new SimpleField() { Instruction = @"SECTIONPAGES" });
//Add paragraph to header
AddParagraphToHeader(mainPart,paragraph);
//Save document
wordDocument.SaveAs(filename);
}
}
}
private static void AddParagraphToHeader(MainDocumentPart mainPart,Paragraph paragraph)
{
var part = mainPart.AddNewPart<HeaderPart>();
var header = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
header.AddNamespaceDeclaration("wpc","http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
header.AddNamespaceDeclaration("mc","http://schemas.openxmlformats.org/markup-compatibility/2006");
header.AddNamespaceDeclaration("o","urn:schemas-microsoft-com:office:office");
header.AddNamespaceDeclaration("r","http://schemas.openxmlformats.org/officeDocument/2006/relationships");
header.AddNamespaceDeclaration("m","http://schemas.openxmlformats.org/officeDocument/2006/math");
header.AddNamespaceDeclaration("v","urn:schemas-microsoft-com:vml");
header.AddNamespaceDeclaration("wp14","http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
header.AddNamespaceDeclaration("wp","http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
header.AddNamespaceDeclaration("w10","urn:schemas-microsoft-com:office:word");
header.AddNamespaceDeclaration("w","http://schemas.openxmlformats.org/wordprocessingml/2006/main");
header.AddNamespaceDeclaration("w14","http://schemas.microsoft.com/office/word/2010/wordml");
header.AddNamespaceDeclaration("wpg","http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
header.AddNamespaceDeclaration("wpi","http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
header.AddNamespaceDeclaration("wne","http://schemas.microsoft.com/office/word/2006/wordml");
header.AddNamespaceDeclaration("wps","http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
paragraph.RsidParagraphAddition = "00164C17";
paragraph.RsidRunAdditionDefault = "00164C17";
header.Append(paragraph);
part.Header = header;
var headerPartId = mainPart.GetIdOfPart(part);
mainPart.Document.PrependChild<HeaderReference>((new HeaderReference() { Id = headerPartId }));
}