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); }
public void Test52449() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("52449.docx"); XWPFStyles styles = doc.GetStyles(); Assert.IsNotNull(styles); XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(doc); styles = docIn.GetStyles(); Assert.IsNotNull(styles); }
public void TestLanguages() { XWPFDocument docOut = new XWPFDocument(); XWPFStyles styles = docOut.CreateStyles(); styles.SetEastAsia("Chinese"); styles.SetSpellingLanguage("English"); CT_Fonts def = new CT_Fonts(); styles.SetDefaultFonts(def); }
public XWPFStyles CreateStyles() { if (this.styles == null) { StylesDocument stylesDocument = new StylesDocument(); XWPFRelation styles = XWPFRelation.STYLES; int relationIndex = this.GetRelationIndex(styles); XWPFStyles relationship = (XWPFStyles)this.CreateRelationship((POIXMLRelation)styles, (POIXMLFactory)XWPFFactory.GetInstance(), relationIndex); relationship.SetStyles(stylesDocument.Styles); this.styles = relationship; } return(this.styles); }
public void TestAddStylesToDocument() { XWPFDocument docOut = new XWPFDocument(); XWPFStyles styles = docOut.CreateStyles(); String strStyleName = "headline1"; CT_Style ctStyle = new CT_Style(); ctStyle.styleId = (strStyleName); XWPFStyle s = new XWPFStyle(ctStyle); styles.AddStyle(s); XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(docOut); styles = docIn.GetStyles(); Assert.IsTrue(styles.StyleExist(strStyleName)); }
public void TestSetStyles_Bug57254() { XWPFDocument docOut = new XWPFDocument(); XWPFStyles styles = docOut.CreateStyles(); CT_Styles ctStyles = new CT_Styles(); String strStyleId = "headline1"; CT_Style ctStyle = ctStyles.AddNewStyle(); ctStyle.styleId = (/*setter*/ strStyleId); styles.SetStyles(ctStyles); Assert.IsTrue(styles.StyleExist(strStyleId)); XWPFDocument docIn = XWPFTestDataSamples.WriteOutAndReadBack(docOut); styles = docIn.GetStyles(); Assert.IsTrue(styles.StyleExist(strStyleId)); }
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]); } }
public XWPFLatentStyles(CT_LatentStyles latentStyles, XWPFStyles styles) { this.latentStyles = latentStyles; this.styles = styles; }
public XWPFStyle(CT_Style style, XWPFStyles styles) { this.ctStyle = style; this.styles = styles; }
/** * constructor * @param style * @param styles */ public XWPFStyle(CT_Style style, XWPFStyles styles) { this.ctStyle = style; this.styles = styles; }
internal override void OnDocumentRead() { try { XmlDocument xmldoc = ConvertStreamToXml(GetPackagePart().GetInputStream()); DocumentDocument doc = DocumentDocument.Parse(xmldoc, NamespaceManager); ctDocument = doc.Document; InitFootnotes(); // parse the document with cursor and add // // the XmlObject to its lists foreach (object o in ctDocument.body.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); bodyElements.Add(p); paragraphs.Add(p); } else if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); bodyElements.Add(t); tables.Add(t); } else if (o is CT_SdtBlock) { XWPFSDT c = new XWPFSDT((CT_SdtBlock)o, this); bodyElements.Add(c); contentControls.Add(c); } } // Sort out headers and footers if (doc.Document.body.sectPr != null) headerFooterPolicy = new XWPFHeaderFooterPolicy(this); // Create for each XML-part in the Package a PartClass foreach (POIXMLDocumentPart p in GetRelations()) { String relation = p.GetPackageRelationship().RelationshipType; if (relation.Equals(XWPFRelation.STYLES.Relation)) { this.styles = (XWPFStyles)p; this.styles.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.NUMBERING.Relation)) { this.numbering = (XWPFNumbering)p; this.numbering.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.FOOTER.Relation)) { XWPFFooter footer = (XWPFFooter)p; footers.Add(footer); footer.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.HEADER.Relation)) { XWPFHeader header = (XWPFHeader)p; headers.Add(header); header.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.COMMENT.Relation)) { XmlDocument xml = ConvertStreamToXml(p.GetPackagePart().GetInputStream()); CommentsDocument cmntdoc = CommentsDocument.Parse(xml ,NamespaceManager); foreach (CT_Comment ctcomment in cmntdoc.Comments.comment) { comments.Add(new XWPFComment(ctcomment, this)); } } else if (relation.Equals(XWPFRelation.SETTINGS.Relation)) { Settings = (XWPFSettings)p; Settings.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.IMAGES.Relation)) { XWPFPictureData picData = (XWPFPictureData)p; picData.OnDocumentRead(); RegisterPackagePictureData(picData); pictures.Add(picData); } else if (relation.Equals(XWPFRelation.GLOSSARY_DOCUMENT.Relation)) { // We don't currently process the glossary itself // Until we do, we do need to load the glossary child parts of it foreach (POIXMLDocumentPart gp in p.GetRelations()) { // Trigger the onDocumentRead for all the child parts // Otherwise we'll hit issues on Styles, Settings etc on save try { gp.OnDocumentRead(); //Method onDocumentRead = gp.getClass().getDeclaredMethod("onDocumentRead"); //onDocumentRead.setAccessible(true); //onDocumentRead.invoke(gp); } catch (Exception e) { throw new POIXMLException(e); } } } } InitHyperlinks(); } catch (XmlException e) { throw new POIXMLException(e); } }
/** * Creates an empty styles for the document if one does not already exist * @return styles */ public XWPFStyles CreateStyles() { if (styles == null) { StylesDocument stylesDoc = new StylesDocument(); XWPFRelation relation = XWPFRelation.STYLES; int i = GetRelationIndex(relation); XWPFStyles wrapper = (XWPFStyles)CreateRelationship(relation, XWPFFactory.GetInstance(), i); wrapper.SetStyles(stylesDoc.Styles); styles = wrapper; } return styles; }
internal override void OnDocumentRead() { try { DocumentDocument doc = DocumentDocument.Parse(GetPackagePart().GetInputStream()); ctDocument = doc.Document; InitFootnotes(); foreach (object o in ctDocument.body.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); bodyElements.Add(p); paragraphs.Add(p); } else if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); bodyElements.Add(t); tables.Add(t); } } // parse the document with cursor and add // // the XmlObject to its lists // XmlCursor cursor = ctDocument.Body.NewCursor(); // cursor.SelectPath("./*"); // while (cursor.ToNextSelection()) { // XmlObject o = cursor.Object; // if (o is CTP) { // XWPFParagraph p = new XWPFParagraph((CTP) o, this); // bodyElements.Add(p); // paragraphs.Add(p); // } else if (o is CTTbl) { // XWPFTable t = new XWPFTable((CTTbl) o, this); // bodyElements.Add(t); // tables.Add(t); // } // } // cursor.Dispose(); // Sort out headers and footers if (doc.Document.body.sectPr != null) headerFooterPolicy = new XWPFHeaderFooterPolicy(this); // Create for each XML-part in the Package a PartClass foreach (POIXMLDocumentPart p in GetRelations()) { String relation = p.GetPackageRelationship().RelationshipType; if (relation.Equals(XWPFRelation.STYLES.Relation)) { this.styles = (XWPFStyles)p; this.styles.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.NUMBERING.Relation)) { this.numbering = (XWPFNumbering)p; this.numbering.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.FOOTER.Relation)) { XWPFFooter footer = (XWPFFooter)p; footers.Add(footer); footer.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.HEADER.Relation)) { XWPFHeader header = (XWPFHeader)p; headers.Add(header); header.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.COMMENT.Relation)) { // TODO Create according XWPFComment class, extending POIXMLDocumentPart CommentsDocument cmntdoc = CommentsDocument.Parse(p.GetPackagePart().GetInputStream()); foreach (CT_Comment ctcomment in cmntdoc.Comments.comment) { comments.Add(new XWPFComment(ctcomment, this)); } } else if (relation.Equals(XWPFRelation.SETTINGS.Relation)) { Settings = (XWPFSettings)p; Settings.OnDocumentRead(); } else if (relation.Equals(XWPFRelation.IMAGES.Relation)) { XWPFPictureData picData = (XWPFPictureData)p; picData.OnDocumentRead(); RegisterPackagePictureData(picData); pictures.Add(picData); } } InitHyperlinks(); } catch (XmlException e) { throw new POIXMLException(e); } }
internal override void OnDocumentRead() { try { DocumentDocument documentDocument = DocumentDocument.Parse(this.GetPackagePart().GetInputStream()); this.ctDocument = documentDocument.Document; this.InitFootnotes(); foreach (object obj in this.ctDocument.body.Items) { if (obj is CT_P) { XWPFParagraph xwpfParagraph = new XWPFParagraph((CT_P)obj, (IBody)this); this.bodyElements.Add((IBodyElement)xwpfParagraph); this.paragraphs.Add(xwpfParagraph); } else if (obj is CT_Tbl) { XWPFTable xwpfTable = new XWPFTable((CT_Tbl)obj, (IBody)this); this.bodyElements.Add((IBodyElement)xwpfTable); this.tables.Add(xwpfTable); } } if (documentDocument.Document.body.sectPr != null) { this.headerFooterPolicy = new XWPFHeaderFooterPolicy(this); } foreach (POIXMLDocumentPart relation1 in this.GetRelations()) { string relationshipType = relation1.GetPackageRelationship().RelationshipType; if (relationshipType.Equals(XWPFRelation.STYLES.Relation)) { this.styles = (XWPFStyles)relation1; this.styles.OnDocumentRead(); } else if (relationshipType.Equals(XWPFRelation.NUMBERING.Relation)) { this.numbering = (XWPFNumbering)relation1; this.numbering.OnDocumentRead(); } else if (relationshipType.Equals(XWPFRelation.FOOTER.Relation)) { XWPFFooter xwpfFooter = (XWPFFooter)relation1; this.footers.Add(xwpfFooter); xwpfFooter.OnDocumentRead(); } else if (relationshipType.Equals(XWPFRelation.HEADER.Relation)) { XWPFHeader xwpfHeader = (XWPFHeader)relation1; this.headers.Add(xwpfHeader); xwpfHeader.OnDocumentRead(); } else if (relationshipType.Equals(XWPFRelation.COMMENT.Relation)) { foreach (CT_Comment comment in CommentsDocument.Parse(relation1.GetPackagePart().GetInputStream()).Comments.comment) { this.comments.Add(new XWPFComment(comment, this)); } } else if (relationshipType.Equals(XWPFRelation.SETTINGS.Relation)) { this.Settings = (XWPFSettings)relation1; this.Settings.OnDocumentRead(); } else if (relationshipType.Equals(XWPFRelation.IMAGES.Relation)) { XWPFPictureData picData = (XWPFPictureData)relation1; picData.OnDocumentRead(); this.RegisterPackagePictureData(picData); this.pictures.Add(picData); } else if (relationshipType.Equals(XWPFRelation.GLOSSARY_DOCUMENT.Relation)) { foreach (POIXMLDocumentPart relation2 in relation1.GetRelations()) { try { relation2.OnDocumentRead(); } catch (Exception ex) { throw new POIXMLException(ex); } } } } this.InitHyperlinks(); } catch (XmlException ex) { throw new POIXMLException((Exception)ex); } }