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

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

问题描述

我使用 open 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,添加与SdtBlockParagraph> 然后删除 SdtBlock,这是 Word 在您选择编辑内容时删除内容控件"选项时执行的操作.RunProperties 元素.以下应该做你所追求的.

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天全站免登陆