| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Xml.Linq;
-
- namespace Novacode
- {
- /// <summary>
- /// All DocX types are derived from DocXElement.
- /// This class contains properties which every element of a DocX must contain.
- /// </summary>
- public abstract class DocXElement
- {
- /// <summary>
- /// This is the actual Xml that gives this element substance.
- /// For example, a Paragraphs Xml might look something like the following
- /// <p>
- /// <r>
- /// <t>Hello World!</t>
- /// </r>
- /// </p>
- /// </summary>
- private XElement xml;
- internal XElement Xml { get { return xml; } set { xml = value; } }
-
- /// <summary>
- /// This is a reference to the DocX object that this element belongs to.
- /// Every DocX element is connected to a document.
- /// </summary>
- private DocX document;
- internal DocX Document { get { return document; } set { document = value; } }
-
- /// <summary>
- /// Store both the document and xml so that they can be accessed by derived types.
- /// </summary>
- /// <param name="document">The document that this element belongs to.</param>
- /// <param name="xml">The Xml that gives this element substance</param>
- public DocXElement(DocX document, XElement xml)
- {
- this.document = document;
- this.xml = xml;
- }
- }
-
- /// <summary>
- /// This class provides functions for inserting new DocXElements before or after the current DocXElement.
- /// Only certain DocXElements can support these functions without creating invalid documents, at the moment these are Paragraphs and Table.
- /// </summary>
- public abstract class InsertBeforeOrAfter:DocXElement
- {
- public InsertBeforeOrAfter(DocX document, XElement xml):base(document, xml) { }
-
- public virtual void InsertPageBreakBeforeSelf()
- {
- XElement p = new XElement
- (
- XName.Get("p", DocX.w.NamespaceName),
- new XElement
- (
- XName.Get("r", DocX.w.NamespaceName),
- new XElement
- (
- XName.Get("br", DocX.w.NamespaceName),
- new XAttribute(XName.Get("type", DocX.w.NamespaceName), "page")
- )
- )
- );
-
- Xml.AddBeforeSelf(p);
- }
-
- public virtual void InsertPageBreakAfterSelf()
- {
- XElement p = new XElement
- (
- XName.Get("p", DocX.w.NamespaceName),
- new XElement
- (
- XName.Get("r", DocX.w.NamespaceName),
- new XElement
- (
- XName.Get("br", DocX.w.NamespaceName),
- new XAttribute(XName.Get("type", DocX.w.NamespaceName), "page")
- )
- )
- );
-
- Xml.AddAfterSelf(p);
- }
-
- public virtual Paragraph InsertParagraphBeforeSelf(Paragraph p)
- {
- Xml.AddBeforeSelf(p.Xml);
- XElement newlyInserted = Xml.ElementsBeforeSelf().First();
-
- p.Xml = newlyInserted;
- DocX.RebuildParagraphs(Document);
-
- return p;
- }
-
- public virtual Paragraph InsertParagraphAfterSelf(Paragraph p)
- {
- Xml.AddAfterSelf(p.Xml);
- XElement newlyInserted = Xml.ElementsAfterSelf().First();
-
- p.Xml = newlyInserted;
- DocX.RebuildParagraphs(Document);
-
- return p;
- }
-
- public virtual Paragraph InsertParagraphBeforeSelf(string text)
- {
- return InsertParagraphBeforeSelf(text, false, new Formatting());
- }
-
- public virtual Paragraph InsertParagraphAfterSelf(string text)
- {
- return InsertParagraphAfterSelf(text, false, new Formatting());
- }
-
- public virtual Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges)
- {
- return InsertParagraphBeforeSelf(text, trackChanges, new Formatting());
- }
-
- public virtual Paragraph InsertParagraphAfterSelf(string text, bool trackChanges)
- {
- return InsertParagraphAfterSelf(text, trackChanges, new Formatting());
- }
-
- public virtual Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges, Formatting formatting)
- {
- XElement newParagraph = new XElement
- (
- XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), DocX.FormatInput(text, formatting.Xml)
- );
-
- if (trackChanges)
- newParagraph = Paragraph.CreateEdit(EditType.ins, DateTime.Now, newParagraph);
-
- Xml.AddBeforeSelf(newParagraph);
- XElement newlyInserted = Xml.ElementsBeforeSelf().First();
-
- Paragraph p = new Paragraph(Document, newlyInserted, -1);
- DocX.RebuildParagraphs(Document);
-
- return p;
- }
-
- public virtual Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting)
- {
- XElement newParagraph = new XElement
- (
- XName.Get("p", DocX.w.NamespaceName), new XElement(XName.Get("pPr", DocX.w.NamespaceName)), DocX.FormatInput(text, formatting.Xml)
- );
-
- if (trackChanges)
- newParagraph = Paragraph.CreateEdit(EditType.ins, DateTime.Now, newParagraph);
-
- Xml.AddAfterSelf(newParagraph);
- XElement newlyInserted = Xml.ElementsAfterSelf().First();
-
- Paragraph p = new Paragraph(Document, newlyInserted, -1);
- DocX.RebuildParagraphs(Document);
-
- return p;
- }
-
- public virtual Table InsertTableAfterSelf(int rowCount, int coloumnCount)
- {
- XElement newTable = DocX.CreateTable(rowCount, coloumnCount);
- Xml.AddAfterSelf(newTable);
- XElement newlyInserted = Xml.ElementsAfterSelf().First();
-
- DocX.RebuildTables(Document);
- DocX.RebuildParagraphs(Document);
- return new Table(Document, newlyInserted);
- }
-
- public virtual Table InsertTableAfterSelf(Table t)
- {
- Xml.AddAfterSelf(t.Xml);
- XElement newlyInserted = Xml.ElementsAfterSelf().First();
-
- t.Xml = newlyInserted;
- DocX.RebuildTables(Document);
- DocX.RebuildParagraphs(Document);
-
- return t;
- }
-
- public virtual Table InsertTableBeforeSelf(int rowCount, int coloumnCount)
- {
- XElement newTable = DocX.CreateTable(rowCount, coloumnCount);
- Xml.AddBeforeSelf(newTable);
- XElement newlyInserted = Xml.ElementsBeforeSelf().First();
-
- DocX.RebuildTables(Document);
- DocX.RebuildParagraphs(Document);
- return new Table(Document, newlyInserted);
- }
-
- public virtual Table InsertTableBeforeSelf(Table t)
- {
- Xml.AddBeforeSelf(t.Xml);
- XElement newlyInserted = Xml.ElementsBeforeSelf().First();
-
- t.Xml = newlyInserted;
- DocX.RebuildTables(Document);
- DocX.RebuildParagraphs(Document);
-
- return t;
- }
- }
- }
|