private static string BuildSwitchString(TableOfContentsSwitches switches, int lastIncludeLevel) { var allSwitches = Enum.GetValues(typeof (TableOfContentsSwitches)).Cast<TableOfContentsSwitches>(); var switchString = "TOC"; foreach (var s in allSwitches.Where(s => s != TableOfContentsSwitches.None && switches.HasFlag(s))) { switchString += " " + s.EnumDescription(); if (s == TableOfContentsSwitches.O) { switchString += string.Format(" '{0}-{1}'", 1, lastIncludeLevel); } } return switchString; }
private static string BuildSwitchString(TableOfContentsSwitches switches, int lastIncludeLevel) { var allSwitches = Enum.GetValues(typeof(TableOfContentsSwitches)).Cast <TableOfContentsSwitches>(); var switchString = "TOC"; foreach (var s in allSwitches.Where(s => s != TableOfContentsSwitches.None && switches.HasFlag(s))) { switchString += " " + s.EnumDescription(); if (s == TableOfContentsSwitches.O) { switchString += string.Format(" '{0}-{1}'", 1, lastIncludeLevel); } } return(switchString); }
private static string BuildSwitchString(TableOfContentsSwitches switches, int lastIncludeLevel) { IEnumerable <TableOfContentsSwitches> source = Enum.GetValues(typeof(TableOfContentsSwitches)).Cast <TableOfContentsSwitches>(); string text = "TOC"; foreach (TableOfContentsSwitches item in from s in source where s != 0 && switches.HasFlag(s) select s) { text = text + " " + item.EnumDescription(); if (item == TableOfContentsSwitches.O) { text += $" '{1}-{lastIncludeLevel}'"; } } return(text); }
internal static TableOfContents CreateTableOfContents(DocX document, string title, TableOfContentsSwitches switches, string headerStyle = null, int lastIncludeLevel = 3, int? rightTabPos = null) { var reader = XmlReader.Create(new StringReader(string.Format(XmlTemplateBases.TocXmlBase, headerStyle ?? HeaderStyle, title, rightTabPos ?? RightTabPos, BuildSwitchString(switches, lastIncludeLevel)))); var xml = XElement.Load(reader); return new TableOfContents(document, xml, headerStyle); }
internal static TableOfContents CreateTableOfContents(DocX document, string title, TableOfContentsSwitches switches, string headerStyle = null, int lastIncludeLevel = 3, int?rightTabPos = null) { var reader = XmlReader.Create(new StringReader(string.Format(XmlTemplates.TableOfContentsXmlBase, headerStyle ?? HeaderStyle, title, rightTabPos ?? RightTabPos, BuildSwitchString(switches, lastIncludeLevel)))); var xml = XElement.Load(reader); return(new TableOfContents(document, xml, headerStyle)); }
internal static TableOfContents CreateTableOfContents(DocX document, string title, TableOfContentsSwitches switches, string headerStyle = null, int lastIncludeLevel = 3, int?rightTabPos = default(int?)) { XmlReader reader = XmlReader.Create(new StringReader(string.Format("<w:sdt xmlns:w='http://schemas.openxmlformats.org/wordprocessingml/2006/main'>\r\n <w:sdtPr>\r\n <w:docPartObj>\r\n <w:docPartGallery w:val='Table of Contents'/>\r\n <w:docPartUnique/>\r\n </w:docPartObj>\\\r\n </w:sdtPr>\r\n <w:sdtEndPr>\r\n <w:rPr>\r\n <w:rFonts w:asciiTheme='minorHAnsi' w:cstheme='minorBidi' w:eastAsiaTheme='minorHAnsi' w:hAnsiTheme='minorHAnsi'/>\r\n <w:color w:val='auto'/>\r\n <w:sz w:val='22'/>\r\n <w:szCs w:val='22'/>\r\n <w:lang w:eastAsia='en-US'/>\r\n </w:rPr>\r\n </w:sdtEndPr>\r\n <w:sdtContent>\r\n <w:p>\r\n <w:pPr>\r\n <w:pStyle w:val='{0}'/>\r\n </w:pPr>\r\n <w:r>\r\n <w:t>{1}</w:t>\r\n </w:r>\r\n </w:p>\r\n <w:p>\r\n <w:pPr>\r\n <w:pStyle w:val='TOC1'/>\r\n <w:tabs>\r\n <w:tab w:val='right' w:leader='dot' w:pos='{2}'/>\r\n </w:tabs>\r\n <w:rPr>\r\n <w:noProof/>\r\n </w:rPr>\r\n </w:pPr>\r\n <w:r>\r\n <w:fldChar w:fldCharType='begin' w:dirty='true'/>\r\n </w:r>\r\n <w:r>\r\n <w:instrText xml:space='preserve'> {3} </w:instrText>\r\n </w:r>\r\n <w:r>\r\n <w:fldChar w:fldCharType='separate'/>\r\n </w:r>\r\n </w:p>\r\n <w:p>\r\n <w:r>\r\n <w:rPr>\r\n <w:b/>\r\n <w:bCs/>\r\n <w:noProof/>\r\n </w:rPr>\r\n <w:fldChar w:fldCharType='end'/>\r\n </w:r>\r\n </w:p>\r\n </w:sdtContent>\r\n </w:sdt>\r\n ", headerStyle ?? "TOCHeading", title, rightTabPos ?? 9350, BuildSwitchString(switches, lastIncludeLevel)))); XElement xml = XElement.Load(reader); return(new TableOfContents(document, xml, headerStyle)); }
/// <summary> /// Inserts at TOC into the current document before the provided <see cref="reference"/> /// </summary> /// <param name="reference">The paragraph to use as reference</param> /// <param name="title">The title of the TOC</param> /// <param name="switches">Switches to be applied, see: http://officeopenxml.com/WPtableOfContents.php </param> /// <param name="headerStyle">Lets you set the style name of the TOC header</param> /// <param name="maxIncludeLevel">Lets you specify how many header levels should be included - default is 1-3</param> /// <param name="rightTabPos">Lets you override the right tab position - this is not common</param> /// <returns>The inserted TableOfContents</returns> public TableOfContents InsertTableOfContents(Paragraph reference, string title, TableOfContentsSwitches switches, string headerStyle = null, int maxIncludeLevel = 3, int? rightTabPos = null) { var toc = TableOfContents.CreateTableOfContents(this, title, switches, headerStyle, maxIncludeLevel, rightTabPos); reference.Xml.AddBeforeSelf(toc.Xml); return toc; }
internal static Dictionary <TableOfContentsSwitches, string> BuildTOCSwitchesDictionary(TableOfContentsSwitches switches, int maxIncludeLevel = 3) { var dict = new Dictionary <TableOfContentsSwitches, string>(); var allSwitches = Enum.GetValues(typeof(TableOfContentsSwitches)).Cast <TableOfContentsSwitches>(); foreach (var s in allSwitches.Where(s => s != TableOfContentsSwitches.None && switches.HasFlag(s))) { if (s == TableOfContentsSwitches.O) { dict.Add(s, "1-" + maxIncludeLevel.ToString()); } else { dict.Add(s, ""); } } return(dict); }