public XWPFSDTContent(CT_SdtContentBlock block, IBody part, IRunBody parent) { foreach (object o in block.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, part); bodyElements.Add(p); paragraphs.Add(p); } else if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, part); bodyElements.Add(t); tables.Add(t); } else if (o is CT_SdtBlock) { XWPFSDT c = new XWPFSDT(((CT_SdtBlock)o), part); bodyElements.Add(c); contentControls.Add(c); } else if (o is CT_R) { XWPFRun run = new XWPFRun((CT_R)o, parent); runs.Add(run); bodyElements.Add(run); } } }
public void SetUp() { XWPFDocument doc = new XWPFDocument(); p = doc.CreateParagraph(); this.ctRun = new CT_R(); }
public XWPFHeader(XWPFDocument doc, CT_HdrFtr hdrFtr) : base(doc, hdrFtr) { /* XmlCursor cursor = headerFooter.NewCursor(); cursor.SelectPath("./*"); while (cursor.ToNextSelection()) { XmlObject o = cursor.Object; if (o is CTP) { XWPFParagraph p = new XWPFParagraph((CTP) o, this); paragraphs.Add(p); } if (o is CTTbl) { XWPFTable t = new XWPFTable((CTTbl) o, this); tables.Add(t); } } cursor.Dispose();*/ foreach (object o in hdrFtr.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); paragraphs.Add(p); } if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); tables.Add(t); } } }
public XWPFTable(CT_Tbl table, IBody part) { this.part = part; this.ctTbl = table; tableRows = new List<XWPFTableRow>(); // is an empty table: I add one row and one column as default if (table.SizeOfTrArray() == 0) CreateEmptyTable(table); foreach (CT_Row row in table.GetTrList()) { StringBuilder rowText = new StringBuilder(); row.Table = table; XWPFTableRow tabRow = new XWPFTableRow(row, this); tableRows.Add(tabRow); foreach (CT_Tc cell in row.GetTcList()) { cell.TableRow = row; foreach (CT_P ctp in cell.GetPList()) { XWPFParagraph p = new XWPFParagraph(ctp, part); if (rowText.Length > 0) { rowText.Append('\t'); } rowText.Append(p.GetText()); } } if (rowText.Length > 0) { this.text.Append(rowText); this.text.Append('\n'); } } }
public void TestTika792() { //This test forces the loading of CTMoveBookmark and //CTMoveBookmarkImpl into ooxml-lite. XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("Tika-792.docx"); XWPFParagraph paragraph = doc.Paragraphs[(0)]; Assert.AreEqual("s", paragraph.Text); }
public void TestSmartTags() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("smarttag-snippet.docx"); XWPFParagraph p = doc.GetParagraphArray(0); //About NPOI: because the serializer bug(the CT_Run contains whitespace will discard the whitespace), //Text is "CarnegieMellonUniversitySchool of Computer Science" Assert.IsTrue(p.Text.Contains("Carnegie Mellon University School of Computer Science")); p = doc.GetParagraphArray(2); Assert.IsTrue(p.Text.Contains("Alice's Adventures")); }
public XWPFComment(CT_Comment comment, XWPFDocument document) { text = new StringBuilder(); id = comment.id.ToString(); author = comment.author; foreach(CT_P ctp in comment.GetPList()) { XWPFParagraph p = new XWPFParagraph(ctp, document); text.Append(p.Text); } }
public XWPFComment(CT_Comment comment, XWPFDocument document) { text = new StringBuilder(); id = comment.id.ToString(); author = comment.author; foreach (CT_P ctp in comment.GetPList()) { XWPFParagraph p = new XWPFParagraph(ctp, document); text.Append(p.GetText()); } }
public void TestOverrideList() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("NumberingWOverrides.docx"); XWPFParagraph p = doc.Paragraphs[(4)]; XWPFNumbering numbering = doc.GetNumbering(); CT_Num ctNum = numbering.GetNum(p.GetNumID()).GetCTNum(); Assert.AreEqual(9, ctNum.SizeOfLvlOverrideArray()); CT_NumLvl ctNumLvl = ctNum.GetLvlOverrideArray(0); Assert.AreEqual("upperLetter", ctNumLvl.lvl.numFmt.val.ToString()); }
/** * Reads the document * @throws IOException */ internal override void OnDocumentRead() { base.OnDocumentRead(); HdrDocument hdrDocument = null; Stream is1; try { is1 = GetPackagePart().GetInputStream(); hdrDocument = HdrDocument.Parse(is1); headerFooter = hdrDocument.Hdr; foreach (object o in headerFooter.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); paragraphs.Add(p); bodyElements.Add(p); } if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); tables.Add(t); bodyElements.Add(t); } } // parse the document with cursor and add // the XmlObject to its lists /*XmlCursor cursor = headerFooter.NewCursor(); * cursor.SelectPath("./*"); * while (cursor.ToNextSelection()) { * XmlObject o = cursor.Object; * if (o is CTP) { * XWPFParagraph p = new XWPFParagraph((CTP)o, this); * paragraphs.Add(p); * bodyElements.Add(p); * } * if (o is CTTbl) { * XWPFTable t = new XWPFTable((CTTbl)o, this); * tables.Add(t); * bodyElements.Add(t); * } * } * cursor.Dispose();*/ } catch (Exception e) { throw new POIXMLException(e); } }
public void Test58618() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("58618.docx"); XWPFParagraph para = (XWPFParagraph)doc.BodyElements[0]; Assert.IsNotNull(para); Assert.AreEqual("Some text some hyper links link link and some text.....", para.Text); XWPFRun run = para.InsertNewRun(para.Runs.Count); run.SetText("New Text"); Assert.AreEqual("Some text some hyper links link link and some text.....New Text", para.Text); para.RemoveRun(para.Runs.Count - 2); Assert.AreEqual("Some text some hyper links link linkNew Text", para.Text); }
public void TestAddPicture() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("TestDocument.docx"); XWPFParagraph p = doc.GetParagraphArray(2); XWPFRun r = p.GetRuns()[0]; Assert.AreEqual(0, doc.AllPictures.Count); Assert.AreEqual(0, r.GetEmbeddedPictures().Count); r.AddPicture(new MemoryStream(new byte[0]), (int)PictureType.JPEG, "test.jpg", 21, 32); Assert.AreEqual(1, doc.AllPictures.Count); Assert.AreEqual(1, r.GetEmbeddedPictures().Count); }
public void TestRuns() { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.CreateParagraph(); CT_R run = new CT_R(); XWPFRun r = new XWPFRun(run, doc.CreateParagraph()); p.AddRun(r); p.AddRun(r); Assert.IsNotNull(p.GetRun(run)); Assert.IsNull(p.GetRun(null)); }
public XWPFCommentsDecorator(XWPFParagraph paragraph, XWPFParagraphDecorator nextDecorator) : base(paragraph, nextDecorator) { ; XWPFComment comment; commentText = new StringBuilder(); foreach (CT_MarkupRange anchor in paragraph.GetCTP().GetCommentRangeStartList()) { if ((comment = paragraph.Document.GetCommentByID(anchor.id)) != null) commentText.Append("\tComment by " + comment.GetAuthor() + ": " + comment.GetText()); } }
public void Bug57495_getParagraphArrayInTableCell() { XWPFDocument doc = new XWPFDocument(); //let's create a table for the test XWPFTable table = doc.CreateTable(2, 2); Assert.IsNotNull(table); XWPFParagraph p = table.GetRow(0).GetCell(0).GetParagraphArray(0); Assert.IsNotNull(p); //let's check also that returns the correct paragraph XWPFParagraph same = table.GetRow(0).GetCell(0).Paragraphs[0]; Assert.AreEqual(p, same); }
public XWPFRun(CT_R r, XWPFParagraph p) { this.run = r; this.paragraph = p; IList <CT_Drawing> drawingList = r.GetDrawingList(); foreach (CT_Drawing ctDrawing in (IEnumerable <CT_Drawing>)drawingList) { foreach (CT_Anchor anchor in ctDrawing.GetAnchorList()) { if (anchor.docPr != null) { this.GetDocument().GetDrawingIdManager().Reserve((long)anchor.docPr.id); } } foreach (CT_Inline inline in ctDrawing.GetInlineList()) { if (inline.docPr != null) { this.GetDocument().GetDrawingIdManager().Reserve((long)inline.docPr.id); } } } StringBuilder stringBuilder = new StringBuilder(); List <object> objectList = new List <object>(); foreach (NPOI.OpenXmlFormats.Wordprocessing.CT_Picture pict in (IEnumerable <NPOI.OpenXmlFormats.Wordprocessing.CT_Picture>)r.GetPictList()) { objectList.Add((object)pict); } foreach (CT_Drawing ctDrawing in (IEnumerable <CT_Drawing>)drawingList) { objectList.Add((object)ctDrawing); } foreach (object obj in objectList) { ; } this.pictureText = stringBuilder.ToString(); this.pictures = new List <XWPFPicture>(); foreach (object o in objectList) { foreach (NPOI.OpenXmlFormats.Dml.Picture.CT_Picture ctPicture in this.GetCTPictures(o)) { this.pictures.Add(new XWPFPicture(ctPicture, this)); } } }
public void TestHeaderParagraph() { XWPFDocument xml = XWPFTestDataSamples.OpenSampleDocument("ThreeColHead.docx"); XWPFHeader hdr = xml.GetHeaderFooterPolicy().GetDefaultHeader(); Assert.IsNotNull(hdr); IList <XWPFParagraph> ps = hdr.Paragraphs; Assert.AreEqual(1, ps.Count); XWPFParagraph p = ps[(0)]; Assert.AreEqual(5, p.GetCTP().GetRList().Count); Assert.AreEqual("First header column!\tMid header\tRight header!", p.Text); }
public void TestBookmarks() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("bookmarks.docx"); XWPFParagraph paragraph = doc.Paragraphs[0]; Assert.AreEqual("Sample Word Document", paragraph.Text); Assert.AreEqual(1, paragraph.GetCTP().SizeOfBookmarkStartArray()); Assert.AreEqual(0, paragraph.GetCTP().SizeOfBookmarkEndArray()); CT_Bookmark ctBookmark = paragraph.GetCTP().GetBookmarkStartArray(0); Assert.AreEqual("poi", ctBookmark.name); foreach (CT_Bookmark bookmark in paragraph.GetCTP().GetBookmarkStartList()) { Assert.AreEqual("poi", bookmark.name); } }
public void TestSetGetPageBreak() { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.CreateParagraph(); CT_P ctp = p.GetCTP(); CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr; CT_OnOff pageBreak = ppr.AddNewPageBreakBefore(); pageBreak.val = false; Assert.AreEqual(false, p.IsPageBreak); p.IsPageBreak = (true); Assert.AreEqual(true, ppr.pageBreakBefore.val); }
public void TestSetGetWordWrap() { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.CreateParagraph(); CT_P ctp = p.GetCTP(); CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr; CT_OnOff wordWrap = ppr.AddNewWordWrap(); wordWrap.val = false; Assert.AreEqual(false, p.IsWordWrap); p.IsWordWrap = true; Assert.AreEqual(true, ppr.wordWrap.val); }
/** * If a table cell does not include at least one block-level element, then this document shall be considered corrupt */ public XWPFTableCell(CT_Tc cell, XWPFTableRow tableRow, IBody part) { this.ctTc = cell; this.part = part; this.tableRow = tableRow; // NB: If a table cell does not include at least one block-level element, then this document shall be considered corrupt. if (cell.GetPList().Count < 1) { cell.AddNewP(); } bodyElements = new List <IBodyElement>(); paragraphs = new List <XWPFParagraph>(); tables = new List <XWPFTable>(); foreach (object o in ctTc.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); paragraphs.Add(p); bodyElements.Add(p); } if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); tables.Add(t); bodyElements.Add(t); } } /* * XmlCursor cursor = ctTc.NewCursor(); * cursor.SelectPath("./*"); * while (cursor.ToNextSelection()) { * XmlObject o = cursor.Object; * if (o is CTP) { * XWPFParagraph p = new XWPFParagraph((CTP)o, this); * paragraphs.Add(p); * bodyElements.Add(p); * } * if (o is CTTbl) { * XWPFTable t = new XWPFTable((CTTbl)o, this); * tables.Add(t); * bodyElements.Add(t); * } * } * cursor.Dispose();*/ }
public void TestLvlText() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("Numbering.docx"); Assert.AreEqual("%1.%2.%3.", doc.Paragraphs[(12)].NumLevelText); Assert.AreEqual("NEW-%1-FORMAT", doc.Paragraphs[(14)].NumLevelText); XWPFParagraph p = doc.Paragraphs[(18)]; Assert.AreEqual("%1.", p.NumLevelText); //test that null doesn't throw NPE //Assert.IsNull(p.GetNumFmt()); //C# enum is never null Assert.AreEqual([email protected](), p.GetNumFmt()); }
public void TestSetGetVerticalAlignment() { //new clean instance of paragraph XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.CreateParagraph(); CT_P ctp = p.GetCTP(); CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr; CT_TextAlignment txtAlign = ppr.AddNewTextAlignment(); txtAlign.val = (ST_TextAlignment.center); Assert.AreEqual(TextAlignment.CENTER, p.VerticalAlignment); p.VerticalAlignment = (TextAlignment.BOTTOM); Assert.AreEqual(ST_TextAlignment.bottom, ppr.textAlignment.val); }
/// <summary> /// Read the document /// </summary> internal override void OnDocumentRead() { base.OnDocumentRead(); HdrDocument hdrDocument = null; Stream is1 = null; try { is1 = GetPackagePart().GetInputStream(); XmlDocument xmldoc = DocumentHelper.LoadDocument(is1); hdrDocument = HdrDocument.Parse(xmldoc, NamespaceManager); headerFooter = hdrDocument.Hdr; foreach (object o in headerFooter.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); paragraphs.Add(p); bodyElements.Add(p); } if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); tables.Add(t); bodyElements.Add(t); } if (o is CT_SdtBlock) { XWPFSDT c = new XWPFSDT((CT_SdtBlock)o, this); bodyElements.Add(c); } } } catch (Exception e) { throw new POIXMLException(e); } finally { if (is1 != null) { is1.Close(); } } }
public void TestSetGetSpacing() { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.CreateParagraph(); CT_P ctp = p.GetCTP(); CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr; Assert.AreEqual(-1, p.SpacingAfter); CT_Spacing spacing = ppr.AddNewSpacing(); spacing.after = 10; Assert.AreEqual(10, p.SpacingAfter); p.SpacingAfter = 100; Assert.AreEqual(100, (int)spacing.after); }
public void TestSearchTextFound() { XWPFDocument xml = XWPFTestDataSamples.OpenSampleDocument("ThreeColHead.docx"); IList <XWPFParagraph> ps = xml.Paragraphs; Assert.AreEqual(10, ps.Count); XWPFParagraph p = ps[(0)]; TextSegement segment = p.SearchText("sample word document", new PositionInParagraph()); Assert.IsNotNull(segment); Assert.AreEqual("sample word document", p.GetText(segment)); Assert.IsTrue(p.RemoveRun(0)); }
public void TestSetGetSpacingLineRule() { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.CreateParagraph(); CT_P ctp = p.GetCTP(); CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr; Assert.AreEqual(LineSpacingRule.AUTO, p.SpacingLineRule); CT_Spacing spacing = ppr.AddNewSpacing(); spacing.lineRule = (ST_LineSpacingRule.atLeast); Assert.AreEqual(LineSpacingRule.ATLEAST, p.SpacingLineRule); p.SpacingAfter = 100; Assert.AreEqual(100, (int)spacing.after); }
/** * If a table cell does not include at least one block-level element, then this document shall be considered corrupt */ public XWPFTableCell(CT_Tc cell, XWPFTableRow tableRow, IBody part) { this.ctTc = cell; this.part = part; this.tableRow = tableRow; // NB: If a table cell does not include at least one block-level element, then this document shall be considered corrupt. if(cell.GetPList().Count<1) cell.AddNewP(); bodyElements = new List<IBodyElement>(); paragraphs = new List<XWPFParagraph>(); tables = new List<XWPFTable>(); foreach (object o in ctTc.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); paragraphs.Add(p); bodyElements.Add(p); } if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); tables.Add(t); bodyElements.Add(t); } } /* XmlCursor cursor = ctTc.NewCursor(); cursor.SelectPath("./*"); while (cursor.ToNextSelection()) { XmlObject o = cursor.Object; if (o is CTP) { XWPFParagraph p = new XWPFParagraph((CTP)o, this); paragraphs.Add(p); bodyElements.Add(p); } if (o is CTTbl) { XWPFTable t = new XWPFTable((CTTbl)o, this); tables.Add(t); bodyElements.Add(t); } } cursor.Dispose();*/ }
public void TestAddingRuns() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("sample.docx"); XWPFParagraph p = doc.Paragraphs[0]; Assert.AreEqual(2, p.Runs.Count); XWPFRun r = p.CreateRun(); Assert.AreEqual(3, p.Runs.Count); Assert.AreEqual(2, p.Runs.IndexOf(r)); XWPFRun r2 = p.InsertNewRun(1); Assert.AreEqual(4, p.Runs.Count); Assert.AreEqual(1, p.Runs.IndexOf(r2)); Assert.AreEqual(3, p.Runs.IndexOf(r)); }
public void ReadHdrFtr() { bodyElements = new List <IBodyElement>(); paragraphs = new List <XWPFParagraph>(); tables = new List <XWPFTable>(); // parse the document with cursor and add // the XmlObject to its lists foreach (object o in headerFooter.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); paragraphs.Add(p); bodyElements.Add(p); } if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); tables.Add(t); bodyElements.Add(t); } } /*XmlCursor cursor = headerFooter.NewCursor(); * cursor.SelectPath("./*"); * while (cursor.ToNextSelection()) * { * XmlObject o = cursor.Object; * if (o is CTP) * { * XWPFParagraph p = new XWPFParagraph((CTP)o, this); * paragraphs.Add(p); * bodyElements.Add(p); * } * if (o is CTTbl) * { * XWPFTable t = new XWPFTable((CTTbl)o, this); * tables.Add(t); * bodyElements.Add(t); * } * } * cursor.Dispose();*/ }
public void TestSetAlignment() { //new clean instance of paragraph XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.CreateParagraph(); Assert.AreEqual(ParagraphAlignment.LEFT, p.Alignment); CT_P ctp = p.GetCTP(); CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr; CT_Jc align = ppr.AddNewJc(); align.val = (ST_Jc.center); Assert.AreEqual(ParagraphAlignment.CENTER, p.Alignment); p.Alignment = (ParagraphAlignment.BOTH); Assert.AreEqual((int)ST_Jc.both, (int)ppr.jc.val); }
public void TestSetGetIndentation() { XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.CreateParagraph(); Assert.AreEqual(-1, p.IndentationLeft); CT_P ctp = p.GetCTP(); CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr; Assert.AreEqual(-1, p.IndentationLeft); CT_Ind ind = ppr.AddNewInd(); ind.left = "10"; Assert.AreEqual(10, p.IndentationLeft); p.IndentationLeft = 100; Assert.AreEqual(100, int.Parse(ind.left)); }
public void TestSetFontFamily_52288() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("52288.docx"); IEnumerator <XWPFParagraph> paragraphs = doc.Paragraphs.GetEnumerator(); while (paragraphs.MoveNext()) { XWPFParagraph paragraph = paragraphs.Current; foreach (XWPFRun run in paragraph.Runs) { if (run != null) { String text = run.GetText(0); if (text != null) { run.SetFontFamily("Times New Roman"); } } } } }
public void ReadHdrFtr() { this.bodyElements = new List <IBodyElement>(); this.paragraphs = new List <XWPFParagraph>(); this.tables = new List <XWPFTable>(); foreach (object obj in this.headerFooter.Items) { if (obj is CT_P) { XWPFParagraph xwpfParagraph = new XWPFParagraph((CT_P)obj, (IBody)this); this.paragraphs.Add(xwpfParagraph); this.bodyElements.Add((IBodyElement)xwpfParagraph); } if (obj is CT_Tbl) { XWPFTable xwpfTable = new XWPFTable((CT_Tbl)obj, (IBody)this); this.tables.Add(xwpfTable); this.bodyElements.Add((IBodyElement)xwpfTable); } } }
public void TestAddParagraph() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("sample.docx"); Assert.AreEqual(3, doc.Paragraphs.Count); XWPFParagraph p = doc.CreateParagraph(); Assert.AreEqual(p, doc.Paragraphs[(3)]); Assert.AreEqual(4, doc.Paragraphs.Count); Assert.AreEqual(3, doc.GetParagraphPos(3)); Assert.AreEqual(3, doc.GetPosOfParagraph(p)); //CTP ctp = p.CTP; //XWPFParagraph newP = doc.GetParagraph(ctp); //Assert.AreSame(p, newP); //XmlCursor cursor = doc.Document.Body.GetPArray(0).newCursor(); //XWPFParagraph cP = doc.InsertNewParagraph(cursor); //Assert.AreSame(cP, doc.Paragraphs[(0)]); //Assert.AreEqual(5, doc.Paragraphs.Count); }
/** * @param prgrph The paragraph of text to work on * @param outputHyperlinkUrls Should we output the links too, or just the link text? */ public XWPFHyperlinkDecorator(XWPFParagraph prgrph, XWPFParagraphDecorator nextDecorator, bool outputHyperlinkUrls) : base(prgrph, nextDecorator) { hyperlinkText = new StringBuilder(); // loop over hyperlink anchors foreach (CT_Hyperlink1 link in paragraph.GetCTP().GetHyperlinkList()) { foreach (CT_R r in link.GetRList()) { // Loop over text Runs foreach (CT_Text text in r.GetTList()) { hyperlinkText.Append(text.Value); } } if (outputHyperlinkUrls && paragraph.GetDocument().GetHyperlinkByID(link.id) != null) { hyperlinkText.Append(" <" + paragraph.GetDocument().GetHyperlinkByID(link.id).URL + ">"); } } }
public void TestSetBorderTop() { //new clean instance of paragraph XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.CreateParagraph(); Assert.AreEqual(ST_Border.none, EnumConverter.ValueOf <ST_Border, Borders>(p.BorderTop)); CT_P ctp = p.GetCTP(); CT_PPr ppr = ctp.pPr == null?ctp.AddNewPPr() : ctp.pPr; //bordi CT_PBdr bdr = ppr.AddNewPBdr(); CT_Border borderTop = bdr.AddNewTop(); borderTop.val = (ST_Border.@double); bdr.top = (borderTop); Assert.AreEqual(Borders.Double, p.BorderTop); p.BorderTop = (Borders.Single); Assert.AreEqual(ST_Border.single, borderTop.val); }
public void TestFieldRuns() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("FldSimple.docx"); IList <XWPFParagraph> ps = doc.Paragraphs; Assert.AreEqual(1, ps.Count); XWPFParagraph p = ps[0]; Assert.AreEqual(1, p.Runs.Count); Assert.AreEqual(1, p.IRuns.Count); XWPFRun r = p.Runs[0]; Assert.AreEqual(typeof(XWPFFieldRun), r.GetType()); XWPFFieldRun fr = (XWPFFieldRun)r; Assert.AreEqual(" FILENAME \\* MERGEFORMAT ", fr.FieldInstruction); Assert.AreEqual("FldSimple.docx", fr.Text); Assert.AreEqual("FldSimple.docx", p.Text); }
/** * If a table cell does not include at least one block-level element, then this document shall be considered corrupt */ public XWPFTableCell(CT_Tc cell, XWPFTableRow tableRow, IBody part) { this.ctTc = cell; this.part = part; this.tableRow = tableRow; // NB: If a table cell does not include at least one block-level element, then this document shall be considered corrupt. if(cell.GetPList().Count<1) cell.AddNewP(); bodyElements = new List<IBodyElement>(); paragraphs = new List<XWPFParagraph>(); tables = new List<XWPFTable>(); foreach (object o in ctTc.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); paragraphs.Add(p); bodyElements.Add(p); } if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); tables.Add(t); bodyElements.Add(t); } if (o is CT_SdtBlock) { XWPFSDT c = new XWPFSDT((CT_SdtBlock)o, this); bodyElements.Add(c); } if (o is CT_SdtRun) { XWPFSDT c = new XWPFSDT((CT_SdtRun)o, this); bodyElements.Add(c); } } }
/** * Appends a new paragraph to this document * @return a new paragraph */ public XWPFParagraph CreateParagraph() { XWPFParagraph p = new XWPFParagraph(ctDocument.body.AddNewP(), this); bodyElements.Add(p); paragraphs.Add(p); return p; }
/** * add a Paragraph to this TableCell * @param p the paragaph which has to be Added */ public void AddParagraph(XWPFParagraph p) { paragraphs.Add(p); }
public void SetParagraph(XWPFParagraph p) { if (ctTc.SizeOfPArray() == 0) { ctTc.AddNewP(); } ctTc.SetPArray(0, p.GetCTP()); }
/** * @param r the CT_R bean which holds the run.attributes * @param p the parent paragraph */ public XWPFRun(CT_R r, XWPFParagraph p) { this.run = r; this.paragraph = p; /** * reserve already occupied Drawing ids, so reserving new ids later will * not corrupt the document */ IList<NPOI.OpenXmlFormats.Wordprocessing.CT_Drawing> drawingList = r.GetDrawingList(); foreach (NPOI.OpenXmlFormats.Wordprocessing.CT_Drawing ctDrawing in drawingList) { List<CT_Anchor> anchorList = ctDrawing.GetAnchorList(); foreach (CT_Anchor anchor in anchorList) { if (anchor.docPr != null) { GetDocument().GetDrawingIdManager().Reserve(anchor.docPr.id); } } List<CT_Inline> inlineList = ctDrawing.GetInlineList(); foreach (CT_Inline inline in inlineList) { if (inline.docPr != null) { GetDocument().GetDrawingIdManager().Reserve(inline.docPr.id); } } } //// Look for any text in any of our pictures or Drawings StringBuilder text = new StringBuilder(); List<object> pictTextObjs = new List<object>(); foreach (NPOI.OpenXmlFormats.Wordprocessing.CT_Picture pic in r.GetPictList()) pictTextObjs.Add(pic); foreach (NPOI.OpenXmlFormats.Wordprocessing.CT_Drawing draw in drawingList) pictTextObjs.Add(draw); foreach (object o in pictTextObjs) { //XmlObject[] t = o.SelectPath("declare namespace w='http://schemas.openxmlformats.org/wordprocessingml/2006/main' .//w:t"); //for (int m = 0; m < t.Length; m++) //{ // NodeList kids = t[m].DomNode.ChildNodes; // for (int n = 0; n < kids.Length; n++) // { // if (kids.Item(n) is Text) // { // if (text.Length > 0) // text.Append("\n"); // text.Append(kids.Item(n).NodeValue); // } // } //} } pictureText = text.ToString(); // Do we have any embedded pictures? // (They're a different CT_Picture, under the Drawingml namespace) pictures = new List<XWPFPicture>(); foreach (object o in pictTextObjs) { foreach (OpenXmlFormats.Dml.Picture.CT_Picture pict in GetCTPictures(o)) { XWPFPicture picture = new XWPFPicture(pict, this); pictures.Add(picture); } } }
private CT_HdrFtr buildHdrFtr(String pStyle, XWPFParagraph[] paragraphs) { CT_HdrFtr ftr = new CT_HdrFtr(); if (paragraphs != null) { for (int i = 0 ; i < paragraphs.Length ; i++) { CT_P p = ftr.AddNewP(); //ftr.PArray=(0, paragraphs[i].CTP); // MB 23 May 2010 ftr.SetPArray(i, paragraphs[i].GetCTP()); // MB 23 May 2010 } } else { CT_P p = ftr.AddNewP(); byte[] rsidr = doc.Document.body.GetPArray(0).rsidR; byte[] rsidrdefault = doc.Document.body.GetPArray(0).rsidRDefault; p.rsidR = (rsidr); p.rsidRDefault = (rsidrdefault); CT_PPr pPr = p.AddNewPPr(); pPr.AddNewPStyle().val = (pStyle); } return ftr; }
private CT_HdrFtr buildHdr(ST_HdrFtr type, String pStyle, XWPFHeaderFooter wrapper, XWPFParagraph[] pars) { //CTHdrFtr hdr = buildHdrFtr(pStyle, pars); // MB 24 May 2010 CT_HdrFtr hdr = buildHdrFtr(pStyle, pars, wrapper); // MB 24 May 2010 SetHeaderReference(type, wrapper); return hdr; }
public XWPFFooter CreateFooter(ST_HdrFtr type, XWPFParagraph[] pars) { XWPFRelation relation = XWPFRelation.FOOTER; String pStyle = "Footer"; int i = GetRelationIndex(relation); FtrDocument ftrDoc = new FtrDocument(); XWPFFooter wrapper = (XWPFFooter)doc.CreateRelationship(relation, XWPFFactory.GetInstance(), i); CT_HdrFtr ftr = buildFtr(type, pStyle, wrapper, pars); wrapper.SetHeaderFooter(ftr); Stream outputStream = wrapper.GetPackagePart().GetOutputStream(); ftrDoc.SetFtr((CT_Ftr)ftr); //XmlOptions xmlOptions = Commit(wrapper); assignFooter(wrapper, type); ftrDoc.Save(outputStream, Commit(wrapper)); outputStream.Close(); return wrapper; }
public XWPFHeader CreateHeader(ST_HdrFtr type, XWPFParagraph[] pars) { XWPFRelation relation = XWPFRelation.HEADER; String pStyle = "Header"; int i = GetRelationIndex(relation); HdrDocument hdrDoc = new HdrDocument(); XWPFHeader wrapper = (XWPFHeader)doc.CreateRelationship(relation, XWPFFactory.GetInstance(), i); CT_HdrFtr hdr = buildHdr(type, pStyle, wrapper, pars); wrapper.SetHeaderFooter(hdr); hdrDoc.SetHdr((CT_Hdr)hdr); assignHeader(wrapper, type); using (Stream outputStream = wrapper.GetPackagePart().GetOutputStream()) { hdrDoc.Save(outputStream); } return wrapper; }
/** * copies content of a paragraph to a existing paragraph in the list paragraphs at position pos * @param paragraph * @param pos */ public void SetParagraph(XWPFParagraph paragraph, int pos) { paragraphs[pos]= paragraph; ctDocument.body.SetPArray(pos, paragraph.GetCTP()); /* TODO update body element, update xwpf element, verify that * incoming paragraph belongs to this document or if not, XML was * copied properly (namespace-abbreviations, etc.) */ }
private void process(XWPFParagraph paragraph) { }
/** * MB 24 May 2010. Created this overloaded buildHdrFtr() method because testing demonstrated * that the XWPFFooter or XWPFHeader object returned by calls to the CreateHeader(int, XWPFParagraph[]) * and CreateFooter(int, XWPFParagraph[]) methods or the GetXXXXXHeader/Footer methods where * headers or footers had been Added to a document since it had been Created/opened, returned * an object that Contained no XWPFParagraph objects even if the header/footer itself did contain * text. The reason was that this line of code; CTHdrFtr ftr = CTHdrFtr.Factory.NewInstance(); * Created a brand new instance of the CTHDRFtr class which was then populated with data when * it should have recovered the CTHdrFtr object encapsulated within the XWPFHeaderFooter object * that had previoulsy been instantiated in the CreateHeader(int, XWPFParagraph[]) or * CreateFooter(int, XWPFParagraph[]) methods. */ private CT_HdrFtr buildHdrFtr(String pStyle, XWPFParagraph[] paragraphs, XWPFHeaderFooter wrapper) { CT_HdrFtr ftr = wrapper._getHdrFtr(); if (paragraphs != null) { for (int i = 0 ; i < paragraphs.Length ; i++) { CT_P p = ftr.AddNewP(); ftr.SetPArray(i, paragraphs[i].GetCTP()); } } else { CT_P p = ftr.AddNewP(); byte[] rsidr = doc.Document.body.GetPArray(0).rsidR; byte[] rsidrdefault = doc.Document.body.GetPArray(0).rsidRDefault; p.rsidP=(rsidr); p.rsidRDefault=(rsidrdefault); CT_PPr pPr = p.AddNewPPr(); pPr.AddNewPStyle().val = (pStyle); } return ftr; }
public void TestExisting() { XWPFDocument doc = XWPFTestDataSamples.OpenSampleDocument("TestDocument.docx"); XWPFParagraph p; XWPFRun run; // First paragraph is simple p = doc.GetParagraphArray(0); Assert.AreEqual("This is a test document.", p.Text); Assert.AreEqual(2, p.Runs.Count); run = p.Runs[0]; Assert.AreEqual("This is a test document", run.ToString()); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, run.IsItalic); Assert.AreEqual(false, run.IsStrike); Assert.AreEqual(null, run.GetCTR().rPr); run = p.Runs[1]; Assert.AreEqual(".", run.ToString()); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, run.IsItalic); Assert.AreEqual(false, run.IsStrike); Assert.AreEqual(null, run.GetCTR().rPr); // Next paragraph is all in one style, but a different one p = doc.GetParagraphArray(1); Assert.AreEqual("This bit is in bold and italic", p.Text); Assert.AreEqual(1, p.Runs.Count); run = p.Runs[0]; Assert.AreEqual("This bit is in bold and italic", run.ToString()); Assert.AreEqual(true, run.IsBold); Assert.AreEqual(true, run.IsItalic); Assert.AreEqual(false, run.IsStrike); Assert.AreEqual(true, run.GetCTR().rPr.IsSetB()); Assert.AreEqual(false, run.GetCTR().rPr.b.IsSetVal()); // Back to normal p = doc.GetParagraphArray(2); Assert.AreEqual("Back to normal", p.Text); Assert.AreEqual(1, p.Runs.Count); run = p.Runs[(0)]; Assert.AreEqual("Back to normal", run.ToString()); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, run.IsItalic); Assert.AreEqual(false, run.IsStrike); Assert.AreEqual(null, run.GetCTR().rPr); // Different styles in one paragraph p = doc.GetParagraphArray(3); Assert.AreEqual("This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.", p.Text); Assert.AreEqual(11, p.Runs.Count); run = p.Runs[(0)]; Assert.AreEqual("This contains ", run.ToString()); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, run.IsItalic); Assert.AreEqual(false, run.IsStrike); Assert.AreEqual(null, run.GetCTR().rPr); run = p.Runs[(1)]; Assert.AreEqual("BOLD", run.ToString()); Assert.AreEqual(true, run.IsBold); Assert.AreEqual(false, run.IsItalic); Assert.AreEqual(false, run.IsStrike); run = p.Runs[2]; Assert.AreEqual(", ", run.ToString()); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, run.IsItalic); Assert.AreEqual(false, run.IsStrike); Assert.AreEqual(null, run.GetCTR().rPr); run = p.Runs[(3)]; Assert.AreEqual("ITALIC", run.ToString()); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(true, run.IsItalic); Assert.AreEqual(false, run.IsStrike); run = p.Runs[(4)]; Assert.AreEqual(" and ", run.ToString()); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, run.IsItalic); Assert.AreEqual(false, run.IsStrike); Assert.AreEqual(null, run.GetCTR().rPr); run = p.Runs[(5)]; Assert.AreEqual("BOTH", run.ToString()); Assert.AreEqual(true, run.IsBold); Assert.AreEqual(true, run.IsItalic); Assert.AreEqual(false, run.IsStrike); run = p.Runs[(6)]; Assert.AreEqual(", as well as ", run.ToString()); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, run.IsItalic); Assert.AreEqual(false, run.IsStrike); Assert.AreEqual(null, run.GetCTR().rPr); run = p.Runs[(7)]; Assert.AreEqual("RED", run.ToString()); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, run.IsItalic); Assert.AreEqual(false, run.IsStrike); run = p.Runs[(8)]; Assert.AreEqual(" and ", run.ToString()); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, run.IsItalic); Assert.AreEqual(false, run.IsStrike); Assert.AreEqual(null, run.GetCTR().rPr); run = p.Runs[(9)]; Assert.AreEqual("YELLOW", run.ToString()); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, run.IsItalic); Assert.AreEqual(false, run.IsStrike); run = p.Runs[(10)]; Assert.AreEqual(" text.", run.ToString()); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, run.IsItalic); Assert.AreEqual(false, run.IsStrike); Assert.AreEqual(null, run.GetCTR().rPr); }
public void CreateWatermark(String text) { XWPFParagraph[] pars = new XWPFParagraph[1]; try { pars[0] = GetWatermarkParagraph(text, 1); CreateHeader(DEFAULT, pars); pars[0] = GetWatermarkParagraph(text, 2); CreateHeader(FIRST, pars); pars[0] = GetWatermarkParagraph(text, 3); CreateHeader(EVEN, pars); } catch (IOException e) { // TODO Auto-generated catch block //e.PrintStackTrace(); Console.Write(e.StackTrace); } }
public XWPFRun(CT_R r, XWPFParagraph p) : this(r, (IRunBody)p) { }
public void ReadHdrFtr() { bodyElements = new List<IBodyElement>(); paragraphs = new List<XWPFParagraph>(); tables = new List<XWPFTable>(); // parse the document with cursor and add // the XmlObject to its lists foreach (object o in headerFooter.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); paragraphs.Add(p); bodyElements.Add(p); } if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); tables.Add(t); bodyElements.Add(t); } } /*XmlCursor cursor = headerFooter.NewCursor(); cursor.SelectPath("./*"); while (cursor.ToNextSelection()) { XmlObject o = cursor.Object; if (o is CTP) { XWPFParagraph p = new XWPFParagraph((CTP)o, this); paragraphs.Add(p); bodyElements.Add(p); } if (o is CTTbl) { XWPFTable t = new XWPFTable((CTTbl)o, this); tables.Add(t); bodyElements.Add(t); } } cursor.Dispose();*/ }
/** * Add a Paragraph to this Table Cell * @return The paragraph which was Added */ public XWPFParagraph AddParagraph() { XWPFParagraph p = new XWPFParagraph(ctTc.AddNewP(), this); AddParagraph(p); return p; }
internal override void OnDocumentRead() { base.OnDocumentRead(); FtrDocument ftrDocument = null; try { XmlDocument xmldoc = ConvertStreamToXml(GetPackagePart().GetInputStream()); ftrDocument = FtrDocument.Parse(xmldoc, NamespaceManager); headerFooter = ftrDocument.Ftr; // parse the document with cursor and add // the XmlObject to its lists foreach (object o in headerFooter.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); paragraphs.Add(p); bodyElements.Add(p); } if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); tables.Add(t); bodyElements.Add(t); } } } catch (Exception e) { throw new POIXMLException(e); } }
public void SetText(String text) { CT_P ctP = (ctTc.SizeOfPArray() == 0) ? ctTc.AddNewP() : ctTc.GetPArray(0); XWPFParagraph par = new XWPFParagraph(ctP, this); par.CreateRun().SetText(text); }
/** * Reads the document * @throws IOException */ internal override void OnDocumentRead() { base.OnDocumentRead(); HdrDocument hdrDocument = null; Stream is1; try { is1 = GetPackagePart().GetInputStream(); hdrDocument = HdrDocument.Parse(is1); headerFooter = hdrDocument.Hdr; foreach (object o in headerFooter.Items) { if (o is CT_P) { XWPFParagraph p = new XWPFParagraph((CT_P)o, this); paragraphs.Add(p); bodyElements.Add(p); } if (o is CT_Tbl) { XWPFTable t = new XWPFTable((CT_Tbl)o, this); tables.Add(t); bodyElements.Add(t); } } // parse the document with cursor and add // the XmlObject to its lists /*XmlCursor cursor = headerFooter.NewCursor(); cursor.SelectPath("./*"); while (cursor.ToNextSelection()) { XmlObject o = cursor.Object; if (o is CTP) { XWPFParagraph p = new XWPFParagraph((CTP)o, this); paragraphs.Add(p); bodyElements.Add(p); } if (o is CTTbl) { XWPFTable t = new XWPFTable((CTTbl)o, this); tables.Add(t); bodyElements.Add(t); } } cursor.Dispose();*/ } catch (Exception e) { throw new POIXMLException(e); } }
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); } }
/** * Get the position of the paragraph, within the list * of all the body elements. * @param p The paragraph to find * @return The location, or -1 if the paragraph couldn't be found */ public int GetPosOfParagraph(XWPFParagraph p) { return GetPosOfBodyElement(p); }