2013-03-28 15 views
3

我已經使用的代碼如下申請兩種不同的字符樣式到一個段落的兩分:不同的樣式在文字處理在C#中符合opxnxml

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

      Run Run1 = new Run() { RsidRunProperties = "009531B2" }; 
      Text Text_Run1 = new Text("THIS IS TEST RUN 1"); 
      Run1.Append(Text_Run1); 
      RunProperties rpr_Run1 = new RunProperties(); 
      rpr_Run1.RunStyle = new RunStyle() { Val = "CharacterStyle1" }; 

      Run Run2 = new Run(); 
      RunProperties rpr_Run2 = new RunProperties(); 
      rpr_Run2.RunStyle = new RunStyle() { Val = "CharacterStyle2" }; 
      Text text_Run2 = new Text("THIS IS TEST RUN 2"); 
      Run2.Append(text_Run2); 

      heading.Append(Run1); 
      heading.Append(Run2); 
      body.Append(heading); 

但在運行代碼後,在Word文件這些運行獲得正常風格。 我可以將段落樣式應用於段落,但我無法應用字體樣式運行,我的代碼中哪裏出錯?

在結論:

我怎麼能申請字符樣式運行以及如何與不同的樣式運行一個段落?

回答

1

您需要在其屬性部分中指定段落的格式,否則它將回退到文檔的默認值,在此例中爲Normal。如果您的自定義樣式未保存到文檔的樣式部分,也可能發生這種情況。

你的代碼更改爲:

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

headingParagraphMarkRunProperties.Append(runStyle1); 
heading_pPr.Append(headingParagraphMarkRunProperties); 

這將使你的款採用自定義格式。您仍然需要將各個樣式應用於run元素,以更改其格式,正如您在其餘代碼中正確做出的那樣。

相關問題