Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Container.cs 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Packaging;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using System.Xml.Linq;
  8. namespace Novacode
  9. {
  10. public abstract class Container : DocXElement
  11. {
  12. /// <summary>
  13. /// Returns a list of all Paragraphs inside this container.
  14. /// </summary>
  15. /// <example>
  16. /// <code>
  17. /// Load a document.
  18. /// using (DocX document = DocX.Load(@"Test.docx"))
  19. /// {
  20. /// // All Paragraphs in this document.
  21. /// List<Paragraph> documentParagraphs = document.Paragraphs;
  22. ///
  23. /// // Make sure this document contains at least one Table.
  24. /// if (document.Tables.Count() > 0)
  25. /// {
  26. /// // Get the first Table in this document.
  27. /// Table t = document.Tables[0];
  28. ///
  29. /// // All Paragraphs in this Table.
  30. /// List<Paragraph> tableParagraphs = t.Paragraphs;
  31. ///
  32. /// // Make sure this Table contains at least one Row.
  33. /// if (t.Rows.Count() > 0)
  34. /// {
  35. /// // Get the first Row in this document.
  36. /// Row r = t.Rows[0];
  37. ///
  38. /// // All Paragraphs in this Row.
  39. /// List<Paragraph> rowParagraphs = r.Paragraphs;
  40. ///
  41. /// // Make sure this Row contains at least one Cell.
  42. /// if (r.Cells.Count() > 0)
  43. /// {
  44. /// // Get the first Cell in this document.
  45. /// Cell c = r.Cells[0];
  46. ///
  47. /// // All Paragraphs in this Cell.
  48. /// List<Paragraph> cellParagraphs = c.Paragraphs;
  49. /// }
  50. /// }
  51. /// }
  52. ///
  53. /// // Save all changes to this document.
  54. /// document.Save();
  55. /// }// Release this document from memory.
  56. /// </code>
  57. /// </example>
  58. public virtual List<Paragraph> Paragraphs
  59. {
  60. get
  61. {
  62. List<Paragraph> paragraphs = GetParagraphs();
  63. foreach (var p in paragraphs)
  64. {
  65. if ((p.Xml.ElementsAfterSelf().FirstOrDefault() != null) && (p.Xml.ElementsAfterSelf().First().Name.Equals(DocX.w + "tbl")))
  66. p.FollowingTable = new Table(this.Document, p.Xml.ElementsAfterSelf().First());
  67. p.ParentContainer = GetParentFromXmlName(p.Xml.Ancestors().First().Name.LocalName);
  68. if (p.IsListItem)
  69. {
  70. GetListItemType(p);
  71. }
  72. }
  73. return paragraphs;
  74. }
  75. }
  76. public virtual List<Section> Sections
  77. {
  78. get
  79. {
  80. var allParas = Paragraphs;
  81. var parasInASection = new List<Paragraph>();
  82. var sections = new List<Section>();
  83. foreach (var para in allParas)
  84. {
  85. var sectionInPara = para.Xml.Descendants().FirstOrDefault(s => s.Name.LocalName == "sectPr");
  86. if (sectionInPara == null)
  87. {
  88. parasInASection.Add(para);
  89. }
  90. else
  91. {
  92. parasInASection.Add(para);
  93. var section = new Section(Document, sectionInPara) { SectionParagraphs = parasInASection };
  94. sections.Add(section);
  95. parasInASection = new List<Paragraph>();
  96. }
  97. }
  98. XElement body = Xml.Element(XName.Get("body", DocX.w.NamespaceName));
  99. XElement baseSectionXml = body.Element(XName.Get("sectPr", DocX.w.NamespaceName));
  100. var baseSection = new Section(Document, baseSectionXml) { SectionParagraphs = parasInASection };
  101. sections.Add(baseSection);
  102. return sections;
  103. }
  104. }
  105. private void GetListItemType(Paragraph p)
  106. {
  107. var ilvlNode = p.ParagraphNumberProperties.Descendants().FirstOrDefault(el => el.Name.LocalName == "ilvl");
  108. var ilvlValue = ilvlNode.Attribute(DocX.w + "val").Value;
  109. var numIdNode = p.ParagraphNumberProperties.Descendants().FirstOrDefault(el => el.Name.LocalName == "numId");
  110. var numIdValue = numIdNode.Attribute(DocX.w + "val").Value;
  111. //find num node in numbering
  112. var numNodes = Document.numbering.Descendants().Where(n => n.Name.LocalName == "num");
  113. XElement numNode = numNodes.FirstOrDefault(node => node.Attribute(DocX.w + "numId").Value.Equals(numIdValue));
  114. //Get abstractNumId node and its value from numNode
  115. var abstractNumIdNode = numNode.Descendants().First(n => n.Name.LocalName == "abstractNumId");
  116. var abstractNumNodeValue = abstractNumIdNode.Attribute(DocX.w + "val").Value;
  117. var abstractNumNodes = Document.numbering.Descendants().Where(n => n.Name.LocalName == "abstractNum");
  118. XElement abstractNumNode =
  119. abstractNumNodes.FirstOrDefault(node => node.Attribute(DocX.w + "abstractNumId").Value.Equals(abstractNumNodeValue));
  120. //Find lvl node
  121. var lvlNodes = abstractNumNode.Descendants().Where(n => n.Name.LocalName == "lvl");
  122. XElement lvlNode = null;
  123. foreach (XElement node in lvlNodes)
  124. {
  125. if (node.Attribute(DocX.w + "ilvl").Value.Equals(ilvlValue))
  126. {
  127. lvlNode = node;
  128. break;
  129. }
  130. }
  131. var numFmtNode = lvlNode.Descendants().First(n => n.Name.LocalName == "numFmt");
  132. p.ListItemType = GetListItemType(numFmtNode.Attribute(DocX.w + "val").Value);
  133. }
  134. public ContainerType ParentContainer;
  135. internal List<Paragraph> GetParagraphs()
  136. {
  137. // Need some memory that can be updated by the recursive search.
  138. int index = 0;
  139. List<Paragraph> paragraphs = new List<Paragraph>();
  140. GetParagraphsRecursive(Xml, ref index, ref paragraphs);
  141. return paragraphs;
  142. }
  143. internal void GetParagraphsRecursive(XElement Xml, ref int index, ref List<Paragraph> paragraphs)
  144. {
  145. // sdtContent are for PageNumbers inside Headers or Footers, don't go any deeper.
  146. //if (Xml.Name.LocalName == "sdtContent")
  147. // return;
  148. if (Xml.Name.LocalName == "p")
  149. {
  150. paragraphs.Add(new Paragraph(Document, Xml, index));
  151. index += HelperFunctions.GetText(Xml).Length;
  152. }
  153. else
  154. {
  155. if (Xml.HasElements)
  156. {
  157. foreach (XElement e in Xml.Elements())
  158. {
  159. GetParagraphsRecursive(e, ref index, ref paragraphs);
  160. }
  161. }
  162. }
  163. }
  164. public virtual List<Table> Tables
  165. {
  166. get
  167. {
  168. List<Table> tables =
  169. (
  170. from t in Xml.Descendants(DocX.w + "tbl")
  171. select new Table(Document, t)
  172. ).ToList();
  173. return tables;
  174. }
  175. }
  176. public virtual List<List> Lists
  177. {
  178. get
  179. {
  180. var lists = new List<List>();
  181. var list = new List(Document, Xml);
  182. foreach (var paragraph in Paragraphs)
  183. {
  184. if (paragraph.IsListItem)
  185. {
  186. if (list.CanAddListItem(paragraph))
  187. {
  188. list.AddItem(paragraph);
  189. }
  190. else
  191. {
  192. lists.Add(list);
  193. list = new List(Document, Xml);
  194. list.AddItem(paragraph);
  195. }
  196. }
  197. }
  198. lists.Add(list);
  199. return lists;
  200. }
  201. }
  202. public virtual List<Hyperlink> Hyperlinks
  203. {
  204. get
  205. {
  206. List<Hyperlink> hyperlinks = new List<Hyperlink>();
  207. foreach (Paragraph p in Paragraphs)
  208. hyperlinks.AddRange(p.Hyperlinks);
  209. return hyperlinks;
  210. }
  211. }
  212. public virtual List<Picture> Pictures
  213. {
  214. get
  215. {
  216. List<Picture> pictures = new List<Picture>();
  217. foreach (Paragraph p in Paragraphs)
  218. pictures.AddRange(p.Pictures);
  219. return pictures;
  220. }
  221. }
  222. /// <summary>
  223. /// Sets the Direction of content.
  224. /// </summary>
  225. /// <param name="direction">Direction either LeftToRight or RightToLeft</param>
  226. /// <example>
  227. /// Set the Direction of content in a Paragraph to RightToLeft.
  228. /// <code>
  229. /// // Load a document.
  230. /// using (DocX document = DocX.Load(@"Test.docx"))
  231. /// {
  232. /// // Get the first Paragraph from this document.
  233. /// Paragraph p = document.InsertParagraph();
  234. ///
  235. /// // Set the Direction of this Paragraph.
  236. /// p.Direction = Direction.RightToLeft;
  237. ///
  238. /// // Make sure the document contains at lest one Table.
  239. /// if (document.Tables.Count() > 0)
  240. /// {
  241. /// // Get the first Table from this document.
  242. /// Table t = document.Tables[0];
  243. ///
  244. /// /*
  245. /// * Set the direction of the entire Table.
  246. /// * Note: The same function is available at the Row and Cell level.
  247. /// */
  248. /// t.SetDirection(Direction.RightToLeft);
  249. /// }
  250. ///
  251. /// // Save all changes to this document.
  252. /// document.Save();
  253. /// }// Release this document from memory.
  254. /// </code>
  255. /// </example>
  256. public virtual void SetDirection(Direction direction)
  257. {
  258. foreach (Paragraph p in Paragraphs)
  259. p.Direction = direction;
  260. }
  261. public virtual List<int> FindAll(string str)
  262. {
  263. return FindAll(str, RegexOptions.None);
  264. }
  265. public virtual List<int> FindAll(string str, RegexOptions options)
  266. {
  267. List<int> list = new List<int>();
  268. foreach (Paragraph p in Paragraphs)
  269. {
  270. List<int> indexes = p.FindAll(str, options);
  271. for (int i = 0; i < indexes.Count(); i++)
  272. indexes[0] += p.startIndex;
  273. list.AddRange(indexes);
  274. }
  275. return list;
  276. }
  277. /// <summary>
  278. /// Find all unique instances of the given Regex Pattern,
  279. /// returning the list of the unique strings found
  280. /// </summary>
  281. /// <param name="str"></param>
  282. /// <param name="options"></param>
  283. /// <returns></returns>
  284. public virtual List<string> FindUniqueByPattern(string pattern, RegexOptions options)
  285. {
  286. List<string> rawResults = new List<string>();
  287. foreach (Paragraph p in Paragraphs)
  288. { // accumulate the search results from all paragraphs
  289. List<string> partials = p.FindAllByPattern(pattern, options);
  290. rawResults.AddRange(partials);
  291. }
  292. // this dictionary is used to collect results and test for uniqueness
  293. Dictionary<string, int> uniqueResults = new Dictionary<string, int>();
  294. foreach (string currValue in rawResults)
  295. {
  296. if (!uniqueResults.ContainsKey(currValue))
  297. { // if the dictionary doesn't have it, add it
  298. uniqueResults.Add(currValue, 0);
  299. }
  300. }
  301. return uniqueResults.Keys.ToList(); // return the unique list of results
  302. }
  303. public virtual void ReplaceText(string oldValue, string newValue, bool trackChanges = false, RegexOptions options = RegexOptions.None, Formatting newFormatting = null, Formatting matchFormatting = null, MatchFormattingOptions fo = MatchFormattingOptions.SubsetMatch)
  304. {
  305. // PATCH BY HeDo - Arguments check!
  306. if (oldValue == null || oldValue.Length == 0)
  307. throw new ArgumentException("oldValue cannot be null or empty", "oldValue");
  308. if (newValue == null || newValue.Length == 0)
  309. throw new ArgumentException("newValue cannot be null or empty", "newValue");
  310. // ReplaceText in Headers of the document.
  311. Headers headers = Document.Headers;
  312. List<Header> headerList = new List<Header> { headers.first, headers.even, headers.odd };
  313. foreach (Header h in headerList)
  314. if (h != null)
  315. foreach (Paragraph p in h.Paragraphs)
  316. p.ReplaceText(oldValue, newValue, trackChanges, options, newFormatting, matchFormatting, fo);
  317. // ReplaceText int main body of document.
  318. foreach (Paragraph p in Paragraphs)
  319. p.ReplaceText(oldValue, newValue, trackChanges, options, newFormatting, matchFormatting, fo);
  320. // ReplaceText in Footers of the document.
  321. Footers footers = Document.Footers;
  322. List<Footer> footerList = new List<Footer> { footers.first, footers.even, footers.odd };
  323. foreach (Footer f in footerList)
  324. if (f != null)
  325. foreach (Paragraph p in f.Paragraphs)
  326. p.ReplaceText(oldValue, newValue, trackChanges, options, newFormatting, matchFormatting, fo);
  327. }
  328. public virtual Paragraph InsertParagraph(int index, string text, bool trackChanges)
  329. {
  330. return InsertParagraph(index, text, trackChanges, null);
  331. }
  332. public virtual Paragraph InsertParagraph()
  333. {
  334. return InsertParagraph(string.Empty, false);
  335. }
  336. public virtual Paragraph InsertParagraph(int index, Paragraph p)
  337. {
  338. XElement newXElement = new XElement(p.Xml);
  339. p.Xml = newXElement;
  340. Paragraph paragraph = HelperFunctions.GetFirstParagraphEffectedByInsert(Document, index);
  341. if (paragraph == null)
  342. Xml.Add(p.Xml);
  343. else
  344. {
  345. XElement[] split = HelperFunctions.SplitParagraph(paragraph, index - paragraph.startIndex);
  346. paragraph.Xml.ReplaceWith
  347. (
  348. split[0],
  349. newXElement,
  350. split[1]
  351. );
  352. }
  353. GetParent(p);
  354. return p;
  355. }
  356. public virtual Paragraph InsertParagraph(Paragraph p)
  357. {
  358. #region Styles
  359. XDocument style_document;
  360. if (p.styles.Count() > 0)
  361. {
  362. Uri style_package_uri = new Uri("/word/styles.xml", UriKind.Relative);
  363. if (!Document.package.PartExists(style_package_uri))
  364. {
  365. PackagePart style_package = Document.package.CreatePart(style_package_uri, "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml", CompressionOption.Maximum);
  366. using (TextWriter tw = new StreamWriter(style_package.GetStream()))
  367. {
  368. style_document = new XDocument
  369. (
  370. new XDeclaration("1.0", "UTF-8", "yes"),
  371. new XElement(XName.Get("styles", DocX.w.NamespaceName))
  372. );
  373. style_document.Save(tw);
  374. }
  375. }
  376. PackagePart styles_document = Document.package.GetPart(style_package_uri);
  377. using (TextReader tr = new StreamReader(styles_document.GetStream()))
  378. {
  379. style_document = XDocument.Load(tr);
  380. XElement styles_element = style_document.Element(XName.Get("styles", DocX.w.NamespaceName));
  381. var ids = from d in styles_element.Descendants(XName.Get("style", DocX.w.NamespaceName))
  382. let a = d.Attribute(XName.Get("styleId", DocX.w.NamespaceName))
  383. where a != null
  384. select a.Value;
  385. foreach (XElement style in p.styles)
  386. {
  387. // If styles_element does not contain this element, then add it.
  388. if (!ids.Contains(style.Attribute(XName.Get("styleId", DocX.w.NamespaceName)).Value))
  389. styles_element.Add(style);
  390. }
  391. }
  392. using (TextWriter tw = new StreamWriter(styles_document.GetStream()))
  393. style_document.Save(tw);
  394. }
  395. #endregion
  396. XElement newXElement = new XElement(p.Xml);
  397. Xml.Add(newXElement);
  398. int index = 0;
  399. if (Document.paragraphLookup.Keys.Count() > 0)
  400. {
  401. index = Document.paragraphLookup.Last().Key;
  402. if (Document.paragraphLookup.Last().Value.Text.Length == 0)
  403. index++;
  404. else
  405. index += Document.paragraphLookup.Last().Value.Text.Length;
  406. }
  407. Paragraph newParagraph = new Paragraph(Document, newXElement, index);
  408. Document.paragraphLookup.Add(index, newParagraph);
  409. GetParent(newParagraph);
  410. return newParagraph;
  411. }
  412. public virtual Paragraph InsertParagraph(int index, string text, bool trackChanges, Formatting formatting)
  413. {
  414. Paragraph newParagraph = new Paragraph(Document, new XElement(DocX.w + "p"), index);
  415. newParagraph.InsertText(0, text, trackChanges, formatting);
  416. Paragraph firstPar = HelperFunctions.GetFirstParagraphEffectedByInsert(Document, index);
  417. if (firstPar != null)
  418. {
  419. XElement[] splitParagraph = HelperFunctions.SplitParagraph(firstPar, index - firstPar.startIndex);
  420. firstPar.Xml.ReplaceWith
  421. (
  422. splitParagraph[0],
  423. newParagraph.Xml,
  424. splitParagraph[1]
  425. );
  426. }
  427. else
  428. Xml.Add(newParagraph);
  429. GetParent(newParagraph);
  430. return newParagraph;
  431. }
  432. private ContainerType GetParentFromXmlName(string xmlName)
  433. {
  434. ContainerType parent;
  435. switch (xmlName)
  436. {
  437. case "body":
  438. parent = ContainerType.Body;
  439. break;
  440. case "p":
  441. parent = ContainerType.Paragraph;
  442. break;
  443. case "tbl":
  444. parent = ContainerType.Table;
  445. break;
  446. case "sectPr":
  447. parent = ContainerType.Section;
  448. break;
  449. case "tc":
  450. parent = ContainerType.Cell;
  451. break;
  452. default:
  453. parent = ContainerType.None;
  454. break;
  455. }
  456. return parent;
  457. }
  458. private void GetParent(Paragraph newParagraph)
  459. {
  460. var containerType = GetType();
  461. switch (containerType.Name)
  462. {
  463. case "Body":
  464. newParagraph.ParentContainer = ContainerType.Body;
  465. break;
  466. case "Table":
  467. newParagraph.ParentContainer = ContainerType.Table;
  468. break;
  469. case "TOC":
  470. newParagraph.ParentContainer = ContainerType.TOC;
  471. break;
  472. case "Section":
  473. newParagraph.ParentContainer = ContainerType.Section;
  474. break;
  475. case "Cell":
  476. newParagraph.ParentContainer = ContainerType.Cell;
  477. break;
  478. case "Header":
  479. newParagraph.ParentContainer = ContainerType.Header;
  480. break;
  481. case "Footer":
  482. newParagraph.ParentContainer = ContainerType.Footer;
  483. break;
  484. case "Paragraph":
  485. newParagraph.ParentContainer = ContainerType.Paragraph;
  486. break;
  487. }
  488. }
  489. private ListItemType GetListItemType(string styleName)
  490. {
  491. ListItemType listItemType;
  492. switch (styleName)
  493. {
  494. case "bullet":
  495. listItemType = ListItemType.Bulleted;
  496. break;
  497. default:
  498. listItemType = ListItemType.Numbered;
  499. break;
  500. }
  501. return listItemType;
  502. }
  503. public virtual void InsertSection()
  504. {
  505. InsertSection(false);
  506. }
  507. public virtual void InsertSection(bool trackChanges)
  508. {
  509. var newParagraphSection = new XElement
  510. (
  511. XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName), new XElement(XName.Get("sectPr", DocX.w.NamespaceName), new XElement(XName.Get("type", DocX.w.NamespaceName), new XAttribute(DocX.w + "val", "continuous"))))
  512. );
  513. if (trackChanges)
  514. newParagraphSection = HelperFunctions.CreateEdit(EditType.ins, DateTime.Now, newParagraphSection);
  515. Xml.Add(newParagraphSection);
  516. }
  517. public virtual void InsertSectionPageBreak(bool trackChanges = false)
  518. {
  519. var newParagraphSection = new XElement
  520. (
  521. XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName), new XElement(XName.Get("sectPr", DocX.w.NamespaceName)))
  522. );
  523. if (trackChanges)
  524. newParagraphSection = HelperFunctions.CreateEdit(EditType.ins, DateTime.Now, newParagraphSection);
  525. Xml.Add(newParagraphSection);
  526. }
  527. public virtual Paragraph InsertParagraph(string text)
  528. {
  529. return InsertParagraph(text, false, new Formatting());
  530. }
  531. public virtual Paragraph InsertParagraph(string text, bool trackChanges)
  532. {
  533. return InsertParagraph(text, trackChanges, new Formatting());
  534. }
  535. public virtual Paragraph InsertParagraph(string text, bool trackChanges, Formatting formatting)
  536. {
  537. XElement newParagraph = new XElement
  538. (
  539. XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), HelperFunctions.FormatInput(text, formatting.Xml)
  540. );
  541. if (trackChanges)
  542. newParagraph = HelperFunctions.CreateEdit(EditType.ins, DateTime.Now, newParagraph);
  543. Xml.Add(newParagraph);
  544. var paragraphAdded = Paragraphs.Last();
  545. GetParent(paragraphAdded);
  546. return paragraphAdded;
  547. }
  548. public virtual Paragraph InsertEquation(string equation)
  549. {
  550. Paragraph p = InsertParagraph();
  551. p.AppendEquation(equation);
  552. return p;
  553. }
  554. public virtual Table InsertTable(int rowCount, int columnCount) //Dmitchern, changed to virtual, and overrided in Table.Cell
  555. {
  556. XElement newTable = HelperFunctions.CreateTable(rowCount, columnCount);
  557. Xml.Add(newTable);
  558. return new Table(Document, newTable);
  559. }
  560. public Table InsertTable(int index, int rowCount, int columnCount)
  561. {
  562. XElement newTable = HelperFunctions.CreateTable(rowCount, columnCount);
  563. Paragraph p = HelperFunctions.GetFirstParagraphEffectedByInsert(Document, index);
  564. if (p == null)
  565. Xml.Elements().First().AddFirst(newTable);
  566. else
  567. {
  568. XElement[] split = HelperFunctions.SplitParagraph(p, index - p.startIndex);
  569. p.Xml.ReplaceWith
  570. (
  571. split[0],
  572. newTable,
  573. split[1]
  574. );
  575. }
  576. return new Table(Document, newTable);
  577. }
  578. public Table InsertTable(Table t)
  579. {
  580. XElement newXElement = new XElement(t.Xml);
  581. Xml.Add(newXElement);
  582. Table newTable = new Table(Document, newXElement);
  583. newTable.Design = t.Design;
  584. return newTable;
  585. }
  586. public Table InsertTable(int index, Table t)
  587. {
  588. Paragraph p = HelperFunctions.GetFirstParagraphEffectedByInsert(Document, index);
  589. XElement[] split = HelperFunctions.SplitParagraph(p, index - p.startIndex);
  590. XElement newXElement = new XElement(t.Xml);
  591. p.Xml.ReplaceWith
  592. (
  593. split[0],
  594. newXElement,
  595. split[1]
  596. );
  597. Table newTable = new Table(Document, newXElement);
  598. newTable.Design = t.Design;
  599. return newTable;
  600. }
  601. internal Container(DocX document, XElement xml)
  602. : base(document, xml)
  603. {
  604. }
  605. public List InsertList(List list)
  606. {
  607. foreach (var item in list.Items)
  608. {
  609. // item.Font(System.Drawing.FontFamily fontFamily)
  610. Xml.Add(item.Xml);
  611. }
  612. return list;
  613. }
  614. public List InsertList(List list, double fontSize)
  615. {
  616. foreach (var item in list.Items)
  617. {
  618. item.FontSize(fontSize);
  619. Xml.Add(item.Xml);
  620. }
  621. return list;
  622. }
  623. public List InsertList(List list, System.Drawing.FontFamily fontFamily, double fontSize)
  624. {
  625. foreach (var item in list.Items)
  626. {
  627. item.Font(fontFamily);
  628. item.FontSize(fontSize);
  629. Xml.Add(item.Xml);
  630. }
  631. return list;
  632. }
  633. public List InsertList(int index, List list)
  634. {
  635. Paragraph p = HelperFunctions.GetFirstParagraphEffectedByInsert(Document, index);
  636. XElement[] split = HelperFunctions.SplitParagraph(p, index - p.startIndex);
  637. var elements = new List<XElement> { split[0] };
  638. elements.AddRange(list.Items.Select(i => new XElement(i.Xml)));
  639. elements.Add(split[1]);
  640. p.Xml.ReplaceWith(elements.ToArray());
  641. return list;
  642. }
  643. }
  644. }