Covers Heading 1 - Heading 9 Example available in examples under DocumentHeading() method.master
| <Compile Include="Charts\XElementHelpers.cs" /> | <Compile Include="Charts\XElementHelpers.cs" /> | ||||
| <Compile Include="Container.cs" /> | <Compile Include="Container.cs" /> | ||||
| <Compile Include="DocumentTypes.cs" /> | <Compile Include="DocumentTypes.cs" /> | ||||
| <Compile Include="ExtensionsHeadings.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; | |||||
| 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(); | |||||
| } | |||||
| } | |||||
| } | |||||
| } |
| namespace Novacode | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.ComponentModel; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| namespace Novacode | |||||
| { | { | ||||
| public enum ListItemType | public enum ListItemType | ||||
| Auto, | Auto, | ||||
| None | 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 | |||||
| } | |||||
| } | } |
| 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 | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } |
| using System.Linq; | using System.Linq; | ||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
| using Novacode; | 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 | namespace Examples | ||||
| { | { | ||||
| HeadersAndFootersWithImagesAndTables(); | HeadersAndFootersWithImagesAndTables(); | ||||
| HeadersAndFootersWithImagesAndTablesUsingInsertPicture(); | HeadersAndFootersWithImagesAndTablesUsingInsertPicture(); | ||||
| DocumentsWithListsFontChange(); | DocumentsWithListsFontChange(); | ||||
| DocumentHeading(); | |||||
| // Advanced | // Advanced | ||||
| Console.WriteLine("\nRunning Advanced Examples"); | Console.WriteLine("\nRunning Advanced Examples"); | ||||
| Console.WriteLine("\tCreated: docs\\Equations.docx\n"); | 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() | private static void Bookmarks() | ||||
| { | { | ||||
| Console.WriteLine("\nBookmarks()"); | Console.WriteLine("\nBookmarks()"); |
| [TestMethod] | [TestMethod] | ||||
| public void Test_Paragraph_RemoveTextManyLetters() | public void Test_Paragraph_RemoveTextManyLetters() | ||||
| { | { | ||||
| using (DocX document = DocX.Create(@"docs\HelloWorldRemovingManyLetters.docx")) | |||||
| using (DocX document = DocX.Create(@"HelloWorldRemovingManyLetters.docx")) | |||||
| { | { | ||||
| Paragraph p3 = document.InsertParagraph(""); | Paragraph p3 = document.InsertParagraph(""); | ||||
| Assert.IsFalse(deleted); | Assert.IsFalse(deleted); | ||||
| Assert.AreEqual(0, row.Cells[0].Paragraphs.Count()); | 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(); | |||||
| } | |||||
| } | |||||
| } | } |