C# OpenXml(.net):未格式化SimpleField

C# OpenXml(.net):未格式化SimpleField,c#,.net,ms-word,openxml,C#,.net,Ms Word,Openxml,我试图创建一个页眉带有页码的word文档。我使用SimpleField插入页码和页数。 但是,与文本不同,这些字段不受RunProperties中定义的颜色和字体大小的影响(如下图所示) 如何格式化SimpleField 谢谢 private static void CreateReport(字符串文件名) { 使用(var mem=new MemoryStream()) { 使用(var wordDocument=WordprocessingDocument.Create(mem,Wordp

我试图创建一个页眉带有页码的word文档。我使用SimpleField插入页码和页数。 但是,与文本不同,这些字段不受RunProperties中定义的颜色和字体大小的影响(如下图所示)

如何格式化SimpleField

谢谢

private static void CreateReport(字符串文件名)
{
使用(var mem=new MemoryStream())
{
使用(var wordDocument=WordprocessingDocument.Create(mem,WordprocessingDocumentType.Document,true))
{
//添加主文档部分。
MainDocumentPart mainPart=wordDocument.AddMainDocumentPart();
//创建文档结构
mainPart.Document=新文档();
mainPart.Document.Body=新主体();
//为标题创建段落
var段落=新段落();
var run=段落.AppendChild(new run());
Append(新的RunProperties()
{
Bold=新的Bold(),
FontSize=新的FontSize(){Val=“48”},
Color=new Color(){Val=“FF0000”/*red*/}
}
);
Append(newtext(){Text=“Page:”});
Append(新的SimpleField(){Instruction=@“PAGE”});
Append(新文本(){Text=“/”});
Append(新SimpleField(){Instruction=@“SECTIONPAGES”});
//在标题中添加段落
将段落添加到标题(主要部分,段落);
//保存文档
wordDocument.SaveAs(文件名);
}
}
}
私有静态void AddParagraphToHeader(main文档部分main部分段落)
{
var part=mainPart.AddNewPart();
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");
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-MicrosoftCOM: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");
段落.RsidParagraphAddition=“00164C17”;
段落.rsidrunaditiondefault=“00164C17”;
标题.附加(段落);
部分标题=标题;
var headerPartId=mainPart.GetIdOfPart(部分);
mainPart.Document.PrependChild((新的HeaderReference(){Id=headerPartId}));
}

我从未在Open XML中尝试过这一点,但在UI中,为了强制字段结果选择应用于第一个字符的格式(字段代码括号
}
),需要使用
\*CharFormat
开关。生成的字段代码如下所示:
{PAGE\*CharFormat}
因此,我假设需要分配
指令
@“PAGE\*CharFormat”
我从未在打开的XML中尝试过,但在UI中尝试过,以便强制字段结果提取应用于第一个字符的格式(字段代码括号
}
)它需要
\*CharFormat
开关。生成的字段代码看起来像:
{PAGE\*CharFormat}
因此,我假设需要分配
指令:
@“PAGE\*CharFormat”
    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 }));
    }