| <SccProvider>SAK</SccProvider> | <SccProvider>SAK</SccProvider> | ||||
| <StartupObject> | <StartupObject> | ||||
| </StartupObject> | </StartupObject> | ||||
| <SignAssembly>true</SignAssembly> | |||||
| <AssemblyOriginatorKeyFile>StrongNameKey.pfx</AssemblyOriginatorKeyFile> | |||||
| </PropertyGroup> | </PropertyGroup> | ||||
| <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||||
| <DebugSymbols>true</DebugSymbols> | <DebugSymbols>true</DebugSymbols> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <Compile Include="CustomProperty.cs" /> | <Compile Include="CustomProperty.cs" /> | ||||
| <Compile Include="DocProperty.cs" /> | |||||
| <Compile Include="Table.cs" /> | <Compile Include="Table.cs" /> | ||||
| <Compile Include="Enumerations.cs" /> | <Compile Include="Enumerations.cs" /> | ||||
| <Compile Include="Extensions.cs" /> | <Compile Include="Extensions.cs" /> | ||||
| <Compile Include="Text.cs" /> | <Compile Include="Text.cs" /> | ||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <None Include="Help\DocX v1.0.0.5 - Documentation.chm" /> | |||||
| <None Include="Help\Documentation - DocX v 1.0.0.7.chm" /> | |||||
| <None Include="StrongNameKey.pfx" /> | |||||
| <EmbeddedResource Include="Resources\default_styles.xml.gz" /> | |||||
| <EmbeddedResource Include="Resources\styles.xml.gz" /> | |||||
| </ItemGroup> | </ItemGroup> | ||||
| <ItemGroup> | <ItemGroup> | ||||
| <Content Include="License\License.html" /> | <Content Include="License\License.html" /> | ||||
| <Content Include="Resources\styles.xml" /> | |||||
| <EmbeddedResource Include="styles.xml" /> | |||||
| </ItemGroup> | </ItemGroup> | ||||
| <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||||
| <!-- To modify your build process, add your task inside one of the targets below and uncomment it. | <!-- To modify your build process, add your task inside one of the targets below and uncomment it. |
| { | { | ||||
| get | get | ||||
| { | { | ||||
| rPr = new XElement(XName.Get("rPr", DocX.w.NamespaceName)); | |||||
| if(spacing.HasValue) | if(spacing.HasValue) | ||||
| rPr.Add(new XElement(XName.Get("spacing", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), spacing.Value * 20))); | rPr.Add(new XElement(XName.Get("spacing", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), spacing.Value * 20))); | ||||
| /// <summary> | /// <summary> | ||||
| /// The font familt of this formatting. | /// The font familt of this formatting. | ||||
| /// </summary> | /// </summary> | ||||
| public FontFamily FontFamily { get { return FontFamily; } set { fontFamily = value; } } | |||||
| /// <!-- | |||||
| /// Bug found and fixed by krugs525 on August 12 2009. | |||||
| /// Use TFS compare to see exact code change. | |||||
| /// --> | |||||
| public FontFamily FontFamily { get { return fontFamily; } set { fontFamily = value; } } | |||||
| } | } | ||||
| } | } |
| using System.Text.RegularExpressions; | using System.Text.RegularExpressions; | ||||
| using System.Security.Principal; | using System.Security.Principal; | ||||
| using System.Collections; | using System.Collections; | ||||
| using System.IO.Packaging; | |||||
| using System.IO; | |||||
| namespace Novacode | namespace Novacode | ||||
| { | { | ||||
| /// Represents a document paragraph. | /// Represents a document paragraph. | ||||
| /// </summary> | /// </summary> | ||||
| public class Paragraph | public class Paragraph | ||||
| { | |||||
| { | |||||
| // This paragraphs text alignment | // This paragraphs text alignment | ||||
| private Alignment alignment; | private Alignment alignment; | ||||
| /// </summary> | /// </summary> | ||||
| public List<Picture> Pictures { get { return pictures; } } | public List<Picture> Pictures { get { return pictures; } } | ||||
| DocX document; | |||||
| internal Paragraph(DocX document, int startIndex, XElement p) | |||||
| // A collection of field type DocProperty. | |||||
| private List<DocProperty> docProperties; | |||||
| internal List<XElement> styles = new List<XElement>(); | |||||
| /// <summary> | |||||
| /// Returns a list of field type DocProperty in this document. | |||||
| /// </summary> | |||||
| public List<DocProperty> DocumentProperties | |||||
| { | { | ||||
| get { return docProperties; } | |||||
| } | |||||
| internal DocX document; | |||||
| internal Paragraph(DocX document, int startIndex, XElement p) | |||||
| { | |||||
| this.document = document; | this.document = document; | ||||
| this.startIndex = startIndex; | this.startIndex = startIndex; | ||||
| this.endIndex = startIndex + GetElementTextLength(p); | this.endIndex = startIndex + GetElementTextLength(p); | ||||
| // Get all of the images in this document | // Get all of the images in this document | ||||
| pictures = (from i in p.Descendants(XName.Get("drawing", DocX.w.NamespaceName)) | pictures = (from i in p.Descendants(XName.Get("drawing", DocX.w.NamespaceName)) | ||||
| select new Picture(i)).ToList(); | select new Picture(i)).ToList(); | ||||
| RebuildDocProperties(); | |||||
| #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)) | |||||
| where styleId != null && styleId.Value == id | |||||
| select d | |||||
| ).Single(); | |||||
| styles.Add(style); | |||||
| } | |||||
| } | |||||
| } | |||||
| #endregion | |||||
| #region Pictures | |||||
| // Check if this Paragraph contains any Pictures | |||||
| List<string> pictureElementIDs = | |||||
| ( | |||||
| from d in xml.Descendants() | |||||
| let embed = d.Attribute(XName.Get("embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")) | |||||
| where embed != null | |||||
| select embed.Value | |||||
| ).ToList(); | |||||
| #endregion | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Table before this Paragraph, this Table can be from this document or another document. | |||||
| /// </summary> | |||||
| /// <param name="t">The Table t to be inserted.</param> | |||||
| /// <returns>A new Table inserted before this Paragraph.</returns> | |||||
| /// <example> | |||||
| /// Insert a new Table before this Paragraph. | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Table InsertTableBeforeSelf(Table t) | |||||
| { | |||||
| xml.AddBeforeSelf(t.xml); | |||||
| XElement newlyInserted = xml.ElementsBeforeSelf().First(); | |||||
| t.xml = newlyInserted; | |||||
| DocX.RebuildTables(document); | |||||
| DocX.RebuildParagraphs(document); | |||||
| return t; | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Table into this document before this Paragraph. | |||||
| /// </summary> | |||||
| /// <param name="rowCount">The number of rows this Table should have.</param> | |||||
| /// <param name="coloumnCount">The number of coloumns this Table should have.</param> | |||||
| /// <returns>A new Table inserted before this Paragraph.</returns> | |||||
| /// <example> | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public 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); | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Table after this Paragraph. | |||||
| /// </summary> | |||||
| /// <param name="t">The Table t to be inserted.</param> | |||||
| /// <returns>A new Table inserted after this Paragraph.</returns> | |||||
| /// <example> | |||||
| /// Insert a new Table after this Paragraph. | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Table InsertTableAfterSelf(Table t) | |||||
| { | |||||
| xml.AddAfterSelf(t.xml); | |||||
| XElement newlyInserted = xml.ElementsAfterSelf().First(); | |||||
| t.xml = newlyInserted; | |||||
| DocX.RebuildTables(document); | |||||
| DocX.RebuildParagraphs(document); | |||||
| return t; | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Table into this document after this Paragraph. | |||||
| /// </summary> | |||||
| /// <param name="rowCount">The number of rows this Table should have.</param> | |||||
| /// <param name="coloumnCount">The number of coloumns this Table should have.</param> | |||||
| /// <returns>A new Table inserted after this Paragraph.</returns> | |||||
| /// <example> | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public 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); | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a Paragraph before this Paragraph, this Paragraph may have come from the same or another document. | |||||
| /// </summary> | |||||
| /// <param name="p">The Paragraph to insert.</param> | |||||
| /// <returns>The Paragraph now associated with this document.</returns> | |||||
| /// <example> | |||||
| /// Take a Paragraph from document a, and insert it into document b before this Paragraph. | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph InsertParagraphBeforeSelf(Paragraph p) | |||||
| { | |||||
| xml.AddBeforeSelf(p.xml); | |||||
| XElement newlyInserted = xml.ElementsBeforeSelf().First(); | |||||
| p.xml = newlyInserted; | |||||
| DocX.RebuildParagraphs(document); | |||||
| return p; | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Paragraph before this Paragraph. | |||||
| /// </summary> | |||||
| /// <param name="text">The initial text for this new Paragraph.</param> | |||||
| /// <returns>A new Paragraph inserted before this Paragraph.</returns> | |||||
| /// <example> | |||||
| /// Insert a new paragraph before the first Paragraph in this document. | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph InsertParagraphBeforeSelf(string text) | |||||
| { | |||||
| return InsertParagraphBeforeSelf(text, false, new Formatting()); | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Paragraph before this Paragraph. | |||||
| /// </summary> | |||||
| /// <param name="text">The initial text for this new Paragraph.</param> | |||||
| /// <param name="trackChanges">Should this insertion be tracked as a change?</param> | |||||
| /// <returns>A new Paragraph inserted before this Paragraph.</returns> | |||||
| /// <example> | |||||
| /// Insert a new paragraph before the first Paragraph in this document. | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges) | |||||
| { | |||||
| return InsertParagraphBeforeSelf(text, trackChanges, new Formatting()); | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Paragraph before this Paragraph. | |||||
| /// </summary> | |||||
| /// <param name="text">The initial text for this new Paragraph.</param> | |||||
| /// <param name="trackChanges">Should this insertion be tracked as a change?</param> | |||||
| /// <param name="formatting">The formatting to apply to this insertion.</param> | |||||
| /// <returns>A new Paragraph inserted before this Paragraph.</returns> | |||||
| /// <example> | |||||
| /// Insert a new paragraph before the first Paragraph in this document. | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public 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 = CreateEdit(EditType.ins, DateTime.Now, newParagraph); | |||||
| xml.AddBeforeSelf(newParagraph); | |||||
| XElement newlyInserted = xml.ElementsBeforeSelf().First(); | |||||
| Paragraph p = new Paragraph(document, -1, newlyInserted); | |||||
| DocX.RebuildParagraphs(document); | |||||
| return p; | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a Paragraph after this Paragraph, this Paragraph may have come from the same or another document. | |||||
| /// </summary> | |||||
| /// <param name="p">The Paragraph to insert.</param> | |||||
| /// <returns>The Paragraph now associated with this document.</returns> | |||||
| /// <example> | |||||
| /// Take a Paragraph from document a, and insert it into document b after this Paragraph. | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph InsertParagraphAfterSelf(Paragraph p) | |||||
| { | |||||
| xml.AddAfterSelf(p.xml); | |||||
| XElement newlyInserted = xml.ElementsAfterSelf().First(); | |||||
| p.xml = newlyInserted; | |||||
| DocX.RebuildParagraphs(document); | |||||
| return p; | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Paragraph after this Paragraph. | |||||
| /// </summary> | |||||
| /// <param name="text">The initial text for this new Paragraph.</param> | |||||
| /// <param name="trackChanges">Should this insertion be tracked as a change?</param> | |||||
| /// <param name="formatting">The formatting to apply to this insertion.</param> | |||||
| /// <returns>A new Paragraph inserted after this Paragraph.</returns> | |||||
| /// <example> | |||||
| /// Insert a new paragraph after the first Paragraph in this document. | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public 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 = CreateEdit(EditType.ins, DateTime.Now, newParagraph); | |||||
| xml.AddAfterSelf(newParagraph); | |||||
| XElement newlyInserted = xml.ElementsAfterSelf().First(); | |||||
| Paragraph p = new Paragraph(document, -1, newlyInserted); | |||||
| DocX.RebuildParagraphs(document); | |||||
| return p; | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Paragraph after this Paragraph. | |||||
| /// </summary> | |||||
| /// <param name="text">The initial text for this new Paragraph.</param> | |||||
| /// <param name="trackChanges">Should this insertion be tracked as a change?</param> | |||||
| /// <returns>A new Paragraph inserted after this Paragraph.</returns> | |||||
| /// <example> | |||||
| /// Insert a new paragraph after the first Paragraph in this document. | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph InsertParagraphAfterSelf(string text, bool trackChanges) | |||||
| { | |||||
| return InsertParagraphAfterSelf(text, trackChanges, new Formatting()); | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Paragraph after this Paragraph. | |||||
| /// </summary> | |||||
| /// <param name="text">The initial text for this new Paragraph.</param> | |||||
| /// <returns>A new Paragraph inserted after this Paragraph.</returns> | |||||
| /// <example> | |||||
| /// Insert a new paragraph after the first Paragraph in this document. | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph InsertParagraphAfterSelf(string text) | |||||
| { | |||||
| return InsertParagraphAfterSelf(text, false, new Formatting()); | |||||
| } | |||||
| private void RebuildDocProperties() | |||||
| { | |||||
| docProperties = | |||||
| ( | |||||
| from dp in xml.Descendants(XName.Get("fldSimple", DocX.w.NamespaceName)) | |||||
| select new DocProperty(dp) | |||||
| ).ToList(); | |||||
| } | } | ||||
| /// <summary> | /// <summary> | ||||
| DocX.RebuildParagraphs(document); | DocX.RebuildParagraphs(document); | ||||
| } | } | ||||
| private void BuildRunLookup(XElement p) | |||||
| internal void BuildRunLookup(XElement p) | |||||
| { | { | ||||
| // Get the runs in this paragraph | // Get the runs in this paragraph | ||||
| IEnumerable<XElement> runs = p.Descendants(XName.Get("r", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")); | IEnumerable<XElement> runs = p.Descendants(XName.Get("r", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")); | ||||
| public Picture InsertPicture(string imageID, string name, string description) | public Picture InsertPicture(string imageID, string name, string description) | ||||
| { | { | ||||
| Picture p = new Picture(document, imageID, name, description); | Picture p = new Picture(document, imageID, name, description); | ||||
| xml.Add(p.i); | |||||
| xml.Add(p.xml); | |||||
| pictures.Add(p); | pictures.Add(p); | ||||
| return p; | return p; | ||||
| } | } | ||||
| public Picture InsertPicture(string imageID) | |||||
| { | |||||
| return InsertPicture(imageID, string.Empty, string.Empty); | |||||
| } | |||||
| //public Picture InsertPicture(int index, Picture picture) | //public Picture InsertPicture(int index, Picture picture) | ||||
| //{ | //{ | ||||
| // Picture p = picture; | // Picture p = picture; | ||||
| Run run = GetFirstRunEffectedByEdit(index); | Run run = GetFirstRunEffectedByEdit(index); | ||||
| if (run == null) | if (run == null) | ||||
| xml.Add(picture.i); | |||||
| xml.Add(picture.xml); | |||||
| else | else | ||||
| { | { | ||||
| // Split this run at the point you want to insert | // Split this run at the point you want to insert | ||||
| run.xml.ReplaceWith | run.xml.ReplaceWith | ||||
| ( | ( | ||||
| splitRun[0], | splitRun[0], | ||||
| picture.i, | |||||
| picture.xml, | |||||
| splitRun[1] | splitRun[1] | ||||
| ); | ); | ||||
| } | } | ||||
| return picture; | return picture; | ||||
| } | } | ||||
| public Picture InsertPicture(int index, string imageID) | |||||
| { | |||||
| return InsertPicture(index, imageID, string.Empty, string.Empty); | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Creates an Edit either a ins or a del with the specified content and date | /// Creates an Edit either a ins or a del with the specified content and date | ||||
| /// </summary> | /// </summary> | ||||
| /// <param name="edit_time">The time stamp to use for this edit</param> | /// <param name="edit_time">The time stamp to use for this edit</param> | ||||
| /// <param name="content">The initial content of this edit</param> | /// <param name="content">The initial content of this edit</param> | ||||
| /// <returns></returns> | /// <returns></returns> | ||||
| private XElement CreateEdit(EditType t, DateTime edit_time, object content) | |||||
| internal static XElement CreateEdit(EditType t, DateTime edit_time, object content) | |||||
| { | { | ||||
| if (t == EditType.del) | if (t == EditType.del) | ||||
| { | { | ||||
| throw new ArgumentOutOfRangeException(); | throw new ArgumentOutOfRangeException(); | ||||
| } | } | ||||
| private List<XElement> FormatInput(string text, XElement rPr) | |||||
| { | |||||
| // Need to support /n as non breaking space | |||||
| List<XElement> newRuns = new List<XElement>(); | |||||
| XElement tabRun = new XElement(DocX.w + "tab"); | |||||
| string[] runTexts = text.Split('\t'); | |||||
| XElement firstRun; | |||||
| if (runTexts[0] != String.Empty) | |||||
| { | |||||
| XElement firstText = new XElement(DocX.w + "t", runTexts[0]); | |||||
| Novacode.Text.PreserveSpace(firstText); | |||||
| firstRun = new XElement(DocX.w + "r", rPr, firstText); | |||||
| newRuns.Add(firstRun); | |||||
| } | |||||
| if (runTexts.Length > 1) | |||||
| { | |||||
| for (int k = 1; k < runTexts.Length; k++) | |||||
| { | |||||
| XElement newText = new XElement(DocX.w + "t", runTexts[k]); | |||||
| XElement newRun; | |||||
| if (runTexts[k] == String.Empty) | |||||
| newRun = new XElement(DocX.w + "r", tabRun); | |||||
| else | |||||
| { | |||||
| // Value begins or ends with a space | |||||
| Novacode.Text.PreserveSpace(newText); | |||||
| newRun = new XElement(DocX.w + "r", rPr, tabRun, newText); | |||||
| } | |||||
| newRuns.Add(newRun); | |||||
| } | |||||
| } | |||||
| return newRuns; | |||||
| } | |||||
| /// <!-- | |||||
| /// Bug found and fixed by krugs525 on August 12 2009. | |||||
| /// Use TFS compare to see exact code change. | |||||
| /// --> | |||||
| static internal int GetElementTextLength(XElement run) | static internal int GetElementTextLength(XElement run) | ||||
| { | { | ||||
| int count = 0; | int count = 0; | ||||
| if (run == null) | if (run == null) | ||||
| return count; | return count; | ||||
| { | { | ||||
| switch (d.Name.LocalName) | switch (d.Name.LocalName) | ||||
| { | { | ||||
| case "tab": goto case "br"; | |||||
| case "tab": | |||||
| if (d.Parent.Name.LocalName != "tabs") | |||||
| goto case "br"; break; | |||||
| case "br": count++; break; | case "br": count++; break; | ||||
| case "t": goto case "delText"; | case "t": goto case "delText"; | ||||
| case "delText": count += d.Value.Length; break; | case "delText": count += d.Value.Length; break; | ||||
| /// <param name="trackChanges">Flag this insert as a change.</param> | /// <param name="trackChanges">Flag this insert as a change.</param> | ||||
| public void InsertText(string value, bool trackChanges) | public void InsertText(string value, bool trackChanges) | ||||
| { | { | ||||
| List<XElement> newRuns = FormatInput(value, null); | |||||
| List<XElement> newRuns = DocX.FormatInput(value, null); | |||||
| xml.Add(newRuns); | xml.Add(newRuns); | ||||
| runLookup.Clear(); | runLookup.Clear(); | ||||
| /// <param name="formatting">The text formatting.</param> | /// <param name="formatting">The text formatting.</param> | ||||
| public void InsertText(string value, bool trackChanges, Formatting formatting) | public void InsertText(string value, bool trackChanges, Formatting formatting) | ||||
| { | { | ||||
| List<XElement> newRuns = FormatInput(value, formatting.Xml); | |||||
| List<XElement> newRuns = DocX.FormatInput(value, formatting.Xml); | |||||
| xml.Add(newRuns); | xml.Add(newRuns); | ||||
| runLookup.Clear(); | runLookup.Clear(); | ||||
| BuildRunLookup(xml); | BuildRunLookup(xml); | ||||
| DocX.RenumberIDs(document); | DocX.RenumberIDs(document); | ||||
| DocX.RebuildParagraphs(document); | |||||
| } | } | ||||
| /// <summary> | /// <summary> | ||||
| { | { | ||||
| object insert; | object insert; | ||||
| if (formatting != null) | if (formatting != null) | ||||
| insert = FormatInput(value, formatting.Xml); | |||||
| insert = DocX.FormatInput(value, formatting.Xml); | |||||
| else | else | ||||
| insert = FormatInput(value, null); | |||||
| insert = DocX.FormatInput(value, null); | |||||
| if (trackChanges) | if (trackChanges) | ||||
| insert = CreateEdit(EditType.ins, insert_datetime, insert); | insert = CreateEdit(EditType.ins, insert_datetime, insert); | ||||
| { | { | ||||
| object newRuns; | object newRuns; | ||||
| if (formatting != null) | if (formatting != null) | ||||
| newRuns = FormatInput(value, formatting.Xml); | |||||
| newRuns = DocX.FormatInput(value, formatting.Xml); | |||||
| else | else | ||||
| newRuns = FormatInput(value, run.xml.Element(XName.Get("rPr", DocX.w.NamespaceName))); | |||||
| newRuns = DocX.FormatInput(value, run.xml.Element(XName.Get("rPr", DocX.w.NamespaceName))); | |||||
| // The parent of this Run | // The parent of this Run | ||||
| XElement parentElement = run.xml.Parent; | XElement parentElement = run.xml.Parent; | ||||
| BuildRunLookup(xml); | BuildRunLookup(xml); | ||||
| DocX.RenumberIDs(document); | DocX.RenumberIDs(document); | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Insert a field of type document property, this field will display the custom property cp, at the end of this paragraph. | |||||
| /// </summary> | |||||
| /// <param name="cp">The custom property to display.</param> | |||||
| /// <param name="f">The formatting to use for this text.</param> | |||||
| /// <example> | |||||
| /// Create, add and display a custom property in a document. | |||||
| /// <code> | |||||
| /// // Load a document | |||||
| /// using (DocX document = DocX.Create(@"Test.docx")) | |||||
| /// { | |||||
| /// // Create a custom property. | |||||
| /// CustomProperty name = new CustomProperty("name", "Cathal Coffey"); | |||||
| /// | |||||
| /// // Add this custom property to this document. | |||||
| /// document.AddCustomProperty(name); | |||||
| /// | |||||
| /// // Create a text formatting. | |||||
| /// Formatting f = new Formatting(); | |||||
| /// f.Bold = true; | |||||
| /// f.Size = 14; | |||||
| /// f.StrikeThrough = StrickThrough.strike; | |||||
| /// | |||||
| /// // Insert a new paragraph. | |||||
| /// Paragraph p = document.InsertParagraph("Author: ", false, f); | |||||
| /// | |||||
| /// // Insert a field of type document property to display the custom property name | |||||
| /// p.InsertDocProperty(name, f); | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// }// Release this document from memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public void InsertDocProperty(CustomProperty cp, Formatting f) | |||||
| { | |||||
| XElement e = new XElement | |||||
| ( | |||||
| XName.Get("fldSimple", DocX.w.NamespaceName), | |||||
| new XAttribute(XName.Get("instr", DocX.w.NamespaceName), string.Format(@"DOCPROPERTY {0} \* MERGEFORMAT", cp.Name)), | |||||
| new XElement(XName.Get("r", DocX.w.NamespaceName), | |||||
| new XElement(XName.Get("t", DocX.w.NamespaceName), f.Xml, cp.Value)) | |||||
| ); | |||||
| xml.Add(e); | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a field of type document property, this field will display the custom property cp, at the end of this paragraph. | |||||
| /// </summary> | |||||
| /// <param name="cp">The custom property to display.</param> | |||||
| /// <example> | |||||
| /// Create, add and display a custom property in a document. | |||||
| /// <code> | |||||
| /// // Load a document | |||||
| /// using (DocX document = DocX.Create(@"Test.docx")) | |||||
| /// { | |||||
| /// // Create a custom property. | |||||
| /// CustomProperty name = new CustomProperty("name", "Cathal Coffey"); | |||||
| /// | |||||
| /// // Add this custom property to this document. | |||||
| /// document.AddCustomProperty(name); | |||||
| /// | |||||
| /// // Insert a new paragraph. | |||||
| /// Paragraph p = document.InsertParagraph("Author: ", false); | |||||
| /// | |||||
| /// // Insert a field of type document property to display the custom property name | |||||
| /// p.InsertDocProperty(name); | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// }// Release this document from memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public void InsertDocProperty(CustomProperty cp) | |||||
| { | |||||
| InsertDocProperty(cp, new Formatting()); | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Removes characters from a Novacode.DocX.Paragraph. | /// Removes characters from a Novacode.DocX.Paragraph. | ||||
| } | } | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Find all instances of a string in this paragraph and return their indexes in a List. | |||||
| /// </summary> | |||||
| /// <param name="str">The string to find</param> | |||||
| /// <returns>A list of indexes.</returns> | |||||
| /// <example> | |||||
| /// Find all instances of Hello in this document and insert 'don't' in frount of them. | |||||
| /// <code> | |||||
| /// // Load a document | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Loop through the paragraphs in this document. | |||||
| /// foreach(Paragraph p in document.Paragraphs) | |||||
| /// { | |||||
| /// // Find all instances of 'go' in this paragraph. | |||||
| /// List<int> gos = document.FindAll("go"); | |||||
| /// | |||||
| /// /* | |||||
| /// * Insert 'don't' in frount of every instance of 'go' in this document to produce 'don't go'. | |||||
| /// * An important trick here is to do the inserting in reverse document order. If you inserted | |||||
| /// * in document order, every insert would shift the index of the remaining matches. | |||||
| /// */ | |||||
| /// gos.Reverse(); | |||||
| /// foreach (int index in gos) | |||||
| /// { | |||||
| /// p.InsertText(index, "don't ", false); | |||||
| /// } | |||||
| /// } | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// }// Release this document from memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public List<int> FindAll(string str) | |||||
| { | |||||
| return FindAll(str, RegexOptions.None); | |||||
| } | |||||
| /// <summary> | |||||
| /// Find all instances of a string in this paragraph and return their indexes in a List. | |||||
| /// </summary> | |||||
| /// <param name="str">The string to find</param> | |||||
| /// <param name="options">The options to use when finding a string match.</param> | |||||
| /// <returns>A list of indexes.</returns> | |||||
| /// <example> | |||||
| /// Find all instances of Hello in this document and insert 'don't' in frount of them. | |||||
| /// <code> | |||||
| /// // Load a document | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Loop through the paragraphs in this document. | |||||
| /// foreach(Paragraph p in document.Paragraphs) | |||||
| /// { | |||||
| /// // Find all instances of 'go' in this paragraph (Ignore case). | |||||
| /// List<int> gos = document.FindAll("go", RegexOptions.IgnoreCase); | |||||
| /// | |||||
| /// /* | |||||
| /// * Insert 'don't' in frount of every instance of 'go' in this document to produce 'don't go'. | |||||
| /// * An important trick here is to do the inserting in reverse document order. If you inserted | |||||
| /// * in document order, every insert would shift the index of the remaining matches. | |||||
| /// */ | |||||
| /// gos.Reverse(); | |||||
| /// foreach (int index in gos) | |||||
| /// { | |||||
| /// p.InsertText(index, "don't ", false); | |||||
| /// } | |||||
| /// } | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// }// Release this document from memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public List<int> FindAll(string str, RegexOptions options) | |||||
| { | |||||
| MatchCollection mc = Regex.Matches(this.Text, Regex.Escape(str), options); | |||||
| var query = | |||||
| ( | |||||
| from m in mc.Cast<Match>() | |||||
| select m.Index | |||||
| ).ToList(); | |||||
| return query; | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Replaces all occurrences of a specified System.String in this instance, with another specified System.String. | /// Replaces all occurrences of a specified System.String in this instance, with another specified System.String. | ||||
| /// </summary> | /// </summary> |
| private object pictureShape; | private object pictureShape; | ||||
| // The underlying XElement which this Image wraps | // The underlying XElement which this Image wraps | ||||
| internal XElement i; | |||||
| internal XElement xml; | |||||
| private XElement xfrm; | private XElement xfrm; | ||||
| private XElement prstGeom; | private XElement prstGeom; | ||||
| using (System.Drawing.Image img = System.Drawing.Image.FromStream(part.GetStream())) | using (System.Drawing.Image img = System.Drawing.Image.FromStream(part.GetStream())) | ||||
| { | { | ||||
| this.cx = img.Width * 4156; | |||||
| this.cy = img.Height * 4156; | |||||
| this.cx = img.Width * 9526; | |||||
| this.cy = img.Height * 9526; | |||||
| } | } | ||||
| XElement e = new XElement(DocX.w + "drawing"); | XElement e = new XElement(DocX.w + "drawing"); | ||||
| i = XElement.Parse | |||||
| xml = XElement.Parse | |||||
| (string.Format(@" | (string.Format(@" | ||||
| <drawing xmlns = ""http://schemas.openxmlformats.org/wordprocessingml/2006/main""> | <drawing xmlns = ""http://schemas.openxmlformats.org/wordprocessingml/2006/main""> | ||||
| <wp:inline distT=""0"" distB=""0"" distL=""0"" distR=""0"" xmlns:wp=""http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing""> | <wp:inline distT=""0"" distB=""0"" distL=""0"" distR=""0"" xmlns:wp=""http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing""> | ||||
| this.xfrm = | this.xfrm = | ||||
| ( | ( | ||||
| from d in i.Descendants() | |||||
| from d in xml.Descendants() | |||||
| where d.Name.LocalName.Equals("xfrm") | where d.Name.LocalName.Equals("xfrm") | ||||
| select d | select d | ||||
| ).Single(); | ).Single(); | ||||
| this.prstGeom = | this.prstGeom = | ||||
| ( | ( | ||||
| from d in i.Descendants() | |||||
| from d in xml.Descendants() | |||||
| where d.Name.LocalName.Equals("prstGeom") | where d.Name.LocalName.Equals("prstGeom") | ||||
| select d | select d | ||||
| ).Single(); | ).Single(); | ||||
| this.rotation = xfrm.Attribute(XName.Get("rot")) == null ? 0 : uint.Parse(xfrm.Attribute(XName.Get("rot")).Value); | this.rotation = xfrm.Attribute(XName.Get("rot")) == null ? 0 : uint.Parse(xfrm.Attribute(XName.Get("rot")).Value); | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Remove this Picture from this document. | |||||
| /// </summary> | |||||
| public void Remove() | |||||
| { | |||||
| xml.Remove(); | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Wraps an XElement as an Image | /// Wraps an XElement as an Image | ||||
| /// </summary> | /// </summary> | ||||
| /// <param name="i">The XElement i to wrap</param> | /// <param name="i">The XElement i to wrap</param> | ||||
| internal Picture(XElement i) | internal Picture(XElement i) | ||||
| { | { | ||||
| this.i = i; | |||||
| this.xml = i; | |||||
| this.id = | this.id = | ||||
| ( | ( | ||||
| select a.Value | select a.Value | ||||
| ).First(); | ).First(); | ||||
| this.descr = | this.descr = | ||||
| ( | ( | ||||
| from e in i.Descendants() | from e in i.Descendants() | ||||
| let a = e.Attribute(XName.Get("descr")) | let a = e.Attribute(XName.Get("descr")) | ||||
| where (a != null) | where (a != null) | ||||
| select a.Value | select a.Value | ||||
| ).First(); | |||||
| ).FirstOrDefault(); | |||||
| this.cx = | this.cx = | ||||
| ( | ( | ||||
| { | { | ||||
| rotation = (value % 360) * 60000; | rotation = (value % 360) * 60000; | ||||
| XElement xfrm = | XElement xfrm = | ||||
| (from d in i.Descendants() | |||||
| (from d in xml.Descendants() | |||||
| where d.Name.LocalName.Equals("xfrm") | where d.Name.LocalName.Equals("xfrm") | ||||
| select d).Single(); | select d).Single(); | ||||
| { | { | ||||
| name = value; | name = value; | ||||
| foreach (XAttribute a in i.Descendants().Attributes(XName.Get("name"))) | |||||
| foreach (XAttribute a in xml.Descendants().Attributes(XName.Get("name"))) | |||||
| a.Value = name; | a.Value = name; | ||||
| } | } | ||||
| } | } | ||||
| { | { | ||||
| descr = value; | descr = value; | ||||
| foreach (XAttribute a in i.Descendants().Attributes(XName.Get("descr"))) | |||||
| foreach (XAttribute a in xml.Descendants().Attributes(XName.Get("descr"))) | |||||
| a.Value = descr; | a.Value = descr; | ||||
| } | } | ||||
| } | } | ||||
| { | { | ||||
| cx = value; | cx = value; | ||||
| foreach (XAttribute a in i.Descendants().Attributes(XName.Get("cx"))) | |||||
| foreach (XAttribute a in xml.Descendants().Attributes(XName.Get("cx"))) | |||||
| a.Value = (cx * 4156).ToString(); | a.Value = (cx * 4156).ToString(); | ||||
| } | } | ||||
| } | } | ||||
| { | { | ||||
| cy = value; | cy = value; | ||||
| foreach (XAttribute a in i.Descendants().Attributes(XName.Get("cy"))) | |||||
| foreach (XAttribute a in xml.Descendants().Attributes(XName.Get("cy"))) | |||||
| a.Value = (cy * 4156).ToString(); | a.Value = (cy * 4156).ToString(); | ||||
| } | } | ||||
| } | } |
| // You can specify all the values or you can default the Build and Revision Numbers | // You can specify all the values or you can default the Build and Revision Numbers | ||||
| // by using the '*' as shown below: | // by using the '*' as shown below: | ||||
| // [assembly: AssemblyVersion("1.0.*")] | // [assembly: AssemblyVersion("1.0.*")] | ||||
| [assembly: AssemblyVersion("1.0.0.5")] | |||||
| [assembly: AssemblyFileVersion("1.0.0.5")] | |||||
| [assembly: AssemblyVersion("1.0.0.7")] | |||||
| [assembly: AssemblyFileVersion("1.0.0.7")] |
| /// <summary> | /// <summary> | ||||
| /// Designs\Styles that can be applied to a table. | /// Designs\Styles that can be applied to a table. | ||||
| /// </summary> | /// </summary> | ||||
| public enum TableDesign { TableNormal, TableGrid, LightShading, LightShadingAccent1, LightShadingAccent2, LightShadingAccent3, LightShadingAccent4, LightShadingAccent5, LightShadingAccent6, LightList, LightListAccent1, LightListAccent2, LightListAccent3, LightListAccent4, LightListAccent5, LightListAccent6, LightGrid, LightGridAccent1, LightGridAccent2, LightGridAccent3, LightGridAccent4, LightGridAccent5, LightGridAccent6, MediumShading1, MediumShading1Accent1, MediumShading1Accent2, MediumShading1Accent3, MediumShading1Accent4, MediumShading1Accent5, MediumShading1Accent6, MediumShading2, MediumShading2Accent1, MediumShading2Accent2, MediumShading2Accent3, MediumShading2Accent4, MediumShading2Accent5, MediumShading2Accent6, MediumList1, MediumList1Accent1, MediumList1Accent2, MediumList1Accent3, MediumList1Accent4, MediumList1Accent5, MediumList1Accent6, MediumList2, MediumList2Accent1, MediumList2Accent2, MediumList2Accent3, MediumList2Accent4, MediumList2Accent5, MediumList2Accent6, MediumGrid1, MediumGrid1Accent1, MediumGrid1Accent2, MediumGrid1Accent3, MediumGrid1Accent4, MediumGrid1Accent5, MediumGrid1Accent6, MediumGrid2, MediumGrid2Accent1, MediumGrid2Accent2, MediumGrid2Accent3, MediumGrid2Accent4, MediumGrid2Accent5, MediumGrid2Accent6, MediumGrid3, MediumGrid3Accent1, MediumGrid3Accent2, MediumGrid3Accent3, MediumGrid3Accent4, MediumGrid3Accent5, MediumGrid3Accent6, DarkList, DarkListAccent1, DarkListAccent2, DarkListAccent3, DarkListAccent4, DarkListAccent5, DarkListAccent6, ColorfulShading, ColorfulShadingAccent1, ColorfulShadingAccent2, ColorfulShadingAccent3, ColorfulShadingAccent4, ColorfulShadingAccent5, ColorfulShadingAccent6, ColorfulList, ColorfulListAccent1, ColorfulListAccent2, ColorfulListAccent3, ColorfulListAccent4, ColorfulListAccent5, ColorfulListAccent6, ColorfulGrid, ColorfulGridAccent1, ColorfulGridAccent2, ColorfulGridAccent3, ColorfulGridAccent4, ColorfulGridAccent5, ColorfulGridAccent6}; | |||||
| public enum TableDesign { TableNormal, TableGrid, LightShading, LightShadingAccent1, LightShadingAccent2, LightShadingAccent3, LightShadingAccent4, LightShadingAccent5, LightShadingAccent6, LightList, LightListAccent1, LightListAccent2, LightListAccent3, LightListAccent4, LightListAccent5, LightListAccent6, LightGrid, LightGridAccent1, LightGridAccent2, LightGridAccent3, LightGridAccent4, LightGridAccent5, LightGridAccent6, MediumShading1, MediumShading1Accent1, MediumShading1Accent2, MediumShading1Accent3, MediumShading1Accent4, MediumShading1Accent5, MediumShading1Accent6, MediumShading2, MediumShading2Accent1, MediumShading2Accent2, MediumShading2Accent3, MediumShading2Accent4, MediumShading2Accent5, MediumShading2Accent6, MediumList1, MediumList1Accent1, MediumList1Accent2, MediumList1Accent3, MediumList1Accent4, MediumList1Accent5, MediumList1Accent6, MediumList2, MediumList2Accent1, MediumList2Accent2, MediumList2Accent3, MediumList2Accent4, MediumList2Accent5, MediumList2Accent6, MediumGrid1, MediumGrid1Accent1, MediumGrid1Accent2, MediumGrid1Accent3, MediumGrid1Accent4, MediumGrid1Accent5, MediumGrid1Accent6, MediumGrid2, MediumGrid2Accent1, MediumGrid2Accent2, MediumGrid2Accent3, MediumGrid2Accent4, MediumGrid2Accent5, MediumGrid2Accent6, MediumGrid3, MediumGrid3Accent1, MediumGrid3Accent2, MediumGrid3Accent3, MediumGrid3Accent4, MediumGrid3Accent5, MediumGrid3Accent6, DarkList, DarkListAccent1, DarkListAccent2, DarkListAccent3, DarkListAccent4, DarkListAccent5, DarkListAccent6, ColorfulShading, ColorfulShadingAccent1, ColorfulShadingAccent2, ColorfulShadingAccent3, ColorfulShadingAccent4, ColorfulShadingAccent5, ColorfulShadingAccent6, ColorfulList, ColorfulListAccent1, ColorfulListAccent2, ColorfulListAccent3, ColorfulListAccent4, ColorfulListAccent5, ColorfulListAccent6, ColorfulGrid, ColorfulGridAccent1, ColorfulGridAccent2, ColorfulGridAccent3, ColorfulGridAccent4, ColorfulGridAccent5, ColorfulGridAccent6, None}; | |||||
| public enum AutoFit{Contents, Window, ColoumnWidth}; | |||||
| /// <summary> | /// <summary> | ||||
| /// Represents a Table in a document. | /// Represents a Table in a document. | ||||
| /// </summary> | /// </summary> | ||||
| public class Table | public class Table | ||||
| { | { | ||||
| private Alignment alignment; | |||||
| private AutoFit autofit; | |||||
| private List<Row> rows; | private List<Row> rows; | ||||
| private int rowCount, columnCount; | private int rowCount, columnCount; | ||||
| internal XElement xml; | internal XElement xml; | ||||
| internal Table(DocX document, XElement xml) | internal Table(DocX document, XElement xml) | ||||
| { | { | ||||
| autofit = AutoFit.ColoumnWidth; | |||||
| this.xml = xml; | this.xml = xml; | ||||
| this.document = document; | this.document = document; | ||||
| columnCount = rows[0].Cells.Count; | columnCount = rows[0].Cells.Count; | ||||
| XElement style = properties.Element(XName.Get("tblStyle", DocX.w.NamespaceName)); | XElement style = properties.Element(XName.Get("tblStyle", DocX.w.NamespaceName)); | ||||
| XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| design = (TableDesign)Enum.Parse(typeof(TableDesign), val.Value.Replace("-", string.Empty)); | |||||
| if (style != null) | |||||
| { | |||||
| XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| if (val != null) | |||||
| design = (TableDesign)Enum.Parse(typeof(TableDesign), val.Value.Replace("-", string.Empty)); | |||||
| else | |||||
| design = TableDesign.None; | |||||
| } | |||||
| else | |||||
| design = TableDesign.None; | |||||
| } | } | ||||
| public Alignment Alignment | |||||
| { | |||||
| get { return alignment; } | |||||
| set | |||||
| { | |||||
| string alignmentString = string.Empty; | |||||
| switch (value) | |||||
| { | |||||
| case Alignment.left: | |||||
| { | |||||
| alignmentString = "left"; | |||||
| break; | |||||
| } | |||||
| case Alignment.both: | |||||
| { | |||||
| alignmentString = "both"; | |||||
| break; | |||||
| } | |||||
| case Alignment.right: | |||||
| { | |||||
| alignmentString = "right"; | |||||
| break; | |||||
| } | |||||
| case Alignment.center: | |||||
| { | |||||
| alignmentString = "center"; | |||||
| break; | |||||
| } | |||||
| } | |||||
| XElement tblPr = xml.Descendants(XName.Get("tblPr", DocX.w.NamespaceName)).First(); | |||||
| XElement jc = tblPr.Descendants(XName.Get("jc", DocX.w.NamespaceName)).FirstOrDefault(); | |||||
| if(jc != null) | |||||
| jc.Remove(); | |||||
| jc = new XElement(XName.Get("jc", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), alignmentString)); | |||||
| tblPr.Add(jc); | |||||
| alignment = value; | |||||
| } | |||||
| } | |||||
| /// <summary> | |||||
| /// Auto size this table according to some rule. | |||||
| /// </summary> | |||||
| public AutoFit AutoFit | |||||
| { | |||||
| get{return autofit;} | |||||
| set | |||||
| { | |||||
| string attributeValue = string.Empty; | |||||
| switch(value) | |||||
| { | |||||
| case AutoFit.ColoumnWidth: | |||||
| { | |||||
| attributeValue = "dxa"; | |||||
| break; | |||||
| } | |||||
| case AutoFit.Contents: | |||||
| { | |||||
| attributeValue = "auto"; | |||||
| break; | |||||
| } | |||||
| case AutoFit.Window: | |||||
| { | |||||
| attributeValue = "pct"; | |||||
| break; | |||||
| } | |||||
| } | |||||
| var query = from d in xml.Descendants() | |||||
| let type = d.Attribute(XName.Get("type", DocX.w.NamespaceName)) | |||||
| where (d.Name.LocalName == "tcW" || d.Name.LocalName == "tblW") && type != null | |||||
| select type; | |||||
| foreach (XAttribute type in query) | |||||
| type.Value = attributeValue; | |||||
| autofit = value; | |||||
| } | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// The design\style to apply to this table. | /// The design\style to apply to this table. | ||||
| /// </summary> | /// </summary> | ||||
| design = value; | design = value; | ||||
| if (design == TableDesign.None) | |||||
| { | |||||
| if (style != null) | |||||
| style.Remove(); | |||||
| } | |||||
| switch (design) | switch (design) | ||||
| { | { | ||||
| case TableDesign.TableNormal: val.Value = "TableNormal"; break; | case TableDesign.TableNormal: val.Value = "TableNormal"; break; | ||||
| if (tableStyle == null) | if (tableStyle == null) | ||||
| { | { | ||||
| Assembly _assembly = Assembly.GetExecutingAssembly(); | |||||
| TextReader _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("Novacode.styles.xml")); | |||||
| XDocument external_style_doc = XDocument.Load(_textStreamReader); | |||||
| XDocument external_style_doc = DocX.DecompressXMLResource("Novacode.Resources.styles.xml.gz"); | |||||
| var styleElement = | var styleElement = | ||||
| ( | ( | ||||
| return InsertRow(rows.Count); | return InsertRow(rows.Count); | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Returns the index of this Table. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Replace the first table in this document with a new Table. | |||||
| /// <code> | |||||
| /// // Load a document into memory. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get the first Table in this document. | |||||
| /// Table t = document.Tables[0]; | |||||
| /// | |||||
| /// // Get the character index of Table t in this document. | |||||
| /// int index = t.Index; | |||||
| /// | |||||
| /// // Remove Table t. | |||||
| /// t.Remove(); | |||||
| /// | |||||
| /// // Insert a new Table at the original index of Table t. | |||||
| /// Table newTable = document.InsertTable(index, 4, 4); | |||||
| /// | |||||
| /// // Set the design of this new Table, so that we can see it. | |||||
| /// newTable.Design = TableDesign.LightShadingAccent1; | |||||
| /// | |||||
| /// // Save all changes made to the document. | |||||
| /// document.Save(); | |||||
| /// } // Release this document from memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public int Index | |||||
| { | |||||
| get | |||||
| { | |||||
| int index = 0; | |||||
| IEnumerable<XElement> previous = xml.ElementsBeforeSelf(); | |||||
| foreach (XElement e in previous) | |||||
| index += Paragraph.GetElementTextLength(e); | |||||
| return index; | |||||
| } | |||||
| } | |||||
| /// <summary> | |||||
| /// Remove this Table from this document. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Remove the first Table from this document. | |||||
| /// <code> | |||||
| /// // Load a document into memory. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get the first Table in this document. | |||||
| /// Table t = d.Tables[0]; | |||||
| /// | |||||
| /// // Remove this Table. | |||||
| /// t.Remove(); | |||||
| /// | |||||
| /// // Save all changes made to the document. | |||||
| /// document.Save(); | |||||
| /// } // Release this document from memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public void Remove() | |||||
| { | |||||
| xml.Remove(); | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Insert a column to the right of a Table. | /// Insert a column to the right of a Table. | ||||
| /// </summary> | /// </summary> | ||||
| columnCount = rows[0].Cells.Count; | columnCount = rows[0].Cells.Count; | ||||
| } | } | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Insert a new Table before this Table, this Table can be from this document or another document. | |||||
| /// </summary> | |||||
| /// <param name="t">The Table t to be inserted</param> | |||||
| /// <returns>A new Table inserted before this Table.</returns> | |||||
| /// <example> | |||||
| /// Insert a new Table before this Table. | |||||
| /// <code> | |||||
| /// // 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 Table in document b. | |||||
| /// Table t2 = documentB.Tables[0]; | |||||
| /// | |||||
| /// // Insert the Table from document a before this Table. | |||||
| /// Table newTable = t2.InsertTableBeforeSelf(t); | |||||
| /// | |||||
| /// // Save all changes made to document b. | |||||
| /// documentB.Save(); | |||||
| /// }// Release this document from memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Table InsertTableBeforeSelf(Table t) | |||||
| { | |||||
| xml.AddBeforeSelf(t.xml); | |||||
| XElement newlyInserted = xml.ElementsBeforeSelf().First(); | |||||
| t.xml = newlyInserted; | |||||
| DocX.RebuildTables(document); | |||||
| DocX.RebuildParagraphs(document); | |||||
| return t; | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Table into this document before this Table. | |||||
| /// </summary> | |||||
| /// <param name="rowCount">The number of rows this Table should have.</param> | |||||
| /// <param name="coloumnCount">The number of coloumns this Table should have.</param> | |||||
| /// <returns>A new Table inserted before this Table.</returns> | |||||
| /// <example> | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create(@"Test.docx")) | |||||
| /// { | |||||
| /// //Insert a Table into this document. | |||||
| /// Table t = document.InsertTable(2, 2); | |||||
| /// t.Design = TableDesign.LightShadingAccent1; | |||||
| /// t.Alignment = Alignment.center; | |||||
| /// | |||||
| /// // Insert a new Table before this Table. | |||||
| /// Table newTable = t.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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public 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); | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Table after this Table, this Table can be from this document or another document. | |||||
| /// </summary> | |||||
| /// <param name="t">The Table t to be inserted</param> | |||||
| /// <returns>A new Table inserted after this Table.</returns> | |||||
| /// <example> | |||||
| /// Insert a new Table after this Table. | |||||
| /// <code> | |||||
| /// // 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 Table in document b. | |||||
| /// Table t2 = documentB.Tables[0]; | |||||
| /// | |||||
| /// // Insert the Table from document a after this Table. | |||||
| /// Table newTable = t2.InsertTableAfterSelf(t); | |||||
| /// | |||||
| /// // Save all changes made to document b. | |||||
| /// documentB.Save(); | |||||
| /// }// Release this document from memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Table InsertTableAfterSelf(Table t) | |||||
| { | |||||
| xml.AddAfterSelf(t.xml); | |||||
| XElement newlyInserted = xml.ElementsAfterSelf().First(); | |||||
| t.xml = newlyInserted; | |||||
| DocX.RebuildTables(document); | |||||
| DocX.RebuildParagraphs(document); | |||||
| return t; | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Table into this document after this Table. | |||||
| /// </summary> | |||||
| /// <param name="rowCount">The number of rows this Table should have.</param> | |||||
| /// <param name="coloumnCount">The number of coloumns this Table should have.</param> | |||||
| /// <returns>A new Table inserted before this Table.</returns> | |||||
| /// <example> | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create(@"Test.docx")) | |||||
| /// { | |||||
| /// //Insert a Table into this document. | |||||
| /// Table t = document.InsertTable(2, 2); | |||||
| /// t.Design = TableDesign.LightShadingAccent1; | |||||
| /// t.Alignment = Alignment.center; | |||||
| /// | |||||
| /// // Insert a new Table after this Table. | |||||
| /// Table newTable = t.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. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public 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); | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a Paragraph before this Table, this Paragraph may have come from the same or another document. | |||||
| /// </summary> | |||||
| /// <param name="p">The Paragraph to insert.</param> | |||||
| /// <returns>The Paragraph now associated with this document.</returns> | |||||
| /// <example> | |||||
| /// Take a Paragraph from document a, and insert it into document b before this Table. | |||||
| /// <code> | |||||
| /// // 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 Table in document b. | |||||
| /// Table t = documentB.Tables[0]; | |||||
| /// | |||||
| /// // Insert the Paragraph from document a before this Table. | |||||
| /// Paragraph newParagraph = t.InsertParagraphBeforeSelf(p); | |||||
| /// | |||||
| /// // Save all changes made to document b. | |||||
| /// documentB.Save(); | |||||
| /// }// Release this document from memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph InsertParagraphBeforeSelf(Paragraph p) | |||||
| { | |||||
| xml.AddBeforeSelf(p.xml); | |||||
| XElement newlyInserted = xml.ElementsBeforeSelf().First(); | |||||
| p.xml = newlyInserted; | |||||
| DocX.RebuildParagraphs(document); | |||||
| return p; | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Paragraph before this Table. | |||||
| /// </summary> | |||||
| /// <param name="text">The initial text for this new Paragraph.</param> | |||||
| /// <returns>A new Paragraph inserted before this Table.</returns> | |||||
| /// <example> | |||||
| /// Insert a new Paragraph before the first Table in this document. | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create(@"Test.docx")) | |||||
| /// { | |||||
| /// // Insert a Table into this document. | |||||
| /// Table t = document.InsertTable(2, 2); | |||||
| /// | |||||
| /// t.InsertParagraphBeforeSelf("I was inserted before the next Table."); | |||||
| /// | |||||
| /// // Save all changes made to this new document. | |||||
| /// document.Save(); | |||||
| /// }// Release this new document form memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph InsertParagraphBeforeSelf(string text) | |||||
| { | |||||
| return InsertParagraphBeforeSelf(text, false, new Formatting()); | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Paragraph before this Table. | |||||
| /// </summary> | |||||
| /// <param name="text">The initial text for this new Paragraph.</param> | |||||
| /// <param name="trackChanges">Should this insertion be tracked as a change?</param> | |||||
| /// <returns>A new Paragraph inserted before this Table.</returns> | |||||
| /// <example> | |||||
| /// Insert a new paragraph before the first Table in this document. | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create(@"Test.docx")) | |||||
| /// { | |||||
| /// // Insert a Table into this document. | |||||
| /// Table t = document.InsertTable(2, 2); | |||||
| /// | |||||
| /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false); | |||||
| /// | |||||
| /// // Save all changes made to this new document. | |||||
| /// document.Save(); | |||||
| /// }// Release this new document form memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph InsertParagraphBeforeSelf(string text, bool trackChanges) | |||||
| { | |||||
| return InsertParagraphBeforeSelf(text, trackChanges, new Formatting()); | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Paragraph before this Table. | |||||
| /// </summary> | |||||
| /// <param name="text">The initial text for this new Paragraph.</param> | |||||
| /// <param name="trackChanges">Should this insertion be tracked as a change?</param> | |||||
| /// <param name="formatting">The formatting to apply to this insertion.</param> | |||||
| /// <returns>A new Paragraph inserted before this Table.</returns> | |||||
| /// <example> | |||||
| /// Insert a new paragraph before the first Table in this document. | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create(@"Test.docx")) | |||||
| /// { | |||||
| /// // Insert a Table into this document. | |||||
| /// Table t = document.InsertTable(2, 2); | |||||
| /// | |||||
| /// Formatting boldFormatting = new Formatting(); | |||||
| /// boldFormatting.Bold = true; | |||||
| /// | |||||
| /// t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false, boldFormatting); | |||||
| /// | |||||
| /// // Save all changes made to this new document. | |||||
| /// document.Save(); | |||||
| /// }// Release this new document form memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public 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, -1, newlyInserted); | |||||
| DocX.RebuildParagraphs(document); | |||||
| return p; | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a Paragraph after this Table, this Paragraph may have come from the same or another document. | |||||
| /// </summary> | |||||
| /// <param name="p">The Paragraph to insert.</param> | |||||
| /// <returns>The Paragraph now associated with this document.</returns> | |||||
| /// <example> | |||||
| /// Take a Paragraph from document a, and insert it into document b after this Table. | |||||
| /// <code> | |||||
| /// // 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 Table in document b. | |||||
| /// Table t = documentB.Tables[0]; | |||||
| /// | |||||
| /// // Insert the Paragraph from document a after this Table. | |||||
| /// Paragraph newParagraph = t.InsertParagraphAfterSelf(p); | |||||
| /// | |||||
| /// // Save all changes made to document b. | |||||
| /// documentB.Save(); | |||||
| /// }// Release this document from memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph InsertParagraphAfterSelf(Paragraph p) | |||||
| { | |||||
| xml.AddAfterSelf(p.xml); | |||||
| XElement newlyInserted = xml.ElementsAfterSelf().First(); | |||||
| p.xml = newlyInserted; | |||||
| DocX.RebuildParagraphs(document); | |||||
| return p; | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Paragraph after this Table. | |||||
| /// </summary> | |||||
| /// <param name="text">The initial text for this new Paragraph.</param> | |||||
| /// <param name="trackChanges">Should this insertion be tracked as a change?</param> | |||||
| /// <param name="formatting">The formatting to apply to this insertion.</param> | |||||
| /// <returns>A new Paragraph inserted after this Table.</returns> | |||||
| /// <example> | |||||
| /// Insert a new paragraph after the first Table in this document. | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create(@"Test.docx")) | |||||
| /// { | |||||
| /// // Insert a Table into this document. | |||||
| /// Table t = document.InsertTable(2, 2); | |||||
| /// | |||||
| /// Formatting boldFormatting = new Formatting(); | |||||
| /// boldFormatting.Bold = true; | |||||
| /// | |||||
| /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false, boldFormatting); | |||||
| /// | |||||
| /// // Save all changes made to this new document. | |||||
| /// document.Save(); | |||||
| /// }// Release this new document form memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public 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, -1, newlyInserted); | |||||
| DocX.RebuildParagraphs(document); | |||||
| return p; | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Paragraph after this Table. | |||||
| /// </summary> | |||||
| /// <param name="text">The initial text for this new Paragraph.</param> | |||||
| /// <param name="trackChanges">Should this insertion be tracked as a change?</param> | |||||
| /// <returns>A new Paragraph inserted after this Table.</returns> | |||||
| /// <example> | |||||
| /// Insert a new paragraph after the first Table in this document. | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create(@"Test.docx")) | |||||
| /// { | |||||
| /// // Insert a Table into this document. | |||||
| /// Table t = document.InsertTable(2, 2); | |||||
| /// | |||||
| /// t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false); | |||||
| /// | |||||
| /// // Save all changes made to this new document. | |||||
| /// document.Save(); | |||||
| /// }// Release this new document form memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph InsertParagraphAfterSelf(string text, bool trackChanges) | |||||
| { | |||||
| return InsertParagraphAfterSelf(text, trackChanges, new Formatting()); | |||||
| } | |||||
| /// <summary> | |||||
| /// Insert a new Paragraph after this Table. | |||||
| /// </summary> | |||||
| /// <param name="text">The initial text for this new Paragraph.</param> | |||||
| /// <returns>A new Paragraph inserted after this Table.</returns> | |||||
| /// <example> | |||||
| /// Insert a new Paragraph after the first Table in this document. | |||||
| /// <code> | |||||
| /// // Create a new document. | |||||
| /// using (DocX document = DocX.Create(@"Test.docx")) | |||||
| /// { | |||||
| /// // Insert a Table into this document. | |||||
| /// Table t = document.InsertTable(2, 2); | |||||
| /// | |||||
| /// t.InsertParagraphAfterSelf("I was inserted after the previous Table."); | |||||
| /// | |||||
| /// // Save all changes made to this new document. | |||||
| /// document.Save(); | |||||
| /// }// Release this new document form memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Paragraph InsertParagraphAfterSelf(string text) | |||||
| { | |||||
| return InsertParagraphAfterSelf(text, false, new Formatting()); | |||||
| } | |||||
| } | } | ||||
| /// <summary> | /// <summary> |