简体   繁体   English

如何在打开的xml文档上保留样式

[英]How to keep style on open xml documents

I am using open XML(Microsoft Word - .docx) as a file template to automatically generate other documents. 我使用开放XML(Microsoft Word - .docx)作为文件模板来自动生成其他文档。 In the template document I have defined content controls, and I have written code to replace content in these content controls. 在模板文档中,我定义了内容控件,并且我编写了代码来替换这些内容控件中的内容。

The content is replaced and the documents are generated, but I am struggling with keeping the style. 内容被替换并生成文档,但我正在努力保持风格。 In Word, when inspecting properties of the content control, I have checked the checbox for "Use a style to format text into the empty control: style", and also checked for "Remove content controls when content are edited". 在Word中,当检查内容控件的属性时,我检查了checbox“使用样式将文本格式化为空控件:样式”,并检查“在编辑内容时删除内容控件”。 This doesn't seem to have any impact when documents are generated by code. 当代码生成文档时,这似乎没有任何影响。

以下是我在Word中设置内容控件的属性的方法 This is my code (which a community member here was kind enough to help with) for replacing the data in the content controls. 这是我的代码(这里的社区成员非常友好地帮助)来替换内容控件中的数据。 Any ideas what I should do in order to keep the formatting? 任何想法,我应该做什么,以保持格式? The formatting is simple text formatting, like size and font. 格式化是简单的文本格式,如大小和字体。 Please advice: 请指教:

    private static void ReplaceTags(MainDocumentPart mainPart, string tagName, string tagValue)
    {
        //grab all the tag fields
        var tagFields = mainPart.Document.Body.Descendants<SdtBlock>().Where
            (r => r.SdtProperties.GetFirstChild<Tag>().Val == tagName);

        foreach (var field in tagFields)
        {
            //remove all paragraphs from the content block
            field.SdtContentBlock.RemoveAllChildren<DocumentFormat.OpenXml.Wordprocessing.Paragraph>();
            //create a new paragraph containing a run and a text element
            var newParagraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
            var newRun = new DocumentFormat.OpenXml.Wordprocessing.Run();
            var newText = new DocumentFormat.OpenXml.Wordprocessing.Text(tagValue);
            newRun.Append(newText);
            newParagraph.Append(newRun);
            //add the new paragraph to the content block
            field.SdtContentBlock.Append(newParagraph);
        }
    }

When you assign a style to the content control a new RunProperties element is added under the SdtProperties . 当您将样式应用到内容控件的新RunProperties元素的下添加SdtProperties For example, if I assign a new style called Style1 I can see the following XML is generated: 例如,如果我分配一个名为Style1的新样式,我可以看到生成了以下XML:

<w:sdt>
    <w:sdtPr>
        <w:rPr>
            <w:rStyle w:val="Style1" />
        </w:rPr>
    <w:alias w:val="LastName" />
    <w:tag w:val="LastName" />
    ....

You need to grab this value and assign it to the new Paragraph you are creating, add the Paragraph at the same level as the SdtBlock and then remove the SdtBlock which is what Word does when you select the "Remove content control when contents are edited" option. 您需要获取此值并将其分配给您正在创建的新Paragraph ,将Paragraph添加到与SdtBlock相同的级别,然后删除SdtBlock ,这是Word在您选择“编辑内容时删除内容控件”时所执行的操作选项。 The RunProperties are the <w:rPr> element. RunProperties<w:rPr>元素。 The following should do what you're after. 以下应该做你想要的。

private static void ReplaceTags(MainDocumentPart mainPart, string tagName, string tagValue)
{
    //grab all the tag fields
    IEnumerable<SdtBlock> tagFields = mainPart.Document.Body.Descendants<SdtBlock>().Where
        (r => r.SdtProperties.GetFirstChild<Tag>().Val == tagName);

    foreach (var field in tagFields)
    {
        //grab the RunProperties from the SdtBlcok
        RunProperties runProp = field.SdtProperties.GetFirstChild<RunProperties>();

        //create a new paragraph containing a run and a text element
        Paragraph newParagraph = new Paragraph();
        Run newRun = new Run();
        if (runProp != null)
        {
            //assign the RunProperties to our new run
            newRun.Append(runProp.CloneNode(true));
        }
        Text newText = new Text(tagValue);
        newRun.Append(newText);
        newParagraph.Append(newRun);
        //insert the new paragraph before the field we're going to remove
        field.Parent.InsertBefore(newParagraph, field);

        //remove the SdtBlock to mimic the Remove content control when contents are edited option
        field.Remove();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM