2013-04-10 28 views
4

当我在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

回答

2

,为了风格的段落,你必须应用样式的段落标记以及任何运行:

17.3.1.29 RPR(运行属性的段落标记)

该元素指定用于 表示段落标记的本 段的物理位置的集合施加到字形运行属性。该段落标记作为 文档中的物理字符可以进行格式化,因此应该能够代表 文档中的任何其他字符。如果此元素不存在,则与任何其他文本运行一样,段落标记为 未格式化。

所以在代码中解决这个问题..尝试这个..

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 

在您的代码中。它也需要属性适用于运行seperately为了得到应用于run元素的格式为run有自己的属性部分:

就像一个段落可以有属性,所以也可以运行。 r元素中的所有 元素的属性均受相应的可选rPr运行属性元素(第17.7.9.1; 第17.3.2.27)的控制,该属性应为r元素的第一个子元素。在 回合中,rPr元素是一组属性元素集合 的容器,应用于r元素的其余子元素。 [注意: rPr容器元素中的元素允许消费者通过 控制下列运行内容中的内容是粗体, 是带下划线还是可见。尾注]

希望这有助于。 @ Flowerking

+1

谢谢,这是代码输出当我改变它作为你说:<?XML版本= “1.0” 编码= “UTF-8”> ' THIS IS TEST RUN 1 ' _but它并没有改变run1_ – 2013-04-11 01:50:11

+0

的风格@ RezaM.A请检查更新。 – Flowerking 2013-04-11 08:11:35

相关问题