/// <summary> /// 加入标题定义 /// </summary> /// <param name="docxDocument"></param> /// <param name="strStyleId"></param> /// <param name="headingLevel"></param> private static void addCustomHeadingStyle(XWPFDocument docxDocument, String strStyleId, int headingLevel) { CT_Style ctStyle = new CT_Style(); ctStyle.styleId = strStyleId; CT_String styleName = new CT_String(); styleName.val = strStyleId; ctStyle.name = styleName; CT_DecimalNumber indentNumber = new CT_DecimalNumber(); indentNumber.val = BigInteger.ValueOf(headingLevel).ToString(); // lower number > style is more prominent in the formats bar ctStyle.uiPriority = indentNumber; CT_OnOff onoffnull = new CT_OnOff(); ctStyle.unhideWhenUsed = onoffnull; // style shows up in the formats bar ctStyle.qFormat = onoffnull; // style defines a heading of the given level CT_PPr ppr = new CT_PPr(); ppr.outlineLvl = indentNumber; ctStyle.pPr = ppr; XWPFStyle style = new XWPFStyle(ctStyle); // is a null op if already defined XWPFStyles styles = docxDocument.CreateStyles(); style.StyleType = ST_StyleType.paragraph; if (styles.GetStyle(strStyleId) == null) { styles.AddStyle(style); } }
public void TestAddStylesToDocument() { XWPFDocument docOut = new XWPFDocument(); XWPFStyles styles = docOut.CreateStyles(); String strStyleId = "headline1"; CT_Style ctStyle = new CT_Style(); ctStyle.styleId = (strStyleId); XWPFStyle s = new XWPFStyle(ctStyle); styles.AddStyle(s); Assert.IsTrue(styles.StyleExist(strStyleId)); XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(docOut); styles = docIn.GetStyles(); Assert.IsTrue(styles.StyleExist(strStyleId)); }