Я пытался создать текстовый документ с номером страницы в заголовке. Я использовал 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 }));
}