Exemple #1
0
        public void TestEasyAccessToStyles()
        {
            XWPFDocument doc    = XWPFTestDataSamples.OpenSampleDocument("SampleDoc.docx");
            XWPFStyles   styles = doc.GetStyles();

            Assert.IsNotNull(styles);

            // Has 3 paragraphs on page one, a break, and 3 on page 2
            Assert.AreEqual(7, doc.Paragraphs.Count);

            // Check the first three have no run styles, just default paragraph style
            for (int i = 0; i < 3; i++)
            {
                XWPFParagraph p = doc.Paragraphs[(i)];
                Assert.AreEqual(null, p.Style);
                Assert.AreEqual(null, p.StyleID);
                Assert.AreEqual(1, p.Runs.Count);

                XWPFRun r = p.Runs[(0)];
                Assert.AreEqual(null, r.GetColor());
                Assert.AreEqual(null, r.FontFamily);
                Assert.AreEqual(null, r.FontName);
                Assert.AreEqual(-1, r.FontSize);
            }

            // On page two, has explicit styles, but on Runs not on
            //  the paragraph itself
            for (int i = 4; i < 7; i++)
            {
                XWPFParagraph p = doc.Paragraphs[(i)];
                Assert.AreEqual(null, p.Style);
                Assert.AreEqual(null, p.StyleID);
                Assert.AreEqual(1, p.Runs.Count);

                XWPFRun r = p.Runs[(0)];
                Assert.AreEqual("Arial Black", r.FontFamily);
                Assert.AreEqual("Arial Black", r.FontName);
                Assert.AreEqual(16, r.FontSize);
                Assert.AreEqual("548DD4", r.GetColor());
            }

            // Check the document styles
            // Should have a style defined for each type
            Assert.AreEqual(4, styles.NumberOfStyles);
            Assert.IsNotNull(styles.GetStyle("Normal"));
            Assert.IsNotNull(styles.GetStyle("DefaultParagraphFont"));
            Assert.IsNotNull(styles.GetStyle("TableNormal"));
            Assert.IsNotNull(styles.GetStyle("NoList"));

            // We can't do much yet with latent styles
            Assert.AreEqual(137, styles.LatentStyles.NumberOfStyles);

            // Check the default styles
            Assert.IsNotNull(styles.DefaultRunStyle);
            Assert.IsNotNull(styles.DefaultParagraphStyle);

            Assert.AreEqual(11, styles.DefaultRunStyle.FontSize);
            Assert.AreEqual(200, styles.DefaultParagraphStyle.SpacingAfter);
        }
Exemple #2
0
        public void TestGetUsedStyles()
        {
            XWPFDocument     sampleDoc         = XWPFTestDataSamples.OpenSampleDocument("Styles.docx");
            List <XWPFStyle> testUsedStyleList = new List <XWPFStyle>();
            XWPFStyles       styles            = sampleDoc.GetStyles();
            XWPFStyle        style             = styles.GetStyle("berschrift1");

            testUsedStyleList.Add(style);
            testUsedStyleList.Add(styles.GetStyle("Standard"));
            testUsedStyleList.Add(styles.GetStyle("berschrift1Zchn"));
            testUsedStyleList.Add(styles.GetStyle("Absatz-Standardschriftart"));
            style.HasSameName(style);

            List <XWPFStyle> usedStyleList = styles.GetUsedStyleList(style);

            //Assert.AreEqual(usedStyleList, testUsedStyleList);
            Assert.AreEqual(usedStyleList.Count, testUsedStyleList.Count);
            for (int i = 0; i < usedStyleList.Count; i++)
            {
                Assert.AreEqual(usedStyleList[i], testUsedStyleList[i]);
            }
        }
Exemple #3
0
        /// <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);
            }
        }