gpt4 book ai didi

c# - 如何将字符样式应用于字处理文档中的运行?

转载 作者:行者123 更新时间:2023-11-30 12:30:30 25 4
gpt4 key购买 nike

当我在 c# 中使用 openxml sdk 2 创建段落样式并将其应用于段落时,一切都会正确,并且运行时不会出现任何问题。

但是使用下面的代码,当我创建一个字符样式并将其应用于运行时,它不会改变文档的运行:

以下代码将创建样式并将其保存到文档的样式部分:

            StyleDefinitionsPart stylePart = mainPart.AddNewPart<StyleDefinitionsPart>();
Style style = new Style()
{
Type = StyleValues.Character,
CustomStyle = true,
StyleId = "CharacterStyle1"
};
LinkedStyle linkedStyle1 = new LinkedStyle() { Val = "LinkedStyle" };
style.Append(linkedStyle1);
style.Append(new Name() { Val = "CharacterStyle1" });
StyleRunProperties styleRunProperties1 = new StyleRunProperties();
Color color = new Color() { Val = "FF0000" };
RunFonts font1 = new RunFonts() { ComplexScript = "Tahoma" };
styleRunProperties1.Append(color);
styleRunProperties1.Append(font1);
styleRunProperties1.Append(new Bold());
styleRunProperties1.Append(new FontSize() { Val = "48" });
style.Append(styleRunProperties1);
stylePart.Styles = new Styles();

stylePart.Styles.Append(style);

下面的代码是我为将样式应用于运行而编写的代码:

            Paragraph heading = new Paragraph();
ParagraphProperties headingPPr = new ParagraphProperties();
heading.Append(headingPPr);

Run run1 = new Run();
Text textRun1 = new Text("THIS IS TEST RUN 1");
run1.Append(textRun1);
RunProperties rprRun1 = new RunProperties {RunStyle = new RunStyle() {Val = "CharacterStyle1"}};

heading.Append(run1);
body.Append(heading);

这些是 document.xml 的输出代码:

 <?xml version="1.0" encoding="utf-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p>
<w:pPr />
<w:r w:rsidRPr="009531B2">
<w:t>THIS IS TEST RUN 1</w:t>
</w:r>
</w:p>
</w:body>
</w:document>

风格不适用于我的运行!

最后,这是我打开输出文档时样式库的截图,这张图显示样式已经成功添加到文档中,但没有应用到运行:

picture of style in documnet

如何将字符样式应用于运行?

最佳答案

基于ECMA specification for OpenXML ,为了设置段落中任何运行的样式,您还必须将样式应用于段落标记:

17.3.1.29 rPr (Run Properties for the Paragraph Mark)

This element specifies the set of run properties applied to the glyph used torepresent the physical location of the paragraph mark for thisparagraph. This paragraph mark, being a physical character in thedocument, can be formatted, and therefore shall be capable ofrepresenting this formatting like any other character in thedocument. If this element is not present, the paragraph mark isunformatted, as with any other run of text.

所以要在您的代码中解决这个问题..试试这个..

Paragraph heading = new Paragraph();
ParagraphProperties headingPPr = new ParagraphProperties();
heading.Append(headingPPr);
ParagraphMarkRunProperties headingParagraphMarkRunProperties = new ParagraphMarkRunProperties();
RunStyle runStyle1 = new RunStyle(){ Val = "CharacterStyle1" };

headingParagraphMarkRunProperties.Append(runStyle1);
headingPPr.Append(headingParagraphMarkRunProperties);

Run run1 = new Run();
Text textRun1 = new Text("THIS IS TEST RUN 1");
run1.Append(textRun1);
RunProperties rprRun1 = new RunProperties {RunStyle = new RunStyle() {Val = "CharacterStyle1"}};

run1.Append(rprRun1);

heading.Append(run2);
body.Append(heading);

更新:

根据您在评论中打开的 xml 片段,您忘记包含

RunProperties rprRun1 = new RunProperties {RunStyle = new RunStyle() {Val = "CharacterStyle1"}};

run1.Append(rprRun1); //Adding run properties to the run

在您的代码中。还需要将属性单独应用于运行,以便将格式应用于 run 元素,因为 run 有自己的属性部分:

Just as a paragraph can have properties, so too can a run. All of theelements inside an r element have their properties controlled by acorresponding optional rPr run properties element (§17.7.9.1;§17.3.2.27), which shall be the first child of the r element. Inturn, the rPr element is a container for a set of property elementsthat are applied to the rest of the children of the r element. [Note:The elements inside the rPr container element allow the consumer tocontrol whether the content in the following run content is bold,underlined, or visible, for example. end note]

希望这对您有所帮助。

关于c# - 如何将字符样式应用于字处理文档中的运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15917747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com