Added Getter and Setter functions for Hyperlink. Also added Hyperlink properties for Document, Paragraph, Table, Row and Cell. Added similar functions for Pictures.master
| internal List<PackagePart> footers = new List<PackagePart>(); | internal List<PackagePart> footers = new List<PackagePart>(); | ||||
| // The collection of Paragraphs in this document. | // The collection of Paragraphs in this document. | ||||
| private List<Paragraph> paragraphs = new List<Paragraph>(); | private List<Paragraph> paragraphs = new List<Paragraph>(); | ||||
| // A dictionary of CustomProperties in this document. | |||||
| private Dictionary<string, CustomProperty> customProperties = new Dictionary<string,CustomProperty>(); | |||||
| // A list of Images in this document. | |||||
| private List<Image> images = new List<Image>(); | |||||
| // A collection of Tables in this Paragraph | |||||
| private List<Table> tables = new List<Table>(); | |||||
| #endregion | #endregion | ||||
| #region Internal variables defined foreach DocX object | #region Internal variables defined foreach DocX object | ||||
| /// </summary> | /// </summary> | ||||
| public List<Table> Tables | public List<Table> Tables | ||||
| { | { | ||||
| get { return tables; } | |||||
| get | |||||
| { | |||||
| List<Table> tables = | |||||
| ( | |||||
| from t in mainDoc.Descendants(XName.Get("body", DocX.w.NamespaceName)).Elements() | |||||
| where (t.Name.LocalName == "tbl") | |||||
| select new Table(this, t) | |||||
| ).ToList(); | |||||
| return tables; | |||||
| } | |||||
| } | } | ||||
| /// <summary> | /// <summary> | ||||
| /// <seealso cref="Paragraph.InsertPicture"/> | /// <seealso cref="Paragraph.InsertPicture"/> | ||||
| public List<Image> Images | public List<Image> Images | ||||
| { | { | ||||
| get { return images; } | |||||
| get | |||||
| { | |||||
| PackagePart word_document = package.GetPart(new Uri("/word/document.xml", UriKind.Relative)); | |||||
| PackageRelationshipCollection imageRelationships = word_document.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"); | |||||
| if (imageRelationships.Count() > 0) | |||||
| { | |||||
| return | |||||
| ( | |||||
| from i in imageRelationships | |||||
| select new Image(this, i) | |||||
| ).ToList(); | |||||
| } | |||||
| return new List<Image>(); | |||||
| } | |||||
| } | } | ||||
| /// <summary> | /// <summary> | ||||
| /// <seealso cref="AddCustomProperty"/> | /// <seealso cref="AddCustomProperty"/> | ||||
| public Dictionary<string, CustomProperty> CustomProperties | public Dictionary<string, CustomProperty> CustomProperties | ||||
| { | { | ||||
| get { return customProperties; } | |||||
| get | |||||
| { | |||||
| if (package.PartExists(new Uri("/docProps/custom.xml", UriKind.Relative))) | |||||
| { | |||||
| PackagePart docProps_custom = package.GetPart(new Uri("/docProps/custom.xml", UriKind.Relative)); | |||||
| XDocument customPropDoc; | |||||
| using (TextReader tr = new StreamReader(docProps_custom.GetStream(FileMode.Open, FileAccess.Read))) | |||||
| customPropDoc = XDocument.Load(tr, LoadOptions.PreserveWhitespace); | |||||
| // Get all of the custom properties in this document | |||||
| return | |||||
| ( | |||||
| from p in customPropDoc.Descendants(XName.Get("property", customPropertiesSchema.NamespaceName)) | |||||
| let Name = p.Attribute(XName.Get("name")).Value | |||||
| let Type = p.Descendants().Single().Name.LocalName | |||||
| let Value = p.Descendants().Single().Value | |||||
| select new CustomProperty(Name, Type, Value) | |||||
| ).ToDictionary(p => p.Name, StringComparer.CurrentCultureIgnoreCase); | |||||
| } | |||||
| return new Dictionary<string, CustomProperty>(); | |||||
| } | |||||
| } | } | ||||
| static internal void RebuildParagraphs(DocX document) | static internal void RebuildParagraphs(DocX document) | ||||
| #endregion | #endregion | ||||
| RebuildParagraphs(this); | RebuildParagraphs(this); | ||||
| RebuildTables(this); | |||||
| RebuildImages(this); | |||||
| RebuildCustomProperties(this); | |||||
| } | } | ||||
| /// <summary> | /// <summary> | ||||
| XElement newTable = CreateTable(rowCount, coloumnCount); | XElement newTable = CreateTable(rowCount, coloumnCount); | ||||
| mainDoc.Descendants(XName.Get("body", DocX.w.NamespaceName)).First().Add(newTable); | mainDoc.Descendants(XName.Get("body", DocX.w.NamespaceName)).First().Add(newTable); | ||||
| RebuildTables(this); | |||||
| RebuildParagraphs(this); | RebuildParagraphs(this); | ||||
| return new Table(this, newTable); | return new Table(this, newTable); | ||||
| } | } | ||||
| Table newTable = new Table(this, newXElement); | Table newTable = new Table(this, newXElement); | ||||
| newTable.Design = t.Design; | newTable.Design = t.Design; | ||||
| RebuildTables(this); | |||||
| RebuildParagraphs(this); | RebuildParagraphs(this); | ||||
| return newTable; | return newTable; | ||||
| } | } | ||||
| Table newTable = new Table(this, newXElement); | Table newTable = new Table(this, newXElement); | ||||
| newTable.Design = t.Design; | newTable.Design = t.Design; | ||||
| tables.Add(newTable); | |||||
| return newTable; | return newTable; | ||||
| } | } | ||||
| } | } | ||||
| RebuildParagraphs(this); | RebuildParagraphs(this); | ||||
| RebuildTables(this); | |||||
| return new Table(this, newTable); | |||||
| } | |||||
| static internal void RebuildTables(DocX document) | |||||
| { | |||||
| document.tables = | |||||
| ( | |||||
| from t in document.mainDoc.Descendants(XName.Get("tbl", DocX.w.NamespaceName)) | |||||
| select new Table(document, t) | |||||
| ).ToList(); | |||||
| return new Table(this, newTable); | |||||
| } | } | ||||
| /// <summary> | /// <summary> | ||||
| document.mainDoc = XDocument.Load(tr, LoadOptions.PreserveWhitespace); | document.mainDoc = XDocument.Load(tr, LoadOptions.PreserveWhitespace); | ||||
| RebuildParagraphs(document); | RebuildParagraphs(document); | ||||
| RebuildTables(document); | |||||
| RebuildImages(document); | |||||
| #endregion | |||||
| #region CustomFilePropertiesPart | |||||
| RebuildCustomProperties(document); | |||||
| #endregion | #endregion | ||||
| #region Headers | #region Headers | ||||
| return document; | return document; | ||||
| } | } | ||||
| static internal void RebuildImages(DocX document) | |||||
| { | |||||
| PackagePart word_document = document.package.GetPart(new Uri("/word/document.xml", UriKind.Relative)); | |||||
| PackageRelationshipCollection imageRelationships = word_document.GetRelationshipsByType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"); | |||||
| if (imageRelationships.Count() > 0) | |||||
| { | |||||
| document.images = | |||||
| ( | |||||
| from i in imageRelationships | |||||
| select new Image(document, i) | |||||
| ).ToList(); | |||||
| } | |||||
| } | |||||
| internal static void RebuildCustomProperties(DocX document) | |||||
| { | |||||
| if(document.package.PartExists(new Uri("/docProps/custom.xml", UriKind.Relative))) | |||||
| { | |||||
| PackagePart docProps_custom = document.package.GetPart(new Uri("/docProps/custom.xml", UriKind.Relative)); | |||||
| XDocument customPropDoc; | |||||
| using (TextReader tr = new StreamReader(docProps_custom.GetStream(FileMode.Open, FileAccess.Read))) | |||||
| customPropDoc = XDocument.Load(tr, LoadOptions.PreserveWhitespace); | |||||
| // Get all of the custom properties in this document | |||||
| document.customProperties = | |||||
| ( | |||||
| from p in customPropDoc.Descendants(XName.Get("property", customPropertiesSchema.NamespaceName)) | |||||
| let Name = p.Attribute(XName.Get("name")).Value | |||||
| let Type = p.Descendants().Single().Name.LocalName | |||||
| let Value = p.Descendants().Single().Value | |||||
| select new CustomProperty(Name, Type, Value) | |||||
| ).ToDictionary(p => p.Name, StringComparer.CurrentCultureIgnoreCase); | |||||
| } | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Loads a document into a DocX object using a Stream. | /// Loads a document into a DocX object using a Stream. | ||||
| /// </summary> | /// </summary> | ||||
| return h; | return h; | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Returns a list of Hyperlinks in this document. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get all of the hyperlinks in this document | |||||
| /// List<Hyperlink> hyperlinks = document.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(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public List<Hyperlink> Hyperlinks | |||||
| { | |||||
| get | |||||
| { | |||||
| List<Hyperlink> hyperlinks = new List<Hyperlink>(); | |||||
| foreach (Paragraph p in Paragraphs) | |||||
| hyperlinks.AddRange(p.Hyperlinks); | |||||
| return hyperlinks; | |||||
| } | |||||
| } | |||||
| /// <summary> | |||||
| /// Returns a list of all Pictures in a Document. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Returns a list of all Pictures in a Document. | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get all of the Pictures in this Document. | |||||
| /// List<Picture> pictures = document.Pictures; | |||||
| /// | |||||
| /// // Save this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public List<Picture> Pictures | |||||
| { | |||||
| get | |||||
| { | |||||
| List<Picture> pictures = new List<Picture>(); | |||||
| foreach (Paragraph p in Paragraphs) | |||||
| pictures.AddRange(p.Pictures); | |||||
| return pictures; | |||||
| } | |||||
| } | |||||
| internal void AddHyperlinkStyleIfNotPresent() | internal void AddHyperlinkStyleIfNotPresent() | ||||
| { | { | ||||
| Uri word_styles_Uri = new Uri("/word/styles.xml", UriKind.Relative); | Uri word_styles_Uri = new Uri("/word/styles.xml", UriKind.Relative); | ||||
| .Select(r => r.Id).First(); | .Select(r => r.Id).First(); | ||||
| // Return the Image object | // Return the Image object | ||||
| return images.Where(i => i.Id == id).First(); | |||||
| return Images.Where(i => i.Id == id).First(); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| }// Close the Stream to the new image. | }// Close the Stream to the new image. | ||||
| }// Close the Stream to the new image part. | }// Close the Stream to the new image part. | ||||
| Image newImg = new Image(this, rel); | |||||
| images.Add(newImg); | |||||
| return newImg; | |||||
| return new Image(this, rel); | |||||
| } | } | ||||
| /// <summary> | /// <summary> | ||||
| // Refresh all fields in this document which display this custom property. | // Refresh all fields in this document which display this custom property. | ||||
| UpdateCustomPropertyValue(this, cp.Name, cp.Value.ToString()); | UpdateCustomPropertyValue(this, cp.Name, cp.Value.ToString()); | ||||
| // Get all of the custom properties in this document | |||||
| customProperties = | |||||
| ( | |||||
| from p in customPropDoc.Descendants(XName.Get("property", customPropertiesSchema.NamespaceName)) | |||||
| let Name = p.Attribute(XName.Get("name")).Value | |||||
| let Type = p.Descendants().Single().Name.LocalName | |||||
| let Value = p.Descendants().Single().Value | |||||
| select new CustomProperty(Name, Type, Value) | |||||
| ).ToDictionary(p => p.Name, StringComparer.CurrentCultureIgnoreCase); | |||||
| } | } | ||||
| internal static void CreateCustomPropertiesPart(DocX document) | internal static void CreateCustomPropertiesPart(DocX document) |
| using System.Linq; | using System.Linq; | ||||
| using System.Text; | using System.Text; | ||||
| using System.Xml.Linq; | using System.Xml.Linq; | ||||
| using System.IO.Packaging; | |||||
| namespace Novacode | namespace Novacode | ||||
| { | { | ||||
| /// <summary> | |||||
| /// Represents a Hyperlink in a document. | |||||
| /// </summary> | |||||
| public class Hyperlink: DocXElement | public class Hyperlink: DocXElement | ||||
| { | { | ||||
| private string text; | |||||
| public string Text { get{return text;} set{text = value;} } | |||||
| /// <summary> | |||||
| /// Change the Text of a Hyperlink. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Change the Text of a Hyperlink. | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get all of the hyperlinks in this document | |||||
| /// List<Hyperlink> hyperlinks = document.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(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public string Text | |||||
| { | |||||
| get | |||||
| { | |||||
| // Create a string builder. | |||||
| StringBuilder sb = new StringBuilder(); | |||||
| private Uri uri; | |||||
| public Uri Uri { get{return uri;} set{uri = value;} } | |||||
| // Get all the runs in this Text. | |||||
| var runs = from r in Xml.Elements() | |||||
| where r.Name.LocalName == "r" | |||||
| select new Run(Document, r, 0); | |||||
| // Remove each run. | |||||
| foreach (Run r in runs) | |||||
| sb.Append(r.Value); | |||||
| return sb.ToString(); | |||||
| } | |||||
| set | |||||
| { | |||||
| // Get all the runs in this Text. | |||||
| var runs = from r in Xml.Elements() | |||||
| where r.Name.LocalName == "r" | |||||
| select r; | |||||
| // Remove each run. | |||||
| for (int i = 0; i < runs.Count(); i++) | |||||
| runs.Remove(); | |||||
| XElement rPr = | |||||
| new XElement | |||||
| ( | |||||
| DocX.w + "rPr", | |||||
| new XElement | |||||
| ( | |||||
| DocX.w + "rStyle", | |||||
| new XAttribute(DocX.w + "val", "Hyperlink") | |||||
| ) | |||||
| ); | |||||
| // Format and add the new text. | |||||
| List<XElement> newRuns = DocX.FormatInput(value, rPr); | |||||
| Xml.Add(newRuns); | |||||
| } | |||||
| } | |||||
| // Return the Id of this Hyperlink. | |||||
| internal string GetId() | |||||
| { | |||||
| return Xml.Attribute(DocX.r + "id").Value; | |||||
| } | |||||
| /// <summary> | |||||
| /// Change the Uri of a Hyperlink. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Change the Uri of a Hyperlink. | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get all of the hyperlinks in this document | |||||
| /// List<Hyperlink> hyperlinks = document.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(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public Uri Uri | |||||
| { | |||||
| get | |||||
| { | |||||
| // Get the word\document.xml part | |||||
| PackagePart word_document = Document.package.GetPart(new Uri("/word/document.xml", UriKind.Relative)); | |||||
| // Get the Hyperlink relation based on its Id. | |||||
| PackageRelationship r = word_document.GetRelationship(GetId()); | |||||
| // Return the Hyperlinks Uri. | |||||
| return r.TargetUri; | |||||
| } | |||||
| set | |||||
| { | |||||
| // Get the word\document.xml part | |||||
| PackagePart word_document = Document.package.GetPart(new Uri("/word/document.xml", UriKind.Relative)); | |||||
| // Get the Hyperlink relation based on its Id. | |||||
| PackageRelationship r = word_document.GetRelationship(GetId()); | |||||
| // Get all of the information about this relationship. | |||||
| TargetMode r_tm = r.TargetMode; | |||||
| string r_rt = r.RelationshipType; | |||||
| string r_id = r.Id; | |||||
| // Delete the relationship | |||||
| word_document.DeleteRelationship(r_id); | |||||
| word_document.CreateRelationship(value, r_tm, r_rt, r_id); | |||||
| } | |||||
| } | |||||
| internal Hyperlink(DocX document, XElement i): base(document, i) | internal Hyperlink(DocX document, XElement i): base(document, i) | ||||
| { | { |
| private List<Picture> pictures; | private List<Picture> pictures; | ||||
| /// <summary> | /// <summary> | ||||
| /// Returns a list of Pictures in this Paragraph. | |||||
| /// Returns a list of all Pictures in a Paragraph. | |||||
| /// </summary> | /// </summary> | ||||
| public List<Picture> Pictures { get { return pictures; } } | |||||
| /// <example> | |||||
| /// Returns a list of all Pictures in a Paragraph. | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get the first Paragraph in a document. | |||||
| /// Paragraph p = document.Paragraphs[0]; | |||||
| /// | |||||
| /// // Get all of the Pictures in this Paragraph. | |||||
| /// List<Picture> pictures = p.Pictures; | |||||
| /// | |||||
| /// // Save this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public List<Picture> Pictures | |||||
| { | |||||
| get | |||||
| { | |||||
| List<Picture> pictures = | |||||
| ( | |||||
| from p in Xml.Descendants() | |||||
| where (p.Name.LocalName == "drawing") | |||||
| select new Picture(Document, p) | |||||
| ).ToList(); | |||||
| return pictures; | |||||
| } | |||||
| } | |||||
| /// <summary> | |||||
| /// Returns a list of Hyperlinks in this Paragraph. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// <code> | |||||
| /// // 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. | |||||
| /// List<Hyperlink> 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(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public List<Hyperlink> Hyperlinks | |||||
| { | |||||
| get | |||||
| { | |||||
| List<Hyperlink> hyperlinks = | |||||
| ( | |||||
| from h in Xml.Elements() | |||||
| where (h.Name.LocalName == "hyperlink") | |||||
| select new Hyperlink(Document, h) | |||||
| ).ToList(); | |||||
| return hyperlinks; | |||||
| } | |||||
| } | |||||
| // A collection of field type DocProperty. | // A collection of field type DocProperty. | ||||
| private List<DocProperty> docProperties; | private List<DocProperty> docProperties; |
| private List<Row> rows; | private List<Row> rows; | ||||
| private int rowCount, columnCount; | private int rowCount, columnCount; | ||||
| /// <summary> | |||||
| /// Returns a list of all Pictures in a Table. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Returns a list of all Pictures in a Table. | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get the first Table in a document. | |||||
| /// Table t = document.Tables[0]; | |||||
| /// | |||||
| /// // Get all of the Pictures in this Table. | |||||
| /// List<Picture> pictures = t.Pictures; | |||||
| /// | |||||
| /// // Save this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public List<Picture> Pictures | |||||
| { | |||||
| get | |||||
| { | |||||
| List<Picture> pictures = new List<Picture>(); | |||||
| foreach (Row r in Rows) | |||||
| pictures.AddRange(r.Pictures); | |||||
| return pictures; | |||||
| } | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Set the direction of all content in this Table. | /// Set the direction of all content in this Table. | ||||
| /// </summary> | /// </summary> | ||||
| r.SetDirection(direction); | r.SetDirection(direction); | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Get all of the Hyperlinks in this Table. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Get all of the Hyperlinks in this Table. | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get the first Table in this document. | |||||
| /// Table t = document.Tables[0]; | |||||
| /// | |||||
| /// // Get a list of all Hyperlinks in this Table. | |||||
| /// List<Hyperlink> hyperlinks = t.Hyperlinks; | |||||
| /// | |||||
| /// // Save this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public List<Hyperlink> Hyperlinks | |||||
| { | |||||
| get | |||||
| { | |||||
| List<Hyperlink> hyperlinks = new List<Hyperlink>(); | |||||
| foreach (Row r in Rows) | |||||
| hyperlinks.AddRange(r.Hyperlinks); | |||||
| return hyperlinks; | |||||
| } | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// If the tblPr element doesent exist it is created, either way it is returned by this function. | /// If the tblPr element doesent exist it is created, either way it is returned by this function. | ||||
| /// </summary> | /// </summary> | ||||
| select new Cell(document, c)).ToList(); | select new Cell(document, c)).ToList(); | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Returns a list of all Pictures in a Row. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Returns a list of all Pictures in a Row. | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get the first Table in a document. | |||||
| /// Table t = document.Tables[0]; | |||||
| /// | |||||
| /// // Get the first Row in a Table. | |||||
| /// Row r = t.Rows[0]; | |||||
| /// | |||||
| /// // Get all of the Pictures in this Row. | |||||
| /// List<Picture> pictures = r.Pictures; | |||||
| /// | |||||
| /// // Save this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public List<Picture> Pictures | |||||
| { | |||||
| get | |||||
| { | |||||
| List<Picture> pictures = new List<Picture>(); | |||||
| foreach (Cell c in Cells) | |||||
| pictures.AddRange(c.Pictures); | |||||
| return pictures; | |||||
| } | |||||
| } | |||||
| /// <summary> | |||||
| /// Get all of the Hyperlinks in this Row. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Get all of the Hyperlinks in this Row. | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get the first Table in this document. | |||||
| /// Table t = document.Tables[0]; | |||||
| /// | |||||
| /// // Get the first Row in this Table. | |||||
| /// Row r = t.Rows[0]; | |||||
| /// | |||||
| /// // Get a list of all Hyperlinks in this Row. | |||||
| /// List<Hyperlink> hyperlinks = r.Hyperlinks; | |||||
| /// | |||||
| /// // Save this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public List<Hyperlink> Hyperlinks | |||||
| { | |||||
| get | |||||
| { | |||||
| List<Hyperlink> hyperlinks = new List<Hyperlink>(); | |||||
| foreach (Cell c in Cells) | |||||
| hyperlinks.AddRange(c.Hyperlinks); | |||||
| return hyperlinks; | |||||
| } | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Set the content direction of a single Row in a Table. | /// Set the content direction of a single Row in a Table. | ||||
| /// </summary> | /// </summary> | ||||
| paragraphs = xml.Elements(XName.Get("p", DocX.w.NamespaceName)).Select(p => new Paragraph(document, p, 0)).ToList(); | paragraphs = xml.Elements(XName.Get("p", DocX.w.NamespaceName)).Select(p => new Paragraph(document, p, 0)).ToList(); | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Returns a list of all Pictures in a Cell. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Returns a list of all Pictures in a Cell. | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get the first Table in a document. | |||||
| /// Table t = document.Tables[0]; | |||||
| /// | |||||
| /// // Get the first Row in a Table. | |||||
| /// Row r = t.Rows[0]; | |||||
| /// | |||||
| /// // Get the first Cell in a Row. | |||||
| /// Cell c = r.Cells[0]; | |||||
| /// | |||||
| /// // Get all of the Pictures in this Cell. | |||||
| /// List<Picture> pictures = c.Pictures; | |||||
| /// | |||||
| /// // Save this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public List<Picture> Pictures | |||||
| { | |||||
| get | |||||
| { | |||||
| List<Picture> pictures = new List<Picture>(); | |||||
| foreach (Paragraph p in Paragraphs) | |||||
| pictures.AddRange(p.Pictures); | |||||
| return pictures; | |||||
| } | |||||
| } | |||||
| /// <summary> | |||||
| /// Get all of the Hyperlinks in this Cell. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// Get all of the Hyperlinks in this Cell. | |||||
| /// <code> | |||||
| /// // Create a document. | |||||
| /// using (DocX document = DocX.Load(@"Test.docx")) | |||||
| /// { | |||||
| /// // Get the first Table in this document. | |||||
| /// Table t = document.Tables[0]; | |||||
| /// | |||||
| /// // Get the first Row in this Table. | |||||
| /// Row r = t.Rows[0]; | |||||
| /// | |||||
| /// // Get the first Cell in this Row. | |||||
| /// Cell c = r.Cells[0]; | |||||
| /// | |||||
| /// // Get a list of all Hyperlinks in this Cell. | |||||
| /// List<Hyperlink> hyperlinks = c.Hyperlinks; | |||||
| /// | |||||
| /// // Save this document. | |||||
| /// document.Save(); | |||||
| /// } | |||||
| /// </code> | |||||
| /// </example> | |||||
| public List<Hyperlink> Hyperlinks | |||||
| { | |||||
| get | |||||
| { | |||||
| List<Hyperlink> hyperlinks = new List<Hyperlink>(); | |||||
| foreach (Paragraph p in Paragraphs) | |||||
| hyperlinks.AddRange(p.Hyperlinks); | |||||
| return hyperlinks; | |||||
| } | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Set the content direction of a single Cell in a Table. | /// Set the content direction of a single Cell in a Table. | ||||
| /// </summary> | /// </summary> |
| Xml.AddAfterSelf(newTable); | Xml.AddAfterSelf(newTable); | ||||
| XElement newlyInserted = Xml.ElementsAfterSelf().First(); | XElement newlyInserted = Xml.ElementsAfterSelf().First(); | ||||
| DocX.RebuildTables(Document); | |||||
| ////DocX.RebuildTables(Document); | |||||
| DocX.RebuildParagraphs(Document); | DocX.RebuildParagraphs(Document); | ||||
| return new Table(Document, newlyInserted); | return new Table(Document, newlyInserted); | ||||
| } | } | ||||
| XElement newlyInserted = Xml.ElementsAfterSelf().First(); | XElement newlyInserted = Xml.ElementsAfterSelf().First(); | ||||
| t.Xml = newlyInserted; | t.Xml = newlyInserted; | ||||
| DocX.RebuildTables(Document); | |||||
| //DocX.RebuildTables(Document); | |||||
| DocX.RebuildParagraphs(Document); | DocX.RebuildParagraphs(Document); | ||||
| return t; | return t; | ||||
| Xml.AddBeforeSelf(newTable); | Xml.AddBeforeSelf(newTable); | ||||
| XElement newlyInserted = Xml.ElementsBeforeSelf().First(); | XElement newlyInserted = Xml.ElementsBeforeSelf().First(); | ||||
| DocX.RebuildTables(Document); | |||||
| //DocX.RebuildTables(Document); | |||||
| DocX.RebuildParagraphs(Document); | DocX.RebuildParagraphs(Document); | ||||
| return new Table(Document, newlyInserted); | return new Table(Document, newlyInserted); | ||||
| } | } | ||||
| XElement newlyInserted = Xml.ElementsBeforeSelf().First(); | XElement newlyInserted = Xml.ElementsBeforeSelf().First(); | ||||
| t.Xml = newlyInserted; | t.Xml = newlyInserted; | ||||
| DocX.RebuildTables(Document); | |||||
| //DocX.RebuildTables(Document); | |||||
| DocX.RebuildParagraphs(Document); | DocX.RebuildParagraphs(Document); | ||||
| return t; | return t; |