| using System; | |||||
| using System.Linq; | |||||
| using System.Collections.Generic; | |||||
| namespace Novacode | |||||
| namespace Novacode | |||||
| { | { | ||||
| public class Bookmark | public class Bookmark | ||||
| { | { |
| using System; | |||||
| using System.Linq; | |||||
| using System.Linq; | |||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Xml.Linq; | using System.Xml.Linq; | ||||
| public void SetText(string newText) | public void SetText(string newText) | ||||
| { | { | ||||
| string x = this.Tag; | |||||
| XElement el = Xml; | |||||
| Xml.Descendants(XName.Get("t", DocX.w.NamespaceName)).First().Value = newText; | Xml.Descendants(XName.Get("t", DocX.w.NamespaceName)).First().Value = newText; | ||||
| } | } | ||||
| } | } |
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Linq; | using System.Linq; | ||||
| using System.Text; | |||||
| namespace Novacode | namespace Novacode | ||||
| { | { |
| { | { | ||||
| public class CustomProperty | public class CustomProperty | ||||
| { | { | ||||
| private string name; | |||||
| private object value; | |||||
| private string type; | |||||
| /// <summary> | /// <summary> | ||||
| /// The name of this CustomProperty. | /// The name of this CustomProperty. | ||||
| /// </summary> | /// </summary> | ||||
| public string Name { get { return name;} } | |||||
| public string Name { get; } | |||||
| /// <summary> | /// <summary> | ||||
| /// The value of this CustomProperty. | /// The value of this CustomProperty. | ||||
| /// </summary> | /// </summary> | ||||
| public object Value { get { return value; } } | |||||
| public object Value { get; } | |||||
| internal string Type { get { return type; } } | |||||
| internal string Type { get; } | |||||
| internal CustomProperty(string name, string type, string value) | internal CustomProperty(string name, string type, string value) | ||||
| { | { | ||||
| default: throw new Exception(); | default: throw new Exception(); | ||||
| } | } | ||||
| this.name = name; | |||||
| this.type = type; | |||||
| this.value = realValue; | |||||
| Name = name; | |||||
| Type = type; | |||||
| Value = realValue; | |||||
| } | } | ||||
| private CustomProperty(string name, string type, object value) | private CustomProperty(string name, string type, object value) | ||||
| { | { | ||||
| this.name = name; | |||||
| this.type = type; | |||||
| this.value = value; | |||||
| Name = name; | |||||
| Type = type; | |||||
| Value = value; | |||||
| } | } | ||||
| /// <summary> | /// <summary> | ||||
| /// </summary> | /// </summary> | ||||
| /// <param name="name">The name of this CustomProperty.</param> | /// <param name="name">The name of this CustomProperty.</param> | ||||
| /// <param name="value">The value of this CustomProperty.</param> | /// <param name="value">The value of this CustomProperty.</param> | ||||
| public CustomProperty(string name, int value) : this(name, "i4", value as object) { } | |||||
| public CustomProperty(string name, int value) : this(name, "i4", value) { } | |||||
| /// <summary> | /// <summary> | ||||
| /// </summary> | /// </summary> | ||||
| /// <param name="name">The name of this CustomProperty.</param> | /// <param name="name">The name of this CustomProperty.</param> | ||||
| /// <param name="value">The value of this CustomProperty.</param> | /// <param name="value">The value of this CustomProperty.</param> | ||||
| public CustomProperty(string name, double value) : this(name, "r8", value as object) { } | |||||
| public CustomProperty(string name, double value) : this(name, "r8", value) { } | |||||
| /// <summary> | /// <summary> | ||||
| /// </summary> | /// </summary> | ||||
| /// <param name="name">The name of this CustomProperty.</param> | /// <param name="name">The name of this CustomProperty.</param> | ||||
| /// <param name="value">The value of this CustomProperty.</param> | /// <param name="value">The value of this CustomProperty.</param> | ||||
| public CustomProperty(string name, DateTime value) : this(name, "filetime", value.ToUniversalTime() as object) { } | |||||
| public CustomProperty(string name, DateTime value) : this(name, "filetime", value.ToUniversalTime()) { } | |||||
| /// <summary> | /// <summary> | ||||
| /// Create a new CustomProperty to hold a bool. | /// Create a new CustomProperty to hold a bool. | ||||
| /// </summary> | /// </summary> | ||||
| /// <param name="name">The name of this CustomProperty.</param> | /// <param name="name">The name of this CustomProperty.</param> | ||||
| /// <param name="value">The value of this CustomProperty.</param> | /// <param name="value">The value of this CustomProperty.</param> | ||||
| public CustomProperty(string name, bool value) : this(name, "bool", value as object) { } | |||||
| public CustomProperty(string name, bool value) : this(name, "bool", value) { } | |||||
| } | } | ||||
| } | } |
| public class DocProperty: DocXElement | public class DocProperty: DocXElement | ||||
| { | { | ||||
| internal Regex extractName = new Regex(@"DOCPROPERTY (?<name>.*) "); | internal Regex extractName = new Regex(@"DOCPROPERTY (?<name>.*) "); | ||||
| private string name; | |||||
| /// <summary> | /// <summary> | ||||
| /// The custom property to display. | /// The custom property to display. | ||||
| /// </summary> | /// </summary> | ||||
| public string Name { get { return name; } } | |||||
| public string Name { get; } | |||||
| internal DocProperty(DocX document, XElement xml):base(document, xml) | internal DocProperty(DocX document, XElement xml):base(document, xml) | ||||
| { | { | ||||
| string instr = Xml.Attribute(XName.Get("instr", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")).Value; | string instr = Xml.Attribute(XName.Get("instr", "http://schemas.openxmlformats.org/wordprocessingml/2006/main")).Value; | ||||
| this.name = extractName.Match(instr.Trim()).Groups["name"].Value; | |||||
| Name = extractName.Match(instr.Trim()).Groups["name"].Value; | |||||
| } | } | ||||
| } | } | ||||
| } | } |
| using System; | |||||
| using System.Linq; | |||||
| using System.Collections.Generic; | |||||
| namespace Novacode | |||||
| namespace Novacode | |||||
| { | { | ||||
| public enum DocumentTypes | public enum DocumentTypes | ||||
| { | { |
| using System; | using System; | ||||
| using System.Collections.Generic; | |||||
| using System.ComponentModel; | using System.ComponentModel; | ||||
| using System.IO; | |||||
| using System.Linq; | |||||
| using System.Reflection; | using System.Reflection; | ||||
| using System.Text; | |||||
| using System.Xml.Linq; | |||||
| using Novacode; | |||||
| namespace Novacode | namespace Novacode | ||||
| { | { | ||||
| { | { | ||||
| return enumAttributes[0].Description; | return enumAttributes[0].Description; | ||||
| } | } | ||||
| else | |||||
| { | |||||
| return enumValue.ToString(); | |||||
| } | |||||
| return enumValue.ToString(); | |||||
| } | } | ||||
| /// <summary> | /// <summary> | ||||
| } | } | ||||
| } | } | ||||
| } | } |
| using System.Security.Principal; | using System.Security.Principal; | ||||
| using System.Text; | using System.Text; | ||||
| using System.Xml.Linq; | using System.Xml.Linq; | ||||
| using System.Xml; | |||||
| namespace Novacode | namespace Novacode | ||||
| { | { |
| using System; | using System; | ||||
| using System.IO; | |||||
| using System.Linq; | using System.Linq; | ||||
| using System.Drawing; | using System.Drawing; | ||||
| using System.Xml.Linq; | using System.Xml.Linq; | ||||
| { | { | ||||
| var element = this.GetOrCreate_pPr(); | var element = this.GetOrCreate_pPr(); | ||||
| var styleElement = element.Element(XName.Get("pStyle", DocX.w.NamespaceName)); | var styleElement = element.Element(XName.Get("pStyle", DocX.w.NamespaceName)); | ||||
| if (styleElement != null) | |||||
| var attr = styleElement?.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| if (!string.IsNullOrEmpty(attr?.Value)) | |||||
| { | { | ||||
| var attr = styleElement.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| if (attr != null && !string.IsNullOrEmpty(attr.Value)) | |||||
| { | |||||
| return attr.Value; | |||||
| } | |||||
| return attr.Value; | |||||
| } | } | ||||
| return "Normal"; | return "Normal"; | ||||
| } | } | ||||
| XElement pPr = GetOrCreate_pPr(); | XElement pPr = GetOrCreate_pPr(); | ||||
| XElement bidi = pPr.Element(XName.Get("bidi", DocX.w.NamespaceName)); | XElement bidi = pPr.Element(XName.Get("bidi", DocX.w.NamespaceName)); | ||||
| if (bidi == null) | |||||
| return Direction.LeftToRight; | |||||
| else | |||||
| return Direction.RightToLeft; | |||||
| return bidi == null ? Direction.LeftToRight : Direction.RightToLeft; | |||||
| } | } | ||||
| set | set | ||||
| if (bidi == null) | if (bidi == null) | ||||
| pPr.Add(new XElement(XName.Get("bidi", DocX.w.NamespaceName))); | pPr.Add(new XElement(XName.Get("bidi", DocX.w.NamespaceName))); | ||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| if (bidi != null) | |||||
| bidi.Remove(); | |||||
| bidi?.Remove(); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| { | { | ||||
| pPr.Add(new XElement(XName.Get("keepLines", DocX.w.NamespaceName))); | pPr.Add(new XElement(XName.Get("keepLines", DocX.w.NamespaceName))); | ||||
| } | } | ||||
| if (!keepTogether && keepLinesE != null) | |||||
| if (!keepTogether) | |||||
| { | { | ||||
| keepLinesE.Remove(); | |||||
| keepLinesE?.Remove(); | |||||
| } | } | ||||
| return this; | return this; | ||||
| } | } | ||||
| { | { | ||||
| get | get | ||||
| { | { | ||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| GetOrCreate_pPr(); | |||||
| XElement ind = GetOrCreate_pPr_ind(); | XElement ind = GetOrCreate_pPr_ind(); | ||||
| XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName)); | XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName)); | ||||
| // Paragraph can either be firstLine or hanging (Remove hanging). | // Paragraph can either be firstLine or hanging (Remove hanging). | ||||
| XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName)); | XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName)); | ||||
| if (hanging != null) | |||||
| hanging.Remove(); | |||||
| hanging?.Remove(); | |||||
| string indentation = ((indentationFirstLine / 0.1) * 57).ToString(); | string indentation = ((indentationFirstLine / 0.1) * 57).ToString(); | ||||
| XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName)); | XAttribute firstLine = ind.Attribute(XName.Get("firstLine", DocX.w.NamespaceName)); | ||||
| { | { | ||||
| get | get | ||||
| { | { | ||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| GetOrCreate_pPr(); | |||||
| XElement ind = GetOrCreate_pPr_ind(); | XElement ind = GetOrCreate_pPr_ind(); | ||||
| XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName)); | XAttribute hanging = ind.Attribute(XName.Get("hanging", DocX.w.NamespaceName)); | ||||
| } | } | ||||
| } | } | ||||
| private float indentationAfter = 0.0f; | |||||
| private float indentationAfter; | |||||
| /// <summary> | /// <summary> | ||||
| /// Set the after indentation in cm for this Paragraph. | /// Set the after indentation in cm for this Paragraph. | ||||
| /// </summary> | /// </summary> | ||||
| { | { | ||||
| get | get | ||||
| { | { | ||||
| XElement pPr = GetOrCreate_pPr(); | |||||
| GetOrCreate_pPr(); | |||||
| XElement ind = GetOrCreate_pPr_ind(); | XElement ind = GetOrCreate_pPr_ind(); | ||||
| XAttribute right = ind.Attribute(XName.Get("right", DocX.w.NamespaceName)); | XAttribute right = ind.Attribute(XName.Get("right", DocX.w.NamespaceName)); |
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.IO.Packaging; | |||||
| using System.Xml.Linq; | using System.Xml.Linq; | ||||
| namespace Novacode | namespace Novacode |
| * Get the tcPr (table cell properties) element for the first cell in this merge, | * Get the tcPr (table cell properties) element for the first cell in this merge, | ||||
| * null will be returned if no such element exists. | * null will be returned if no such element exists. | ||||
| */ | */ | ||||
| XElement start_tcPr = null; | |||||
| XElement start_tcPr; | |||||
| if (columnIndex > Rows[startRow].Cells.Count) | if (columnIndex > Rows[startRow].Cells.Count) | ||||
| start_tcPr = Rows[startRow].Cells[Rows[startRow].Cells.Count - 1].Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | start_tcPr = Rows[startRow].Cells[Rows[startRow].Cells.Count - 1].Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| else | else | ||||
| { | { | ||||
| get | get | ||||
| { | { | ||||
| List<Paragraph> paragraphs = new List<Paragraph>(); | |||||
| var paragraphs = new List<Paragraph>(); | |||||
| foreach (Row r in Rows) | foreach (Row r in Rows) | ||||
| paragraphs.AddRange(r.Paragraphs); | paragraphs.AddRange(r.Paragraphs); | ||||
| { | { | ||||
| get | get | ||||
| { | { | ||||
| List<Picture> pictures = new List<Picture>(); | |||||
| var pictures = new List<Picture>(); | |||||
| foreach (Row r in Rows) | foreach (Row r in Rows) | ||||
| pictures.AddRange(r.Pictures); | pictures.AddRange(r.Pictures); | ||||
| { | { | ||||
| get | get | ||||
| { | { | ||||
| List<Hyperlink> hyperlinks = new List<Hyperlink>(); | |||||
| var hyperlinks = new List<Hyperlink>(); | |||||
| foreach (Row r in Rows) | foreach (Row r in Rows) | ||||
| hyperlinks.AddRange(r.Hyperlinks); | hyperlinks.AddRange(r.Hyperlinks); | ||||
| } | } | ||||
| // remove all existing values | // remove all existing values | ||||
| grid.RemoveAll(); | |||||
| grid?.RemoveAll(); | |||||
| // append new column widths | // append new column widths | ||||
| XElement gridCol = null; | |||||
| Int32 i = 0; | Int32 i = 0; | ||||
| Double value = width; | |||||
| Double total = 0; | |||||
| foreach (var w in widths) | foreach (var w in widths) | ||||
| { | { | ||||
| value = w; | |||||
| double value = w; | |||||
| if (i == index) value = width; | if (i == index) value = width; | ||||
| gridCol = new XElement(XName.Get("gridCol", DocX.w.NamespaceName), | |||||
| new XAttribute(XName.Get("w", DocX.w.NamespaceName), value)); | |||||
| grid.Add(gridCol); | |||||
| var gridCol = new XElement(XName.Get("gridCol", DocX.w.NamespaceName), | |||||
| new XAttribute(XName.Get("w", DocX.w.NamespaceName), value)); | |||||
| grid?.Add(gridCol); | |||||
| i += 1; | i += 1; | ||||
| total += value; | |||||
| } | } | ||||
| // remove cell widths | // remove cell widths | ||||
| { | { | ||||
| get | get | ||||
| { | { | ||||
| List<Double> widths = new List<Double>(); | |||||
| var widths = new List<Double>(); | |||||
| // get the table grid props | // get the table grid props | ||||
| XElement grid = Xml.Element(XName.Get("tblGrid", DocX.w.NamespaceName)); | XElement grid = Xml.Element(XName.Get("tblGrid", DocX.w.NamespaceName)); | ||||
| if (grid == null) return null; | |||||
| // get col properties | // get col properties | ||||
| var cols = grid.Elements(XName.Get("gridCol", DocX.w.NamespaceName)); | |||||
| var cols = grid?.Elements(XName.Get("gridCol", DocX.w.NamespaceName)); | |||||
| if (cols == null) return null; | if (cols == null) return null; | ||||
| String value = String.Empty; | |||||
| foreach (var col in cols) | foreach (var col in cols) | ||||
| { | { | ||||
| value = col.GetAttribute(XName.Get("w", DocX.w.NamespaceName)); | |||||
| string value = col.GetAttribute(XName.Get("w", DocX.w.NamespaceName)); | |||||
| widths.Add(Convert.ToDouble(value)); | widths.Add(Convert.ToDouble(value)); | ||||
| } | } | ||||
| return widths; | return widths; | ||||
| XElement properties = xml.Element(XName.Get("tblPr", DocX.w.NamespaceName)); | XElement properties = xml.Element(XName.Get("tblPr", DocX.w.NamespaceName)); | ||||
| XElement style = properties.Element(XName.Get("tblStyle", DocX.w.NamespaceName)); | |||||
| XElement style = properties?.Element(XName.Get("tblStyle", DocX.w.NamespaceName)); | |||||
| if (style != null) | if (style != null) | ||||
| { | { | ||||
| XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName)); | XAttribute val = style.Attribute(XName.Get("val", DocX.w.NamespaceName)); | ||||
| else | else | ||||
| design = TableDesign.None; | design = TableDesign.None; | ||||
| XElement tableLook = properties.Element(XName.Get("tblLook", DocX.w.NamespaceName)); | |||||
| XElement tableLook = properties?.Element(XName.Get("tblLook", DocX.w.NamespaceName)); | |||||
| if (tableLook != null) | if (tableLook != null) | ||||
| { | { | ||||
| TableLook = new TableLook(); | |||||
| TableLook.FirstRow = tableLook.GetAttribute(XName.Get("firstRow", DocX.w.NamespaceName)) == "1"; | |||||
| TableLook.LastRow = tableLook.GetAttribute(XName.Get("lastRow", DocX.w.NamespaceName)) == "1"; | |||||
| TableLook.FirstColumn = tableLook.GetAttribute(XName.Get("firstColumn", DocX.w.NamespaceName)) == "1"; | |||||
| TableLook.LastColumn = tableLook.GetAttribute(XName.Get("lastColumn", DocX.w.NamespaceName)) == "1"; | |||||
| TableLook.NoHorizontalBanding = tableLook.GetAttribute(XName.Get("noHBand", DocX.w.NamespaceName)) == "1"; | |||||
| TableLook.NoVerticalBanding = tableLook.GetAttribute(XName.Get("noVBand", DocX.w.NamespaceName)) == "1"; | |||||
| TableLook = new TableLook | |||||
| { | |||||
| FirstRow = tableLook.GetAttribute(XName.Get("firstRow", DocX.w.NamespaceName)) == "1", | |||||
| LastRow = tableLook.GetAttribute(XName.Get("lastRow", DocX.w.NamespaceName)) == "1", | |||||
| FirstColumn = tableLook.GetAttribute(XName.Get("firstColumn", DocX.w.NamespaceName)) == "1", | |||||
| LastColumn = tableLook.GetAttribute(XName.Get("lastColumn", DocX.w.NamespaceName)) == "1", | |||||
| NoHorizontalBanding = tableLook.GetAttribute(XName.Get("noHBand", DocX.w.NamespaceName)) == "1", | |||||
| NoVerticalBanding = tableLook.GetAttribute(XName.Get("noVBand", DocX.w.NamespaceName)) == "1" | |||||
| }; | |||||
| } | } | ||||
| } | } | ||||
| get | get | ||||
| { | { | ||||
| XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName)); | XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName)); | ||||
| if (tblPr != null) | |||||
| XElement caption = tblPr?.Element(XName.Get("tblCaption", DocX.w.NamespaceName)); | |||||
| if (caption != null) | |||||
| { | { | ||||
| XElement caption = tblPr.Element(XName.Get("tblCaption", DocX.w.NamespaceName)); | |||||
| if (caption != null) | |||||
| { | |||||
| _tableCaption = caption.GetAttribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| } | |||||
| _tableCaption = caption.GetAttribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| } | } | ||||
| return _tableCaption; | return _tableCaption; | ||||
| } | } | ||||
| XElement tblDescription = | XElement tblDescription = | ||||
| tblPr.Descendants(XName.Get("tblDescription", DocX.w.NamespaceName)).FirstOrDefault(); | tblPr.Descendants(XName.Get("tblDescription", DocX.w.NamespaceName)).FirstOrDefault(); | ||||
| if (tblDescription != null) | |||||
| tblDescription.Remove(); | |||||
| tblDescription?.Remove(); | |||||
| tblDescription = new XElement(XName.Get("tblDescription", DocX.w.NamespaceName), | tblDescription = new XElement(XName.Get("tblDescription", DocX.w.NamespaceName), | ||||
| new XAttribute(XName.Get("val", DocX.w.NamespaceName), value)); | new XAttribute(XName.Get("val", DocX.w.NamespaceName), value)); | ||||
| get | get | ||||
| { | { | ||||
| XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName)); | XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName)); | ||||
| if (tblPr != null) | |||||
| XElement caption = tblPr?.Element(XName.Get("tblDescription", DocX.w.NamespaceName)); | |||||
| if (caption != null) | |||||
| { | { | ||||
| XElement caption = tblPr.Element(XName.Get("tblDescription", DocX.w.NamespaceName)); | |||||
| if (caption != null) | |||||
| { | |||||
| _tableDescription = caption.GetAttribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| } | |||||
| _tableDescription = caption.GetAttribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| } | } | ||||
| return _tableDescription; | return _tableDescription; | ||||
| } | } | ||||
| XElement tblPr = Xml.Descendants(XName.Get("tblPr", DocX.w.NamespaceName)).First(); | XElement tblPr = Xml.Descendants(XName.Get("tblPr", DocX.w.NamespaceName)).First(); | ||||
| XElement jc = tblPr.Descendants(XName.Get("jc", DocX.w.NamespaceName)).FirstOrDefault(); | XElement jc = tblPr.Descendants(XName.Get("jc", DocX.w.NamespaceName)).FirstOrDefault(); | ||||
| if (jc != null) | |||||
| jc.Remove(); | |||||
| jc?.Remove(); | |||||
| jc = new XElement(XName.Get("jc", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), alignmentString)); | jc = new XElement(XName.Get("jc", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), alignmentString)); | ||||
| tblPr.Add(jc); | tblPr.Add(jc); | ||||
| if (tblLayout == null) | if (tblLayout == null) | ||||
| { | { | ||||
| XElement tmp = tblPr.Element(XName.Get("tblInd", DocX.w.NamespaceName)); | |||||
| if (tmp == null) | |||||
| { | |||||
| tmp = tblPr.Element(XName.Get("tblW", DocX.w.NamespaceName)); | |||||
| } | |||||
| XElement tmp = tblPr.Element(XName.Get("tblInd", DocX.w.NamespaceName)) ?? | |||||
| tblPr.Element(XName.Get("tblW", DocX.w.NamespaceName)); | |||||
| tmp.AddAfterSelf(new XElement(XName.Get("tblLayout", DocX.w.NamespaceName))); | tmp.AddAfterSelf(new XElement(XName.Get("tblLayout", DocX.w.NamespaceName))); | ||||
| tmp = tblPr.Element(XName.Get("tblLayout", DocX.w.NamespaceName)); | tmp = tblPr.Element(XName.Get("tblLayout", DocX.w.NamespaceName)); | ||||
| design = value; | design = value; | ||||
| if (design == TableDesign.None) | if (design == TableDesign.None) | ||||
| { | |||||
| if (style != null) | |||||
| style.Remove(); | |||||
| } | |||||
| style.Remove(); | |||||
| if (design == TableDesign.Custom) | if (design == TableDesign.Custom) | ||||
| { | { | ||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| switch (design) | switch (design) | ||||
| { | { | ||||
| case TableDesign.TableNormal: | case TableDesign.TableNormal: | ||||
| case TableDesign.ColorfulGridAccent6: | case TableDesign.ColorfulGridAccent6: | ||||
| val.Value = "ColorfulGrid-Accent6"; | val.Value = "ColorfulGrid-Accent6"; | ||||
| break; | break; | ||||
| default: | |||||
| break; | |||||
| } | } | ||||
| } | } | ||||
| if (Document.styles == null) | if (Document.styles == null) | ||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| bool directionTest = true; | |||||
| var positionIndex = 1; | var positionIndex = 1; | ||||
| var actualPosition = 1; | var actualPosition = 1; | ||||
| var gridAfterVal = 0; | var gridAfterVal = 0; | ||||
| if ((index - gridAfterVal) >= actualPosition | if ((index - gridAfterVal) >= actualPosition | ||||
| && (index - gridAfterVal) <= (actualPosition + gridSpanVal)) | && (index - gridAfterVal) <= (actualPosition + gridSpanVal)) | ||||
| { | { | ||||
| if (index == (actualPosition + gridSpanVal) && direction == true) | |||||
| bool directionTest; | |||||
| if (index == (actualPosition + gridSpanVal) && direction) | |||||
| { | { | ||||
| directionTest = true; | directionTest = true; | ||||
| } | } | ||||
| /// <param name="celIndex">index of the cell you want to remove</param> | /// <param name="celIndex">index of the cell you want to remove</param> | ||||
| public void DeleteAndShiftCellsLeft(int rowIndex, int celIndex) | public void DeleteAndShiftCellsLeft(int rowIndex, int celIndex) | ||||
| { | { | ||||
| XAttribute gridAfterVal = new XAttribute(XName.Get("val", DocX.w.NamespaceName), 0); | |||||
| var trPr = Rows[rowIndex].Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | var trPr = Rows[rowIndex].Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | ||||
| if (trPr != null) | if (trPr != null) | ||||
| { | { | ||||
| if (gridAfter != null) | if (gridAfter != null) | ||||
| { | { | ||||
| var val = gridAfter.Attribute(XName.Get("val", DocX.w.NamespaceName)); | var val = gridAfter.Attribute(XName.Get("val", DocX.w.NamespaceName)); | ||||
| if (val != null) | |||||
| { | |||||
| val.Value = (int.Parse(val.Value) + 1).ToString(); | |||||
| } | |||||
| else | |||||
| { | |||||
| val.Value = "1"; | |||||
| } | |||||
| val.Value = (int.Parse(val.Value) + 1).ToString(); | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| var gridAfterElement = new XElement("gridAfter"); | |||||
| var gridAfterValAttribute = new XAttribute("val", 1); | |||||
| gridAfter.SetAttributeValue("val", 1); | gridAfter.SetAttributeValue("val", 1); | ||||
| } | } | ||||
| } | } | ||||
| * Get the 'borderType' (table border) element for this Table, | * Get the 'borderType' (table border) element for this Table, | ||||
| * null will be return if no such element exists. | * null will be return if no such element exists. | ||||
| */ | */ | ||||
| string tbordertype; | |||||
| tbordertype = borderType.ToString(); | |||||
| var tbordertype = borderType.ToString(); | |||||
| // only lower the first char of string (because of insideH and insideV) | // only lower the first char of string (because of insideH and insideV) | ||||
| tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1); | tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1); | ||||
| // instance with default border values | // instance with default border values | ||||
| Border b = new Border(); | Border b = new Border(); | ||||
| /* | |||||
| * Get the tblPr (table properties) element for this Table, | |||||
| * null will be return if no such element exists. | |||||
| */ | |||||
| // Get the tblPr (table properties) element for this Table, | |||||
| // null will be return if no such element exists. | |||||
| XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName)); | XElement tblPr = Xml.Element(XName.Get("tblPr", DocX.w.NamespaceName)); | ||||
| if (tblPr == null) | if (tblPr == null) | ||||
| { | { | ||||
| * Get the 'borderType' (table border) element for this Table, | * Get the 'borderType' (table border) element for this Table, | ||||
| * null will be return if no such element exists. | * null will be return if no such element exists. | ||||
| */ | */ | ||||
| string tbordertype; | |||||
| tbordertype = borderType.ToString(); | |||||
| var tbordertype = borderType.ToString(); | |||||
| // only lower the first char of string (because of insideH and insideV) | // only lower the first char of string (because of insideH and insideV) | ||||
| tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1); | tbordertype = tbordertype.Substring(0, 1).ToLower() + tbordertype.Substring(1); | ||||
| if (trPr != null) | if (trPr != null) | ||||
| { | { | ||||
| var gridAfter = trPr.Element(XName.Get("gridAfter", DocX.w.NamespaceName)); | var gridAfter = trPr.Element(XName.Get("gridAfter", DocX.w.NamespaceName)); | ||||
| if (gridAfter != null) | |||||
| var val = gridAfter?.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| if (val != null) | |||||
| { | { | ||||
| var val = gridAfter.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| if (val != null) | |||||
| { | |||||
| gridAfterValue += int.Parse(val.Value); | |||||
| } | |||||
| gridAfterValue += int.Parse(val.Value); | |||||
| } | } | ||||
| } | } | ||||
| return gridAfterValue; | return gridAfterValue; | ||||
| XElement table = Xml.Parent; | XElement table = Xml.Parent; | ||||
| Xml.Remove(); | Xml.Remove(); | ||||
| if (table.Elements(XName.Get("tr", DocX.w.NamespaceName)).Count() == 0) | |||||
| if (!table.Elements(XName.Get("tr", DocX.w.NamespaceName)).Any()) | |||||
| table.Remove(); | table.Remove(); | ||||
| } | } | ||||
| { | { | ||||
| get | get | ||||
| { | { | ||||
| /* | |||||
| * Get the trPr (table row properties) element for this Row, | |||||
| * null will be return if no such element exists. | |||||
| */ | |||||
| // Get the trPr (table row properties) element for this Row, | |||||
| // null will be return if no such element exists. | |||||
| XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | ||||
| // If trPr is null, this row contains no height information. | // If trPr is null, this row contains no height information. | ||||
| if (trPr == null) | |||||
| return double.NaN; | |||||
| /* | |||||
| * Get the trHeight element for this Row, | |||||
| * null will be return if no such element exists. | |||||
| */ | |||||
| XElement trHeight = trPr.Element(XName.Get("trHeight", DocX.w.NamespaceName)); | |||||
| // Get the trHeight element for this Row, | |||||
| // null will be return if no such element exists. | |||||
| XElement trHeight = trPr?.Element(XName.Get("trHeight", DocX.w.NamespaceName)); | |||||
| // If trHeight is null, this row contains no height information. | // If trHeight is null, this row contains no height information. | ||||
| if (trHeight == null) | |||||
| return double.NaN; | |||||
| // Get the val attribute for this trHeight element. | // Get the val attribute for this trHeight element. | ||||
| XAttribute val = trHeight.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| XAttribute val = trHeight?.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| // If w is null, this cell contains no width information. | // If w is null, this cell contains no width information. | ||||
| if (val == null) | if (val == null) | ||||
| { | { | ||||
| XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | ||||
| XElement tblHeader = trPr.Element(XName.Get("tblHeader", DocX.w.NamespaceName)); | XElement tblHeader = trPr.Element(XName.Get("tblHeader", DocX.w.NamespaceName)); | ||||
| if (tblHeader == null) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| else | |||||
| { | |||||
| return true; | |||||
| } | |||||
| return tblHeader != null; | |||||
| } | } | ||||
| set | set | ||||
| { | { | ||||
| { | { | ||||
| XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | ||||
| if (trPr == null) | |||||
| return true; | |||||
| XElement trCantSplit = trPr.Element(XName.Get("cantSplit", DocX.w.NamespaceName)); | |||||
| XElement trCantSplit = trPr?.Element(XName.Get("cantSplit", DocX.w.NamespaceName)); | |||||
| if (trCantSplit == null) | |||||
| return true; | |||||
| return false; | |||||
| return trCantSplit == null; | |||||
| } | } | ||||
| set | set | ||||
| if (trCantSplit == null) | if (trCantSplit == null) | ||||
| trPr.SetElementValue(XName.Get("cantSplit", DocX.w.NamespaceName), string.Empty); | trPr.SetElementValue(XName.Get("cantSplit", DocX.w.NamespaceName), string.Empty); | ||||
| } | } | ||||
| if (value == true) | |||||
| else | |||||
| { | { | ||||
| XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | XElement trPr = Xml.Element(XName.Get("trPr", DocX.w.NamespaceName)); | ||||
| if (trPr != null) | |||||
| { | |||||
| XElement trCantSplit = trPr.Element(XName.Get("cantSplit", DocX.w.NamespaceName)); | |||||
| if (trCantSplit != null) | |||||
| trCantSplit.Remove(); | |||||
| } | |||||
| XElement trCantSplit = trPr?.Element(XName.Get("cantSplit", DocX.w.NamespaceName)); | |||||
| trCantSplit?.Remove(); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| foreach (Cell c in Cells.Where((z, i) => i > startIndex && i <= endIndex)) | foreach (Cell c in Cells.Where((z, i) => i > startIndex && i <= endIndex)) | ||||
| { | { | ||||
| XElement tcPr = c.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | XElement tcPr = c.Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| if (tcPr != null) | |||||
| XElement gridSpan = tcPr?.Element(XName.Get("gridSpan", DocX.w.NamespaceName)); | |||||
| if (gridSpan != null) | |||||
| { | { | ||||
| XElement gridSpan = tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName)); | |||||
| if (gridSpan != null) | |||||
| { | |||||
| XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| int value = 0; | |||||
| if (val != null) | |||||
| if (int.TryParse(val.Value, out value)) | |||||
| gridSpanSum += value - 1; | |||||
| } | |||||
| int value; | |||||
| if (val != null && int.TryParse(val.Value, out value)) | |||||
| gridSpanSum += value - 1; | |||||
| } | } | ||||
| // Add this cells Pragraph to the merge start Cell. | // Add this cells Pragraph to the merge start Cell. | ||||
| { | { | ||||
| var gridSpanVal = 0; | var gridSpanVal = 0; | ||||
| XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| if (tcPr != null) | |||||
| XElement gridSpan = tcPr?.Element(XName.Get("gridSpan", DocX.w.NamespaceName)); | |||||
| if (gridSpan != null) | |||||
| { | { | ||||
| XElement gridSpan = tcPr.Element(XName.Get("gridSpan", DocX.w.NamespaceName)); | |||||
| if (gridSpan != null) | |||||
| { | |||||
| XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| XAttribute val = gridSpan.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| int value = 0; | |||||
| if (val != null) | |||||
| if (int.TryParse(val.Value, out value)) | |||||
| gridSpanVal = value; | |||||
| } | |||||
| int value; | |||||
| if (val != null && int.TryParse(val.Value, out value)) | |||||
| gridSpanVal = value; | |||||
| } | } | ||||
| return gridSpanVal; | return gridSpanVal; | ||||
| } | } | ||||
| XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| // If tcPr is null, this cell contains no width information. | // If tcPr is null, this cell contains no width information. | ||||
| if (tcPr == null) | |||||
| return VerticalAlignment.Center; | |||||
| /* | |||||
| * Get the vAlign (table cell vertical alignment) element for this Cell, | |||||
| * null will be return if no such element exists. | |||||
| */ | |||||
| XElement vAlign = tcPr.Element(XName.Get("vAlign", DocX.w.NamespaceName)); | |||||
| // Get the vAlign (table cell vertical alignment) element for this Cell, | |||||
| // null will be return if no such element exists. | |||||
| XElement vAlign = tcPr?.Element(XName.Get("vAlign", DocX.w.NamespaceName)); | |||||
| // If vAlign is null, this cell contains no vertical alignment information. | // If vAlign is null, this cell contains no vertical alignment information. | ||||
| if (vAlign == null) | |||||
| return VerticalAlignment.Center; | |||||
| // Get the val attribute of the vAlign element. | // Get the val attribute of the vAlign element. | ||||
| XAttribute val = vAlign.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| XAttribute val = vAlign?.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| // If val is null, this cell contains no vAlign information. | // If val is null, this cell contains no vAlign information. | ||||
| if (val == null) | if (val == null) | ||||
| set | set | ||||
| { | { | ||||
| /* | |||||
| * Get the tcPr (table cell properties) element for this Cell, | |||||
| * null will be return if no such element exists. | |||||
| */ | |||||
| // Get the tcPr (table cell properties) element for this Cell, | |||||
| // null will be return if no such element exists. | |||||
| XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| if (tcPr == null) | if (tcPr == null) | ||||
| { | { | ||||
| tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| } | } | ||||
| /* | |||||
| * Get the vAlign (table cell vertical alignment) element for this Cell, | |||||
| * null will be return if no such element exists. | |||||
| */ | |||||
| // Get the vAlign (table cell vertical alignment) element for this Cell, | |||||
| // null will be return if no such element exists. | |||||
| XElement vAlign = tcPr.Element(XName.Get("vAlign", DocX.w.NamespaceName)); | XElement vAlign = tcPr.Element(XName.Get("vAlign", DocX.w.NamespaceName)); | ||||
| if (vAlign == null) | if (vAlign == null) | ||||
| { | { | ||||
| { | { | ||||
| get | get | ||||
| { | { | ||||
| /* | |||||
| * Get the tcPr (table cell properties) element for this Cell, | |||||
| * null will be return if no such element exists. | |||||
| */ | |||||
| // Get the tcPr (table cell properties) element for this Cell, | |||||
| // null will be return if no such element exists. | |||||
| XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| // If tcPr is null, this cell contains no Color information. | // If tcPr is null, this cell contains no Color information. | ||||
| if (tcPr == null) | |||||
| return Color.White; | |||||
| /* | |||||
| * Get the shd (table shade) element for this Cell, | |||||
| * null will be return if no such element exists. | |||||
| */ | |||||
| XElement shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName)); | |||||
| // Get the shd (table shade) element for this Cell, | |||||
| // null will be return if no such element exists. | |||||
| XElement shd = tcPr?.Element(XName.Get("shd", DocX.w.NamespaceName)); | |||||
| // If shd is null, this cell contains no Color information. | // If shd is null, this cell contains no Color information. | ||||
| if (shd == null) | |||||
| return Color.White; | |||||
| // Get the w attribute of the tcW element. | // Get the w attribute of the tcW element. | ||||
| XAttribute fill = shd.Attribute(XName.Get("fill", DocX.w.NamespaceName)); | |||||
| XAttribute fill = shd?.Attribute(XName.Get("fill", DocX.w.NamespaceName)); | |||||
| // If fill is null, this cell contains no Color information. | // If fill is null, this cell contains no Color information. | ||||
| if (fill == null) | if (fill == null) | ||||
| set | set | ||||
| { | { | ||||
| /* | |||||
| * Get the tcPr (table cell properties) element for this Cell, | |||||
| * null will be return if no such element exists. | |||||
| */ | |||||
| // Get the tcPr (table cell properties) element for this Cell, | |||||
| // null will be return if no such element exists. | |||||
| XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| if (tcPr == null) | if (tcPr == null) | ||||
| { | { | ||||
| { | { | ||||
| get | get | ||||
| { | { | ||||
| /* | |||||
| * Get the tcPr (table cell properties) element for this Cell, | |||||
| * null will be return if no such element exists. | |||||
| */ | |||||
| // Get the tcPr (table cell properties) element for this Cell, | |||||
| // null will be return if no such element exists. | |||||
| XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| // If tcPr is null, this cell contains no width information. | // If tcPr is null, this cell contains no width information. | ||||
| if (tcPr == null) | |||||
| return double.NaN; | |||||
| /* | |||||
| * Get the tcW (table cell width) element for this Cell, | |||||
| * null will be return if no such element exists. | |||||
| */ | |||||
| XElement tcW = tcPr.Element(XName.Get("tcW", DocX.w.NamespaceName)); | |||||
| // Get the tcW (table cell width) element for this Cell, | |||||
| // null will be return if no such element exists. | |||||
| XElement tcW = tcPr?.Element(XName.Get("tcW", DocX.w.NamespaceName)); | |||||
| // If tcW is null, this cell contains no width information. | // If tcW is null, this cell contains no width information. | ||||
| if (tcW == null) | |||||
| return double.NaN; | |||||
| // Get the w attribute of the tcW element. | // Get the w attribute of the tcW element. | ||||
| XAttribute w = tcW.Attribute(XName.Get("w", DocX.w.NamespaceName)); | |||||
| XAttribute w = tcW?.Attribute(XName.Get("w", DocX.w.NamespaceName)); | |||||
| // If w is null, this cell contains no width information. | // If w is null, this cell contains no width information. | ||||
| if (w == null) | if (w == null) | ||||
| // remove cell width; due to set on table prop. | // remove cell width; due to set on table prop. | ||||
| tcW.Remove(); | tcW.Remove(); | ||||
| return; | return; | ||||
| //tcW.SetAttributeValue(XName.Get("type", DocX.w.NamespaceName), "auto"); | |||||
| //return; | |||||
| } | } | ||||
| // The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch. | // The type attribute needs to be set to dxa which represents "twips" or twentieths of a point. In other words, 1/1440th of an inch. | ||||
| XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName)); | XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName)); | ||||
| // If tcMar is null, this cell contains no margin information. | // If tcMar is null, this cell contains no margin information. | ||||
| if (tcMar == null) | |||||
| return double.NaN; | |||||
| // Get the left (LeftMargin) element | // Get the left (LeftMargin) element | ||||
| XElement tcMarLeft = tcMar.Element(XName.Get("left", DocX.w.NamespaceName)); | |||||
| XElement tcMarLeft = tcMar?.Element(XName.Get("left", DocX.w.NamespaceName)); | |||||
| // If tcMarLeft is null, this cell contains no left margin information. | // If tcMarLeft is null, this cell contains no left margin information. | ||||
| if (tcMarLeft == null) | |||||
| return double.NaN; | |||||
| // Get the w attribute of the tcMarLeft element. | // Get the w attribute of the tcMarLeft element. | ||||
| XAttribute w = tcMarLeft.Attribute(XName.Get("w", DocX.w.NamespaceName)); | |||||
| XAttribute w = tcMarLeft?.Attribute(XName.Get("w", DocX.w.NamespaceName)); | |||||
| // If w is null, this cell contains no width information. | // If w is null, this cell contains no width information. | ||||
| if (w == null) | if (w == null) | ||||
| XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName)); | XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName)); | ||||
| // If tcMar is null, this cell contains no margin information. | // If tcMar is null, this cell contains no margin information. | ||||
| if (tcMar == null) | |||||
| return double.NaN; | |||||
| // Get the right (RightMargin) element | // Get the right (RightMargin) element | ||||
| XElement tcMarRight = tcMar.Element(XName.Get("right", DocX.w.NamespaceName)); | |||||
| XElement tcMarRight = tcMar?.Element(XName.Get("right", DocX.w.NamespaceName)); | |||||
| // If tcMarRight is null, this cell contains no right margin information. | // If tcMarRight is null, this cell contains no right margin information. | ||||
| if (tcMarRight == null) | |||||
| return double.NaN; | |||||
| // Get the w attribute of the tcMarRight element. | // Get the w attribute of the tcMarRight element. | ||||
| XAttribute w = tcMarRight.Attribute(XName.Get("w", DocX.w.NamespaceName)); | |||||
| XAttribute w = tcMarRight?.Attribute(XName.Get("w", DocX.w.NamespaceName)); | |||||
| // If w is null, this cell contains no width information. | // If w is null, this cell contains no width information. | ||||
| if (w == null) | if (w == null) | ||||
| XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName)); | XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName)); | ||||
| // If tcMar is null, this cell contains no margin information. | // If tcMar is null, this cell contains no margin information. | ||||
| if (tcMar == null) | |||||
| return double.NaN; | |||||
| // Get the top (TopMargin) element | // Get the top (TopMargin) element | ||||
| XElement tcMarTop = tcMar.Element(XName.Get("top", DocX.w.NamespaceName)); | |||||
| XElement tcMarTop = tcMar?.Element(XName.Get("top", DocX.w.NamespaceName)); | |||||
| // If tcMarTop is null, this cell contains no top margin information. | // If tcMarTop is null, this cell contains no top margin information. | ||||
| if (tcMarTop == null) | |||||
| return double.NaN; | |||||
| // Get the w attribute of the tcMarTop element. | // Get the w attribute of the tcMarTop element. | ||||
| XAttribute w = tcMarTop.Attribute(XName.Get("w", DocX.w.NamespaceName)); | |||||
| XAttribute w = tcMarTop?.Attribute(XName.Get("w", DocX.w.NamespaceName)); | |||||
| // If w is null, this cell contains no width information. | // If w is null, this cell contains no width information. | ||||
| if (w == null) | if (w == null) | ||||
| XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| // If tcPr is null, this cell contains no width information. | // If tcPr is null, this cell contains no width information. | ||||
| if (tcPr == null) | |||||
| return double.NaN; | |||||
| /* | /* | ||||
| * Get the tcMar | * Get the tcMar | ||||
| * | * | ||||
| */ | */ | ||||
| XElement tcMar = tcPr.Element(XName.Get("tcMar", DocX.w.NamespaceName)); | |||||
| XElement tcMar = tcPr?.Element(XName.Get("tcMar", DocX.w.NamespaceName)); | |||||
| // If tcMar is null, this cell contains no margin information. | // If tcMar is null, this cell contains no margin information. | ||||
| if (tcMar == null) | if (tcMar == null) | ||||
| XElement tcMarBottom = tcMar.Element(XName.Get("bottom", DocX.w.NamespaceName)); | XElement tcMarBottom = tcMar.Element(XName.Get("bottom", DocX.w.NamespaceName)); | ||||
| // If tcMarBottom is null, this cell contains no bottom margin information. | // If tcMarBottom is null, this cell contains no bottom margin information. | ||||
| if (tcMarBottom == null) | |||||
| return double.NaN; | |||||
| // Get the w attribute of the tcMarBottom element. | // Get the w attribute of the tcMarBottom element. | ||||
| XAttribute w = tcMarBottom.Attribute(XName.Get("w", DocX.w.NamespaceName)); | |||||
| XAttribute w = tcMarBottom?.Attribute(XName.Get("w", DocX.w.NamespaceName)); | |||||
| // If w is null, this cell contains no width information. | // If w is null, this cell contains no width information. | ||||
| if (w == null) | if (w == null) | ||||
| public Border GetBorder(TableCellBorderType borderType) | public Border GetBorder(TableCellBorderType borderType) | ||||
| { | { | ||||
| // instance with default border values | // instance with default border values | ||||
| Border b = new Border(); | |||||
| var b = new Border(); | |||||
| /* | /* | ||||
| * Get the tcPr (table cell properties) element for this Cell, | * Get the tcPr (table cell properties) element for this Cell, | ||||
| * Get the 'borderType' (cell border) element for this Cell, | * Get the 'borderType' (cell border) element for this Cell, | ||||
| * null will be return if no such element exists. | * null will be return if no such element exists. | ||||
| */ | */ | ||||
| string tcbordertype; | |||||
| tcbordertype = borderType.ToString(); | |||||
| var tcbordertype = borderType.ToString(); | |||||
| switch (tcbordertype) | switch (tcbordertype) | ||||
| { | { | ||||
| * null will be return if no such element exists. | * null will be return if no such element exists. | ||||
| */ | */ | ||||
| XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| if (tcPr == null) | |||||
| XElement shd = tcPr?.Element(XName.Get("shd", DocX.w.NamespaceName)); | |||||
| XAttribute fill = shd?.Attribute(XName.Get("fill", DocX.w.NamespaceName)); | |||||
| if (fill == null) | |||||
| return Color.Empty; | return Color.Empty; | ||||
| else | |||||
| { | |||||
| XElement shd = tcPr.Element(XName.Get("shd", DocX.w.NamespaceName)); | |||||
| if (shd == null) | |||||
| return Color.Empty; | |||||
| else | |||||
| { | |||||
| XAttribute fill = shd.Attribute(XName.Get("fill", DocX.w.NamespaceName)); | |||||
| if (fill == null) | |||||
| return Color.Empty; | |||||
| else | |||||
| { | |||||
| int argb = Int32.Parse(fill.Value.Replace("#", ""), NumberStyles.HexNumber); | |||||
| return Color.FromArgb(argb); | |||||
| } | |||||
| } | |||||
| } | |||||
| int argb = Int32.Parse(fill.Value.Replace("#", ""), NumberStyles.HexNumber); | |||||
| return Color.FromArgb(argb); | |||||
| } | } | ||||
| set | set | ||||
| XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | XElement tcPr = Xml.Element(XName.Get("tcPr", DocX.w.NamespaceName)); | ||||
| // If tcPr is null, this cell contains no width information. | // If tcPr is null, this cell contains no width information. | ||||
| if (tcPr == null) | |||||
| return TextDirection.right; | |||||
| XElement textDirection = tcPr.Element(XName.Get("textDirection", DocX.w.NamespaceName)); | |||||
| if (textDirection == null) | |||||
| return TextDirection.right; | |||||
| XAttribute val = textDirection.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| XElement textDirection = tcPr?.Element(XName.Get("textDirection", DocX.w.NamespaceName)); | |||||
| XAttribute val = textDirection?.Attribute(XName.Get("val", DocX.w.NamespaceName)); | |||||
| if (val == null) | if (val == null) | ||||
| return TextDirection.right; | return TextDirection.right; | ||||
| { | { | ||||
| return (TextDirection)Enum.Parse(typeof(TextDirection), val.Value, true); | return (TextDirection)Enum.Parse(typeof(TextDirection), val.Value, true); | ||||
| } | } | ||||
| catch | catch | ||||
| { | { | ||||
| val.Remove(); | val.Remove(); |
| { | { | ||||
| return new Paragraph(Document, newlyInserted, (this as Paragraph).endIndex); | return new Paragraph(Document, newlyInserted, (this as Paragraph).endIndex); | ||||
| } | } | ||||
| else | |||||
| { | |||||
| p.Xml = newlyInserted; //IMPORTANT: I think we have return new paragraph in any case, but I dont know what to put as startIndex parameter into Paragraph constructor | |||||
| return p; | |||||
| } | |||||
| p.Xml = newlyInserted; //IMPORTANT: I think we have return new paragraph in any case, but I dont know what to put as startIndex parameter into Paragraph constructor | |||||
| return p; | |||||
| } | } | ||||
| public virtual Paragraph InsertParagraphBeforeSelf(string text) | public virtual Paragraph InsertParagraphBeforeSelf(string text) | ||||
| Xml.AddBeforeSelf(newParagraph); | Xml.AddBeforeSelf(newParagraph); | ||||
| XElement newlyInserted = Xml.ElementsBeforeSelf().Last(); | XElement newlyInserted = Xml.ElementsBeforeSelf().Last(); | ||||
| Paragraph p = new Paragraph(Document, newlyInserted, -1); | |||||
| return p; | |||||
| return new Paragraph(Document, newlyInserted, -1); | |||||
| } | } | ||||
| public virtual Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting) | public virtual Paragraph InsertParagraphAfterSelf(string text, bool trackChanges, Formatting formatting) | ||||
| XElement newlyInserted = Xml.ElementsAfterSelf().First(); | XElement newlyInserted = Xml.ElementsAfterSelf().First(); | ||||
| //Dmitchern | //Dmitchern | ||||
| return new Table(Document, newlyInserted) { mainPart = mainPart }; //return new table, dont affect parameter table | return new Table(Document, newlyInserted) { mainPart = mainPart }; //return new table, dont affect parameter table | ||||
| //t.Xml = newlyInserted; | |||||
| //return t; | |||||
| } | } | ||||
| public virtual Table InsertTableBeforeSelf(int rowCount, int columnCount) | public virtual Table InsertTableBeforeSelf(int rowCount, int columnCount) | ||||
| //Dmitchern | //Dmitchern | ||||
| return new Table(Document, newlyInserted) { mainPart=mainPart}; //return new table, dont affect parameter table | return new Table(Document, newlyInserted) { mainPart=mainPart}; //return new table, dont affect parameter table | ||||
| //t.Xml = newlyInserted; | |||||
| //return t; | |||||
| } | } | ||||
| } | } | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Linq; | using System.Linq; | ||||
| using System.Drawing; | using System.Drawing; | ||||
| using System.ComponentModel; | |||||
| using System.IO; | |||||
| using System.Reflection; | |||||
| using System.Text; | |||||
| using System.Xml.Linq; | using System.Xml.Linq; | ||||
| namespace Novacode | namespace Novacode | ||||
| { | { | ||||
| XNamespace ab = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; | XNamespace ab = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; | ||||
| var tempElement = document.PageLayout.Xml.Descendants(ab + "pgMar"); | var tempElement = document.PageLayout.Xml.Descendants(ab + "pgMar"); | ||||
| var e = tempElement.GetEnumerator(); | |||||
| foreach (var item in tempElement) | foreach (var item in tempElement) | ||||
| { | { |
| { | { | ||||
| private readonly string _directoryDocuments; | private readonly string _directoryDocuments; | ||||
| private readonly string _directoryWithFiles; | private readonly string _directoryWithFiles; | ||||
| private static Border BlankBorder = new Border(BorderStyle.Tcbs_none, 0, 0, WindowsColor.White); | |||||
| private static readonly Border BlankBorder = new Border(BorderStyle.Tcbs_none, 0, 0, WindowsColor.White); | |||||
| const string package_part_document = "/word/document.xml"; | const string package_part_document = "/word/document.xml"; | ||||
| foreach (var cell in r.Cells) | foreach (var cell in r.Cells) | ||||
| { | { | ||||
| cell.Paragraphs.First().Append("Col " + cx); | cell.Paragraphs.First().Append("Col " + cx); | ||||
| //cell.Width = colWidth; | |||||
| cell.MarginBottom = 0; | cell.MarginBottom = 0; | ||||
| cell.MarginLeft = 0; | cell.MarginLeft = 0; | ||||
| cell.MarginRight = 0; | cell.MarginRight = 0; | ||||
| foreach (var cell in r.Cells) | foreach (var cell in r.Cells) | ||||
| { | { | ||||
| cell.Paragraphs.First().Append("Col " + cx); | cell.Paragraphs.First().Append("Col " + cx); | ||||
| //cell.Width = colWidth; | |||||
| cell.MarginBottom = 0; | cell.MarginBottom = 0; | ||||
| cell.MarginLeft = 0; | cell.MarginLeft = 0; | ||||
| cell.MarginRight = 0; | cell.MarginRight = 0; | ||||
| foreach (var cell in r.Cells) | foreach (var cell in r.Cells) | ||||
| { | { | ||||
| cell.Paragraphs.First().Append("Col " + cx); | cell.Paragraphs.First().Append("Col " + cx); | ||||
| //cell.Width = colWidth; | |||||
| cell.MarginBottom = 0; | cell.MarginBottom = 0; | ||||
| cell.MarginLeft = 0; | cell.MarginLeft = 0; | ||||
| cell.MarginRight = 0; | cell.MarginRight = 0; | ||||
| doc.Save(); | doc.Save(); | ||||
| } | } | ||||
| } | } | ||||
| } | |||||
| [Test] | |||||
| public void TestPatternFuncReplacement() | |||||
| { | |||||
| } | } | ||||
| public string ReplaceFunc(string findStr) | public string ReplaceFunc(string findStr) | ||||
| { | { | ||||
| Dictionary<string, string> testPatterns = new Dictionary<string, string>() | |||||
| var testPatterns = new Dictionary<string, string> | |||||
| { | { | ||||
| {"COURT NAME","Fred Frump"}, | {"COURT NAME","Fred Frump"}, | ||||
| {"Case Number","cr-md-2011-1234567"} | {"Case Number","cr-md-2011-1234567"} | ||||
| var findPattern = "<(.*?)>"; | var findPattern = "<(.*?)>"; | ||||
| var sample = "<Match This> text"; | var sample = "<Match This> text"; | ||||
| var matchCollection = Regex.Matches(sample, findPattern, RegexOptions.IgnoreCase); | var matchCollection = Regex.Matches(sample, findPattern, RegexOptions.IgnoreCase); | ||||
| int i = 1; | |||||
| } | } | ||||
| [Test] | [Test] | ||||
| public void Test_Pattern_Replacement() | public void Test_Pattern_Replacement() | ||||
| { | { | ||||
| Dictionary<string, string> testPatterns = new Dictionary<string, string>() | |||||
| var testPatterns = new Dictionary<string, string> | |||||
| { | { | ||||
| {"COURT NAME","Fred Frump"}, | {"COURT NAME","Fred Frump"}, | ||||
| {"Case Number","cr-md-2011-1234567"} | {"Case Number","cr-md-2011-1234567"} | ||||
| Assert.IsTrue(document.CustomProperties.Count == 1); | Assert.IsTrue(document.CustomProperties.Count == 1); | ||||
| Assert.IsTrue(document.CustomProperties.ContainsKey("fname")); | Assert.IsTrue(document.CustomProperties.ContainsKey("fname")); | ||||
| Assert.IsTrue((String)document.CustomProperties["fname"].Value == "cathal"); | |||||
| Assert.IsTrue((string)document.CustomProperties["fname"].Value == "cathal"); | |||||
| document.AddCustomProperty(new CustomProperty("age", 24)); | document.AddCustomProperty(new CustomProperty("age", 24)); | ||||
| Assert.IsTrue(document.CustomProperties.Count == 3); | Assert.IsTrue(document.CustomProperties.Count == 3); | ||||
| Assert.IsTrue(document.CustomProperties.ContainsKey("male")); | Assert.IsTrue(document.CustomProperties.ContainsKey("male")); | ||||
| Assert.IsTrue((bool)document.CustomProperties["male"].Value == true); | |||||
| Assert.IsTrue((bool)document.CustomProperties["male"].Value); | |||||
| document.AddCustomProperty(new CustomProperty("newyear2012", new DateTime(2012, 1, 1))); | document.AddCustomProperty(new CustomProperty("newyear2012", new DateTime(2012, 1, 1))); | ||||
| using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Test.docx"))) | using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Test.docx"))) | ||||
| { | { | ||||
| // Add an Image to this document. | // Add an Image to this document. | ||||
| Novacode.Image img = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png")); | |||||
| Image img = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png")); | |||||
| // Create a Picture from this Image. | // Create a Picture from this Image. | ||||
| Picture pic = img.CreatePicture(); | Picture pic = img.CreatePicture(); | ||||
| using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Test.docx"))) | using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "Test.docx"))) | ||||
| { | { | ||||
| // Add an Image to this document. | // Add an Image to this document. | ||||
| Novacode.Image img = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png")); | |||||
| Image img = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png")); | |||||
| // Create a Picture from this Image. | // Create a Picture from this Image. | ||||
| Picture pic = img.CreatePicture(); | Picture pic = img.CreatePicture(); | ||||
| using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "test_add_images.docx"))) | using (DocX document = DocX.Create(Path.Combine(_directoryDocuments, "test_add_images.docx"))) | ||||
| { | { | ||||
| // Add a png to into this document | // Add a png to into this document | ||||
| Novacode.Image png = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png")); | |||||
| Image png = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png")); | |||||
| Assert.IsTrue(document.Images.Count == 1); | Assert.IsTrue(document.Images.Count == 1); | ||||
| Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".png"); | Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".png"); | ||||
| // Add a tiff into to this document | // Add a tiff into to this document | ||||
| Novacode.Image tif = document.AddImage(Path.Combine(_directoryWithFiles, "yellow.tif")); | |||||
| Image tif = document.AddImage(Path.Combine(_directoryWithFiles, "yellow.tif")); | |||||
| Assert.IsTrue(document.Images.Count == 2); | Assert.IsTrue(document.Images.Count == 2); | ||||
| Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".tif"); | Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".tif"); | ||||
| // Add a gif into to this document | // Add a gif into to this document | ||||
| Novacode.Image gif = document.AddImage(Path.Combine(_directoryWithFiles, "orange.gif")); | |||||
| Image gif = document.AddImage(Path.Combine(_directoryWithFiles, "orange.gif")); | |||||
| Assert.IsTrue(document.Images.Count == 3); | Assert.IsTrue(document.Images.Count == 3); | ||||
| Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".gif"); | Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".gif"); | ||||
| // Add a jpg into to this document | // Add a jpg into to this document | ||||
| Novacode.Image jpg = document.AddImage(Path.Combine(_directoryWithFiles, "green.jpg")); | |||||
| Image jpg = document.AddImage(Path.Combine(_directoryWithFiles, "green.jpg")); | |||||
| Assert.IsTrue(document.Images.Count == 4); | Assert.IsTrue(document.Images.Count == 4); | ||||
| Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpg"); | Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpg"); | ||||
| // Add a bitmap to this document | // Add a bitmap to this document | ||||
| Novacode.Image bitmap = document.AddImage(Path.Combine(_directoryWithFiles, "red.bmp")); | |||||
| Image bitmap = document.AddImage(Path.Combine(_directoryWithFiles, "red.bmp")); | |||||
| Assert.IsTrue(document.Images.Count == 5); | Assert.IsTrue(document.Images.Count == 5); | ||||
| // Word does not allow bmp make sure it was inserted as a png. | // Word does not allow bmp make sure it was inserted as a png. | ||||
| Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".png"); | Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".png"); | ||||
| // DocX will always insert Images that come from Streams as jpeg. | // DocX will always insert Images that come from Streams as jpeg. | ||||
| // Add a png to into this document | // Add a png to into this document | ||||
| Novacode.Image png = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "purple.png"), FileMode.Open)); | |||||
| Image png = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "purple.png"), FileMode.Open)); | |||||
| Assert.IsTrue(document.Images.Count == 1); | Assert.IsTrue(document.Images.Count == 1); | ||||
| Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".jpeg"); | Assert.IsTrue(Path.GetExtension(png.pr.TargetUri.OriginalString) == ".jpeg"); | ||||
| // Add a tiff into to this document | // Add a tiff into to this document | ||||
| Novacode.Image tif = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "yellow.tif"), FileMode.Open)); | |||||
| Image tif = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "yellow.tif"), FileMode.Open)); | |||||
| Assert.IsTrue(document.Images.Count == 2); | Assert.IsTrue(document.Images.Count == 2); | ||||
| Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".jpeg"); | Assert.IsTrue(Path.GetExtension(tif.pr.TargetUri.OriginalString) == ".jpeg"); | ||||
| // Add a gif into to this document | // Add a gif into to this document | ||||
| Novacode.Image gif = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "orange.gif"), FileMode.Open)); | |||||
| Image gif = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "orange.gif"), FileMode.Open)); | |||||
| Assert.IsTrue(document.Images.Count == 3); | Assert.IsTrue(document.Images.Count == 3); | ||||
| Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".jpeg"); | Assert.IsTrue(Path.GetExtension(gif.pr.TargetUri.OriginalString) == ".jpeg"); | ||||
| // Add a jpg into to this document | // Add a jpg into to this document | ||||
| Novacode.Image jpg = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "green.jpg"), FileMode.Open)); | |||||
| Image jpg = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "green.jpg"), FileMode.Open)); | |||||
| Assert.IsTrue(document.Images.Count == 4); | Assert.IsTrue(document.Images.Count == 4); | ||||
| Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpeg"); | Assert.IsTrue(Path.GetExtension(jpg.pr.TargetUri.OriginalString) == ".jpeg"); | ||||
| // Add a bitmap to this document | // Add a bitmap to this document | ||||
| Novacode.Image bitmap = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "red.bmp"), FileMode.Open)); | |||||
| Image bitmap = document.AddImage(new FileStream(Path.Combine(_directoryWithFiles, "red.bmp"), FileMode.Open)); | |||||
| Assert.IsTrue(document.Images.Count == 5); | Assert.IsTrue(document.Images.Count == 5); | ||||
| // Word does not allow bmp make sure it was inserted as a png. | // Word does not allow bmp make sure it was inserted as a png. | ||||
| Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".jpeg"); | Assert.IsTrue(Path.GetExtension(bitmap.pr.TargetUri.OriginalString) == ".jpeg"); | ||||
| using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "Images.docx"))) | using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "Images.docx"))) | ||||
| { | { | ||||
| // Extract Images from Document. | // Extract Images from Document. | ||||
| List<Novacode.Image> document_images = document.Images; | |||||
| List<Image> document_images = document.Images; | |||||
| // Make sure there are 3 Images in this document. | // Make sure there are 3 Images in this document. | ||||
| Assert.IsTrue(document_images.Count() == 3); | Assert.IsTrue(document_images.Count() == 3); | ||||
| #region Header_First | #region Header_First | ||||
| // Extract Images from the first Header. | // Extract Images from the first Header. | ||||
| List<Novacode.Image> header_first_images = header_first.Images; | |||||
| List<Image> header_first_images = header_first.Images; | |||||
| // Make sure there is 1 Image in the first header. | // Make sure there is 1 Image in the first header. | ||||
| Assert.IsTrue(header_first_images.Count() == 1); | Assert.IsTrue(header_first_images.Count() == 1); | ||||
| #region Header_Odd | #region Header_Odd | ||||
| // Extract Images from the odd Header. | // Extract Images from the odd Header. | ||||
| List<Novacode.Image> header_odd_images = header_odd.Images; | |||||
| List<Image> header_odd_images = header_odd.Images; | |||||
| // Make sure there is 1 Image in the first header. | // Make sure there is 1 Image in the first header. | ||||
| Assert.IsTrue(header_odd_images.Count() == 1); | Assert.IsTrue(header_odd_images.Count() == 1); | ||||
| #region Header_Even | #region Header_Even | ||||
| // Extract Images from the odd Header. | // Extract Images from the odd Header. | ||||
| List<Novacode.Image> header_even_images = header_even.Images; | |||||
| List<Image> header_even_images = header_even.Images; | |||||
| // Make sure there is 1 Image in the first header. | // Make sure there is 1 Image in the first header. | ||||
| Assert.IsTrue(header_even_images.Count() == 1); | Assert.IsTrue(header_even_images.Count() == 1); | ||||
| document.DifferentOddAndEvenPages = true; | document.DifferentOddAndEvenPages = true; | ||||
| // Add an Image to this document. | // Add an Image to this document. | ||||
| Novacode.Image img = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png")); | |||||
| Image img = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png")); | |||||
| // Create a Picture from this Image. | // Create a Picture from this Image. | ||||
| Picture pic = img.CreatePicture(); | Picture pic = img.CreatePicture(); | ||||
| document.DifferentOddAndEvenPages = true; | document.DifferentOddAndEvenPages = true; | ||||
| // Add an Image to this document. | // Add an Image to this document. | ||||
| Novacode.Image img = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png")); | |||||
| Image img = document.AddImage(Path.Combine(_directoryWithFiles, "purple.png")); | |||||
| // Create a Picture from this Image. | // Create a Picture from this Image. | ||||
| Picture pic = img.CreatePicture(); | Picture pic = img.CreatePicture(); | ||||
| Paragraph p1 = document.InsertParagraph("AC"); | Paragraph p1 = document.InsertParagraph("AC"); | ||||
| p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC"); | p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC"); | ||||
| p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink"); | p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink"); | ||||
| p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink"); | |||||
| p1.InsertHyperlink(h, p1.Text.IndexOf("C", StringComparison.Ordinal)); Assert.IsTrue(p1.Text == "linkAlinkClink"); | |||||
| // Difficult | // Difficult | ||||
| Paragraph p2 = document.InsertParagraph("\tA\tC\t"); | Paragraph p2 = document.InsertParagraph("\tA\tC\t"); | ||||
| p2.InsertHyperlink(h); Assert.IsTrue(p2.Text == "link\tA\tC\t"); | p2.InsertHyperlink(h); Assert.IsTrue(p2.Text == "link\tA\tC\t"); | ||||
| p2.InsertHyperlink(h, p2.Text.Length); Assert.IsTrue(p2.Text == "link\tA\tC\tlink"); | p2.InsertHyperlink(h, p2.Text.Length); Assert.IsTrue(p2.Text == "link\tA\tC\tlink"); | ||||
| p2.InsertHyperlink(h, p2.Text.IndexOf("C")); Assert.IsTrue(p2.Text == "link\tA\tlinkC\tlink"); | |||||
| p2.InsertHyperlink(h, p2.Text.IndexOf("C", StringComparison.Ordinal)); Assert.IsTrue(p2.Text == "link\tA\tlinkC\tlink"); | |||||
| // Contrived | // Contrived | ||||
| // Add a contrived Hyperlink to this document. | // Add a contrived Hyperlink to this document. | ||||
| Paragraph p3 = document.InsertParagraph("\tA\tC\t"); | Paragraph p3 = document.InsertParagraph("\tA\tC\t"); | ||||
| p3.InsertHyperlink(h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t"); | p3.InsertHyperlink(h2); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t"); | ||||
| p3.InsertHyperlink(h2, p3.Text.Length); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t\tlink\t"); | p3.InsertHyperlink(h2, p3.Text.Length); Assert.IsTrue(p3.Text == "\tlink\t\tA\tC\t\tlink\t"); | ||||
| p3.InsertHyperlink(h2, p3.Text.IndexOf("C")); Assert.IsTrue(p3.Text == "\tlink\t\tA\t\tlink\tC\t\tlink\t"); | |||||
| p3.InsertHyperlink(h2, p3.Text.IndexOf("C", StringComparison.Ordinal)); Assert.IsTrue(p3.Text == "\tlink\t\tA\t\tlink\tC\t\tlink\t"); | |||||
| } | } | ||||
| } | } | ||||
| Paragraph p1 = document.InsertParagraph("AC"); | Paragraph p1 = document.InsertParagraph("AC"); | ||||
| p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC"); | p1.InsertHyperlink(h); Assert.IsTrue(p1.Text == "linkAC"); | ||||
| p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink"); | p1.InsertHyperlink(h, p1.Text.Length); Assert.IsTrue(p1.Text == "linkAClink"); | ||||
| p1.InsertHyperlink(h, p1.Text.IndexOf("C")); Assert.IsTrue(p1.Text == "linkAlinkClink"); | |||||
| p1.InsertHyperlink(h, p1.Text.IndexOf("C", StringComparison.Ordinal)); Assert.IsTrue(p1.Text == "linkAlinkClink"); | |||||
| // Try and remove a Hyperlink using a negative index. | // Try and remove a Hyperlink using a negative index. | ||||
| // This should throw an exception. | // This should throw an exception. | ||||
| count = 0; | count = 0; | ||||
| foreach (var p in document.Paragraphs) | foreach (var p in document.Paragraphs) | ||||
| { | { | ||||
| p.ReplaceText("Text", "Replaced text", false, RegexOptions.None, null, desiredFormat, MatchFormattingOptions.SubsetMatch); | |||||
| p.ReplaceText("Text", "Replaced text", false, RegexOptions.None, null, desiredFormat); | |||||
| if (p.Text.StartsWith("Replaced text")) | if (p.Text.StartsWith("Replaced text")) | ||||
| { | { | ||||
| ++count; | ++count; | ||||
| Paragraph p1 = document.InsertParagraph("HelloWorld"); | Paragraph p1 = document.InsertParagraph("HelloWorld"); | ||||
| p1.RemoveText(0, 1); Assert.IsTrue(p1.Text == "elloWorld"); | p1.RemoveText(0, 1); Assert.IsTrue(p1.Text == "elloWorld"); | ||||
| p1.RemoveText(p1.Text.Length - 1, 1); Assert.IsTrue(p1.Text == "elloWorl"); | p1.RemoveText(p1.Text.Length - 1, 1); Assert.IsTrue(p1.Text == "elloWorl"); | ||||
| p1.RemoveText(p1.Text.IndexOf("o"), 1); Assert.IsTrue(p1.Text == "ellWorl"); | |||||
| p1.RemoveText(p1.Text.IndexOf("o", StringComparison.Ordinal), 1); Assert.IsTrue(p1.Text == "ellWorl"); | |||||
| // Try and remove text at an index greater than the last. | // Try and remove text at an index greater than the last. | ||||
| // This should throw an exception. | // This should throw an exception. | ||||
| Paragraph p2 = document.InsertParagraph("A\tB\tC"); | Paragraph p2 = document.InsertParagraph("A\tB\tC"); | ||||
| p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\tB\tC"); | p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\tB\tC"); | ||||
| p2.RemoveText(p2.Text.Length - 1, 1); Assert.IsTrue(p2.Text == "\tB\t"); | p2.RemoveText(p2.Text.Length - 1, 1); Assert.IsTrue(p2.Text == "\tB\t"); | ||||
| p2.RemoveText(p2.Text.IndexOf("B"), 1); Assert.IsTrue(p2.Text == "\t\t"); | |||||
| p2.RemoveText(p2.Text.IndexOf("B", StringComparison.Ordinal), 1); Assert.IsTrue(p2.Text == "\t\t"); | |||||
| p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\t"); | p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == "\t"); | ||||
| p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == ""); | p2.RemoveText(0, 1); Assert.IsTrue(p2.Text == ""); | ||||
| Paragraph p1 = document.InsertParagraph("HelloWorld"); | Paragraph p1 = document.InsertParagraph("HelloWorld"); | ||||
| p1.InsertText(0, "-"); Assert.IsTrue(p1.Text == "-HelloWorld"); | p1.InsertText(0, "-"); Assert.IsTrue(p1.Text == "-HelloWorld"); | ||||
| p1.InsertText(p1.Text.Length, "-"); Assert.IsTrue(p1.Text == "-HelloWorld-"); | p1.InsertText(p1.Text.Length, "-"); Assert.IsTrue(p1.Text == "-HelloWorld-"); | ||||
| p1.InsertText(p1.Text.IndexOf("W"), "-"); Assert.IsTrue(p1.Text == "-Hello-World-"); | |||||
| p1.InsertText(p1.Text.IndexOf("W", StringComparison.Ordinal), "-"); Assert.IsTrue(p1.Text == "-Hello-World-"); | |||||
| // Try and insert text at an index greater than the last + 1. | // Try and insert text at an index greater than the last + 1. | ||||
| // This should throw an exception. | // This should throw an exception. | ||||
| Paragraph p2 = document.InsertParagraph("A\tB\tC"); | Paragraph p2 = document.InsertParagraph("A\tB\tC"); | ||||
| p2.InsertText(0, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC"); | p2.InsertText(0, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC"); | ||||
| p2.InsertText(p2.Text.Length, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC-"); | p2.InsertText(p2.Text.Length, "-"); Assert.IsTrue(p2.Text == "-A\tB\tC-"); | ||||
| p2.InsertText(p2.Text.IndexOf("B"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\tC-"); | |||||
| p2.InsertText(p2.Text.IndexOf("C"), "-"); Assert.IsTrue(p2.Text == "-A\t-B\t-C-"); | |||||
| p2.InsertText(p2.Text.IndexOf("B", StringComparison.Ordinal), "-"); Assert.IsTrue(p2.Text == "-A\t-B\tC-"); | |||||
| p2.InsertText(p2.Text.IndexOf("C", StringComparison.Ordinal), "-"); Assert.IsTrue(p2.Text == "-A\t-B\t-C-"); | |||||
| // Contrived 1 | // Contrived 1 | ||||
| //<p> | //<p> | ||||
| p3.InsertText(0, "-"); Assert.IsTrue(p3.Text == "-ABC"); | p3.InsertText(0, "-"); Assert.IsTrue(p3.Text == "-ABC"); | ||||
| p3.InsertText(p3.Text.Length, "-"); Assert.IsTrue(p3.Text == "-ABC-"); | p3.InsertText(p3.Text.Length, "-"); Assert.IsTrue(p3.Text == "-ABC-"); | ||||
| p3.InsertText(p3.Text.IndexOf("B"), "-"); Assert.IsTrue(p3.Text == "-A-BC-"); | |||||
| p3.InsertText(p3.Text.IndexOf("C"), "-"); Assert.IsTrue(p3.Text == "-A-B-C-"); | |||||
| p3.InsertText(p3.Text.IndexOf("B", StringComparison.Ordinal), "-"); Assert.IsTrue(p3.Text == "-A-BC-"); | |||||
| p3.InsertText(p3.Text.IndexOf("C", StringComparison.Ordinal), "-"); Assert.IsTrue(p3.Text == "-A-B-C-"); | |||||
| // Contrived 2 | // Contrived 2 | ||||
| //<p> | //<p> | ||||
| p4.InsertText(0, "\t"); Assert.IsTrue(p4.Text == "\tABC"); | p4.InsertText(0, "\t"); Assert.IsTrue(p4.Text == "\tABC"); | ||||
| p4.InsertText(p4.Text.Length, "\t"); Assert.IsTrue(p4.Text == "\tABC\t"); | p4.InsertText(p4.Text.Length, "\t"); Assert.IsTrue(p4.Text == "\tABC\t"); | ||||
| p4.InsertText(p4.Text.IndexOf("B"), "\t"); Assert.IsTrue(p4.Text == "\tA\tBC\t"); | |||||
| p4.InsertText(p4.Text.IndexOf("C"), "\t"); Assert.IsTrue(p4.Text == "\tA\tB\tC\t"); | |||||
| p4.InsertText(p4.Text.IndexOf("B", StringComparison.Ordinal), "\t"); Assert.IsTrue(p4.Text == "\tA\tBC\t"); | |||||
| p4.InsertText(p4.Text.IndexOf("C", StringComparison.Ordinal), "\t"); Assert.IsTrue(p4.Text == "\tA\tB\tC\t"); | |||||
| } | } | ||||
| } | } | ||||
| { | { | ||||
| const int level = 0; | const int level = 0; | ||||
| XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; | XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"; | ||||
| var list = document.AddList("First Item", level, ListItemType.Numbered); | |||||
| var list = document.AddList("First Item"); | |||||
| document.InsertList(list); | document.InsertList(list); | ||||
| var listNumPropNode = document.mainDoc.Descendants().First(s => s.Name.LocalName == "numPr"); | var listNumPropNode = document.mainDoc.Descendants().First(s => s.Name.LocalName == "numPr"); | ||||
| Assert.AreEqual(list.Items.First().runs.First().Value, "RunText"); | Assert.AreEqual(list.Items.First().runs.First().Value, "RunText"); | ||||
| } | } | ||||
| } | } | ||||
| [Test] | |||||
| public void WhenCreatingAListTheNumberingShouldGetSaved() | |||||
| { | |||||
| } | |||||
| [Test] | [Test] | ||||
| public void WhenCreatingAListTheListStyleShouldExistOrBeCreated() | public void WhenCreatingAListTheListStyleShouldExistOrBeCreated() | ||||
| { | { | ||||
| { | { | ||||
| using (var document = DocX.Create(Path.Combine(_directoryDocuments, "HyperlinkList.docx"))) | using (var document = DocX.Create(Path.Combine(_directoryDocuments, "HyperlinkList.docx"))) | ||||
| { | { | ||||
| var list = document.AddList("Item 1", listType: ListItemType.Numbered); | |||||
| var list = document.AddList("Item 1"); | |||||
| document.AddListItem(list, "Item 2"); | document.AddListItem(list, "Item 2"); | ||||
| document.AddListItem(list, "Item 3"); | document.AddListItem(list, "Item 3"); | ||||
| [Test] | [Test] | ||||
| public void Test_Table_RemoveParagraphs() | public void Test_Table_RemoveParagraphs() | ||||
| { | { | ||||
| MemoryStream memoryStream; | |||||
| DocX document; | |||||
| memoryStream = new MemoryStream(); | |||||
| document = DocX.Create(memoryStream); | |||||
| var memoryStream = new MemoryStream(); | |||||
| var document = DocX.Create(memoryStream); | |||||
| // Add a Table into the document. | // Add a Table into the document. | ||||
| Table table = document.AddTable(1, 4); // 1 row, 4 cells | Table table = document.AddTable(1, 4); // 1 row, 4 cells | ||||
| table.Design = TableDesign.TableGrid; | table.Design = TableDesign.TableGrid; |