Paragraphs { get; }
- }
-}
\ No newline at end of file
diff --git a/DocX/Image.cs b/DocX/Image.cs
deleted file mode 100644
index f2704743..00000000
--- a/DocX/Image.cs
+++ /dev/null
@@ -1,96 +0,0 @@
-using System;
-using System.IO.Packaging;
-using System.IO;
-
-namespace Novacode
-{
- ///
- /// Represents an Image embedded in a document.
- ///
- public class Image
- {
- ///
- /// A unique id which identifies this Image.
- ///
- private string id;
- private DocX document;
- internal PackageRelationship pr;
-
- public Stream GetStream(FileMode mode, FileAccess access)
- {
- string temp = pr.SourceUri.OriginalString;
- string start = temp.Remove(temp.LastIndexOf('/'));
- string end = pr.TargetUri.OriginalString;
- string full = end.Contains(start) ? end : start + "/" + end;
-
- return (new PackagePartStream(document.package.GetPart(new Uri(full, UriKind.Relative)).GetStream(mode, access)));
- }
-
- ///
- /// Returns the id of this Image.
- ///
- public string Id
- {
- get {return id;}
- }
-
- internal Image(DocX document, PackageRelationship pr)
- {
- this.document = document;
- this.pr = pr;
- this.id = pr.Id;
- }
-
- ///
- /// Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append.
- ///
- ///
- ///
- /// Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append.
- ///
- /// using (DocX document = DocX.Create("Test.docx"))
- /// {
- /// // Add an image to the document.
- /// Image i = document.AddImage(@"Image.jpg");
- ///
- /// // Create a picture i.e. (A custom view of an image)
- /// Picture p = i.CreatePicture();
- /// p.FlipHorizontal = true;
- /// p.Rotation = 10;
- ///
- /// // Create a new Paragraph.
- /// Paragraph par = document.InsertParagraph();
- ///
- /// // Append content to the Paragraph.
- /// par.Append("Here is a cool picture")
- /// .AppendPicture(p)
- /// .Append(" don't you think so?");
- ///
- /// // Save all changes made to this document.
- /// document.Save();
- /// }
- ///
- ///
- public Picture CreatePicture()
- {
- return Paragraph.CreatePicture(document, id, string.Empty, string.Empty);
- }
- public Picture CreatePicture(int height, int width) {
- Picture picture = Paragraph.CreatePicture(document, id, string.Empty, string.Empty);
- picture.Height = height;
- picture.Width = width;
- return picture;
- }
-
- ///
- /// Returns the name of the image file.
- ///
- public string FileName
- {
- get
- {
- return Path.GetFileName(this.pr.TargetUri.ToString());
- }
- }
- }
-}
diff --git a/DocX/KeyWithoutPassword.snk b/DocX/KeyWithoutPassword.snk
deleted file mode 100644
index 9a9fc9c7..00000000
Binary files a/DocX/KeyWithoutPassword.snk and /dev/null differ
diff --git a/DocX/License/License.html b/DocX/License/License.html
deleted file mode 100644
index 93e03003..00000000
--- a/DocX/License/License.html
+++ /dev/null
@@ -1,7 +0,0 @@
-
-
-
- Microsoft Public License (Ms-PL)
This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
1. Definitions
The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
A "contribution" is the original software, or any additions or changes to the software.
A "contributor" is any person that distributes its contribution under this license.
"Licensed patents" are a contributor's patent claims that read directly on its contribution.
2. Grant of Rights
(A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
(B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
3. Conditions and Limitations
(A) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
(B) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
(C) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
(D) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
(E) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.
-
-
-
\ No newline at end of file
diff --git a/DocX/List.cs b/DocX/List.cs
deleted file mode 100644
index 3e44a174..00000000
--- a/DocX/List.cs
+++ /dev/null
@@ -1,233 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Xml.Linq;
-
-namespace Novacode
-{
- ///
- /// Represents a List in a document.
- ///
- public class List : InsertBeforeOrAfter
- {
- ///
- /// This is a list of paragraphs that will be added to the document
- /// when the list is inserted into the document.
- /// The paragraph needs a numPr defined to be in this items collection.
- ///
- public List Items { get; private set; }
- ///
- /// The numId used to reference the list settings in the numbering.xml
- ///
- public int NumId { get; private set; }
- ///
- /// The ListItemType (bullet or numbered) of the list.
- ///
- public ListItemType? ListType { get; private set; }
-
- internal List(DocX document, XElement xml)
- : base(document, xml)
- {
- Items = new List();
- ListType = null;
- }
-
- ///
- /// Adds an item to the list.
- ///
- ///
- ///
- /// Throws an InvalidOperationException if the item cannot be added to the list.
- ///
- public void AddItem(Paragraph paragraph)
- {
- if (paragraph.IsListItem)
- {
- var numIdNode = paragraph.Xml.Descendants().First(s => s.Name.LocalName == "numId");
- var numId = Int32.Parse(numIdNode.Attribute(DocX.w + "val").Value);
-
- if (CanAddListItem(paragraph))
- {
- NumId = numId;
- Items.Add(paragraph);
- }
- else
- throw new InvalidOperationException("New list items can only be added to this list if they are have the same numId.");
- }
- }
-
- public void AddItemWithStartValue(Paragraph paragraph, int start)
- {
- //TODO: Update the numbering
- UpdateNumberingForLevelStartNumber(int.Parse(paragraph.IndentLevel.ToString()), start);
- if (ContainsLevel(start))
- throw new InvalidOperationException("Cannot add a paragraph with a start value if another element already exists in this list with that level.");
- AddItem(paragraph);
- }
-
- private void UpdateNumberingForLevelStartNumber(int iLevel, int start)
- {
- var abstractNum = GetAbstractNum(NumId);
- var level = abstractNum.Descendants().First(el => el.Name.LocalName == "lvl" && el.GetAttribute(DocX.w + "ilvl") == iLevel.ToString());
- level.Descendants().First(el => el.Name.LocalName == "start").SetAttributeValue(DocX.w + "val", start);
- }
-
- ///
- /// Determine if it is able to add the item to the list
- ///
- ///
- ///
- /// Return true if AddItem(...) will succeed with the given paragraph.
- ///
- public bool CanAddListItem(Paragraph paragraph)
- {
- if (paragraph.IsListItem)
- {
- //var lvlNode = paragraph.Xml.Descendants().First(s => s.Name.LocalName == "ilvl");
- var numIdNode = paragraph.Xml.Descendants().First(s => s.Name.LocalName == "numId");
- var numId = Int32.Parse(numIdNode.Attribute(DocX.w + "val").Value);
-
- //Level = Int32.Parse(lvlNode.Attribute(DocX.w + "val").Value);
- if (NumId == 0 || (numId == NumId && numId > 0))
- {
- return true;
- }
- }
- return false;
- }
-
- public bool ContainsLevel(int ilvl)
- {
- return Items.Any(i => i.ParagraphNumberProperties.Descendants().First(el => el.Name.LocalName == "ilvl").Value == ilvl.ToString());
- }
-
- internal void CreateNewNumberingNumId(int level = 0, ListItemType listType = ListItemType.Numbered, int? startNumber = null, bool continueNumbering = false)
- {
- ValidateDocXNumberingPartExists();
- if (Document.numbering.Root == null)
- {
- throw new InvalidOperationException("Numbering section did not instantiate properly.");
- }
-
- ListType = listType;
-
- var numId = GetMaxNumId() + 1;
- var abstractNumId = GetMaxAbstractNumId() + 1;
-
- XDocument listTemplate;
- switch (listType)
- {
- case ListItemType.Bulleted:
- listTemplate = HelperFunctions.DecompressXMLResource("Novacode.Resources.numbering.default_bullet_abstract.xml.gz");
- break;
- case ListItemType.Numbered:
- listTemplate = HelperFunctions.DecompressXMLResource("Novacode.Resources.numbering.default_decimal_abstract.xml.gz");
- break;
- default:
- throw new InvalidOperationException(string.Format("Unable to deal with ListItemType: {0}.", listType.ToString()));
- }
-
- var abstractNumTemplate = listTemplate.Descendants().Single(d => d.Name.LocalName == "abstractNum");
- abstractNumTemplate.SetAttributeValue(DocX.w + "abstractNumId", abstractNumId);
-
- //Fixing an issue where numbering would continue from previous numbered lists. Setting startOverride assures that a numbered list starts on the provided number.
- //The override needs only be on level 0 as this will cascade to the rest of the list.
- var abstractNumXml = GetAbstractNumXml(abstractNumId, numId, startNumber, continueNumbering);
-
- var abstractNumNode = Document.numbering.Root.Descendants().LastOrDefault(xElement => xElement.Name.LocalName == "abstractNum");
- var numXml = Document.numbering.Root.Descendants().LastOrDefault(xElement => xElement.Name.LocalName == "num");
-
- if (abstractNumNode == null || numXml == null)
- {
- Document.numbering.Root.Add(abstractNumTemplate);
- Document.numbering.Root.Add(abstractNumXml);
- }
- else
- {
- abstractNumNode.AddAfterSelf(abstractNumTemplate);
- numXml.AddAfterSelf(
- abstractNumXml
- );
- }
-
- NumId = numId;
- }
-
- private XElement GetAbstractNumXml(int abstractNumId, int numId, int? startNumber, bool continueNumbering)
- {
- //Fixing an issue where numbering would continue from previous numbered lists. Setting startOverride assures that a numbered list starts on the provided number.
- //The override needs only be on level 0 as this will cascade to the rest of the list.
- var startOverride = new XElement(XName.Get("startOverride", DocX.w.NamespaceName), new XAttribute(DocX.w + "val", startNumber ?? 1));
- var lvlOverride = new XElement(XName.Get("lvlOverride", DocX.w.NamespaceName), new XAttribute(DocX.w + "ilvl", 0), startOverride);
- var abstractNumIdElement = new XElement(XName.Get("abstractNumId", DocX.w.NamespaceName), new XAttribute(DocX.w + "val", abstractNumId));
- return continueNumbering
- ? new XElement(XName.Get("num", DocX.w.NamespaceName), new XAttribute(DocX.w + "numId", numId), abstractNumIdElement)
- : new XElement(XName.Get("num", DocX.w.NamespaceName), new XAttribute(DocX.w + "numId", numId), abstractNumIdElement, lvlOverride);
- }
-
- ///
- /// Method to determine the last numId for a list element.
- /// Also useful for determining the next numId to use for inserting a new list element into the document.
- ///
- ///
- /// 0 if there are no elements in the list already.
- /// Increment the return for the next valid value of a new list element.
- ///
- private int GetMaxNumId()
- {
- const int defaultValue = 0;
- if (Document.numbering == null)
- return defaultValue;
-
- var numlist = Document.numbering.Descendants().Where(d => d.Name.LocalName == "num").ToList();
- if (numlist.Any())
- return numlist.Attributes(DocX.w + "numId").Max(e => int.Parse(e.Value));
- return defaultValue;
- }
-
- ///
- /// Method to determine the last abstractNumId for a list element.
- /// Also useful for determining the next abstractNumId to use for inserting a new list element into the document.
- ///
- ///
- /// -1 if there are no elements in the list already.
- /// Increment the return for the next valid value of a new list element.
- ///
- private int GetMaxAbstractNumId()
- {
- const int defaultValue = -1;
-
- if (Document.numbering == null)
- return defaultValue;
-
- var numlist = Document.numbering.Descendants().Where(d => d.Name.LocalName == "abstractNum").ToList();
- if (numlist.Any())
- {
- var maxAbstractNumId = numlist.Attributes(DocX.w + "abstractNumId").Max(e => int.Parse(e.Value));
- return maxAbstractNumId;
- }
- return defaultValue;
- }
-
- ///
- /// Get the abstractNum definition for the given numId
- ///
- /// The numId on the pPr element
- /// XElement representing the requested abstractNum
- internal XElement GetAbstractNum(int numId)
- {
- var num = Document.numbering.Descendants().First(d => d.Name.LocalName == "num" && d.GetAttribute(DocX.w + "numId").Equals(numId.ToString()));
- var abstractNumId = num.Descendants().First(d => d.Name.LocalName == "abstractNumId");
- return Document.numbering.Descendants().First(d => d.Name.LocalName == "abstractNum" && d.GetAttribute("abstractNumId").Equals(abstractNumId.Value));
- }
-
- private void ValidateDocXNumberingPartExists()
- {
- var numberingUri = new Uri("/word/numbering.xml", UriKind.Relative);
-
- // If the internal document contains no /word/numbering.xml create one.
- if (!Document.package.PartExists(numberingUri))
- Document.numbering = HelperFunctions.AddDefaultNumberingXml(Document.package);
- }
- }
-}
diff --git a/DocX/PackagePartStream.cs b/DocX/PackagePartStream.cs
deleted file mode 100644
index b8c0b4ba..00000000
--- a/DocX/PackagePartStream.cs
+++ /dev/null
@@ -1,87 +0,0 @@
-using System.IO;
-
-namespace Novacode
-{
- ///
- /// OpenXML Isolated Storage access is not thread safe.
- /// Use app domain wide lock for writing.
- ///
- public class PackagePartStream : Stream
- {
- private static readonly object lockObject = new object();
-
- private readonly Stream stream;
-
- public PackagePartStream(Stream stream)
- {
- this.stream = stream;
- }
-
- public override bool CanRead
- {
- get { return this.stream.CanRead; }
- }
-
- public override bool CanSeek
- {
- get { return this.stream.CanSeek; }
- }
-
- public override bool CanWrite
- {
- get { return this.stream.CanWrite; }
- }
-
- public override long Length
- {
- get { return this.stream.Length; }
- }
-
- public override long Position
- {
- get { return this.stream.Position; }
- set { this.stream.Position = value; }
- }
-
- public override long Seek(long offset, SeekOrigin origin)
- {
- return this.stream.Seek(offset, origin);
- }
-
- public override void SetLength(long value)
- {
- this.stream.SetLength(value);
- }
-
- public override int Read(byte[] buffer, int offset, int count)
- {
- return this.stream.Read(buffer, offset, count);
- }
-
- public override void Write(byte[] buffer, int offset, int count)
- {
- lock (lockObject)
- {
- this.stream.Write(buffer, offset, count);
- }
- }
-
- public override void Flush()
- {
- lock (lockObject)
- {
- this.stream.Flush();
- }
- }
-
- public override void Close()
- {
- this.stream.Close();
- }
-
- protected override void Dispose(bool disposing)
- {
- this.stream.Dispose();
- }
- }
-}
diff --git a/DocX/PageLayout.cs b/DocX/PageLayout.cs
deleted file mode 100644
index ee832958..00000000
--- a/DocX/PageLayout.cs
+++ /dev/null
@@ -1,74 +0,0 @@
-using System;
-using System.Xml.Linq;
-
-namespace Novacode
-{
- public class PageLayout: DocXElement
- {
- internal PageLayout(DocX document, XElement xml):base(document, xml)
- {
-
- }
-
-
- public Orientation Orientation
- {
- get
- {
- /*
- * Get the pgSz (page size) element for this Section,
- * null will be return if no such element exists.
- */
- XElement pgSz = Xml.Element(XName.Get("pgSz", DocX.w.NamespaceName));
-
- if (pgSz == null)
- return Orientation.Portrait;
-
- // Get the attribute of the pgSz element.
- XAttribute val = pgSz.Attribute(XName.Get("orient", DocX.w.NamespaceName));
-
- // If val is null, this cell contains no information.
- if (val == null)
- return Orientation.Portrait;
-
- if (val.Value.Equals("Landscape", StringComparison.CurrentCultureIgnoreCase))
- return Orientation.Landscape;
- else
- return Orientation.Portrait;
- }
-
- set
- {
- // Check if already correct value.
- if (Orientation == value)
- return;
-
- /*
- * Get the pgSz (page size) element for this Section,
- * null will be return if no such element exists.
- */
- XElement pgSz = Xml.Element(XName.Get("pgSz", DocX.w.NamespaceName));
-
- if (pgSz == null)
- {
- Xml.SetElementValue(XName.Get("pgSz", DocX.w.NamespaceName), string.Empty);
- pgSz = Xml.Element(XName.Get("pgSz", DocX.w.NamespaceName));
- }
-
- pgSz.SetAttributeValue(XName.Get("orient", DocX.w.NamespaceName), value.ToString().ToLower());
-
- if(value == Novacode.Orientation.Landscape)
- {
- pgSz.SetAttributeValue(XName.Get("w", DocX.w.NamespaceName), "16838");
- pgSz.SetAttributeValue(XName.Get("h", DocX.w.NamespaceName), "11906");
- }
-
- else if (value == Novacode.Orientation.Portrait)
- {
- pgSz.SetAttributeValue(XName.Get("w", DocX.w.NamespaceName), "11906");
- pgSz.SetAttributeValue(XName.Get("h", DocX.w.NamespaceName), "16838");
- }
- }
- }
- }
-}
diff --git a/DocX/Paragraph.cs b/DocX/Paragraph.cs
deleted file mode 100644
index 64f6e8de..00000000
--- a/DocX/Paragraph.cs
+++ /dev/null
@@ -1,4732 +0,0 @@
-using System;
-using System.Linq;
-using System.Drawing;
-using System.Xml.Linq;
-using System.Collections;
-using System.IO.Packaging;
-using System.Globalization;
-using System.Security.Principal;
-using System.Collections.Generic;
-using System.IO;
-using System.Text.RegularExpressions;
-
-
-namespace Novacode
-{
- ///
- /// Represents a document paragraph.
- ///
- public class Paragraph : InsertBeforeOrAfter
- {
-
- // The Append family of functions use this List to apply style.
- internal List runs;
-
- // This paragraphs text alignment
- private Alignment alignment;
-
- public ContainerType ParentContainer;
-
- private XElement ParagraphNumberPropertiesBacker { get; set; }
- ///
- /// Fetch the paragraph number properties for a list element.
- ///
- public XElement ParagraphNumberProperties
- {
- get
- {
- return ParagraphNumberPropertiesBacker ?? (ParagraphNumberPropertiesBacker = GetParagraphNumberProperties());
- }
- }
-
- private XElement GetParagraphNumberProperties()
- {
- var numPrNode = Xml.Descendants().FirstOrDefault(el => el.Name.LocalName == "numPr");
- if (numPrNode != null)
- {
- var numIdNode = numPrNode.Descendants().First(numId => numId.Name.LocalName == "numId");
- var numIdAttribute = numIdNode.Attribute(DocX.w + "val");
- if (numIdAttribute != null && numIdAttribute.Value.Equals("0"))
- {
- return null;
- }
- }
-
- return numPrNode;
- }
-
- private bool? IsListItemBacker { get; set; }
- ///
- /// Determine if this paragraph is a list element.
- ///
- public bool IsListItem
- {
- get
- {
- IsListItemBacker = IsListItemBacker ?? (ParagraphNumberProperties != null);
- return (bool)IsListItemBacker;
- }
- }
-
- private int? IndentLevelBacker { get; set; }
- ///
- /// If this element is a list item, get the indentation level of the list item.
- ///
- public int? IndentLevel
- {
- get
- {
- if (!IsListItem)
- {
- return null;
- }
- return IndentLevelBacker ?? (IndentLevelBacker = int.Parse(ParagraphNumberProperties.Descendants().First(el => el.Name.LocalName == "ilvl").GetAttribute(DocX.w + "val")));
- }
- }
-
- ///
- /// Determine if the list element is a numbered list of bulleted list element
- ///
- public ListItemType ListItemType;
-
- internal int startIndex, endIndex;
-
- ///
- /// Returns a list of all Pictures in a Paragraph.
- ///
- ///
- /// Returns a list of all Pictures in a Paragraph.
- ///
- /// pictures = p.Pictures;
- ///
- /// // Save this document.
- /// document.Save();
- /// }
- /// ]]>
- ///
- ///
- public List Pictures
- {
- get
- {
- if (Xml == null)
- {
- return new List();
- }
-
- List pictures =
- (
- from p in Xml.Descendants()
- where (p.Name.LocalName == "drawing")
- let id =
- (
- from e in p.Descendants()
- where e.Name.LocalName.Equals("blip")
- select e.Attribute(XName.Get("embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")).Value
- ).SingleOrDefault()
- where id != null
- let img = new Image(Document, mainPart.GetRelationship(id))
- select new Picture(Document, p, img)
- ).ToList();
-
- List shapes =
- (
- from p in Xml.Descendants()
- where (p.Name.LocalName == "pict")
- let id =
- (
- from e in p.Descendants()
- where e.Name.LocalName.Equals("imagedata")
- select e.Attribute(XName.Get("id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")).Value
- ).SingleOrDefault()
- where id != null
- let img = new Image(Document, mainPart.GetRelationship(id))
- select new Picture(Document, p, img)
- ).ToList();
-
- foreach (Picture p in shapes)
- pictures.Add(p);
-
-
- return pictures;
- }
- }
-
- ///
- /// Returns a list of Hyperlinks in this Paragraph.
- ///
- ///
- ///
- /// // Create a document.
- /// using (DocX document = DocX.Load(@"Test.docx"))
- /// {
- /// // Get the first Paragraph in this document.
- /// Paragraph p = document.Paragraphs[0];
- ///
- /// // Get all of the hyperlinks in this Paragraph.
- /// ]]> hyperlinks = paragraph.Hyperlinks;
- ///
- /// // Change the first hyperlinks text and Uri
- /// Hyperlink h0 = hyperlinks[0];
- /// h0.Text = "DocX";
- /// h0.Uri = new Uri("http://docx.codeplex.com");
- ///
- /// // Save this document.
- /// document.Save();
- /// }
- ///
- ///
- public List Hyperlinks
- {
- get
- {
- List hyperlinks = new List();
-
- List hyperlink_elements =
- (
- from h in Xml.Descendants()
- where (h.Name.LocalName == "hyperlink" || h.Name.LocalName == "instrText")
- select h
- ).ToList();
-
- foreach (XElement he in hyperlink_elements)
- {
- if (he.Name.LocalName == "hyperlink")
- {
- try
- {
- Hyperlink h = new Hyperlink(Document, mainPart, he);
- h.mainPart = mainPart;
- hyperlinks.Add(h);
- }
-
- catch (Exception) { }
- }
-
- else
- {
- // Find the parent run, no matter how deeply nested we are.
- XElement e = he;
- while (e.Name.LocalName != "r")
- e = e.Parent;
-
- // Take every element until we reach w:fldCharType="end"
- List hyperlink_runs = new List();
- foreach (XElement r in e.ElementsAfterSelf(XName.Get("r", DocX.w.NamespaceName)))
- {
- // Add this run to the list.
- hyperlink_runs.Add(r);
-
- XElement fldChar = r.Descendants(XName.Get("fldChar", DocX.w.NamespaceName)).SingleOrDefault();
- if (fldChar != null)
- {
- XAttribute fldCharType = fldChar.Attribute(XName.Get("fldCharType", DocX.w.NamespaceName));
- if (fldCharType != null && fldCharType.Value.Equals("end", StringComparison.CurrentCultureIgnoreCase))
- {
- try
- {
- Hyperlink h = new Hyperlink(Document, he, hyperlink_runs);
- h.mainPart = mainPart;
- hyperlinks.Add(h);
- }
-
- catch (Exception) { }
-
- break;
- }
- }
- }
- }
- }
-
- return hyperlinks;
- }
- }
-
- ///
- /// The style name of the paragraph.
- ///
- public string StyleName
- {
- get
- {
- var element = this.GetOrCreate_pPr();
- var styleElement = element.Element(XName.Get("pStyle", DocX.w.NamespaceName));
- var attr = styleElement?.Attribute(XName.Get("val", DocX.w.NamespaceName));
- if (!string.IsNullOrEmpty(attr?.Value))
- {
- return attr.Value;
- }
- return "Normal";
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = "Normal";
- }
- var element = this.GetOrCreate_pPr();
- var styleElement = element.Element(XName.Get("pStyle", DocX.w.NamespaceName));
- if (styleElement == null)
- {
- element.Add(new XElement(XName.Get("pStyle", DocX.w.NamespaceName)));
- styleElement = element.Element(XName.Get("pStyle", DocX.w.NamespaceName));
- }
- styleElement.SetAttributeValue(XName.Get("val", DocX.w.NamespaceName), value);
- }
- }
-
- // A collection of field type DocProperty.
- private List docProperties;
-
- internal List styles = new List();
-
- ///
- /// Returns a list of field type DocProperty in this document.
- ///
- public List DocumentProperties
- {
- get { return docProperties; }
- }
-
- internal Paragraph(DocX document, XElement xml, int startIndex, ContainerType parent = ContainerType.None)
- : base(document, xml)
- {
- ParentContainer = parent;
- this.startIndex = startIndex;
- this.endIndex = startIndex + GetElementTextLength(xml);
-
- RebuildDocProperties();
-
- // As per Unused code affecting performance (Wiki Link: [discussion:454191]) and coffeycathal suggestion no longer requeried
- //#region It's possible that a Paragraph may have pStyle references
- //// Check if this Paragraph references any pStyle elements.
- //var stylesElements = xml.Descendants(XName.Get("pStyle", DocX.w.NamespaceName));
-
- //// If one or more pStyles are referenced.
- //if (stylesElements.Count() > 0)
- //{
- // Uri style_package_uri = new Uri("/word/styles.xml", UriKind.Relative);
- // PackagePart styles_document = document.package.GetPart(style_package_uri);
-
- // using (TextReader tr = new StreamReader(styles_document.GetStream()))
- // {
- // XDocument style_document = XDocument.Load(tr);
- // XElement styles_element = style_document.Element(XName.Get("styles", DocX.w.NamespaceName));
-
- // var styles_element_ids = stylesElements.Select(e => e.Attribute(XName.Get("val", DocX.w.NamespaceName)).Value);
-
- // //foreach(string id in styles_element_ids)
- // //{
- // // var style =
- // // (
- // // from d in styles_element.Descendants()
- // // let styleId = d.Attribute(XName.Get("styleId", DocX.w.NamespaceName))
- // // let type = d.Attribute(XName.Get("type", DocX.w.NamespaceName))
- // // where type != null && type.Value == "paragraph" && styleId != null && styleId.Value == id
- // // select d
- // // ).First();
-
- // // styles.Add(style);
- // //}
- // }
- //}
- //#endregion
-
- this.runs = Xml.Elements(XName.Get("r", DocX.w.NamespaceName)).ToList();
- }
-
- ///
- /// Insert a new Table before this Paragraph, this Table can be from this document or another document.
- ///
- /// The Table t to be inserted.
- /// A new Table inserted before this Paragraph.
- ///
- /// Insert a new Table before this Paragraph.
- ///
- /// // Place holder for a Table.
- /// Table t;
- ///
- /// // Load document a.
- /// using (DocX documentA = DocX.Load(@"a.docx"))
- /// {
- /// // Get the first Table from this document.
- /// t = documentA.Tables[0];
- /// }
- ///
- /// // Load document b.
- /// using (DocX documentB = DocX.Load(@"b.docx"))
- /// {
- /// // Get the first Paragraph in document b.
- /// Paragraph p2 = documentB.Paragraphs[0];
- ///
- /// // Insert the Table from document a before this Paragraph.
- /// Table newTable = p2.InsertTableBeforeSelf(t);
- ///
- /// // Save all changes made to document b.
- /// documentB.Save();
- /// }// Release this document from memory.
- ///
- ///
- public override Table InsertTableBeforeSelf(Table t)
- {
- t = base.InsertTableBeforeSelf(t);
- t.mainPart = mainPart;
- return t;
- }
-
- private Direction direction;
- ///
- /// Gets or Sets the Direction of content in this Paragraph.
- ///
- /// Create a Paragraph with content that flows right to left. Default is left to right.
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create("Test.docx"))
- /// {
- /// // Create a new Paragraph with the text "Hello World".
- /// Paragraph p = document.InsertParagraph("Hello World.");
- ///
- /// // Make this Paragraph flow right to left. Default is left to right.
- /// p.Direction = Direction.RightToLeft;
- ///
- /// // Save all changes made to this document.
- /// document.Save();
- /// }
- ///
- ///
- ///
- public Direction Direction
- {
- get
- {
- XElement pPr = GetOrCreate_pPr();
- XElement bidi = pPr.Element(XName.Get("bidi", DocX.w.NamespaceName));
-
- return bidi == null ? Direction.LeftToRight : Direction.RightToLeft;
- }
-
- set
- {
- direction = value;
-
- XElement pPr = GetOrCreate_pPr();
- XElement bidi = pPr.Element(XName.Get("bidi", DocX.w.NamespaceName));
-
- if (direction == Direction.RightToLeft)
- {
- if (bidi == null)
- pPr.Add(new XElement(XName.Get("bidi", DocX.w.NamespaceName)));
- }
- else
- {
- bidi?.Remove();
- }
- }
- }
-
- public bool IsKeepWithNext
- {
-
- get
- {
- var pPr = GetOrCreate_pPr();
- var keepWithNextE = pPr.Element(XName.Get("keepNext", DocX.w.NamespaceName));
- if (keepWithNextE == null)
- {
- return false;
- }
- return true;
- }
- }
- ///
- /// This paragraph will be kept on the same page as the next paragraph
- ///
- ///
- /// Create a Paragraph that will stay on the same page as the paragraph that comes next
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create("Test.docx"))
- ///
- /// {
- /// // Create a new Paragraph with the text "Hello World".
- /// Paragraph p = document.InsertParagraph("Hello World.");
- /// p.KeepWithNext();
- /// document.InsertParagraph("Previous paragraph will appear on the same page as this paragraph");
- /// // Save all changes made to this document.
- /// document.Save();
- /// }
- ///
- ///
- ///
- ///
-
- public Paragraph KeepWithNext(bool keepWithNext = true)
- {
- var pPr = GetOrCreate_pPr();
- var keepWithNextE = pPr.Element(XName.Get("keepNext", DocX.w.NamespaceName));
- if (keepWithNextE == null && keepWithNext)
- {
- pPr.Add(new XElement(XName.Get("keepNext", DocX.w.NamespaceName)));
- }
- if (!keepWithNext && keepWithNextE != null)
- {
- keepWithNextE.Remove();
- }
- return this;
-
- }
- ///
- /// Keep all lines in this paragraph together on a page
- ///
- ///
- /// Create a Paragraph whose lines will stay together on a single page
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create("Test.docx"))
- /// {
- /// // Create a new Paragraph with the text "Hello World".
- /// Paragraph p = document.InsertParagraph("All lines of this paragraph will appear on the same page...\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6...");
- /// p.KeepLinesTogether();
- /// // Save all changes made to this document.
- /// document.Save();
- /// }
- ///
- ///
- ///
- ///
- public Paragraph KeepLinesTogether(bool keepTogether = true)
- {
- var pPr = GetOrCreate_pPr();
- var keepLinesE = pPr.Element(XName.Get("keepLines", DocX.w.NamespaceName));
- if (keepLinesE == null && keepTogether)
- {
- pPr.Add(new XElement(XName.Get("keepLines", DocX.w.NamespaceName)));
- }
- if (!keepTogether)
- {
- keepLinesE?.Remove();
- }
- return this;
- }
- ///
- /// If the pPr element doesent exist it is created, either way it is returned by this function.
- ///
- /// The pPr element for this Paragraph.
- internal XElement GetOrCreate_pPr()
- {
- // Get the element.
- XElement pPr = Xml.Element(XName.Get("pPr", DocX.w.NamespaceName));
-
- // If it dosen't exist, create it.
- if (pPr == null)
- {
- Xml.AddFirst(new XElement(XName.Get("pPr", DocX.w.NamespaceName)));
- pPr = Xml.Element(XName.Get("pPr", DocX.w.NamespaceName));
- }
-
- // Return the pPr element for this Paragraph.
- return pPr;
- }
-
- ///
- /// If the ind element doesent exist it is created, either way it is returned by this function.
- ///
- /// The ind element for this Paragraphs pPr.
- internal XElement GetOrCreate_pPr_ind()
- {
- // Get the element.
- XElement pPr = GetOrCreate_pPr();
- XElement ind = pPr.Element(XName.Get("ind", DocX.w.NamespaceName));
-
- // If it dosen't exist, create it.
- if (ind == null)
- {
- pPr.Add(new XElement(XName.Get("ind", DocX.w.NamespaceName)));
- ind = pPr.Element(XName.Get("ind", DocX.w.NamespaceName));
- }
-
- // Return the pPr element for this Paragraph.
- return ind;
- }
-
- private float indentationFirstLine;
- ///
- /// Get or set the indentation of the first line of this Paragraph.
- ///
- ///
- /// Indent only the first line of a Paragraph.
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create("Test.docx"))
- /// {
- /// // Create a new Paragraph.
- /// Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
- ///
- /// // Indent only the first line of the Paragraph.
- /// p.IndentationFirstLine = 2.0f;
- ///
- /// // Save all changes made to this document.
- /// document.Save();
- /// }
- ///
- ///
- public float IndentationFirstLine
- {
- get
- {
- GetOrCreate_pPr();
- XElement ind = GetOrCreate_pPr_ind();
- XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName));
-
- if (firstLine != null)
- return float.Parse(firstLine.Value);
-
- return 0.0f;
- }
-
- set
- {
- if (IndentationFirstLine != value)
- {
- indentationFirstLine = value;
-
- XElement pPr = GetOrCreate_pPr();
- XElement ind = GetOrCreate_pPr_ind();
-
- // Paragraph can either be firstLine or hanging (Remove hanging).
- XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName));
- hanging?.Remove();
-
- string indentation = ((indentationFirstLine / 0.1) * 57).ToString();
- XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName));
- if (firstLine != null)
- firstLine.Value = indentation;
- else
- ind.Add(new XAttribute(XName.Get("firstLine", DocX.w.NamespaceName), indentation));
- }
- }
- }
-
- private float indentationHanging;
- ///
- /// Get or set the indentation of all but the first line of this Paragraph.
- ///
- ///
- /// Indent all but the first line of a Paragraph.
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create("Test.docx"))
- /// {
- /// // Create a new Paragraph.
- /// Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
- ///
- /// // Indent all but the first line of the Paragraph.
- /// p.IndentationHanging = 1.0f;
- ///
- /// // Save all changes made to this document.
- /// document.Save();
- /// }
- ///
- ///
- public float IndentationHanging
- {
- get
- {
- GetOrCreate_pPr();
- XElement ind = GetOrCreate_pPr_ind();
- XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName));
-
- if (hanging != null)
- return float.Parse(hanging.Value) / (57 * 10);
-
- return 0.0f;
- }
-
- set
- {
- if (IndentationHanging != value)
- {
- indentationHanging = value;
-
- XElement pPr = GetOrCreate_pPr();
- XElement ind = GetOrCreate_pPr_ind();
-
- // Paragraph can either be firstLine or hanging (Remove firstLine).
- XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName));
- if (firstLine != null)
- firstLine.Remove();
-
- string indentation = ((indentationHanging / 0.1) * 57).ToString();
- XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName));
- if (hanging != null)
- hanging.Value = indentation;
- else
- ind.Add(new XAttribute(XName.Get("hanging", DocX.w.NamespaceName), indentation));
- }
- }
- }
-
- private float indentationBefore;
- ///
- /// Set the before indentation in cm for this Paragraph.
- ///
- ///
- /// // Indent an entire Paragraph from the left.
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create("Test.docx"))
- /// {
- /// // Create a new Paragraph.
- /// Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
- ///
- /// // Indent this entire Paragraph from the left.
- /// p.IndentationBefore = 2.0f;
- ///
- /// // Save all changes made to this document.
- /// document.Save();
- ///}
- ///
- ///
- public float IndentationBefore
- {
- get
- {
- XElement pPr = GetOrCreate_pPr();
- XElement ind = GetOrCreate_pPr_ind();
-
- XAttribute left = ind.Attribute(XName.Get("left", DocX.w.NamespaceName));
- if (left != null)
- return float.Parse(left.Value) / (57 * 10);
-
- return 0.0f;
- }
-
- set
- {
- if (IndentationBefore != value)
- {
- indentationBefore = value;
-
- XElement pPr = GetOrCreate_pPr();
- XElement ind = GetOrCreate_pPr_ind();
-
- string indentation = ((indentationBefore / 0.1) * 57).ToString(CultureInfo.GetCultureInfo("en-GB"));
-
- XAttribute left = ind.Attribute(XName.Get("left", DocX.w.NamespaceName));
- if (left != null)
- left.Value = indentation;
- else
- ind.Add(new XAttribute(XName.Get("left", DocX.w.NamespaceName), indentation));
- }
- }
- }
-
- private float indentationAfter;
- ///
- /// Set the after indentation in cm for this Paragraph.
- ///
- ///
- /// // Indent an entire Paragraph from the right.
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create("Test.docx"))
- /// {
- /// // Create a new Paragraph.
- /// Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
- ///
- /// // Make the content of this Paragraph flow right to left.
- /// p.Direction = Direction.RightToLeft;
- ///
- /// // Indent this entire Paragraph from the right.
- /// p.IndentationAfter = 2.0f;
- ///
- /// // Save all changes made to this document.
- /// document.Save();
- /// }
- ///
- ///
- public float IndentationAfter
- {
- get
- {
- GetOrCreate_pPr();
- XElement ind = GetOrCreate_pPr_ind();
-
- XAttribute right = ind.Attribute(XName.Get("right", DocX.w.NamespaceName));
- if (right != null)
- return float.Parse(right.Value);
-
- return 0.0f;
- }
-
- set
- {
- if (IndentationAfter != value)
- {
- indentationAfter = value;
-
- XElement pPr = GetOrCreate_pPr();
- XElement ind = GetOrCreate_pPr_ind();
-
- string indentation = ((indentationAfter / 0.1) * 57).ToString();
-
- XAttribute right = ind.Attribute(XName.Get("right", DocX.w.NamespaceName));
- if (right != null)
- right.Value = indentation;
- else
- ind.Add(new XAttribute(XName.Get("right", DocX.w.NamespaceName), indentation));
- }
- }
- }
-
- ///
- /// Insert a new Table into this document before this Paragraph.
- ///
- /// The number of rows this Table should have.
- /// The number of columns this Table should have.
- /// A new Table inserted before this Paragraph.
- ///
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// //Insert a Paragraph into this document.
- /// Paragraph p = document.InsertParagraph("Hello World", false);
- ///
- /// // Insert a new Table before this Paragraph.
- /// Table newTable = p.InsertTableBeforeSelf(2, 2);
- /// newTable.Design = TableDesign.LightShadingAccent2;
- /// newTable.Alignment = Alignment.center;
- ///
- /// // Save all changes made to this document.
- /// document.Save();
- /// }// Release this document from memory.
- ///
- ///
- public override Table InsertTableBeforeSelf(int rowCount, int columnCount)
- {
- return base.InsertTableBeforeSelf(rowCount, columnCount);
- }
-
- ///
- /// Insert a new Table after this Paragraph.
- ///
- /// The Table t to be inserted.
- /// A new Table inserted after this Paragraph.
- ///
- /// Insert a new Table after this Paragraph.
- ///
- /// // Place holder for a Table.
- /// Table t;
- ///
- /// // Load document a.
- /// using (DocX documentA = DocX.Load(@"a.docx"))
- /// {
- /// // Get the first Table from this document.
- /// t = documentA.Tables[0];
- /// }
- ///
- /// // Load document b.
- /// using (DocX documentB = DocX.Load(@"b.docx"))
- /// {
- /// // Get the first Paragraph in document b.
- /// Paragraph p2 = documentB.Paragraphs[0];
- ///
- /// // Insert the Table from document a after this Paragraph.
- /// Table newTable = p2.InsertTableAfterSelf(t);
- ///
- /// // Save all changes made to document b.
- /// documentB.Save();
- /// }// Release this document from memory.
- ///
- ///
- public override Table InsertTableAfterSelf(Table t)
- {
- t = base.InsertTableAfterSelf(t);
- t.mainPart = mainPart;
- return t;
- }
-
- ///
- /// Insert a new Table into this document after this Paragraph.
- ///
- /// The number of rows this Table should have.
- /// The number of columns this Table should have.
- /// A new Table inserted after this Paragraph.
- ///
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// //Insert a Paragraph into this document.
- /// Paragraph p = document.InsertParagraph("Hello World", false);
- ///
- /// // Insert a new Table after this Paragraph.
- /// Table newTable = p.InsertTableAfterSelf(2, 2);
- /// newTable.Design = TableDesign.LightShadingAccent2;
- /// newTable.Alignment = Alignment.center;
- ///
- /// // Save all changes made to this document.
- /// document.Save();
- /// }// Release this document from memory.
- ///
- ///
- public override Table InsertTableAfterSelf(int rowCount, int columnCount)
- {
- return base.InsertTableAfterSelf(rowCount, columnCount);
- }
-
- ///
- /// Insert a Paragraph before this Paragraph, this Paragraph may have come from the same or another document.
- ///
- /// The Paragraph to insert.
- /// The Paragraph now associated with this document.
- ///
- /// Take a Paragraph from document a, and insert it into document b before this Paragraph.
- ///
- /// // Place holder for a Paragraph.
- /// Paragraph p;
- ///
- /// // Load document a.
- /// using (DocX documentA = DocX.Load(@"a.docx"))
- /// {
- /// // Get the first paragraph from this document.
- /// p = documentA.Paragraphs[0];
- /// }
- ///
- /// // Load document b.
- /// using (DocX documentB = DocX.Load(@"b.docx"))
- /// {
- /// // Get the first Paragraph in document b.
- /// Paragraph p2 = documentB.Paragraphs[0];
- ///
- /// // Insert the Paragraph from document a before this Paragraph.
- /// Paragraph newParagraph = p2.InsertParagraphBeforeSelf(p);
- ///
- /// // Save all changes made to document b.
- /// documentB.Save();
- /// }// Release this document from memory.
- ///
- ///
- public override Paragraph InsertParagraphBeforeSelf(Paragraph p)
- {
- Paragraph p2 = base.InsertParagraphBeforeSelf(p);
- p2.PackagePart = mainPart;
- return p2;
- }
-
- ///
- /// Insert a new Paragraph before this Paragraph.
- ///
- /// The initial text for this new Paragraph.
- /// A new Paragraph inserted before this Paragraph.
- ///
- /// Insert a new paragraph before the first Paragraph in this document.
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a Paragraph into this document.
- /// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
- ///
- /// p.InsertParagraphBeforeSelf("I was inserted before the next Paragraph.");
- ///
- /// // Save all changes made to this new document.
- /// document.Save();
- /// }// Release this new document form memory.
- ///
- ///
- public override Paragraph InsertParagraphBeforeSelf(string text)
- {
- Paragraph p = base.InsertParagraphBeforeSelf(text);
- p.PackagePart = mainPart;
- return p;
- }
-
- ///
- /// Insert a new Paragraph before this Paragraph.
- ///
- /// The initial text for this new Paragraph.
- /// Should this insertion be tracked as a change?
- /// A new Paragraph inserted before this Paragraph.
- ///
- /// Insert a new paragraph before the first Paragraph in this document.
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a Paragraph into this document.
- /// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
- ///
- /// p.InsertParagraphBeforeSelf("I was inserted before the next Paragraph.", false);
- ///
- /// // Save all changes made to this new document.
- /// document.Save();
- /// }// Release this new document form memory.
- ///
- ///
- public override Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges)
- {
- Paragraph p = base.InsertParagraphBeforeSelf(text, trackChanges);
- p.PackagePart = mainPart;
- return p;
- }
-
- ///
- /// Insert a new Paragraph before this Paragraph.
- ///
- /// The initial text for this new Paragraph.
- /// Should this insertion be tracked as a change?
- /// The formatting to apply to this insertion.
- /// A new Paragraph inserted before this Paragraph.
- ///
- /// Insert a new paragraph before the first Paragraph in this document.
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a Paragraph into this document.
- /// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
- ///
- /// Formatting boldFormatting = new Formatting();
- /// boldFormatting.Bold = true;
- ///
- /// p.InsertParagraphBeforeSelf("I was inserted before the next Paragraph.", false, boldFormatting);
- ///
- /// // Save all changes made to this new document.
- /// document.Save();
- /// }// Release this new document form memory.
- ///
- ///
- public override Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges, Formatting formatting)
- {
- Paragraph p = base.InsertParagraphBeforeSelf(text, trackChanges, formatting);
- p.PackagePart = mainPart;
- return p;
- }
-
- ///
- /// Insert a page break before a Paragraph.
- ///
- ///
- /// Insert 2 Paragraphs into a document with a page break between them.
- ///
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a new Paragraph.
- /// Paragraph p1 = document.InsertParagraph("Paragraph 1", false);
- ///
- /// // Insert a new Paragraph.
- /// Paragraph p2 = document.InsertParagraph("Paragraph 2", false);
- ///
- /// // Insert a page break before Paragraph two.
- /// p2.InsertPageBreakBeforeSelf();
- ///
- /// // Save this document.
- /// document.Save();
- /// }// Release this document from memory.
- ///
- ///
- public override void InsertPageBreakBeforeSelf()
- {
- base.InsertPageBreakBeforeSelf();
- }
-
- ///
- /// Insert a page break after a Paragraph.
- ///
- ///
- /// Insert 2 Paragraphs into a document with a page break between them.
- ///
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a new Paragraph.
- /// Paragraph p1 = document.InsertParagraph("Paragraph 1", false);
- ///
- /// // Insert a page break after this Paragraph.
- /// p1.InsertPageBreakAfterSelf();
- ///
- /// // Insert a new Paragraph.
- /// Paragraph p2 = document.InsertParagraph("Paragraph 2", false);
- ///
- /// // Save this document.
- /// document.Save();
- /// }// Release this document from memory.
- ///
- ///
- public override void InsertPageBreakAfterSelf()
- {
- base.InsertPageBreakAfterSelf();
- }
-
- [Obsolete("Instead use: InsertHyperlink(Hyperlink h, int index)")]
- public Paragraph InsertHyperlink(int index, Hyperlink h) { return InsertHyperlink(h, index); }
-
- ///
- /// This function inserts a hyperlink into a Paragraph at a specified character index.
- ///
- /// The index to insert at.
- /// The hyperlink to insert.
- /// The Paragraph with the Hyperlink inserted at the specified index.
- ///
- public Paragraph InsertHyperlink(Hyperlink h, int index = 0)
- {
- // Convert the path of this mainPart to its equilivant rels file path.
- string path = mainPart.Uri.OriginalString.Replace("/word/", "");
- Uri rels_path = new Uri(String.Format("/word/_rels/{0}.rels", path), UriKind.Relative);
-
- // Check to see if the rels file exists and create it if not.
- if (!Document.package.PartExists(rels_path))
- HelperFunctions.CreateRelsPackagePart(Document, rels_path);
-
- // Check to see if a rel for this Picture exists, create it if not.
- var Id = GetOrGenerateRel(h);
-
- XElement h_xml;
- if (index == 0)
- {
- // Add this hyperlink as the first element.
- Xml.AddFirst(h.Xml);
-
- // Extract the picture back out of the DOM.
- h_xml = (XElement)Xml.FirstNode;
- }
-
- else
- {
- // Get the first run effected by this Insert
- Run run = GetFirstRunEffectedByEdit(index);
-
- if (run == null)
- {
- // Add this hyperlink as the last element.
- Xml.Add(h.Xml);
-
- // Extract the picture back out of the DOM.
- h_xml = (XElement)Xml.LastNode;
- }
-
- else
- {
- // Split this run at the point you want to insert
- XElement[] splitRun = Run.SplitRun(run, index);
-
- // Replace the origional run.
- run.Xml.ReplaceWith
- (
- splitRun[0],
- h.Xml,
- splitRun[1]
- );
-
- // Get the first run effected by this Insert
- run = GetFirstRunEffectedByEdit(index);
-
- // The picture has to be the next element, extract it back out of the DOM.
- h_xml = (XElement)run.Xml.NextNode;
- }
-
- }
- h_xml.SetAttributeValue( DocX.r + "id", Id );
-
- this.runs = Xml.Elements().Last().Elements( XName.Get( "r", DocX.w.NamespaceName ) ).ToList();
-
- return this;
- }
-
- ///
- /// Remove the Hyperlink at the provided index. The first hyperlink is at index 0.
- /// Using a negative index or an index greater than the index of the last hyperlink will cause an ArgumentOutOfRangeException() to be thrown.
- ///
- /// The index of the hyperlink to be removed.
- ///
- ///
- /// // Crete a new document.
- /// using (DocX document = DocX.Create("Test.docx"))
- /// {
- /// // Add a Hyperlink into this document.
- /// Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
- ///
- /// // Insert a new Paragraph into the document.
- /// Paragraph p1 = document.InsertParagraph("AC");
- ///
- /// // Insert the hyperlink into this Paragraph.
- /// p1.InsertHyperlink(1, h);
- /// Assert.IsTrue(p1.Text == "AlinkC"); // Make sure the hyperlink was inserted correctly;
- ///
- /// // Remove the hyperlink
- /// p1.RemoveHyperlink(0);
- /// Assert.IsTrue(p1.Text == "AC"); // Make sure the hyperlink was removed correctly;
- /// }
- ///
- ///
- public void RemoveHyperlink(int index)
- {
- // Dosen't make sense to remove a Hyperlink at a negative index.
- if (index < 0)
- throw new ArgumentOutOfRangeException();
-
- // Need somewhere to store the count.
- int count = 0;
- bool found = false;
- RemoveHyperlinkRecursive(Xml, index, ref count, ref found);
-
- // If !found then the user tried to remove a hyperlink at an index greater than the last.
- if (!found)
- throw new ArgumentOutOfRangeException();
- }
-
- internal void RemoveHyperlinkRecursive(XElement xml, int index, ref int count, ref bool found)
- {
- if (xml.Name.LocalName.Equals("hyperlink", StringComparison.CurrentCultureIgnoreCase))
- {
- // This is the hyperlink to be removed.
- if (count == index)
- {
- found = true;
- xml.Remove();
- }
-
- else
- count++;
- }
-
- if (xml.HasElements)
- foreach (XElement e in xml.Elements())
- if (!found)
- RemoveHyperlinkRecursive(e, index, ref count, ref found);
- }
-
- ///
- /// Insert a Paragraph after this Paragraph, this Paragraph may have come from the same or another document.
- ///
- /// The Paragraph to insert.
- /// The Paragraph now associated with this document.
- ///
- /// Take a Paragraph from document a, and insert it into document b after this Paragraph.
- ///
- /// // Place holder for a Paragraph.
- /// Paragraph p;
- ///
- /// // Load document a.
- /// using (DocX documentA = DocX.Load(@"a.docx"))
- /// {
- /// // Get the first paragraph from this document.
- /// p = documentA.Paragraphs[0];
- /// }
- ///
- /// // Load document b.
- /// using (DocX documentB = DocX.Load(@"b.docx"))
- /// {
- /// // Get the first Paragraph in document b.
- /// Paragraph p2 = documentB.Paragraphs[0];
- ///
- /// // Insert the Paragraph from document a after this Paragraph.
- /// Paragraph newParagraph = p2.InsertParagraphAfterSelf(p);
- ///
- /// // Save all changes made to document b.
- /// documentB.Save();
- /// }// Release this document from memory.
- ///
- ///
- public override Paragraph InsertParagraphAfterSelf(Paragraph p)
- {
- Paragraph p2 = base.InsertParagraphAfterSelf(p);
- p2.PackagePart = mainPart;
- return p2;
- }
-
- ///
- /// Insert a new Paragraph after this Paragraph.
- ///
- /// The initial text for this new Paragraph.
- /// Should this insertion be tracked as a change?
- /// The formatting to apply to this insertion.
- /// A new Paragraph inserted after this Paragraph.
- ///
- /// Insert a new paragraph after the first Paragraph in this document.
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a Paragraph into this document.
- /// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
- ///
- /// Formatting boldFormatting = new Formatting();
- /// boldFormatting.Bold = true;
- ///
- /// p.InsertParagraphAfterSelf("I was inserted after the previous Paragraph.", false, boldFormatting);
- ///
- /// // Save all changes made to this new document.
- /// document.Save();
- /// }// Release this new document form memory.
- ///
- ///
- public override Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting)
- {
- Paragraph p = base.InsertParagraphAfterSelf(text, trackChanges, formatting);
- p.PackagePart = mainPart;
- return p;
- }
-
- ///
- /// Insert a new Paragraph after this Paragraph.
- ///
- /// The initial text for this new Paragraph.
- /// Should this insertion be tracked as a change?
- /// A new Paragraph inserted after this Paragraph.
- ///
- /// Insert a new paragraph after the first Paragraph in this document.
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a Paragraph into this document.
- /// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
- ///
- /// p.InsertParagraphAfterSelf("I was inserted after the previous Paragraph.", false);
- ///
- /// // Save all changes made to this new document.
- /// document.Save();
- /// }// Release this new document form memory.
- ///
- ///
- public override Paragraph InsertParagraphAfterSelf(string text, bool trackChanges)
- {
- Paragraph p = base.InsertParagraphAfterSelf(text, trackChanges);
- p.PackagePart = mainPart;
- return p;
- }
-
- ///
- /// Insert a new Paragraph after this Paragraph.
- ///
- /// The initial text for this new Paragraph.
- /// A new Paragraph inserted after this Paragraph.
- ///
- /// Insert a new paragraph after the first Paragraph in this document.
- ///
- /// // Create a new document.
- /// using (DocX document = DocX.Create(@"Test.docx"))
- /// {
- /// // Insert a Paragraph into this document.
- /// Paragraph p = document.InsertParagraph("I am a Paragraph", false);
- ///
- /// p.InsertParagraphAfterSelf("I was inserted after the previous Paragraph.");
- ///
- /// // Save all changes made to this new document.
- /// document.Save();
- /// }// Release this new document form memory.
- ///
- ///
- public override Paragraph InsertParagraphAfterSelf(string text)
- {
- Paragraph p = base.InsertParagraphAfterSelf(text);
- p.PackagePart = mainPart;
- return p;
- }
-
- private void RebuildDocProperties()
- {
- docProperties =
- (
- from xml in Xml.Descendants(XName.Get("fldSimple", DocX.w.NamespaceName))
- select new DocProperty(Document, xml)
- ).ToList();
- }
-
- ///
- /// Gets or set this Paragraphs text alignment.
- ///
- public Alignment Alignment
- {
- get
- {
- XElement pPr = GetOrCreate_pPr();
- XElement jc = pPr.Element(XName.Get("jc", DocX.w.NamespaceName));
-
- if (jc != null)
- {
- XAttribute a = jc.Attribute(XName.Get("val", DocX.w.NamespaceName));
-
- switch (a.Value.ToLower())
- {
- case "left": return Novacode.Alignment.left;
- case "right": return Novacode.Alignment.right;
- case "center": return Novacode.Alignment.center;
- case "both": return Novacode.Alignment.both;
- }
- }
-
- return Novacode.Alignment.left;
- }
-
- set
- {
- alignment = value;
-
- XElement pPr = GetOrCreate_pPr();
- XElement jc = pPr.Element(XName.Get("jc", DocX.w.NamespaceName));
-
- if (alignment != Novacode.Alignment.left)
- {
- if (jc == null)
- pPr.Add(new XElement(XName.Get("jc", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), alignment.ToString())));
- else
- jc.Attribute(XName.Get("val", DocX.w.NamespaceName)).Value = alignment.ToString();
- }
-
- else
- {
- if (jc != null)
- jc.Remove();
- }
- }
- }
-
- ///
- /// Remove this Paragraph from the document.
- ///
- /// Should this remove be tracked as a change?
- ///
- /// Remove a Paragraph from a document and track it as a change.
- ///
- /// // Create a document using a relative filename.
- /// using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
- /// {
- /// // Create and Insert a new Paragraph into this document.
- /// Paragraph p = document.InsertParagraph("Hello", false);
- ///
- /// // Remove the Paragraph and track this as a change.
- /// p.Remove(true);
- ///
- /// // Save all changes made to this document.
- /// document.Save();
- /// }// Release this document from memory.
- ///
- ///
- public void Remove(bool trackChanges)
- {
- if (trackChanges)
- {
- DateTime now = DateTime.Now.ToUniversalTime();
-
- List elements = Xml.Elements().ToList();
- List temp = new List();
- for (int i = 0; i < elements.Count(); i++)
- {
- XElement e = elements[i];
-
- if (e.Name.LocalName != "del")
- {
- temp.Add(e);
- e.Remove();
- }
-
- else
- {
- if (temp.Count() > 0)
- {
- e.AddBeforeSelf(CreateEdit(EditType.del, now, temp.Elements()));
- temp.Clear();
- }
- }
- }
-
- if (temp.Count() > 0)
- Xml.Add(CreateEdit(EditType.del, now, temp));
- }
-
- else
- {
- // If this is the only Paragraph in the Cell then we cannot remove it.
- if (Xml.Parent.Name.LocalName == "tc" && Xml.Parent.Elements(XName.Get("p", DocX.w.NamespaceName)).Count() == 1)
- Xml.Value = string.Empty;
-
- else
- {
- // Remove this paragraph from the document
- Xml.Remove();
- Xml = null;
- }
- }
- }
-
- ///
- /// Gets the text value of this Paragraph.
- ///
- public string Text
- {
- // Returns the underlying XElement's Value property.
- get
- {
- return HelperFunctions.GetText(Xml);
- }
- }
-
- ///
- /// Gets the formatted text value of this Paragraph.
- ///
- public List MagicText
- {
- // Returns the underlying XElement's Value property.
- get
- {
- try
- {
- return HelperFunctions.GetFormattedText(Xml);
-
- }
- catch (Exception)
- {
- return null;
- }
-
- }
- }
-
- //public Picture InsertPicture(Picture picture)
- //{
- // Picture newPicture = picture;
- // newPicture.i = new XElement(picture.i);
-
- // xml.Add(newPicture.i);
- // pictures.Add(newPicture);
- // return newPicture;
- //}
-
- //
- // Insert a Picture at the end of this paragraph.
- //
- // A string to describe this Picture.
- // The unique id that identifies the Image this Picture represents.
- // The name of this image.
- // A Picture.
- //
- //
- // // Create a document using a relative filename.
- // using (DocX document = DocX.Create(@"Test.docx"))
- // {
- // // Add a new Paragraph to this document.
- // Paragraph p = document.InsertParagraph("Here is Picture 1", false);
- //
- // // Add an Image to this document.
- // Novacode.Image img = document.AddImage(@"Image.jpg");
- //
- // // Insert pic at the end of Paragraph p.
- // Picture pic = p.InsertPicture(img.Id, "Photo 31415", "A pie I baked.");
- //
- // // Rotate the Picture clockwise by 30 degrees.
- // pic.Rotation = 30;
- //
- // // Resize the Picture.
- // pic.Width = 400;
- // pic.Height = 300;
- //
- // // Set the shape of this Picture to be a cube.
- // pic.SetPictureShape(BasicShapes.cube);
- //
- // // Flip the Picture Horizontally.
- // pic.FlipHorizontal = true;
- //
- // // Save all changes made to this document.
- // document.Save();
- // }// Release this document from memory.
- //
- //
- // Removed to simplify the API.
- //public Picture InsertPicture(string imageID, string name, string description)
- //{
- // Picture p = CreatePicture(Document, imageID, name, description);
- // Xml.Add(p.Xml);
- // return p;
- //}
-
- // Removed because it confusses the API.
- //public Picture InsertPicture(string imageID)
- //{
- // return InsertPicture(imageID, string.Empty, string.Empty);
- //}
-
- //public Picture InsertPicture(int index, Picture picture)
- //{
- // Picture p = picture;
- // p.i = new XElement(picture.i);
-
- // Run run = GetFirstRunEffectedByEdit(index);
-
- // if (run == null)
- // xml.Add(p.i);
- // else
- // {
- // // Split this run at the point you want to insert
- // XElement[] splitRun = Run.SplitRun(run, index);
-
- // // Replace the origional run
- // run.Xml.ReplaceWith
- // (
- // splitRun[0],
- // p.i,
- // splitRun[1]
- // );
- // }
-
- // // Rebuild the run lookup for this paragraph
- // runLookup.Clear();
- // BuildRunLookup(xml);
- // DocX.RenumberIDs(document);
- // return p;
- //}
-
- //
- // Insert a Picture into this Paragraph at a specified index.
- //
- // A string to describe this Picture.
- // The unique id that identifies the Image this Picture represents.
- // The name of this image.
- // The index to insert this Picture at.
- // A Picture.
- //
- //
- // // Create a document using a relative filename.
- // using (DocX document = DocX.Create(@"Test.docx"))
- // {
- // // Add a new Paragraph to this document.
- // Paragraph p = document.InsertParagraph("Here is Picture 1", false);
- //
- // // Add an Image to this document.
- // Novacode.Image img = document.AddImage(@"Image.jpg");
- //
- // // Insert pic at the start of Paragraph p.
- // Picture pic = p.InsertPicture(0, img.Id, "Photo 31415", "A pie I baked.");
- //
- // // Rotate the Picture clockwise by 30 degrees.
- // pic.Rotation = 30;
- //
- // // Resize the Picture.
- // pic.Width = 400;
- // pic.Height = 300;
- //
- // // Set the shape of this Picture to be a cube.
- // pic.SetPictureShape(BasicShapes.cube);
- //
- // // Flip the Picture Horizontally.
- // pic.FlipHorizontal = true;
- //
- // // Save all changes made to this document.
- // document.Save();
- // }// Release this document from memory.
- //
- //
- // Removed to simplify API.
- //public Picture InsertPicture(int index, string imageID, string name, string description)
- //{
- // Picture picture = CreatePicture(Document, imageID, name, description);
-
- // Run run = GetFirstRunEffectedByEdit(index);
-
- // if (run == null)
- // Xml.Add(picture.Xml);
- // else
- // {
- // // Split this run at the point you want to insert
- // XElement[] splitRun = Run.SplitRun(run, index);
-
- // // Replace the origional run
- // run.Xml.ReplaceWith
- // (
- // splitRun[0],
- // picture.Xml,
- // splitRun[1]
- // );
- // }
-
- // HelperFunctions.RenumberIDs(Document);
- // return picture;
- //}
-
- ///
- /// Create a new Picture.
- ///
- ///
- /// A unique id that identifies an Image embedded in this document.
- /// The name of this Picture.
- /// The description of this Picture.
- static internal Picture CreatePicture(DocX document, string id, string name, string descr)
- {
- PackagePart part = document.package.GetPart(document.mainPart.GetRelationship(id).TargetUri);
-
- long newDocPrId = document.GetNextFreeDocPrId();
- int cx, cy;
-
- using (PackagePartStream packagePartStream = new PackagePartStream(part.GetStream()))
- {
- using (System.Drawing.Image img = System.Drawing.Image.FromStream(packagePartStream, useEmbeddedColorManagement: false, validateImageData: false))
- {
- cx = img.Width*9526;
- cy = img.Height*9526;
- }
- }
-
- XElement xml = XElement.Parse
- (string.Format(@"
-
-
-