RadDocument - Programmatically create Word document with table of contents, sections and subsections

16 posts, 0 answers
  1. C
    C avatar
    43 posts
    Member since:
    Jul 2014

    Posted 01 Jul 2014 Link to this post

    How can I programmatically create, using C# and RadDocument, a Microsoft Word document with a cover page, table of contents, then body with several sections and subsections?

    - It is a simple document assembly operation
    - It is for a desktop WPF application, C#, .NET 4.5
    - The document is relatively short with less than 10 top level sections and less than 25 subsections
    - The number of sections is variable depending on the data entered in the rest of the desktop application
    - The document created should be in MS Word 2010 format if possible.
    - The document is created without a Telerik RichTextEdit control visible.  Currently, there is no Telerik RichTextEdit control used in the application.I've attached a sample Word 2010 document.

    Attached are the pages from the sample Word document with dummy data.


    The attached short 3 page Word document with dummy data gives the basic layout.
  2. C
    C avatar
    43 posts
    Member since:
    Jul 2014

    Posted 01 Jul 2014 Link to this post

    Please click on the images to expand to full size.  The png files are ordered by page number.
  3. UI for WPF is Visual Studio 2017 Ready
  4. C
    C avatar
    43 posts
    Member since:
    Jul 2014

    Posted 01 Jul 2014 in reply to C Link to this post

    We are  using the latest Telerik UI for WPF version. 
    Telerik.Windows.Documents 2014.4.617.45 
    Telerik.Windows.Dcouments.Core
    Telerik.Windows.Documents.FormatProviders.OpenXml
  5. C
    C avatar
    43 posts
    Member since:
    Jul 2014

    Posted 01 Jul 2014 in reply to C Link to this post

    I've seen the following forum posts.  

    How to Programattically Insert a Table of Contents
    http://www.telerik.com/forums/how-to-programatically-insert-a-table-of-contents

    Add a TOC from code - How to adapt this if we're not using a RadTextBox?
    http://www.telerik.com/forums/add-toc-from-codeRadDocumentEditor

    RadDocumentEditor help
    http://www.telerik.com/help/silverlight/radrichtextbox-features-raddocumenteditor.html
  6. C
    C avatar
    43 posts
    Member since:
    Jul 2014

    Posted 02 Jul 2014 in reply to C Link to this post

    Adding a field without using a RadDocumentEditor

    Insert Merge Field with Formatting programmatically
    http://www.telerik.com/forums/insert-merge-field-with-formatting-programatically
  7. Petya
    Admin
    Petya avatar
    975 posts

    Posted 03 Jul 2014 Link to this post

    Hello,

    Please check the attached application which demonstrate how you can create a document from code and populate it with the wanted data. You can also see how a TOC field can be inserted at a specific position. As mentioned in the forum thread you found, there is currently an issue with the page numbers in table of contents field which we are working on resolving so I omitted the page numbers from the field. Further, the application gives you the ability to show the document in a RadRichTextBox or export it to docx. 

    On a side note, in case you do not want to show the document in your UI, you could try using the RadWordsProcessing library instead. It is designed for code generation and manipulation of documents, provides export capabilities and the API is somewhat similar to the one of RadDocument. You can find the documentation on the matter here.

    I hope this is useful.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  8. C
    C avatar
    43 posts
    Member since:
    Jul 2014

    Posted 03 Jul 2014 in reply to Petya Link to this post

    Thank you.  Please consider adding this sample code to the help or online C# code samples. 
    I did find one thing about page number computations.  If documentEditor.UpdateAllFields() is called, the page numbers are incorrect (all TOC entries list page 3).  If UpdateAllFields() is not called, the page numbers are correct in the TOC (the last subsection is on page 4).

    private static void CreateWordDocInCodeWithToc()
    {
    //Create a Microsoft Word docx document in C# code without a UI RichTextBox control
    //Create Table of Contents (TOC) with sections and subsections

    RadDocument document = new RadDocument();
    document.LayoutMode = DocumentLayoutMode.Paged;

    #region Create cover page

    Section cover = new Section();
    Paragraph title = new Paragraph();
    title.Inlines.Add(new Span("Title Page Text") { FontSize = 30 });
    cover.Blocks.Add(title);

    document.Sections.Add(cover);

    #endregion

    #region CreateTOCSection

    //The TOC field will be added just before the document is written to a MS Word docx file
    Section tocSection = new Section();
    document.Sections.Add(tocSection);

    #endregion

    #region Create Document Content

    const string paragraphText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ";

    //TOC entries will be the list entries created below
    ListStyle newListStyle = DefaultListStyles.Numbered;
    DocumentList documentList = new DocumentList(newListStyle, document);

    Section content = new Section();

    content.Blocks.Add(CreateHeadingParagraph(documentList, "Section A Heading", 1));

    Paragraph body = new Paragraph();
    body.Inlines.Add(new Span("Paragraph 1 for Section A " + paragraphText));
    content.Blocks.Add(body);

    Paragraph body1 = new Paragraph();
    body1.Inlines.Add(new Span("Paragraph 2 for Section A " + paragraphText));
    content.Blocks.Add(body1);

    content.Blocks.Add(CreateHeadingParagraph(documentList, "Subsection A1 Heading", 2));

    Paragraph body2 = new Paragraph();
    body2.Inlines.Add(new Span("Paragraph 1 for Subsection A1 " + paragraphText));
    content.Blocks.Add(body2);

    content.Blocks.Add(CreateHeadingParagraph(documentList, "Subsection A2 Heading", 2));

    Paragraph body3 = new Paragraph();
    body3.Inlines.Add(new Span("Paragraph 1 for Subsection A2 " + paragraphText));
    content.Blocks.Add(body3);

    content.Blocks.Add(CreateHeadingParagraph(documentList, "Section B Heading", 1));

    Paragraph body4 = new Paragraph();
    body4.Inlines.Add(new Span("Paragraph 1 for Section B " + paragraphText));
    content.Blocks.Add(body4);

    Paragraph body5 = new Paragraph();
    body5.Inlines.Add(new Span("Paragraph 2 for Section B " + paragraphText));
    content.Blocks.Add(body5);

    content.Blocks.Add(CreateHeadingParagraph(documentList, "Subsection B1 Heading", 2));

    Paragraph body6 = new Paragraph();
    body6.Inlines.Add(new Span("Paragraph 1 for Subsection B1 " + paragraphText));
    content.Blocks.Add(body6);

    document.Sections.Add(content);

    #endregion

    #region InsertTOC Field into document

    TableOfContentsField field = new TableOfContentsField();
    RadDocumentEditor documentEditor = new RadDocumentEditor(document);

    //Go to the section where the TOC field is to be inserted
    documentEditor.Document.CaretPosition.MoveToStartOfDocumentElement(tocSection);
    documentEditor.InsertField(field, FieldDisplayMode.Result);
    documentEditor.UpdateAllFields(FieldDisplayMode.Result); //compute page numbers for TOC

    #endregion

    #region Export to a Microsoft Word docx document

    using (Stream outStream = new FileStream("output.docx", FileMode.Create))
    {
    DocxFormatProvider provider = new DocxFormatProvider();
    provider.Export(document, outStream);
    }

    #endregion
    }

    private static Paragraph CreateHeadingParagraph(DocumentList documentList, string headingText, int headingLevel)
    {
    Paragraph heading3 = new Paragraph() {StyleName = RadDocumentDefaultStyles.GetHeadingStyleNameByIndex(headingLevel) };
    heading3.Inlines.Add(new Span(headingText));
    heading3.ListId = documentList.ID; //add it to the TOC list
    if (headingLevel >= 2)
    heading3.ListLevel = headingLevel -1;

    return heading3;
    }
  9. Petya
    Admin
    Petya avatar
    975 posts

    Posted 07 Jul 2014 Link to this post

    Hello,

    Thank you for sharing your findings and posting your code. We will add an end-to-end example for creating and exporting a document to our SDK repository as soon as possible.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  10. C
    C avatar
    43 posts
    Member since:
    Jul 2014

    Posted 28 Jul 2014 in reply to Petya Link to this post

    Hello,

    How can I set the font to use Calibri 12.0 point black color for both the Table of Contents entries as well as the first level of section headings?  The subsection heading in the document body would need to be Calabri 9.0 point black.

    I've tried the following methods without success:
      a. Set font for the Span added to the paragraph which is the header in CreateHeadingParagraph
      b. Change the list font in DocumentList.Style.Levels.Items[0]
      c. Add a new style definition to Document.Styles and use this as Paragraph.StyleName when adding the paragraph for the TOC entry - This style did not show up in the list of styles in the ribbon
      d. Same as C but add it to Document.StyleRepositiory - This style did not show up in the list of styles in the ribbon

    Sample code is included below.

    Also, how can I set it up to use the numbering like:
      1.0 Text_A ................................................................. 1
            1.1. Text_B ...........................................................2
            1.2. Text_C ...........................................................2
      2.0.  Text_D ................................................................3
      3.0.  Text_E ................................................................3

    Call CreateWordDocInCodeWithToc()

    internal static class TestToc2
        {
            internal static void CreateWordDocInCodeWithToc()
            {
                //Create a Microsoft Word docx document in C# code without a UI RichTextBox control
                //Create Table of Contents (TOC) with sections and subsections           
                RadDocument document = new RadDocument();
                document.LayoutMode = DocumentLayoutMode.Paged;           

                #region Create cover page           
               
                Section cover = new Section();
                Paragraph title = new Paragraph();
                title.Inlines.Add(new Span("Title Page Text") { FontSize = 30 });
                cover.Blocks.Add(title);           
                document.Sections.Add(cover);           

                #endregion            

                #region CreateTOCSection            
                
                //The TOC field will be added just before the document is written to a MS Word docx file
                Section tocSection = new Section();
                document.Sections.Add(tocSection);           

                #endregion           

                #region Create Document Content           

               const string paragraphText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ";           

                //TOC entries will be the list entries created below
                DocumentList documentList = new DocumentList(DefaultListStyles.NumberedHierarchical, document);           
               
                Section content = new Section();           
                content.Blocks.Add(CreateHeadingParagraph(documentList, "Section A Heading", 1));           
                Paragraph body = new Paragraph();
                body.Inlines.Add(new Span("Paragraph 1 for Section A " + paragraphText));
                content.Blocks.Add(body);           
                Paragraph body1 = new Paragraph();
                body1.Inlines.Add(new Span("Paragraph 2 for Section A " + paragraphText));
                content.Blocks.Add(body1);            
                content.Blocks.Add(CreateHeadingParagraph(documentList, "Subsection A1 Heading", 2));           

                Paragraph body2 = new Paragraph();
                body2.Inlines.Add(new Span("Paragraph 1 for Subsection A1 " + paragraphText));
                content.Blocks.Add(body2);
                content.Blocks.Add(CreateHeadingParagraph(documentList, "Subsection A2 Heading", 2));
                Paragraph body3 = new Paragraph();
                body3.Inlines.Add(new Span("Paragraph 1 for Subsection A2 " + paragraphText));
                content.Blocks.Add(body3);
                content.Blocks.Add(CreateHeadingParagraph(documentList, "Section B Heading", 1));
                Paragraph body4 = new Paragraph();
                body4.Inlines.Add(new Span("Paragraph 1 for Section B " + paragraphText));
                content.Blocks.Add(body4);
                 Paragraph body5 = new Paragraph();
                body5.Inlines.Add(new Span("Paragraph 2 for Section B " + paragraphText));
                content.Blocks.Add(body5); 
               content.Blocks.Add(CreateHeadingParagraph(documentList, "Subsection B1 Heading", 2));
                Paragraph body6 = new Paragraph();
                body6.Inlines.Add(new Span("Paragraph 1 for Subsection B1 " + paragraphText));
                content.Blocks.Add(body6);
                document.Sections.Add(content);
                #endregion           

    #region InsertTOC Field into document           
    TableOfContentsField field = new TableOfContentsField();
                RadDocumentEditor documentEditor = new RadDocumentEditor(document); 

               //Go to the section where the TOC field is to be inserted
                documentEditor.Document.CaretPosition.MoveToStartOfDocumentElement(tocSection);
                documentEditor.InsertField(field, FieldDisplayMode.Result); 

               //UpdateAllFields will incorrectly compute the page numbers
                //documentEditor.UpdateAllFields(FieldDisplayMode.Result); //compute page numbers for TOC  

              #endregion            #region Export to a Microsoft Word docx document          

      using (Stream outStream = new FileStream("output.docx", FileMode.Create))
                {
                    DocxFormatProvider provider = new DocxFormatProvider();
                    provider.Export(document, outStream);
                }         

       #endregion
            }       

    private static Paragraph CreateHeadingParagraph(DocumentList documentList, string headingText, int headingLevel)
            {
                Paragraph heading3 = new Paragraph() { StyleName = RadDocumentDefaultStyles.GetHeadingStyleNameByIndex(headingLevel) };
                heading3.Inlines.Add(new Span(headingText));
                heading3.ListId = documentList.ID; //add it to the TOC list
                if (headingLevel >= 2)
                    heading3.ListLevel = headingLevel - 1;            return heading3;
            }
        }
  11. C
    C avatar
    43 posts
    Member since:
    Jul 2014

    Posted 28 Jul 2014 in reply to C Link to this post

    Here is a sample image. 
  12. Petya
    Admin
    Petya avatar
    975 posts

    Posted 31 Jul 2014 Link to this post

    Hello,

    When a table of content field is added to a document, the levels in the field are styled with predefined styles called toc1, toc2, etc. That being said, if you want to modify the way TOC looks you need to modify those styles by accessing them from the styles repository and changing their properties.
    StyleDefinition toc1Style = document.StyleRepository[RadDocumentDefaultStyles.GetTocStyleNameByIndex(1)];
    toc1Style.SpanProperties.FontSize = 20;
    toc1Style.SpanProperties.ForeColor = Colors.Yellow;

    The same applies for the heading styles - if you want to modify the way they look you need to access them and change their properties.

    On the other hand, if you want to create a new style you can do it as demonstrated in the Styles article. To use the style you need to add it to the styles repository of the document.
    this.editor.Document.StyleRepository.Add(linkedParagraphStyle);
    If you want to add the style in the Quick Styles gallery as well, you have to set its IsPrimary property to true.

    I hope this is useful.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  13. C
    C avatar
    43 posts
    Member since:
    Jul 2014

    Posted 31 Jul 2014 in reply to Petya Link to this post

    Hi,

    Thank you for the help.  I tried it out and found that Heading 1 and Heading 2 styles do not get updated when you change their font sizes.

    Here are the Font sizes before and after updating the TOC1, TOC2, Heading1 and Heading2 font styles.   Notice that the Heading 1 and 2 did not get updated.

    MS Word shows the following values in the styles ribbon entries
      TOC1 - Calibri 9 point underline
      TOC2 - Calibri 6 point
      Heading 1 - Cambria (Heading) 14 point 
      Heading 2 - Cambria (Heading) 13 point

    See the SetHeadingAndTocStyle method below.

    TOC 1 - before (TOC1) - 16 Verdana #FF000000
    TOC 1 - after (TOC1) - 12 Calibri #FF000000 

    TOC 2 - before (TOC2) - 16 Verdana #FF000000
    TOC 2 - after (TOC2) - 8 Calibri #FF000000

    Heading 1 - before (Heading1) - 18.6666660308838 Verdana #FF4F81BD
    Heading 1 - after (Heading1) - 18.6666660308838 Verdana #FF4F81BD

    Heading 2 - before (Heading2) - 17.3333339691162 Verdana #FF4F81BD
    Heading 2 - after (Heading2) - 17.3333339691162 Verdana #FF4F81BD

    StyleRepositiory defaultDocumentStyle 16
    StyleRepositiory Normal 16
    StyleRepositiory TableNormal 16
    StyleRepositiory TOC1 12
    StyleRepositiory TOC2 8
    StyleRepositiory Heading1 18.6666660308838
    StyleRepositiory Heading1Char 18.6666660308838
    StyleRepositiory Heading2 17.3333339691162
    StyleRepositiory Heading2Char 17.3333339691162

    Here is the sample code to create the Word document.

    internal static class TestToc2
    {
    internal static void CreateWordDocInCodeWithToc()
    {
     //Create a Microsoft Word docx document in C# code without a UI RichTextBox control
     //Create Table of Contents (TOC) with sections and subsections 

    RadDocument document = new RadDocument();
     document.LayoutMode = DocumentLayoutMode.Paged; 

    //-----------------------------------------------------------------------------------
     //setup font, font size, bold, etc style for Table of Contents (TOC) level 1 and 2 entries
     SetHeadingAndTocFontStyle(document); #region Create cover page 

    Section cover = new Section();
     Paragraph title = new Paragraph();
     title.Inlines.Add(new Span("Title Page Text") { FontSize = 30 });
     cover.Blocks.Add(title); 
    document.Sections.Add(cover); 

    #endregion 

    #region CreateTOCSection 
    //The TOC field will be added just before the document is written to a MS Word docx file
     Section tocSection = new Section();
     document.Sections.Add(tocSection); 

    #endregion 

    #region Create Document Content 

    const string paragraphText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. "; 

    //TOC entries will be the list entries created below
     DocumentList documentList = new DocumentList(DefaultListStyles.NumberedHierarchical, document); 

    Section content = new Section(); 
    content.Blocks.Add(CreateHeadingParagraph(documentList, "Section A Heading", 1)); 
    Paragraph body = new Paragraph();
     body.Inlines.Add(new Span("Paragraph 1 for Section A " + paragraphText));
     content.Blocks.Add(body); 

    Paragraph body1 = new Paragraph();
     body1.Inlines.Add(new Span("Paragraph 2 for Section A " + paragraphText));
     content.Blocks.Add(body1); 

    content.Blocks.Add(CreateHeadingParagraph(documentList, "Subsection A1 Heading", 2)); 

    Paragraph body2 = new Paragraph();
     body2.Inlines.Add(new Span("Paragraph 1 for Subsection A1 " + paragraphText));
     content.Blocks.Add(body2); 

    content.Blocks.Add(CreateHeadingParagraph(documentList, "Subsection A2 Heading", 2)); 

    Paragraph body3 = new Paragraph();
     body3.Inlines.Add(new Span("Paragraph 1 for Subsection A2 " + paragraphText));
     content.Blocks.Add(body3); 

    content.Blocks.Add(CreateHeadingParagraph(documentList, "Section B Heading", 1)); 

    Paragraph body4 = new Paragraph();
     body4.Inlines.Add(new Span("Paragraph 1 for Section B " + paragraphText));
     content.Blocks.Add(body4); 

    Paragraph body5 = new Paragraph();
     body5.Inlines.Add(new Span("Paragraph 2 for Section B " + paragraphText));
     content.Blocks.Add(body5); 

    content.Blocks.Add(CreateHeadingParagraph(documentList, "Subsection B1 Heading", 2)); 

    Paragraph body6 = new Paragraph();
     body6.Inlines.Add(new Span("Paragraph 1 for Subsection B1 " + paragraphText));
     content.Blocks.Add(body6); 

    document.Sections.Add(content); 

    #endregion 

    #region InsertTOC Field into document 

    TableOfContentsField field = new TableOfContentsField();
     RadDocumentEditor documentEditor = new RadDocumentEditor(document); 

    //Go to the section where the TOC field is to be inserted
     documentEditor.Document.CaretPosition.MoveToStartOfDocumentElement(tocSection);
     documentEditor.InsertField(field, FieldDisplayMode.Result); //UpdateAllFields will incorrectly compute the page numbers
     //documentEditor.UpdateAllFields(FieldDisplayMode.Result); //compute page numbers for TOC 

    #endregion 

    #region Export to a Microsoft Word docx document 

    using (Stream outStream = new FileStream("output.docx", FileMode.Create))
     {
      DocxFormatProvider provider = new DocxFormatProvider();
      provider.Export(document, outStream);
     } 

    #endregion

    }private static void SetHeadingAndTocFontStyle(RadDocument document)
    {
     FontFamily font = new FontFamily("Calibri");
     Color fontColor = Colors.Black; string styleName;
     StyleDefinition styleTmp; //styleName = RadDocumentDefaultStyles.NormalStyleName;

     //styleTmp = document.StyleRepository[styleName];
     //PrintStyleInfo("Normal - before", styleName, styleTmp);
     //styleTmp.SpanProperties.FontFamily = font;
     //styleTmp.SpanProperties.FontSize = 8.0;
     //PrintStyleInfo("Normal - after", styleName, styleTmp);
     
     styleName = RadDocumentDefaultStyles.GetTocStyleNameByIndex(1);
     styleTmp = document.StyleRepository[styleName];
     PrintStyleInfo("TOC 1 - before", styleName, styleTmp);
     SetSpanFontStyle(styleTmp.SpanProperties, 12.0, fontColor, font, true, false, false);
     styleTmp.IsPrimary = true;      //show in list of styles in Microsoft Word ribbon
     PrintStyleInfo("TOC 1 - after", styleName, styleTmp); 

    styleName = RadDocumentDefaultStyles.GetTocStyleNameByIndex(2);
     styleTmp = document.StyleRepository[styleName];
     PrintStyleInfo("TOC 2 - before", styleName, styleTmp);
     SetSpanFontStyle(styleTmp.SpanProperties, 8.0, fontColor, font, false, false, false);
     styleTmp.IsPrimary = true;      //show in list of styles in Microsoft Word ribbon
     PrintStyleInfo("TOC 2 - after", styleName, styleTmp); 

    styleName = RadDocumentDefaultStyles.GetHeadingStyleNameByIndex(1);
     styleTmp = document.StyleRepository[styleName];
     PrintStyleInfo("Heading 1 - before", styleName, styleTmp);
     SetSpanFontStyle(styleTmp.SpanProperties, 12.0, fontColor, font, false, false, false);
     styleTmp.IsPrimary = true;      //show in list of styles in Microsoft Word ribbon
     PrintStyleInfo("Heading 1 - after", styleName, styleTmp); 

    styleName = RadDocumentDefaultStyles.GetHeadingStyleNameByIndex(2);
     styleTmp = document.StyleRepository[styleName];
     PrintStyleInfo("Heading 2 - before", styleName, styleTmp);
     SetSpanFontStyle(styleTmp.SpanProperties, 8.0, fontColor, font, false, false, false);
     styleTmp.IsPrimary = true;      //show in list of styles in Microsoft Word ribbon
     PrintStyleInfo("Heading 2 - after", styleName, styleTmp); 

    foreach (StyleDefinition sd in document.StyleRepository)
     {
      System.Diagnostics.Debug.WriteLine("StyleRepositiory " + sd.Name + " " + sd.SpanProperties.FontSize.ToString());
     } 

    foreach (StyleDefinition sd in document.Styles)
     {
      System.Diagnostics.Debug.WriteLine("Styles " + sd.Name + " " + sd.SpanProperties.FontSize.ToString());
     }
    }

    private static void PrintStyleInfo(string prefixMsg, string styleName, StyleDefinition styleTmp)
    {
     SpanProperties s = styleTmp.SpanProperties;
     System.Diagnostics.Debug.WriteLine(prefixMsg + " (" + styleName + ") - " + s.FontSize.ToString() + " " + s.FontFamily.ToString() + " " + s.ForeColor.ToString());
    }

    private static void SetSpanFontStyle(SpanProperties span, double fontSize, Color fontColor, FontFamily fontFamily, bool isUnderline, bool isBold, bool isItalic)
    {
     if (span == null)       //span should not be null
         return; 

    System.Diagnostics.Debug.Assert((fontSize > 0.0) && (fontSize <= 500.0));
     System.Diagnostics.Debug.Assert(fontFamily != null); span.FontSize = fontSize;

     span.ForeColor = fontColor;
     span.FontFamily = fontFamily;
     
     if (isUnderline == true)
      span.UnderlineDecoration = UnderlineTypes.Line;
     
     if (isBold == true)
      span.FontWeight = FontWeights.Bold; 

    if (isItalic == true)
      span.FontStyle = FontStyles.Italic;
    }

    private static Paragraph CreateHeadingParagraph(DocumentList documentList, string headingText, int headingLevel)
    {
     Paragraph heading3 = new Paragraph() { StyleName = RadDocumentDefaultStyles.GetHeadingStyleNameByIndex(headingLevel) };

     heading3.Inlines.Add(new Span(headingText));
     heading3.ListId = documentList.ID; //add it to the TOC list

     if (headingLevel >= 2)
      heading3.ListLevel = headingLevel - 1; return heading3;
    }
    }


  14. C
    C avatar
    43 posts
    Member since:
    Jul 2014

    Posted 31 Jul 2014 in reply to C Link to this post

    My main questions are 

    - Why does setting the font size for TOC1, TOC2, Heading1 and Heading 2 does not change the font size displayed in Visual Studio?

    - Why does a different font size display in MS Word for TOC1 and TOC2?

    - Why does the font name for Heading1 and Heading2 not change to Calibri?

    Thank you.
  15. C
    C avatar
    43 posts
    Member since:
    Jul 2014

    Posted 31 Jul 2014 in reply to C Link to this post

    I'm trying to set TOC1 to 12 point Calibri,
    TOC2 to 8 point Calibri,
    Heading1 to 12 point Calibri and
    Heading 2 to 8 point Calibri.
  16. Petya
    Admin
    Petya avatar
    975 posts

    Posted 05 Aug 2014 Link to this post

    Hello,

    The Heading styles are actually of type Linked style which, as explained in the Styles article, means they basically contain two style definitions. To modify the span properties of a linked style you need to access  them through the LinkedStyle property:
    SetSpanFontStyle(styleTmp.LinkedStyle.SpanProperties, 12.0, fontColor, font, false, false, false);

    When it comes to the values you are observing for the font size properties of the styles, this is related to the fact that RadRichTextBox's default measuring is not point, but DIP. This means that to specify a size as 12pt you'd need to convert the value to DIP. There is actually a pretty handy class in the Telerik.Windows.Documents.Model namespace that can help you in this task:
    System.Diagnostics.Debug.Assert((fontSize > 0.0) && (fontSize <= 500.0));
    System.Diagnostics.Debug.Assert(fontFamily != null);
    span.FontSize = Unit.PointToDip(fontSize);

    Let me know if you need additional information regarding this.

    Regards,
    Petya
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  17. C
    C avatar
    43 posts
    Member since:
    Jul 2014

    Posted 01 Oct 2014 in reply to Petya Link to this post

    Thank you for all of the help.
Back to Top
UI for WPF is Visual Studio 2017 Ready