如何在打开的xml文档上保持样式 [英] How to keep style on open xml documents

查看:101
本文介绍了如何在打开的xml文档上保持样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用开放XML(Microsoft Word-.docx)作为文件模板来自动生成其他文档.在模板文档中,我定义了内容控件,并且编写了代码来替换这些内容控件中的内容.

I am using open XML(Microsoft Word - .docx) as a file template to automatically generate other documents. In the template document I have defined content controls, and I have written code to replace content in these content controls.

内容被替换并生成了文档,但是我在保持样式方面很挣扎.在Word中,当检查内容控件的属性时,我已经选中了复选框使用样式将文本格式设置为空控件:样式",还选中了在编辑内容时删除内容控件".用代码生成文档时,这似乎没有任何影响.

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". This doesn't seem to have any impact when documents are generated by code.

这是我的代码(这里的社区成员很乐意为您提供帮助),用于替换内容控件中的数据.有什么想法可以保持格式吗?格式是简单的文本格式,例如大小和字体.请咨询:

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);
        }
    }

推荐答案

当您为内容控件分配样式时,在SdtProperties下会添加一个新的RunProperties元素.例如,如果我分配一个新的样式Style1,我可以看到生成了以下XML:

When you assign a style to the content control a new RunProperties element is added under the SdtProperties. For example, if I assign a new style called Style1 I can see the following XML is generated:

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

您需要获取此值并将其分配给正在创建的新Paragraph,将Paragraph添加到与SdtBlock相同的级别,然后删除SdtBlock,这是Word在您执行操作时所执行的操作选择在编辑内容时删除内容控件"选项. RunProperties<w:rPr>元素.以下应该做的是您想要做的.

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. The RunProperties are the <w:rPr> element. 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();
    }
}

这篇关于如何在打开的xml文档上保持样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆