Covers Heading 1 - Heading 9 Example available in examples under DocumentHeading() method.master
| @@ -97,6 +97,7 @@ | |||
| <Compile Include="Charts\XElementHelpers.cs" /> | |||
| <Compile Include="Container.cs" /> | |||
| <Compile Include="DocumentTypes.cs" /> | |||
| <Compile Include="ExtensionsHeadings.cs" /> | |||
| <Compile Include="Footers.cs" /> | |||
| <Compile Include="Footer.cs" /> | |||
| <Compile Include="CustomProperty.cs" /> | |||
| @@ -0,0 +1,42 @@ | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.ComponentModel; | |||
| using System.IO; | |||
| using System.Linq; | |||
| using System.Reflection; | |||
| using System.Text; | |||
| using System.Xml.Linq; | |||
| using Novacode; | |||
| namespace Novacode | |||
| { | |||
| public static class ExtensionsHeadings | |||
| { | |||
| public static Paragraph Heading(this Paragraph paragraph, HeadingType headingType) | |||
| { | |||
| string StyleName = headingType.EnumDescription(); | |||
| paragraph.StyleName = StyleName; | |||
| return paragraph; | |||
| } | |||
| public static string EnumDescription(this Enum enumValue) | |||
| { | |||
| if (enumValue == null || enumValue.ToString() == "0") | |||
| { | |||
| return string.Empty; | |||
| } | |||
| FieldInfo enumInfo = enumValue.GetType().GetField(enumValue.ToString()); | |||
| DescriptionAttribute[] enumAttributes = (DescriptionAttribute[])enumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false); | |||
| if (enumAttributes.Length > 0) | |||
| { | |||
| return enumAttributes[0].Description; | |||
| } | |||
| else | |||
| { | |||
| return enumValue.ToString(); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @@ -1,4 +1,11 @@ | |||
| namespace Novacode | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.ComponentModel; | |||
| using System.Linq; | |||
| using System.Text; | |||
| namespace Novacode | |||
| { | |||
| public enum ListItemType | |||
| @@ -656,4 +663,79 @@ | |||
| Auto, | |||
| None | |||
| } | |||
| public enum HeadingType | |||
| { | |||
| [Description("Heading1")] | |||
| Heading1, | |||
| [Description("Heading2")] | |||
| Heading2, | |||
| [Description("Heading3")] | |||
| Heading3, | |||
| [Description("Heading4")] | |||
| Heading4, | |||
| [Description("Heading5")] | |||
| Heading5, | |||
| [Description("Heading6")] | |||
| Heading6, | |||
| [Description("Heading7")] | |||
| Heading7, | |||
| [Description("Heading8")] | |||
| Heading8, | |||
| [Description("Heading9")] | |||
| Heading9, | |||
| /* | |||
| * The Character Based Headings below do not work in the same way as the headings 1-9 above, but appear on the same list in word. | |||
| * I have kept them here for reference in case somebody else things its just a matter of adding them in to gain extra headings | |||
| */ | |||
| #region Other character (NOT paragraph) based Headings | |||
| //[Description("NoSpacing")] | |||
| //NoSpacing, | |||
| //[Description("Title")] | |||
| //Title, | |||
| //[Description("Subtitle")] | |||
| //Subtitle, | |||
| //[Description("Quote")] | |||
| //Quote, | |||
| //[Description("IntenseQuote")] | |||
| //IntenseQuote, | |||
| //[Description("Emphasis")] | |||
| //Emphasis, | |||
| //[Description("IntenseEmphasis")] | |||
| //IntenseEmphasis, | |||
| //[Description("Strong")] | |||
| //Strong, | |||
| //[Description("ListParagraph")] | |||
| //ListParagraph, | |||
| //[Description("SubtleReference")] | |||
| //SubtleReference, | |||
| //[Description("IntenseReference")] | |||
| //IntenseReference, | |||
| //[Description("BookTitle")] | |||
| //BookTitle, | |||
| #endregion | |||
| } | |||
| } | |||
| @@ -1,6 +1,10 @@ | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Drawing; | |||
| using System.ComponentModel; | |||
| using System.IO; | |||
| using System.Reflection; | |||
| using System.Text; | |||
| using System.Xml.Linq; | |||
| namespace Novacode | |||
| @@ -94,4 +98,5 @@ namespace Novacode | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @@ -7,6 +7,14 @@ using System.IO; | |||
| using System.Linq; | |||
| using System.Threading.Tasks; | |||
| using Novacode; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Drawing; | |||
| using System.ComponentModel; | |||
| using System.IO; | |||
| using System.Reflection; | |||
| using System.Text; | |||
| using System.Xml.Linq; | |||
| namespace Examples | |||
| { | |||
| @@ -38,6 +46,7 @@ namespace Examples | |||
| HeadersAndFootersWithImagesAndTables(); | |||
| HeadersAndFootersWithImagesAndTablesUsingInsertPicture(); | |||
| DocumentsWithListsFontChange(); | |||
| DocumentHeading(); | |||
| // Advanced | |||
| Console.WriteLine("\nRunning Advanced Examples"); | |||
| @@ -215,7 +224,25 @@ namespace Examples | |||
| Console.WriteLine("\tCreated: docs\\Equations.docx\n"); | |||
| } | |||
| } | |||
| public static void DocumentHeading() | |||
| { | |||
| Console.WriteLine("\nDocumentHeading()"); | |||
| using (DocX document = DocX.Create(@"docs\DocumentHeading.docx")) | |||
| { | |||
| foreach (HeadingType heading in (HeadingType[])Enum.GetValues(typeof(HeadingType))) | |||
| { | |||
| string text = string.Format("{0} - The quick brown fox jumps over the lazy dog", heading.EnumDescription()); | |||
| Paragraph p = document.InsertParagraph(); | |||
| p.AppendLine(text).Heading(heading); | |||
| } | |||
| document.Save(); | |||
| Console.WriteLine("\tCreated: docs\\DocumentHeading.docx\n"); | |||
| } | |||
| } | |||
| private static void Bookmarks() | |||
| { | |||
| Console.WriteLine("\nBookmarks()"); | |||
| @@ -1935,7 +1935,7 @@ namespace UnitTests | |||
| [TestMethod] | |||
| public void Test_Paragraph_RemoveTextManyLetters() | |||
| { | |||
| using (DocX document = DocX.Create(@"docs\HelloWorldRemovingManyLetters.docx")) | |||
| using (DocX document = DocX.Create(@"HelloWorldRemovingManyLetters.docx")) | |||
| { | |||
| Paragraph p3 = document.InsertParagraph(""); | |||
| @@ -2019,6 +2019,24 @@ namespace UnitTests | |||
| Assert.IsFalse(deleted); | |||
| Assert.AreEqual(0, row.Cells[0].Paragraphs.Count()); | |||
| } | |||
| [TestMethod] | |||
| public void GenerateHeadingTestDocument() | |||
| { | |||
| using (DocX document = DocX.Create(@"Document Header Test.docx")) | |||
| { | |||
| foreach (HeadingType heading in (HeadingType[])Enum.GetValues(typeof(HeadingType))) | |||
| { | |||
| string text = string.Format("{0} - The quick brown fox jumps over the lazy dog", heading.EnumDescription()); | |||
| Paragraph p = document.InsertParagraph(); | |||
| p.AppendLine(text).Heading(heading); | |||
| } | |||
| document.Save(); | |||
| } | |||
| } | |||
| } | |||