public void TableOfContentsTest() { //Create new Document TextDocument textDocument = new TextDocument(); textDocument.New(); //Create a new Table of contents TableOfContents tableOfContents = new TableOfContents( textDocument, "Table_Of_Contents", false, false, "Table of Contents"); //Add the toc textDocument.Content.Add(tableOfContents); //Create a new heading, there's no need of the chapter number string sHeading = "A first headline"; //The corresponding text entry, here you need to set the //chapter number string sTocEntry = "1. A first headline"; Header header = new Header( textDocument, Headings.Heading_20_1); header.OutLineLevel = "1"; header.TextContent.Add(new SimpleText(textDocument, sHeading)); //add the header to the content textDocument.Content.Add(header); //add the toc entry text as entry to the Table of contents tableOfContents.InsertEntry(sTocEntry, 1); //Add some text to this chapter Paragraph paragraph = ParagraphBuilder.CreateStandardTextParagraph(textDocument); paragraph.TextContent.Add(new SimpleText(textDocument, "I'm the text for the first chapter!")); textDocument.Content.Add(paragraph); //Add a sub header to the first chapter //Create a new heading, there's no need of the chapter number sHeading = "A first sub headline"; //The corresponding text entry, here you need to set the //chapter number sTocEntry = "1.1. A first sub headline"; header = new Header( textDocument, Headings.Heading_20_2); header.OutLineLevel = "2"; header.TextContent.Add(new SimpleText(textDocument, sHeading)); //add the header to the content textDocument.Content.Add(header); //add the toc entry text as entry to the Table of contents tableOfContents.InsertEntry(sTocEntry, 2); //Add some text to this sub chapter paragraph = ParagraphBuilder.CreateStandardTextParagraph(textDocument); paragraph.TextContent.Add(new SimpleText(textDocument, "I'm the text for the subchapter chapter!")); textDocument.Content.Add(paragraph); // ListStyle listStyle = new ListStyle(textDocument, "TOC_LIST"); // listStyle.AutomaticAddListLevelStyles(ListStyles.Number); // textDocument.Styles.Add(listStyle); //Save it textDocument.SaveTo(AARunMeFirstAndOnce.outPutFolder+"toc.odt"); }
/// <summary> /// Converts the specified heading. /// </summary> /// <param name="heading">The heading.</param> /// <returns>A PDF paragraph representing the ODF heading</returns> public static iTextSharp.text.Paragraph Convert(Header heading) { try { iTextSharp.text.Font font = DefaultDocumentStyles.Instance().DefaultTextFont; IStyle style = heading.Document.CommonStyles.GetStyleByName(heading.StyleName); if(style != null && style is ParagraphStyle) { if((ParagraphStyle)style != null) { if(((ParagraphStyle)style).ParentStyle != null) { IStyle parentStyle = heading.Document.CommonStyles.GetStyleByName( ((ParagraphStyle)style).ParentStyle); if(parentStyle != null && parentStyle is ParagraphStyle && ((ParagraphStyle)parentStyle).TextProperties != null && ((ParagraphStyle)style).TextProperties != null) { // get parent style first font = TextPropertyConverter.GetFont(((ParagraphStyle)parentStyle).TextProperties); // now use the orignal style as multiplier font = TextPropertyConverter.FontMultiplier(((ParagraphStyle)style).TextProperties, font); } else { font = TextPropertyConverter.GetFont(((ParagraphStyle)style).TextProperties); } } else { font = TextPropertyConverter.GetFont(((ParagraphStyle)style).TextProperties); } } } iTextSharp.text.Paragraph paragraph = new iTextSharp.text.Paragraph("", font); // default ctor protected - why ?? paragraph.AddRange(FormatedTextConverter.GetTextContents(heading.TextContent, font)); return paragraph; } catch(Exception ex) { throw; } }
/// <summary> /// Gets a paragraph in ODF format. /// </summary> /// <param name="doc">The ODF Textdocument that is being created</param> /// <returns>List of paragraphs in ODF format.</returns> public override List <IContent> GetODFParagraph(AODL.Document.TextDocuments.TextDocument doc) { //Get the headings enum Headings headingEnum = Headings.Heading; string headingString = string.Format("Heading{0}{1}", _paragraphLevel > 0 ? "_20" : string.Empty, _paragraphLevel > 0 ? "_" + (_paragraphLevel).ToString() : string.Empty); if (Enum.IsDefined(typeof(Headings), headingString)) { headingEnum = (Headings)Enum.Parse(typeof(Headings), headingString); } //Create the header AODL.Document.Content.Text.Header header = new AODL.Document.Content.Text.Header(doc, headingEnum); //Add the formated text foreach (var formatedText in CommonDocumentFunctions.ParseParagraphForODF(doc, _text)) { header.TextContent.Add(formatedText); } //Add the header properties return(new List <IContent>() { header }); }
/// <summary> /// Creates the header. /// </summary> /// <param name="headernode">The headernode.</param> /// <returns></returns> public Header CreateHeader(XmlNode headernode) { try { if (this._debugMode) this.LogNode(headernode, "Log header node before"); //Create a new Header Header header = new Header(headernode, this._document); //Create a ITextCollection ITextCollection textColl = new ITextCollection(); //Recieve the HeaderStyle IStyle headerStyle = this._document.Styles.GetStyleByName(header.StyleName); if (headerStyle != null) header.Style = headerStyle; //Create the IText content foreach(XmlNode nodeChild in header.Node.ChildNodes) { TextContentProcessor tcp = new TextContentProcessor(); IText iText = tcp.CreateTextObject(this._document, nodeChild); if (iText != null) textColl.Add(iText); else { this.OnWarning(new AODLWarning("Couldn't create IText object from header child node!.", nodeChild)); } } //Remove all header.Node.InnerXml = ""; foreach(IText iText in textColl) { if (this._debugMode) this.LogNode(iText.Node, "Log IText node read from header"); header.TextContent.Add(iText); } return header; } catch(Exception ex) { throw new AODLException("Exception while trying to create a Header.", ex); } }
/// <summary> /// Gets the outline string. /// </summary> /// <param name="header">The header.</param> /// <returns></returns> private string GetOutlineString(Header header) { try { int outline1 = 0; int outline2 = 0; int outline3 = 0; int outline4 = 0; int outline5 = 0; int outline6 = 0; if (header.Document != null) { if (header.Document is TextDocuments.TextDocument && header.Document.Content != null) { foreach(IContent content in header.Document.Content) if (content is Header) if (((Header)content).OutLineLevel != null) { int no = Convert.ToInt32(((Header)content).OutLineLevel); if (no == 1) { outline1++; outline2 = 0; outline3 = 0; outline4 = 0; outline5 = 0; outline6 = 0; } else if (no == 2) outline2++; else if (no == 3) outline3++; else if (no == 4) outline4++; else if (no == 5) outline5++; else if (no == 6) outline6++; if (content == header) { string sNumber = outline1.ToString()+"."; string sNumber1 = ""; if (outline6 != 0) sNumber1 = "."+outline6.ToString()+"."; if (outline5 != 0) sNumber1 = sNumber1+"."+outline5.ToString()+"."; if (outline4 != 0) sNumber1 = sNumber1+"."+outline4.ToString()+"."; if (outline3 != 0) sNumber1 = sNumber1+"."+outline3.ToString()+"."; if (outline2 != 0) sNumber1 = sNumber1+"."+outline2.ToString()+"."; sNumber += sNumber1; return sNumber.Replace("..","."); } } } } } catch(Exception ex) { if (OnWarning != null) { AODLWarning warning = new AODLWarning("Exception while trying to get a outline string for a heading.", ex); //warning.InMethod = AODLException.GetExceptionSourceInfo(new StackFrame(1, true)); //warning.OriginalException = ex; warning.Node = header.Node; OnWarning(warning); } } return null; }
/// <summary> /// Gets the heading as HTML. /// </summary> /// <param name="heading">The heading.</param> /// <returns></returns> public string GetHeadingAsHtml(Header heading) { string html = "<p "; try { if (heading != null) { string style = this.HTMLStyleBuilder.GetHeadingStyleAsHtml(heading); if (style.Length > 0) { html += style; html += ">\n"; } else html = html.Replace(" ", "")+">\n"; string headerText = ""; string outlineLevel = this.GetOutlineString(heading); string textContent = this.GetITextCollectionAsHtml(heading.TextContent, null); if (textContent.Length > 0) headerText += textContent; //create an anchor html += "<a name=\""+headerText+"\">\n"+outlineLevel+" "+headerText+"\n</a>\n"; } } catch(Exception ex) { throw new AODLException("Exception while trying to build a HTML string from a Heading object.", ex); } if (!html.Equals("<p ")) html += "</p>\n"; else html = ""; return html; }
public void HeadingFilledWithTextBuilder() { const string headingText = "Some Heading with\n styles\t and more"; //Create a new text document var document = new TextDocument(); document.New(); //Create a new Heading var header = new Header(document, Headings.Heading); //Create a TextCollection from headingText using the TextBuilder var textCol = TextBuilder.BuildTextCollection(document, headingText); //Add text collection header.TextContent = textCol; //Add header document.Content.Add(header); document.SaveTo(AARunMeFirstAndOnce.outPutFolder+"HeadingWithControlCharacter.odt"); }
public void ManipulateACommonStyle() { var document = new TextDocument(); document.New(); Assert.IsTrue(document.CommonStyles.Count > 0, "Common style resp. style templates must be loaded"); //Find a Header template var style = document.CommonStyles.GetStyleByName("Heading_20_1"); Assert.IsNotNull(style, "Style with name Heading_20_1 must exist"); Assert.IsTrue(style is ParagraphStyle, "style must be a ParagraphStyle"); ((ParagraphStyle)style).TextProperties.FontName = FontFamilies.BroadwayBT; //Create a header that use the standard style Heading_20_1 var header = new Header(document, Headings.Heading_20_1); //Add some text header.TextContent.Add(new SimpleText(document, "I am the header text and my style template is modified :)")); //Add header to the document document.Content.Add(header); //save the document document.SaveTo(AARunMeFirstAndOnce.outPutFolder+"modifiedCommonStyle.odt"); }
public void HeadingsTest() { //Create a new text document var document = new TextDocument(); document.New(); //Create a new Heading var header = new Header(document, Headings.Heading); //Add some header text header.TextContent.Add(new SimpleText(document, "I'm the first headline")); //Add header document.Content.Add(header); document.SaveTo(AARunMeFirstAndOnce.outPutFolder+"Heading.odt"); }
/// <summary> /// Gets the heading style as HTML. /// </summary> /// <param name="header">The header.</param> /// <returns></returns> public string GetHeadingStyleAsHtml(Header header) { try { if (header != null) { if (header.StyleName != null) { if (header.StyleName.Equals(Headings.Heading_20_1.ToString())) return this.HeaderHtmlStyles[1]; else if (header.StyleName.Equals(Headings.Heading_20_2.ToString())) return this.HeaderHtmlStyles[2]; else return this.HeaderHtmlStyles[0]; } } } catch(Exception ex) { throw new AODLException("Exception while trying to build a HTML style string from a TextStyle.", ex); } return this.HeaderHtmlStyles[0]; }
/// <summary> /// Creates the header. /// </summary> /// <param name="headernode">The headernode.</param> /// <returns></returns> public Header CreateHeader(XmlNode headernode) { try { #region Old code Todo: delete // XmlNode node = headernode.SelectSingleNode("//@text:style-name", this._document.NamespaceManager); // if(node != null) // { // if(!node.InnerText.StartsWith("Heading")) // { // //Check if a the referenced paragraphstyle reference a heading as parentstyle // XmlNode stylenode = this._document.XmlDoc.SelectSingleNode("//style:style[@style:name='"+node.InnerText+"']", this._document.NamespaceManager); // if(stylenode != null) // { // XmlNode parentstyle = stylenode.SelectSingleNode("@style:parent-style-name", // this._document.NamespaceManager); // if(parentstyle != null) // if(parentstyle.InnerText.StartsWith("Heading")) // headernode.SelectSingleNode("@text:style-name", // this._document.NamespaceManager).InnerText = parentstyle.InnerText; // } // } // } #endregion if(this._debugMode) this.LogNode(headernode, "Log header node before"); //Create a new Header Header header = new Header(headernode, this._document); //Create a ITextCollection ITextCollection textColl = new ITextCollection(); //Recieve the HeaderStyle IStyle headerStyle = this._document.Styles.GetStyleByName(header.StyleName); if(headerStyle != null) header.Style = headerStyle; //Create the IText content foreach(XmlNode nodeChild in header.Node.ChildNodes) { TextContentProcessor tcp = new TextContentProcessor(); IText iText = tcp.CreateTextObject(this._document, nodeChild); if(iText != null) textColl.Add(iText); else { if(this.OnWarning != null) { AODLWarning warning = new AODLWarning("Couldn't create IText object from header child node!."); warning.InMethod = AODLException.GetExceptionSourceInfo(new StackFrame(1, true)); warning.Node = nodeChild; this.OnWarning(warning); } } } //Remove all header.Node.InnerXml = ""; foreach(IText iText in textColl) { if(this._debugMode) this.LogNode(iText.Node, "Log IText node read from header"); header.TextContent.Add(iText); } return header; } catch(Exception ex) { AODLException exception = new AODLException("Exception while trying to create a Header."); exception.InMethod = AODLException.GetExceptionSourceInfo(new StackFrame(1, true)); exception.Node = headernode; exception.OriginalException = ex; throw exception; } return null; }
/// <summary> /// Gets the heading style as HTML. /// </summary> /// <param name="header">The header.</param> /// <returns></returns> public string GetHeadingStyleAsHtml(Header header) { try { if(header != null) { if(header.StyleName != null) { if(header.StyleName.Equals(Headings.Heading_20_1.ToString())) return this.HeaderHtmlStyles[1]; else if(header.StyleName.Equals(Headings.Heading_20_2.ToString())) return this.HeaderHtmlStyles[2]; else return this.HeaderHtmlStyles[0]; } } } catch(Exception ex) { AODLException exception = new AODLException("Exception while trying to build a HTML style string from a TextStyle."); exception.InMethod = AODLException.GetExceptionSourceInfo(new StackFrame(1, true)); exception.OriginalException = ex; if(header != null) if(header.Node != null) exception.Node = header.Node; throw exception; } return this.HeaderHtmlStyles[0]; }