| { | { | ||||
| List<Content> contents = GetContents(); | List<Content> contents = GetContents(); | ||||
| return contents.AsReadOnly(); | return contents.AsReadOnly(); | ||||
| } | } | ||||
| return true; | return true; | ||||
| } | } | ||||
| ++i; | ++i; | ||||
| } | } | ||||
| return false; | return false; | ||||
| } | } | ||||
| //find num node in numbering | //find num node in numbering | ||||
| var numNodes = Document.numbering.Descendants().Where(n => n.Name.LocalName == "num"); | var numNodes = Document.numbering.Descendants().Where(n => n.Name.LocalName == "num"); | ||||
| XElement numNode = numNodes.FirstOrDefault(node => node.Attribute(DocX.w + "numId").Value.Equals(numIdValue)); | XElement numNode = numNodes.FirstOrDefault(node => node.Attribute(DocX.w + "numId").Value.Equals(numIdValue)); | ||||
| if (numNode != null) | |||||
| if (numNode != null) | |||||
| { | { | ||||
| //Get abstractNumId node and its value from numNode | |||||
| var abstractNumIdNode = numNode.Descendants().First(n => n.Name.LocalName == "abstractNumId"); | |||||
| var abstractNumNodeValue = abstractNumIdNode.Attribute(DocX.w + "val").Value; | |||||
| var abstractNumNodes = Document.numbering.Descendants().Where(n => n.Name.LocalName == "abstractNum"); | |||||
| XElement abstractNumNode = | |||||
| abstractNumNodes.FirstOrDefault(node => node.Attribute(DocX.w + "abstractNumId").Value.Equals(abstractNumNodeValue)); | |||||
| //Find lvl node | |||||
| var lvlNodes = abstractNumNode.Descendants().Where(n => n.Name.LocalName == "lvl"); | |||||
| XElement lvlNode = null; | |||||
| foreach (XElement node in lvlNodes) | |||||
| { | |||||
| if (node.Attribute(DocX.w + "ilvl").Value.Equals(ilvlValue)) | |||||
| { | |||||
| lvlNode = node; | |||||
| break; | |||||
| } | |||||
| } | |||||
| var numFmtNode = lvlNode.Descendants().First(n => n.Name.LocalName == "numFmt"); | |||||
| p.ListItemType = GetListItemType(numFmtNode.Attribute(DocX.w + "val").Value); | |||||
| } | |||||
| //Get abstractNumId node and its value from numNode | |||||
| var abstractNumIdNode = numNode.Descendants().First(n => n.Name.LocalName == "abstractNumId"); | |||||
| var abstractNumNodeValue = abstractNumIdNode.Attribute(DocX.w + "val").Value; | |||||
| var abstractNumNodes = Document.numbering.Descendants().Where(n => n.Name.LocalName == "abstractNum"); | |||||
| XElement abstractNumNode = | |||||
| abstractNumNodes.FirstOrDefault(node => node.Attribute(DocX.w + "abstractNumId").Value.Equals(abstractNumNodeValue)); | |||||
| //Find lvl node | |||||
| var lvlNodes = abstractNumNode.Descendants().Where(n => n.Name.LocalName == "lvl"); | |||||
| XElement lvlNode = null; | |||||
| foreach (XElement node in lvlNodes) | |||||
| { | |||||
| if (node.Attribute(DocX.w + "ilvl").Value.Equals(ilvlValue)) | |||||
| { | |||||
| lvlNode = node; | |||||
| break; | |||||
| } | |||||
| } | |||||
| var numFmtNode = lvlNode.Descendants().First(n => n.Name.LocalName == "numFmt"); | |||||
| p.ListItemType = GetListItemType(numFmtNode.Attribute(DocX.w + "val").Value); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| catch (Exception) | catch (Exception) | ||||
| { | { | ||||
| val = "Missing"; | |||||
| val = "Missing"; | |||||
| } | } | ||||
| return val; | return val; | ||||
| } | } | ||||
| internal List<Paragraph> GetParagraphs(bool deepSearch=false) | |||||
| internal List<Paragraph> GetParagraphs(bool deepSearch = false) | |||||
| { | { | ||||
| // Need some memory that can be updated by the recursive search. | // Need some memory that can be updated by the recursive search. | ||||
| int index = 0; | int index = 0; | ||||
| Paragraph paragraph = new Paragraph(Document, e, index); | Paragraph paragraph = new Paragraph(Document, e, index); | ||||
| paragraphs.Add(paragraph); | paragraphs.Add(paragraph); | ||||
| } | } | ||||
| // GetParagraphsRecursive(Xml, ref index, ref paragraphs, deepSearch); | |||||
| // GetParagraphsRecursive(Xml, ref index, ref paragraphs, deepSearch); | |||||
| return paragraphs; | return paragraphs; | ||||
| } | } | ||||
| internal void GetParagraphsRecursive(XElement Xml, ref int index, ref List<Paragraph> paragraphs, bool deepSearch=false) | |||||
| internal void GetParagraphsRecursive(XElement Xml, ref int index, ref List<Paragraph> paragraphs, bool deepSearch = false) | |||||
| { | { | ||||
| // sdtContent are for PageNumbers inside Headers or Footers, don't go any deeper. | // sdtContent are for PageNumbers inside Headers or Footers, don't go any deeper. | ||||
| //if (Xml.Name.LocalName == "sdtContent") | //if (Xml.Name.LocalName == "sdtContent") | ||||
| /// <param name="newFormatting"></param> | /// <param name="newFormatting"></param> | ||||
| /// <param name="matchFormatting"></param> | /// <param name="matchFormatting"></param> | ||||
| /// <param name="formattingOptions"></param> | /// <param name="formattingOptions"></param> | ||||
| public virtual void ReplaceText(string searchValue, Func<string,string> regexMatchHandler, bool trackChanges = false, RegexOptions options = RegexOptions.None, Formatting newFormatting = null, Formatting matchFormatting = null, MatchFormattingOptions formattingOptions = MatchFormattingOptions.SubsetMatch) | |||||
| public virtual void ReplaceText(string searchValue, Func<string, string> regexMatchHandler, bool trackChanges = false, RegexOptions options = RegexOptions.None, Formatting newFormatting = null, Formatting matchFormatting = null, MatchFormattingOptions formattingOptions = MatchFormattingOptions.SubsetMatch) | |||||
| { | { | ||||
| if (string.IsNullOrEmpty(searchValue)) | if (string.IsNullOrEmpty(searchValue)) | ||||
| throw new ArgumentException("oldValue cannot be null or empty", "searchValue"); | throw new ArgumentException("oldValue cannot be null or empty", "searchValue"); | ||||
| if (regexMatchHandler == null) | if (regexMatchHandler == null) | ||||
| throw new ArgumentException("regexMatchHandler cannot be null", "regexMatchHandler"); | throw new ArgumentException("regexMatchHandler cannot be null", "regexMatchHandler"); | ||||
| // ReplaceText in Headers/Footers of the document. | // ReplaceText in Headers/Footers of the document. | ||||
| var containerList = new List<IParagraphContainer> { | var containerList = new List<IParagraphContainer> { | ||||
| Document.Headers.first, Document.Headers.even, Document.Headers.odd, | Document.Headers.first, Document.Headers.even, Document.Headers.odd, | ||||
| public string[] ValidateBookmarks(params string[] bookmarkNames) | public string[] ValidateBookmarks(params string[] bookmarkNames) | ||||
| { | { | ||||
| var headers = new[] {Document.Headers.first, Document.Headers.even, Document.Headers.odd}.Where(h => h != null).ToList(); | |||||
| var footers = new[] {Document.Footers.first, Document.Footers.even, Document.Footers.odd}.Where(f => f != null).ToList(); | |||||
| var headers = new[] { Document.Headers.first, Document.Headers.even, Document.Headers.odd }.Where(h => h != null).ToList(); | |||||
| var footers = new[] { Document.Footers.first, Document.Footers.even, Document.Footers.odd }.Where(f => f != null).ToList(); | |||||
| var nonMatching = new List<string>(); | var nonMatching = new List<string>(); | ||||
| foreach (var bookmarkName in bookmarkNames) | foreach (var bookmarkName in bookmarkNames) | ||||
| if (Paragraphs.Any(p => p.ValidateBookmark(bookmarkName))) return new string[0]; | if (Paragraphs.Any(p => p.ValidateBookmark(bookmarkName))) return new string[0]; | ||||
| nonMatching.Add(bookmarkName); | nonMatching.Add(bookmarkName); | ||||
| } | } | ||||
| return nonMatching.ToArray(); | return nonMatching.ToArray(); | ||||
| } | } | ||||
| if (firstPar != null) | if (firstPar != null) | ||||
| { | { | ||||
| var splitindex = index - firstPar.startIndex; | |||||
| if (splitindex <= 0) | |||||
| { | |||||
| firstPar.Xml.ReplaceWith(newParagraph.Xml, firstPar.Xml); | |||||
| } | |||||
| else { | |||||
| XElement[] splitParagraph = HelperFunctions.SplitParagraph(firstPar, splitindex); | |||||
| firstPar.Xml.ReplaceWith | |||||
| ( | |||||
| splitParagraph[0], | |||||
| newParagraph.Xml, | |||||
| splitParagraph[1] | |||||
| ); | |||||
| } | |||||
| var splitindex = index - firstPar.startIndex; | |||||
| if (splitindex <= 0) | |||||
| { | |||||
| firstPar.Xml.ReplaceWith(newParagraph.Xml, firstPar.Xml); | |||||
| } | |||||
| else | |||||
| { | |||||
| XElement[] splitParagraph = HelperFunctions.SplitParagraph(firstPar, splitindex); | |||||
| firstPar.Xml.ReplaceWith | |||||
| ( | |||||
| splitParagraph[0], | |||||
| newParagraph.Xml, | |||||
| splitParagraph[1] | |||||
| ); | |||||
| } | |||||
| } | } | ||||
| else | else | ||||
| XElement newTable = HelperFunctions.CreateTable(rowCount, columnCount); | XElement newTable = HelperFunctions.CreateTable(rowCount, columnCount); | ||||
| Xml.Add(newTable); | Xml.Add(newTable); | ||||
| return new Table(Document, newTable) { mainPart = mainPart}; | |||||
| return new Table(Document, newTable) { mainPart = mainPart }; | |||||
| } | } | ||||
| public Table InsertTable(int index, int rowCount, int columnCount) | public Table InsertTable(int index, int rowCount, int columnCount) | ||||
| return list; | return list; | ||||
| } | } | ||||
| public List InsertList(List list, System.Drawing.FontFamily fontFamily, double fontSize) | |||||
| public List InsertList(List list, Font fontFamily, double fontSize) | |||||
| { | { | ||||
| foreach (var item in list.Items) | foreach (var item in list.Items) | ||||
| { | { | ||||
| return list; | return list; | ||||
| } | } | ||||
| } | } | ||||
| } | |||||
| } |
| } | } | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Header margin value in points. 1pt = 1/72 of an inch. Word internally writes docx using units = 1/20th of a point. | |||||
| /// </summary> | |||||
| public float MarginHeader | |||||
| { | |||||
| get | |||||
| { | |||||
| return getMarginAttribute(XName.Get("header", DocX.w.NamespaceName)); | |||||
| } | |||||
| set | |||||
| { | |||||
| setMarginAttribute(XName.Get("header", DocX.w.NamespaceName), value); | |||||
| } | |||||
| } | |||||
| /// <summary> | |||||
| /// Footer margin value in points. 1pt = 1/72 of an inch. Word internally writes docx using units = 1/20th of a point. | |||||
| /// </summary> | |||||
| public float MarginFooter | |||||
| { | |||||
| get | |||||
| { | |||||
| return getMarginAttribute(XName.Get("footer", DocX.w.NamespaceName)); | |||||
| } | |||||
| set | |||||
| { | |||||
| setMarginAttribute(XName.Get("footer", DocX.w.NamespaceName), value); | |||||
| } | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// Page width value in points. 1pt = 1/72 of an inch. Word internally writes docx using units = 1/20th of a point. | /// Page width value in points. 1pt = 1/72 of an inch. Word internally writes docx using units = 1/20th of a point. | ||||
| /// </summary> | /// </summary> | ||||
| base.InsertList(list); | base.InsertList(list); | ||||
| return list; | return list; | ||||
| } | } | ||||
| public new List InsertList(List list, System.Drawing.FontFamily fontFamily, double fontSize) | |||||
| public new List InsertList(List list, Font fontFamily, double fontSize) | |||||
| { | { | ||||
| base.InsertList(list, fontFamily, fontSize); | base.InsertList(list, fontFamily, fontSize); | ||||
| return list; | return list; |
| <Compile Include="ContentCollection.cs" /> | <Compile Include="ContentCollection.cs" /> | ||||
| <Compile Include="DocumentTypes.cs" /> | <Compile Include="DocumentTypes.cs" /> | ||||
| <Compile Include="ExtensionsHeadings.cs" /> | <Compile Include="ExtensionsHeadings.cs" /> | ||||
| <Compile Include="Font.cs" /> | |||||
| <Compile Include="Footers.cs" /> | <Compile Include="Footers.cs" /> | ||||
| <Compile Include="Footer.cs" /> | <Compile Include="Footer.cs" /> | ||||
| <Compile Include="CustomProperty.cs" /> | <Compile Include="CustomProperty.cs" /> |
| using System; | |||||
| namespace Novacode | |||||
| { | |||||
| /// <summary> | |||||
| /// Represents a font family | |||||
| /// </summary> | |||||
| public sealed class Font | |||||
| { | |||||
| /// <summary> | |||||
| /// Initializes a new instance of <see cref="Font" /> | |||||
| /// </summary> | |||||
| /// <param name="name">The name of the font family</param> | |||||
| public Font(string name) | |||||
| { | |||||
| if (string.IsNullOrEmpty(name)) | |||||
| { | |||||
| throw new ArgumentNullException(nameof(name)); | |||||
| } | |||||
| Name = name; | |||||
| } | |||||
| /// <summary> | |||||
| /// The name of the font family | |||||
| /// </summary> | |||||
| public string Name { get; private set; } | |||||
| /// <summary> | |||||
| /// Returns a string representation of an object | |||||
| /// </summary> | |||||
| /// <returns>The name of the font family</returns> | |||||
| public override string ToString() | |||||
| { | |||||
| return Name; | |||||
| } | |||||
| } | |||||
| } |
| private UnderlineStyle? underlineStyle; | private UnderlineStyle? underlineStyle; | ||||
| private Misc? misc; | private Misc? misc; | ||||
| private CapsStyle? capsStyle; | private CapsStyle? capsStyle; | ||||
| private FontFamily fontFamily; | |||||
| private Font fontFamily; | |||||
| private int? percentageScale; | private int? percentageScale; | ||||
| private int? kerning; | private int? kerning; | ||||
| private int? position; | private int? position; | ||||
| case "rFonts": | case "rFonts": | ||||
| formatting.FontFamily = | formatting.FontFamily = | ||||
| new FontFamily( | |||||
| new Font( | |||||
| option.GetAttribute(XName.Get("cs", DocX.w.NamespaceName), null) ?? | option.GetAttribute(XName.Get("cs", DocX.w.NamespaceName), null) ?? | ||||
| option.GetAttribute(XName.Get("ascii", DocX.w.NamespaceName), null) ?? | option.GetAttribute(XName.Get("ascii", DocX.w.NamespaceName), null) ?? | ||||
| option.GetAttribute(XName.Get("hAnsi", DocX.w.NamespaceName), null) ?? | option.GetAttribute(XName.Get("hAnsi", DocX.w.NamespaceName), null) ?? | ||||
| /// Bug found and fixed by krugs525 on August 12 2009. | /// Bug found and fixed by krugs525 on August 12 2009. | ||||
| /// Use TFS compare to see exact code change. | /// Use TFS compare to see exact code change. | ||||
| /// --> | /// --> | ||||
| public FontFamily FontFamily { get { return fontFamily; } set { fontFamily = value; } } | |||||
| public Font FontFamily { get { return fontFamily; } set { fontFamily = value; } } | |||||
| public int CompareTo(object obj) | public int CompareTo(object obj) | ||||
| { | { |
| select new Picture(Document, p, img) | select new Picture(Document, p, img) | ||||
| ).ToList(); | ).ToList(); | ||||
| List<Picture> shapes = | |||||
| ( | |||||
| from p in Xml.Descendants() | |||||
| where (p.Name.LocalName == "pict") | |||||
| let id = | |||||
| ( | |||||
| from e in p.Descendants() | |||||
| where e.Name.LocalName.Equals("imagedata") | |||||
| select e.Attribute(XName.Get("id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")).Value | |||||
| ).SingleOrDefault() | |||||
| where id != null | |||||
| let img = new Image(Document, mainPart.GetRelationship(id)) | |||||
| select new Picture(Document, p, img) | |||||
| ).ToList(); | |||||
| foreach (Picture p in shapes) | |||||
| pictures.Add(p); | |||||
| List<Picture> shapes = | |||||
| ( | |||||
| from p in Xml.Descendants() | |||||
| where (p.Name.LocalName == "pict") | |||||
| let id = | |||||
| ( | |||||
| from e in p.Descendants() | |||||
| where e.Name.LocalName.Equals("imagedata") | |||||
| select e.Attribute(XName.Get("id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")).Value | |||||
| ).SingleOrDefault() | |||||
| where id != null | |||||
| let img = new Image(Document, mainPart.GetRelationship(id)) | |||||
| select new Picture(Document, p, img) | |||||
| ).ToList(); | |||||
| foreach (Picture p in shapes) | |||||
| pictures.Add(p); | |||||
| return pictures; | return pictures; | ||||
| } | } | ||||
| // Get the first run effected by this Insert | // Get the first run effected by this Insert | ||||
| Run run = GetFirstRunEffectedByEdit(index); | Run run = GetFirstRunEffectedByEdit(index); | ||||
| if (run == null) | |||||
| { | |||||
| object insert; | |||||
| if (formatting != null) //not sure how to get original formatting here when run == null | |||||
| insert = HelperFunctions.FormatInput(value, formatting.Xml); | |||||
| else | |||||
| insert = HelperFunctions.FormatInput(value, null); | |||||
| if (trackChanges) | |||||
| insert = CreateEdit(EditType.ins, insert_datetime, insert); | |||||
| Xml.Add(insert); | |||||
| } | |||||
| else | |||||
| { | |||||
| object newRuns; | |||||
| var rprel = run.Xml.Element(XName.Get("rPr", DocX.w.NamespaceName)); | |||||
| if (formatting != null) | |||||
| { | |||||
| //merge 2 formattings properly | |||||
| Formatting finfmt = null; | |||||
| Formatting oldfmt = null; | |||||
| if (rprel != null) | |||||
| { | |||||
| oldfmt = Formatting.Parse(rprel); | |||||
| } | |||||
| if (oldfmt != null) | |||||
| { | |||||
| finfmt = oldfmt.Clone(); | |||||
| if (formatting.Bold.HasValue) { finfmt.Bold = formatting.Bold; } | |||||
| if (formatting.CapsStyle.HasValue) { finfmt.CapsStyle = formatting.CapsStyle; } | |||||
| if (formatting.FontColor.HasValue) { finfmt.FontColor = formatting.FontColor; } | |||||
| finfmt.FontFamily = formatting.FontFamily; | |||||
| if (formatting.Hidden.HasValue) { finfmt.Hidden = formatting.Hidden; } | |||||
| if (formatting.Highlight.HasValue) { finfmt.Highlight = formatting.Highlight; } | |||||
| if (formatting.Italic.HasValue) { finfmt.Italic = formatting.Italic; } | |||||
| if (formatting.Kerning.HasValue) { finfmt.Kerning = formatting.Kerning; } | |||||
| finfmt.Language = formatting.Language; | |||||
| if (formatting.Misc.HasValue) { finfmt.Misc = formatting.Misc; } | |||||
| if (formatting.PercentageScale.HasValue) { finfmt.PercentageScale = formatting.PercentageScale; } | |||||
| if (formatting.Position.HasValue) { finfmt.Position = formatting.Position; } | |||||
| if (formatting.Script.HasValue) { finfmt.Script = formatting.Script; } | |||||
| if (formatting.Size.HasValue) { finfmt.Size = formatting.Size; } | |||||
| if (formatting.Spacing.HasValue) { finfmt.Spacing = formatting.Spacing; } | |||||
| if (formatting.StrikeThrough.HasValue) { finfmt.StrikeThrough = formatting.StrikeThrough; } | |||||
| if (formatting.UnderlineColor.HasValue) { finfmt.UnderlineColor = formatting.UnderlineColor; } | |||||
| if (formatting.UnderlineStyle.HasValue) { finfmt.UnderlineStyle = formatting.UnderlineStyle; } | |||||
| } | |||||
| else { | |||||
| finfmt = formatting; | |||||
| } | |||||
| newRuns = HelperFunctions.FormatInput(value, finfmt.Xml); | |||||
| } | |||||
| else | |||||
| { | |||||
| newRuns = HelperFunctions.FormatInput(value, rprel); | |||||
| } | |||||
| if (run == null) | |||||
| { | |||||
| object insert; | |||||
| if (formatting != null) //not sure how to get original formatting here when run == null | |||||
| insert = HelperFunctions.FormatInput(value, formatting.Xml); | |||||
| else | |||||
| insert = HelperFunctions.FormatInput(value, null); | |||||
| if (trackChanges) | |||||
| insert = CreateEdit(EditType.ins, insert_datetime, insert); | |||||
| Xml.Add(insert); | |||||
| } | |||||
| else | |||||
| { | |||||
| object newRuns; | |||||
| var rprel = run.Xml.Element(XName.Get("rPr", DocX.w.NamespaceName)); | |||||
| if (formatting != null) | |||||
| { | |||||
| //merge 2 formattings properly | |||||
| Formatting finfmt = null; | |||||
| Formatting oldfmt = null; | |||||
| if (rprel != null) | |||||
| { | |||||
| oldfmt = Formatting.Parse(rprel); | |||||
| } | |||||
| if (oldfmt != null) | |||||
| { | |||||
| finfmt = oldfmt.Clone(); | |||||
| if (formatting.Bold.HasValue) { finfmt.Bold = formatting.Bold; } | |||||
| if (formatting.CapsStyle.HasValue) { finfmt.CapsStyle = formatting.CapsStyle; } | |||||
| if (formatting.FontColor.HasValue) { finfmt.FontColor = formatting.FontColor; } | |||||
| finfmt.FontFamily = formatting.FontFamily; | |||||
| if (formatting.Hidden.HasValue) { finfmt.Hidden = formatting.Hidden; } | |||||
| if (formatting.Highlight.HasValue) { finfmt.Highlight = formatting.Highlight; } | |||||
| if (formatting.Italic.HasValue) { finfmt.Italic = formatting.Italic; } | |||||
| if (formatting.Kerning.HasValue) { finfmt.Kerning = formatting.Kerning; } | |||||
| finfmt.Language = formatting.Language; | |||||
| if (formatting.Misc.HasValue) { finfmt.Misc = formatting.Misc; } | |||||
| if (formatting.PercentageScale.HasValue) { finfmt.PercentageScale = formatting.PercentageScale; } | |||||
| if (formatting.Position.HasValue) { finfmt.Position = formatting.Position; } | |||||
| if (formatting.Script.HasValue) { finfmt.Script = formatting.Script; } | |||||
| if (formatting.Size.HasValue) { finfmt.Size = formatting.Size; } | |||||
| if (formatting.Spacing.HasValue) { finfmt.Spacing = formatting.Spacing; } | |||||
| if (formatting.StrikeThrough.HasValue) { finfmt.StrikeThrough = formatting.StrikeThrough; } | |||||
| if (formatting.UnderlineColor.HasValue) { finfmt.UnderlineColor = formatting.UnderlineColor; } | |||||
| if (formatting.UnderlineStyle.HasValue) { finfmt.UnderlineStyle = formatting.UnderlineStyle; } | |||||
| } | |||||
| else | |||||
| { | |||||
| finfmt = formatting; | |||||
| } | |||||
| newRuns = HelperFunctions.FormatInput(value, finfmt.Xml); | |||||
| } | |||||
| else | |||||
| { | |||||
| newRuns = HelperFunctions.FormatInput(value, rprel); | |||||
| } | |||||
| // The parent of this Run | // The parent of this Run | ||||
| XElement parentElement = run.Xml.Parent; | XElement parentElement = run.Xml.Parent; | ||||
| new XElement | new XElement | ||||
| ( | ( | ||||
| XName.Get("r", DocX.w.NamespaceName), | XName.Get("r", DocX.w.NamespaceName), | ||||
| new Formatting() { FontFamily = new System.Drawing.FontFamily("Cambria Math") }.Xml, // create formatting | |||||
| new Formatting() { FontFamily = new Font("Cambria Math") }.Xml, // create formatting | |||||
| new XElement(XName.Get("t", DocX.m.NamespaceName), equation) // create equation string | new XElement(XName.Get("t", DocX.m.NamespaceName), equation) // create equation string | ||||
| ) | ) | ||||
| ) | ) | ||||
| .Where(x => x.Attribute(XName.Get("name", DocX.w.NamespaceName)).Value == bookmarkName).SingleOrDefault(); | .Where(x => x.Attribute(XName.Get("name", DocX.w.NamespaceName)).Value == bookmarkName).SingleOrDefault(); | ||||
| if (bookmark != null) | if (bookmark != null) | ||||
| { | { | ||||
| var run = HelperFunctions.FormatInput(toInsert, null); | var run = HelperFunctions.FormatInput(toInsert, null); | ||||
| bookmark.AddBeforeSelf(run); | bookmark.AddBeforeSelf(run); | ||||
| runs = Xml.Elements(XName.Get("r", DocX.w.NamespaceName)).ToList(); | runs = Xml.Elements(XName.Get("r", DocX.w.NamespaceName)).ToList(); | ||||
| /// </example> | /// </example> | ||||
| public Paragraph FontSize(double fontSize) | public Paragraph FontSize(double fontSize) | ||||
| { | { | ||||
| if (fontSize - (int)fontSize == 0) | |||||
| double temp = fontSize * 2; | |||||
| if (temp - (int)temp == 0) | |||||
| { | { | ||||
| if (!(fontSize > 0 && fontSize < 1639)) | if (!(fontSize > 0 && fontSize < 1639)) | ||||
| throw new ArgumentException("Size", "Value must be in the range 0 - 1638"); | throw new ArgumentException("Size", "Value must be in the range 0 - 1638"); | ||||
| else | else | ||||
| throw new ArgumentException("Size", "Value must be either a whole or half number, examples: 32, 32.5"); | throw new ArgumentException("Size", "Value must be either a whole or half number, examples: 32, 32.5"); | ||||
| ApplyTextFormattingProperty(XName.Get("sz", DocX.w.NamespaceName), string.Empty, new XAttribute(XName.Get("val", DocX.w.NamespaceName), fontSize * 2)); | ApplyTextFormattingProperty(XName.Get("sz", DocX.w.NamespaceName), string.Empty, new XAttribute(XName.Get("val", DocX.w.NamespaceName), fontSize * 2)); | ||||
| ApplyTextFormattingProperty(XName.Get("szCs", DocX.w.NamespaceName), string.Empty, new XAttribute(XName.Get("val", DocX.w.NamespaceName), fontSize * 2)); | ApplyTextFormattingProperty(XName.Get("szCs", DocX.w.NamespaceName), string.Empty, new XAttribute(XName.Get("val", DocX.w.NamespaceName), fontSize * 2)); | ||||
| return this; | return this; | ||||
| } | } | ||||
| /// <summary> | |||||
| /// For use with Append() and AppendLine() | |||||
| /// </summary> | |||||
| /// <param name="fontName">The font to use for the appended text.</param> | |||||
| /// <returns>This Paragraph with the last appended text's font changed.</returns> | |||||
| public Paragraph Font(string fontName) | |||||
| { | |||||
| return Font(new Font(fontName)); | |||||
| } | |||||
| /// <summary> | /// <summary> | ||||
| /// For use with Append() and AppendLine() | /// For use with Append() and AppendLine() | ||||
| /// </summary> | /// </summary> | ||||
| /// }// Release this document from memory. | /// }// Release this document from memory. | ||||
| /// </code> | /// </code> | ||||
| /// </example> | /// </example> | ||||
| public Paragraph Font(FontFamily fontFamily) | |||||
| public Paragraph Font(Font fontFamily) | |||||
| { | { | ||||
| ApplyTextFormattingProperty | ApplyTextFormattingProperty | ||||
| ( | ( | ||||
| XName.Get("rFonts", DocX.w.NamespaceName), | XName.Get("rFonts", DocX.w.NamespaceName), | ||||
| string.Empty, | string.Empty, | ||||
| new[] | |||||
| new[] | |||||
| { | { | ||||
| new XAttribute(XName.Get("ascii", DocX.w.NamespaceName), fontFamily.Name), | new XAttribute(XName.Get("ascii", DocX.w.NamespaceName), fontFamily.Name), | ||||
| new XAttribute(XName.Get("hAnsi", DocX.w.NamespaceName), fontFamily.Name), // Added by Maurits Elbers to support non-standard characters. See http://docx.codeplex.com/Thread/View.aspx?ThreadId=70097&ANCHOR#Post453865 | new XAttribute(XName.Get("hAnsi", DocX.w.NamespaceName), fontFamily.Name), // Added by Maurits Elbers to support non-standard characters. See http://docx.codeplex.com/Thread/View.aspx?ThreadId=70097&ANCHOR#Post453865 | ||||
| RemoveText(index, Text.Length - index, trackChanges); | RemoveText(index, Text.Length - index, trackChanges); | ||||
| } | } | ||||
| /// <summary> | |||||
| /// Replaces all occurrences of a specified System.String in this instance, with another specified System.String. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// <code> | |||||
| /// // Load a document using a relative filename. | |||||
| /// using (DocX document = DocX.Load(@"C:\Example\Test.docx")) | |||||
| /// { | |||||
| /// // The formatting to match. | |||||
| /// Formatting matchFormatting = new Formatting(); | |||||
| /// matchFormatting.Size = 10; | |||||
| /// matchFormatting.Italic = true; | |||||
| /// matchFormatting.FontFamily = new FontFamily("Times New Roman"); | |||||
| /// | |||||
| /// // The formatting to apply to the inserted text. | |||||
| /// Formatting newFormatting = new Formatting(); | |||||
| /// newFormatting.Size = 22; | |||||
| /// newFormatting.UnderlineStyle = UnderlineStyle.dotted; | |||||
| /// newFormatting.Bold = true; | |||||
| /// | |||||
| /// // Iterate through the paragraphs in this document. | |||||
| /// foreach (Paragraph p in document.Paragraphs) | |||||
| /// { | |||||
| /// /* | |||||
| /// * Replace all instances of the string "wrong" with the string "right" and ignore case. | |||||
| /// * Each inserted instance of "wrong" should use the Formatting newFormatting. | |||||
| /// * Only replace an instance of "wrong" if it is Size 10, Italic and Times New Roman. | |||||
| /// * SubsetMatch means that the formatting must contain all elements of the match formatting, | |||||
| /// * but it can also contain additional formatting for example Color, UnderlineStyle, etc. | |||||
| /// * ExactMatch means it must not contain additional formatting. | |||||
| /// */ | |||||
| /// p.ReplaceText("wrong", "right", false, RegexOptions.IgnoreCase, newFormatting, matchFormatting, MatchFormattingOptions.SubsetMatch); | |||||
| /// } | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// }// Release this document from memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| /// <seealso cref="Paragraph.RemoveText(int, int, bool)"/> | |||||
| /// <seealso cref="Paragraph.RemoveText(int, bool)"/> | |||||
| /// <seealso cref="Paragraph.InsertText(int, string, bool, Formatting)"/> | |||||
| /// <seealso cref="Paragraph.InsertText(string, bool, Formatting)"/> | |||||
| /// <param name="newValue">A System.String to replace all occurrences of oldValue.</param> | |||||
| /// <param name="oldValue">A System.String to be replaced.</param> | |||||
| /// <param name="options">A bitwise OR combination of RegexOption enumeration options.</param> | |||||
| /// <param name="trackChanges">Track changes</param> | |||||
| /// <param name="newFormatting">The formatting to apply to the text being inserted.</param> | |||||
| /// <param name="matchFormatting">The formatting that the text must match in order to be replaced.</param> | |||||
| /// <param name="fo">How should formatting be matched?</param> | |||||
| /// <param name="escapeRegEx">True if the oldValue needs to be escaped, otherwise false. If it represents a valid RegEx pattern this should be false.</param> | |||||
| /// <param name="useRegExSubstitutions">True if RegEx-like replace should be performed, i.e. if newValue contains RegEx substitutions. Does not perform named-group substitutions (only numbered groups).</param> | |||||
| public void ReplaceText(string oldValue, string newValue, bool trackChanges = false, RegexOptions options = RegexOptions.None, Formatting newFormatting = null, Formatting matchFormatting = null, MatchFormattingOptions fo = MatchFormattingOptions.SubsetMatch, bool escapeRegEx = true, bool useRegExSubstitutions = false) | |||||
| /// <summary> | |||||
| /// Replaces all occurrences of a specified System.String in this instance, with another specified System.String. | |||||
| /// </summary> | |||||
| /// <example> | |||||
| /// <code> | |||||
| /// // Load a document using a relative filename. | |||||
| /// using (DocX document = DocX.Load(@"C:\Example\Test.docx")) | |||||
| /// { | |||||
| /// // The formatting to match. | |||||
| /// Formatting matchFormatting = new Formatting(); | |||||
| /// matchFormatting.Size = 10; | |||||
| /// matchFormatting.Italic = true; | |||||
| /// matchFormatting.FontFamily = new FontFamily("Times New Roman"); | |||||
| /// | |||||
| /// // The formatting to apply to the inserted text. | |||||
| /// Formatting newFormatting = new Formatting(); | |||||
| /// newFormatting.Size = 22; | |||||
| /// newFormatting.UnderlineStyle = UnderlineStyle.dotted; | |||||
| /// newFormatting.Bold = true; | |||||
| /// | |||||
| /// // Iterate through the paragraphs in this document. | |||||
| /// foreach (Paragraph p in document.Paragraphs) | |||||
| /// { | |||||
| /// /* | |||||
| /// * Replace all instances of the string "wrong" with the string "right" and ignore case. | |||||
| /// * Each inserted instance of "wrong" should use the Formatting newFormatting. | |||||
| /// * Only replace an instance of "wrong" if it is Size 10, Italic and Times New Roman. | |||||
| /// * SubsetMatch means that the formatting must contain all elements of the match formatting, | |||||
| /// * but it can also contain additional formatting for example Color, UnderlineStyle, etc. | |||||
| /// * ExactMatch means it must not contain additional formatting. | |||||
| /// */ | |||||
| /// p.ReplaceText("wrong", "right", false, RegexOptions.IgnoreCase, newFormatting, matchFormatting, MatchFormattingOptions.SubsetMatch); | |||||
| /// } | |||||
| /// | |||||
| /// // Save all changes made to this document. | |||||
| /// document.Save(); | |||||
| /// }// Release this document from memory. | |||||
| /// </code> | |||||
| /// </example> | |||||
| /// <seealso cref="Paragraph.RemoveText(int, int, bool)"/> | |||||
| /// <seealso cref="Paragraph.RemoveText(int, bool)"/> | |||||
| /// <seealso cref="Paragraph.InsertText(int, string, bool, Formatting)"/> | |||||
| /// <seealso cref="Paragraph.InsertText(string, bool, Formatting)"/> | |||||
| /// <param name="newValue">A System.String to replace all occurrences of oldValue.</param> | |||||
| /// <param name="oldValue">A System.String to be replaced.</param> | |||||
| /// <param name="options">A bitwise OR combination of RegexOption enumeration options.</param> | |||||
| /// <param name="trackChanges">Track changes</param> | |||||
| /// <param name="newFormatting">The formatting to apply to the text being inserted.</param> | |||||
| /// <param name="matchFormatting">The formatting that the text must match in order to be replaced.</param> | |||||
| /// <param name="fo">How should formatting be matched?</param> | |||||
| /// <param name="escapeRegEx">True if the oldValue needs to be escaped, otherwise false. If it represents a valid RegEx pattern this should be false.</param> | |||||
| /// <param name="useRegExSubstitutions">True if RegEx-like replace should be performed, i.e. if newValue contains RegEx substitutions. Does not perform named-group substitutions (only numbered groups).</param> | |||||
| public void ReplaceText(string oldValue, string newValue, bool trackChanges = false, RegexOptions options = RegexOptions.None, Formatting newFormatting = null, Formatting matchFormatting = null, MatchFormattingOptions fo = MatchFormattingOptions.SubsetMatch, bool escapeRegEx = true, bool useRegExSubstitutions = false) | |||||
| { | { | ||||
| string tText = Text; | |||||
| MatchCollection mc = Regex.Matches(tText, escapeRegEx ? Regex.Escape(oldValue) : oldValue, options); | |||||
| string tText = Text; | |||||
| MatchCollection mc = Regex.Matches(tText, escapeRegEx ? Regex.Escape(oldValue) : oldValue, options); | |||||
| // Loop through the matches in reverse order | // Loop through the matches in reverse order | ||||
| foreach (Match m in mc.Cast<Match>().Reverse()) | foreach (Match m in mc.Cast<Match>().Reverse()) | ||||
| // If the formatting matches, do the replace. | // If the formatting matches, do the replace. | ||||
| if (formattingMatch) | if (formattingMatch) | ||||
| { | { | ||||
| string repl = newValue; | |||||
| //perform RegEx substitutions. Only named groups are not supported. Everything else is supported. However character escapes are not covered. | |||||
| if (useRegExSubstitutions && !String.IsNullOrEmpty(repl)) | |||||
| { | |||||
| repl = repl.Replace("$&", m.Value); | |||||
| if (m.Groups.Count > 0) | |||||
| { | |||||
| int lastcap = 0; | |||||
| for (int k = 0; k < m.Groups.Count; k++) | |||||
| { | |||||
| var g = m.Groups[k]; | |||||
| if ((g == null) || (g.Value == "")) | |||||
| continue; | |||||
| repl = repl.Replace("$" + k.ToString(), g.Value); | |||||
| lastcap = k; | |||||
| //cannot get named groups ATM | |||||
| } | |||||
| repl = repl.Replace("$+", m.Groups[lastcap].Value); | |||||
| } | |||||
| if (m.Index > 0) | |||||
| { | |||||
| repl = repl.Replace("$`", tText.Substring(0, m.Index)); | |||||
| } | |||||
| if ((m.Index + m.Length) < tText.Length) | |||||
| { | |||||
| repl = repl.Replace("$'", tText.Substring(m.Index + m.Length)); | |||||
| } | |||||
| repl = repl.Replace("$_", tText); | |||||
| repl = repl.Replace("$$", "$"); | |||||
| } | |||||
| if (!String.IsNullOrEmpty(repl)) | |||||
| InsertText(m.Index + m.Length, repl, trackChanges, newFormatting); | |||||
| if (m.Length > 0) | |||||
| RemoveText(m.Index, m.Length, trackChanges); | |||||
| string repl = newValue; | |||||
| //perform RegEx substitutions. Only named groups are not supported. Everything else is supported. However character escapes are not covered. | |||||
| if (useRegExSubstitutions && !String.IsNullOrEmpty(repl)) | |||||
| { | |||||
| repl = repl.Replace("$&", m.Value); | |||||
| if (m.Groups.Count > 0) | |||||
| { | |||||
| int lastcap = 0; | |||||
| for (int k = 0; k < m.Groups.Count; k++) | |||||
| { | |||||
| var g = m.Groups[k]; | |||||
| if ((g == null) || (g.Value == "")) | |||||
| continue; | |||||
| repl = repl.Replace("$" + k.ToString(), g.Value); | |||||
| lastcap = k; | |||||
| //cannot get named groups ATM | |||||
| } | |||||
| repl = repl.Replace("$+", m.Groups[lastcap].Value); | |||||
| } | |||||
| if (m.Index > 0) | |||||
| { | |||||
| repl = repl.Replace("$`", tText.Substring(0, m.Index)); | |||||
| } | |||||
| if ((m.Index + m.Length) < tText.Length) | |||||
| { | |||||
| repl = repl.Replace("$'", tText.Substring(m.Index + m.Length)); | |||||
| } | |||||
| repl = repl.Replace("$_", tText); | |||||
| repl = repl.Replace("$$", "$"); | |||||
| } | |||||
| if (!String.IsNullOrEmpty(repl)) | |||||
| InsertText(m.Index + m.Length, repl, trackChanges, newFormatting); | |||||
| if (m.Length > 0) | |||||
| RemoveText(m.Index, m.Length, trackChanges); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| /// <param name="newFormatting"></param> | /// <param name="newFormatting"></param> | ||||
| /// <param name="matchFormatting"></param> | /// <param name="matchFormatting"></param> | ||||
| /// <param name="fo"></param> | /// <param name="fo"></param> | ||||
| public void ReplaceText(string findPattern, Func<string,string> regexMatchHandler, bool trackChanges = false, RegexOptions options = RegexOptions.None, Formatting newFormatting = null, Formatting matchFormatting = null, MatchFormattingOptions fo = MatchFormattingOptions.SubsetMatch) | |||||
| public void ReplaceText(string findPattern, Func<string, string> regexMatchHandler, bool trackChanges = false, RegexOptions options = RegexOptions.None, Formatting newFormatting = null, Formatting matchFormatting = null, MatchFormattingOptions fo = MatchFormattingOptions.SubsetMatch) | |||||
| { | { | ||||
| var matchCollection = Regex.Matches(Text, findPattern, options); | var matchCollection = Regex.Matches(Text, findPattern, options); | ||||
| if (val != null) | if (val != null) | ||||
| { | { | ||||
| try | |||||
| String cleanValue = val.Value.Replace("-", string.Empty); | |||||
| if (Enum.IsDefined(typeof(TableDesign), cleanValue)) | |||||
| { | { | ||||
| design = (TableDesign)Enum.Parse(typeof(TableDesign), val.Value.Replace("-", string.Empty)); | |||||
| design = (TableDesign)Enum.Parse(typeof(TableDesign), cleanValue); | |||||
| } | } | ||||
| catch (Exception) | |||||
| else | |||||
| { | { | ||||
| design = TableDesign.Custom; | design = TableDesign.Custom; | ||||
| } | } |
| using Novacode; | |||||
| using System; | |||||
| using System; | |||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.Data; | using System.Data; | ||||
| using System.Drawing; | |||||
| using System.Drawing.Imaging; | |||||
| using System.IO; | using System.IO; | ||||
| using System.Linq; | using System.Linq; | ||||
| using System.Xml.Linq; | |||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
| using Image = Novacode.Image; | |||||
| using System.Xml.Linq; | |||||
| using Novacode; | |||||
| using WindowsBitmap = System.Drawing.Bitmap; | |||||
| using WindowsBrushes = System.Drawing.Brushes; | |||||
| using WindowsColor = System.Drawing.Color; | |||||
| using WindowsFont = System.Drawing.Font; | |||||
| using WindowsFontFamily = System.Drawing.FontFamily; | |||||
| using WindowsGraphics = System.Drawing.Graphics; | |||||
| using WindowsImageFormat = System.Drawing.Imaging.ImageFormat; | |||||
| namespace Examples | namespace Examples | ||||
| { | { | ||||
| class Program | class Program | ||||
| { | { | ||||
| private static Border BlankBorder = new Border(BorderStyle.Tcbs_none, 0, 0, Color.White); | |||||
| private static Border BlankBorder = new Border(BorderStyle.Tcbs_none, 0, 0, WindowsColor.White); | |||||
| static void Main(string[] args) | static void Main(string[] args) | ||||
| { | { | ||||
| Setup(); | Setup(); | ||||
| // Create and add series | // Create and add series | ||||
| Series s1 = new Series("Microsoft"); | Series s1 = new Series("Microsoft"); | ||||
| s1.Color = Color.GreenYellow; | |||||
| s1.Color = WindowsColor.GreenYellow; | |||||
| s1.Bind(company1, "Mounth", "Money"); | s1.Bind(company1, "Mounth", "Money"); | ||||
| c.AddSeries(s1); | c.AddSeries(s1); | ||||
| Series s2 = new Series("Apple"); | Series s2 = new Series("Apple"); | ||||
| // Create and add series | // Create and add series | ||||
| Series s1 = new Series("Microsoft"); | Series s1 = new Series("Microsoft"); | ||||
| s1.Color = Color.GreenYellow; | |||||
| s1.Color = WindowsColor.GreenYellow; | |||||
| s1.Bind(company1, "Mounth", "Money"); | s1.Bind(company1, "Mounth", "Money"); | ||||
| c.AddSeries(s1); | c.AddSeries(s1); | ||||
| Series s2 = new Series("Apple"); | Series s2 = new Series("Apple"); | ||||
| // Create and add series | // Create and add series | ||||
| Series s = new Series("Microsoft"); | Series s = new Series("Microsoft"); | ||||
| s.Color = Color.GreenYellow; | |||||
| s.Color = WindowsColor.GreenYellow; | |||||
| s.Bind(company1, "Mounth", "Money"); | s.Bind(company1, "Mounth", "Money"); | ||||
| c.AddSeries(s); | c.AddSeries(s); | ||||
| } | } | ||||
| #endregion | #endregion | ||||
| /// <summary> | /// <summary> | ||||
| /// Load a document and set content controls. | /// Load a document and set content controls. | ||||
| /// </summary> | /// </summary> | ||||
| document.SetContent(@"docs\elements.xml"); | document.SetContent(@"docs\elements.xml"); | ||||
| document.SaveAs(@"docs\ContentSetWithPath.docx"); | document.SaveAs(@"docs\ContentSetWithPath.docx"); | ||||
| } | } | ||||
| } | } | ||||
| Paragraph pEquation1 = document.InsertEquation("x = y+z"); | Paragraph pEquation1 = document.InsertEquation("x = y+z"); | ||||
| // Insert second Equation in this document and add formatting. | // Insert second Equation in this document and add formatting. | ||||
| Paragraph pEquation2 = document.InsertEquation("x = (y+z)/t").FontSize(18).Color(Color.Blue); | |||||
| Paragraph pEquation2 = document.InsertEquation("x = (y+z)/t").FontSize(18).Color(WindowsColor.Blue); | |||||
| // Save this document to disk. | // Save this document to disk. | ||||
| document.Save(); | document.Save(); | ||||
| picture.SetPictureShape(BasicShapes.cube); | picture.SetPictureShape(BasicShapes.cube); | ||||
| // Insert a new Paragraph into the document. | // Insert a new Paragraph into the document. | ||||
| Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS")); | |||||
| Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new Font("Comic Sans MS")); | |||||
| title.Alignment = Alignment.center; | title.Alignment = Alignment.center; | ||||
| // Insert a new Paragraph into the document. | // Insert a new Paragraph into the document. | ||||
| picture.SetPictureShape(BasicShapes.cube); | picture.SetPictureShape(BasicShapes.cube); | ||||
| // Insert a new Paragraph into the document. | // Insert a new Paragraph into the document. | ||||
| Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS")); | |||||
| Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new Font("Comic Sans MS")); | |||||
| title.Alignment = Alignment.center; | title.Alignment = Alignment.center; | ||||
| // Create a document. | // Create a document. | ||||
| using (DocX document = DocX.Create(@"docs\DocumentsWithListsFontChange.docx")) | using (DocX document = DocX.Create(@"docs\DocumentsWithListsFontChange.docx")) | ||||
| { | { | ||||
| foreach (FontFamily oneFontFamily in FontFamily.Families) | |||||
| foreach (var oneFontFamily in WindowsFontFamily.Families) | |||||
| { | { | ||||
| FontFamily fontFamily = oneFontFamily; | |||||
| double fontSize = 15; | |||||
| var fontFamily = new Font(oneFontFamily.Name); | |||||
| var fontSize = 15.0; | |||||
| // created numbered lists | // created numbered lists | ||||
| var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered, 1); | var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered, 1); | ||||
| document.InsertList(bulletedList); | document.InsertList(bulletedList); | ||||
| document.InsertList(numberedList, fontFamily, fontSize); | document.InsertList(numberedList, fontFamily, fontSize); | ||||
| } | } | ||||
| // Save this document. | // Save this document. | ||||
| document.Save(); | document.Save(); | ||||
| using (var document = DocX.Create(@"docs\Lists.docx")) | using (var document = DocX.Create(@"docs\Lists.docx")) | ||||
| { | { | ||||
| var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered); | var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered); | ||||
| //Add a numbered list starting at 2 | |||||
| //Add a numbered list starting at 2 | |||||
| document.AddListItem(numberedList, "Second List Item."); | document.AddListItem(numberedList, "Second List Item."); | ||||
| document.AddListItem(numberedList, "Third list item."); | document.AddListItem(numberedList, "Third list item."); | ||||
| document.AddListItem(numberedList, "First sub list item", 1); | document.AddListItem(numberedList, "First sub list item", 1); | ||||
| document.AddListItem(numberedList, "Nested item.", 2); | document.AddListItem(numberedList, "Nested item.", 2); | ||||
| document.AddListItem(numberedList, "Fourth nested item."); | document.AddListItem(numberedList, "Fourth nested item."); | ||||
| var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted); | var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted); | ||||
| document.AddListItem(bulletedList, "Second bullet item"); | document.AddListItem(bulletedList, "Second bullet item"); | ||||
| document.AddListItem(bulletedList, "Sub bullet item", 1); | document.AddListItem(bulletedList, "Sub bullet item", 1); | ||||
| Formatting dark_formatting = new Formatting(); | Formatting dark_formatting = new Formatting(); | ||||
| dark_formatting.Bold = true; | dark_formatting.Bold = true; | ||||
| dark_formatting.Size = 12; | dark_formatting.Size = 12; | ||||
| dark_formatting.FontColor = Color.FromArgb(31, 73, 125); | |||||
| dark_formatting.FontColor = WindowsColor.FromArgb(31, 73, 125); | |||||
| // Light formatting | // Light formatting | ||||
| Formatting light_formatting = new Formatting(); | Formatting light_formatting = new Formatting(); | ||||
| light_formatting.Italic = true; | light_formatting.Italic = true; | ||||
| light_formatting.Size = 11; | light_formatting.Size = 11; | ||||
| light_formatting.FontColor = Color.FromArgb(79, 129, 189); | |||||
| light_formatting.FontColor = WindowsColor.FromArgb(79, 129, 189); | |||||
| #region Company Name | #region Company Name | ||||
| // Get the upper left Paragraph in the layout_table. | // Get the upper left Paragraph in the layout_table. | ||||
| // Append some text and add formatting. | // Append some text and add formatting. | ||||
| p.Append("Hello World!^011Hello World!") | p.Append("Hello World!^011Hello World!") | ||||
| .Font(new FontFamily("Times New Roman")) | |||||
| .Font(new Font("Times New Roman")) | |||||
| .FontSize(32) | .FontSize(32) | ||||
| .Color(Color.Blue) | |||||
| .Color(WindowsColor.Blue) | |||||
| .Bold(); | .Bold(); | ||||
| // Append some text and add formatting. | // Append some text and add formatting. | ||||
| p.Append("Hello World!^011Hello World!") | p.Append("Hello World!^011Hello World!") | ||||
| .Font(new FontFamily("Times New Roman")) | |||||
| .Font(new Font("Times New Roman")) | |||||
| .FontSize(32) | .FontSize(32) | ||||
| .Color(Color.Blue) | |||||
| .Color(WindowsColor.Blue) | |||||
| .Bold(); | .Bold(); | ||||
| p.InsertHorizontalLine("double", 6, 1, "auto"); | p.InsertHorizontalLine("double", 6, 1, "auto"); | ||||
| // Append some text and add formatting. | // Append some text and add formatting. | ||||
| p.Append("Hello World!^011Hello World!") | p.Append("Hello World!^011Hello World!") | ||||
| .Font(new FontFamily("Times New Roman")) | |||||
| .Font(new Font("Times New Roman")) | |||||
| .FontSize(32) | .FontSize(32) | ||||
| .Color(Color.Blue) | |||||
| .Color(WindowsColor.Blue) | |||||
| .Bold(); | .Bold(); | ||||
| .Append("italic").Italic().Append(".") | .Append("italic").Italic().Append(".") | ||||
| .AppendLine("I am ") | .AppendLine("I am ") | ||||
| .Append("Arial Black") | .Append("Arial Black") | ||||
| .Font(new FontFamily("Arial Black")) | |||||
| .Font(new Font("Arial Black")) | |||||
| .Append(" and I am not.") | .Append(" and I am not.") | ||||
| .AppendLine("I am ") | .AppendLine("I am ") | ||||
| .Append("BLUE").Color(Color.Blue) | |||||
| .Append("BLUE").Color(WindowsColor.Blue) | |||||
| .Append(" and I am") | .Append(" and I am") | ||||
| .Append("Red").Color(Color.Red).Append("."); | |||||
| .Append("Red").Color(WindowsColor.Red).Append("."); | |||||
| // Save this document. | // Save this document. | ||||
| document.Save(); | document.Save(); | ||||
| Image img = document.Images[0]; | Image img = document.Images[0]; | ||||
| // Write "Hello World" into this Image. | // Write "Hello World" into this Image. | ||||
| Bitmap b = new Bitmap(img.GetStream(FileMode.Open, FileAccess.ReadWrite)); | |||||
| var b = new WindowsBitmap(img.GetStream(FileMode.Open, FileAccess.ReadWrite)); | |||||
| /* | /* | ||||
| * Get the Graphics object for this Bitmap. | * Get the Graphics object for this Bitmap. | ||||
| * The Graphics object provides functions for drawing. | * The Graphics object provides functions for drawing. | ||||
| */ | */ | ||||
| Graphics g = Graphics.FromImage(b); | |||||
| var g = WindowsGraphics.FromImage(b); | |||||
| // Draw the string "Hello World". | // Draw the string "Hello World". | ||||
| g.DrawString | g.DrawString | ||||
| ( | ( | ||||
| str, | str, | ||||
| new Font("Tahoma", 20), | |||||
| Brushes.Blue, | |||||
| new PointF(0, 0) | |||||
| new WindowsFont("Tahoma", 20), | |||||
| WindowsBrushes.Blue, | |||||
| 0.0f, 0.0f | |||||
| ); | ); | ||||
| // Save this Bitmap back into the document using a Create\Write stream. | // Save this Bitmap back into the document using a Create\Write stream. | ||||
| b.Save(img.GetStream(FileMode.Create, FileAccess.Write), ImageFormat.Png); | |||||
| b.Save(img.GetStream(FileMode.Create, FileAccess.Write), WindowsImageFormat.Png); | |||||
| } | } | ||||
| else | else | ||||
| Console.WriteLine("The provided document contains no Images."); | Console.WriteLine("The provided document contains no Images."); | ||||
| picture.SetPictureShape(BasicShapes.cube); | picture.SetPictureShape(BasicShapes.cube); | ||||
| // Insert a new Paragraph into the document. | // Insert a new Paragraph into the document. | ||||
| Paragraph title = document.InsertParagraph().Append("This is a test for a picture").FontSize(20).Font(new FontFamily("Comic Sans MS")); | |||||
| Paragraph title = document.InsertParagraph().Append("This is a test for a picture").FontSize(20).Font(new Font("Comic Sans MS")); | |||||
| title.Alignment = Alignment.center; | title.Alignment = Alignment.center; | ||||
| // Insert a new Paragraph into the document. | // Insert a new Paragraph into the document. |
| <img alt="AppVeyor Testing" src="https://ci.appveyor.com/api/projects/status/vxpnp8ivvvq2l39m?svg=true" /> | <img alt="AppVeyor Testing" src="https://ci.appveyor.com/api/projects/status/vxpnp8ivvvq2l39m?svg=true" /> | ||||
| </a> | </a> | ||||
| *** | *** | ||||
| ## What is DocX? | ## What is DocX? | ||||
| Cathal has another open source project [sql4csv](https://github.com/ccoffey/sql4csv/wiki). | Cathal has another open source project [sql4csv](https://github.com/ccoffey/sql4csv/wiki). | ||||
| sql4csv is a python library that offers an SQL "like" interface for .csv files. | sql4csv is a python library that offers an SQL "like" interface for .csv files. | ||||
| Cathal also has [3 android applications](https://play.google.com/store/apps/developer?id=Cathal+Coffey&hl=en) on GooglePlay. | |||||
| ## Our supporters | |||||
| *** | |||||
| <a href="https://www.jetbrains.com/"> | |||||
| <img alt="ReSharper" src="https://evotec.xyz/resources/resharper_logos/logo_ReSharper.png" height = 100 /> | |||||
| </a> | |||||
| Last edited 23/11/2015 by [CPonty](https://github.com/CPonty), version 88 | |||||
| *** |
| using System; | using System; | ||||
| using Novacode; | |||||
| using System.Collections.Generic; | |||||
| using System.Collections.ObjectModel; | |||||
| using System.IO; | using System.IO; | ||||
| using System.Linq; | using System.Linq; | ||||
| using System.Drawing; | |||||
| using System.Xml.Linq; | |||||
| using System.Reflection; | |||||
| using System.Collections.Generic; | |||||
| using System.Text.RegularExpressions; | using System.Text.RegularExpressions; | ||||
| using NUnit.Framework; | |||||
| using System.Collections.ObjectModel; | |||||
| using System.Xml; | using System.Xml; | ||||
| using Formatting = Novacode.Formatting; | |||||
| using System.Xml.Linq; | |||||
| using Novacode; | |||||
| using NUnit.Framework; | |||||
| using DocXFormatting = Novacode.Formatting; | |||||
| using WindowsColor = System.Drawing.Color; | |||||
| namespace UnitTests | namespace UnitTests | ||||
| { | { | ||||
| [TestFixture] | [TestFixture] | ||||
| public class DocXUnitTests | public class DocXUnitTests | ||||
| { | { | ||||
| 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, Color.White); | |||||
| private static 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"; | ||||
| using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "VariousTextFormatting.docx"))) | using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "VariousTextFormatting.docx"))) | ||||
| { | { | ||||
| // Removing red text highlighted with yellow | // Removing red text highlighted with yellow | ||||
| var formatting = new Formatting(); | |||||
| formatting.FontColor = Color.Blue; | |||||
| var formatting = new DocXFormatting(); | |||||
| formatting.FontColor = WindowsColor.Blue; | |||||
| // IMPORTANT: default constructor of Formatting sets up language property - set it to NULL to be language independent | // IMPORTANT: default constructor of Formatting sets up language property - set it to NULL to be language independent | ||||
| var desiredFormat = new Formatting() { Language = null, FontColor = Color.FromArgb(255, 0, 0), Highlight = Highlight.yellow }; | |||||
| var desiredFormat = new DocXFormatting() { Language = null, FontColor = WindowsColor.FromArgb(255, 0, 0), Highlight = Highlight.yellow }; | |||||
| var replaced = string.Empty; | var replaced = string.Empty; | ||||
| foreach (var p in document.Paragraphs) | foreach (var p in document.Paragraphs) | ||||
| { | { | ||||
| Assert.AreEqual("New text highlighted with yellow", replaced); | Assert.AreEqual("New text highlighted with yellow", replaced); | ||||
| // Removing red text with no other formatting (ExactMatch) | // Removing red text with no other formatting (ExactMatch) | ||||
| desiredFormat = new Formatting() { Language = null, FontColor = Color.FromArgb(255, 0, 0) }; | |||||
| desiredFormat = new DocXFormatting() { Language = null, FontColor = WindowsColor.FromArgb(255, 0, 0) }; | |||||
| var count = 0; | var 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.ExactMatch); | p.ReplaceText("Text", "Replaced text", false, RegexOptions.None, null, desiredFormat, MatchFormattingOptions.ExactMatch); | ||||
| if (p.Text.StartsWith("Replaced text")) | if (p.Text.StartsWith("Replaced text")) | ||||
| { | { | ||||
| ++count; | ++count; | ||||
| Assert.AreEqual(1, count); | Assert.AreEqual(1, count); | ||||
| // Removing just red text with any other formatting (SubsetMatch) | // Removing just red text with any other formatting (SubsetMatch) | ||||
| desiredFormat = new Formatting() { Language = null, FontColor = Color.FromArgb(255, 0, 0) }; | |||||
| desiredFormat = new DocXFormatting() { Language = null, FontColor = WindowsColor.FromArgb(255, 0, 0) }; | |||||
| count = 0; | count = 0; | ||||
| foreach (var p in document.Paragraphs) | foreach (var p in document.Paragraphs) | ||||
| { | { | ||||
| // Load a document. | // Load a document. | ||||
| using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "VariousTextFormatting.docx"))) | using (DocX document = DocX.Load(Path.Combine(_directoryWithFiles, "VariousTextFormatting.docx"))) | ||||
| { | { | ||||
| var formatting = new Formatting(); | |||||
| formatting.FontColor = Color.Blue; | |||||
| var formatting = new DocXFormatting(); | |||||
| formatting.FontColor = WindowsColor.Blue; | |||||
| // IMPORTANT: default constructor of Formatting sets up language property - set it to NULL to be language independent | // IMPORTANT: default constructor of Formatting sets up language property - set it to NULL to be language independent | ||||
| formatting.Language = null; | formatting.Language = null; | ||||
| var deletedCount = document.RemoveTextInGivenFormat(formatting); | var deletedCount = document.RemoveTextInGivenFormat(formatting); | ||||
| Assert.AreEqual(2, deletedCount); | Assert.AreEqual(2, deletedCount); | ||||
| deletedCount = document.RemoveTextInGivenFormat(new Formatting() { Highlight = Highlight.yellow, Language = null }); | |||||
| deletedCount = document.RemoveTextInGivenFormat(new DocXFormatting() { Highlight = Highlight.yellow, Language = null }); | |||||
| Assert.AreEqual(2, deletedCount); | Assert.AreEqual(2, deletedCount); | ||||
| deletedCount = document.RemoveTextInGivenFormat(new Formatting() { Highlight = Highlight.blue, Language = null, FontColor = Color.FromArgb(0, 255, 0) }); | |||||
| deletedCount = document.RemoveTextInGivenFormat(new DocXFormatting() { Highlight = Highlight.blue, Language = null, FontColor = WindowsColor.FromArgb(0, 255, 0) }); | |||||
| Assert.AreEqual(1, deletedCount); | Assert.AreEqual(1, deletedCount); | ||||
| deletedCount = document.RemoveTextInGivenFormat(new Formatting() { Language = null, FontColor = Color.FromArgb(123, 123, 123) }, MatchFormattingOptions.ExactMatch); | |||||
| deletedCount = document.RemoveTextInGivenFormat(new DocXFormatting() { Language = null, FontColor = WindowsColor.FromArgb(123, 123, 123) }, MatchFormattingOptions.ExactMatch); | |||||
| Assert.AreEqual(2, deletedCount); | Assert.AreEqual(2, deletedCount); | ||||
| } | } | ||||
| } | } | ||||
| { | { | ||||
| Paragraph p = document.InsertParagraph(); | Paragraph p = document.InsertParagraph(); | ||||
| var fontFamily = new FontFamily("Symbol"); | |||||
| var fontFamily = new Font("Symbol"); | |||||
| p.Append("Hello World").Font(fontFamily); | p.Append("Hello World").Font(fontFamily); | ||||