2013-02-18 185 views
0

如何將樣式應用於像「CSS」這樣的文檔,但是使用OpenXML進行編程? 我不想將樣式應用於單個元素,但設置了默認值並讓文檔部分服從它們。將樣式應用於文檔

我發現HTMLToOpenXML,但寧願自己做這個。任何幫助,將不勝感激。

回答

1

解決了它,只需使用OpenXML生產力工具,找到您正在查找的零件的名稱。或者,打開文檔並確定您想要更改的內容,例如標題或部分A或普通。

下面是與樣本變化的代碼:

public static void GetAndSetStyleFromDoc(string file) 
{ 
      bool styleExists = true; 

      using (var document = WordprocessingDocument.Open(file,true)) 
      { 

       // Get the Styles part for this document 
       StyleDefinitionsPart part = document.MainDocumentPart.StyleDefinitionsPart; 

       foreach (Style style in part.RootElement.Elements<Style>()) 
       { 
        // PartA can be changed for "Normal", "Header1" etc 
        if (style.StyleId.Value.Equals("PartA", StringComparison.InvariantCultureIgnoreCase)) 
        { 
         style.StyleParagraphProperties.SpacingBetweenLines.Line = "276"; 
         style.StyleRunProperties.FontSize.Val = "14"; 
         style.StyleRunProperties.Color.Val = "4F81BD"; // font color 

         ParagraphBorders paragraphBorders1 = new ParagraphBorders(); 
         TopBorder topBorder1 = new TopBorder(){ Val = BorderValues.Single, Color = "856363", Size = (UInt32Value)24U, Space = (UInt32Value)0U }; 
         LeftBorder leftBorder1 = new LeftBorder(){ Val = BorderValues.Single, Color = "856363", Size = (UInt32Value)24U, Space = (UInt32Value)0U }; 
         BottomBorder bottomBorder1 = new BottomBorder(){ Val = BorderValues.Single, Color = "856363", Size = (UInt32Value)24U, Space = (UInt32Value)0U }; 
         RightBorder rightBorder1 = new RightBorder(){ Val = BorderValues.Single, Color = "856363", Size = (UInt32Value)24U, Space = (UInt32Value)0U }; 

         paragraphBorders1.Append(topBorder1); 
         paragraphBorders1.Append(leftBorder1); 
         paragraphBorders1.Append(bottomBorder1); 
         paragraphBorders1.Append(rightBorder1); 

         style.StyleParagraphProperties.ParagraphBorders = paragraphBorders1; 
        } 
       } 

      } 
}