Unplanned
Last Updated: 25 May 2023 13:29 by ADMIN
Silvio
Created on: 10 Apr 2023 10:19
Category: WordsProcessing
Type: Bug Report
0
WordsProcessing: Updating a Table of Contents field with a custom TOC Style does not respect it

Updating a Table of Contents field with a custom TOC Style does not respect it.

As a workaround modify the style after the TOC fields are updated.

7 comments
ADMIN
Aleks
Posted on: 25 May 2023 13:29

Hi Adam,

Thank you for the provided feedback.
I am happy to hear that you have found a workaround.

Regards,
Aleks
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Adam-ACS
Posted on: 23 May 2023 23:58

Thanks Aleks, you helped point me in the right direction.  

 

I'm using 8 levels of TOC, so reducing all of the TOC down to one new TCStyle didn't work.

This workaround did, though (sorry, I don't know how to post the text as code_:

 

            var toc1StyleId = Telerik.Windows.Documents.Flow.Model.Styles.BuiltInStyleNames.GetTocStyleIdByIndex(1);
            document.StyleRepository.AddBuiltInStyle(toc1StyleId);

            var toc2StyleId = Telerik.Windows.Documents.Flow.Model.Styles.BuiltInStyleNames.GetTocStyleIdByIndex(2);
            document.StyleRepository.AddBuiltInStyle(toc2StyleId);

            var toc3StyleId = Telerik.Windows.Documents.Flow.Model.Styles.BuiltInStyleNames.GetTocStyleIdByIndex(3);
            document.StyleRepository.AddBuiltInStyle(toc3StyleId);

            var toc4StyleId = Telerik.Windows.Documents.Flow.Model.Styles.BuiltInStyleNames.GetTocStyleIdByIndex(4);
            document.StyleRepository.AddBuiltInStyle(toc4StyleId);

            var toc5StyleId = Telerik.Windows.Documents.Flow.Model.Styles.BuiltInStyleNames.GetTocStyleIdByIndex(5);
            document.StyleRepository.AddBuiltInStyle(toc5StyleId);

            var toc6StyleId = Telerik.Windows.Documents.Flow.Model.Styles.BuiltInStyleNames.GetTocStyleIdByIndex(6);
            document.StyleRepository.AddBuiltInStyle(toc6StyleId);

            var toc7StyleId = Telerik.Windows.Documents.Flow.Model.Styles.BuiltInStyleNames.GetTocStyleIdByIndex(7);
            document.StyleRepository.AddBuiltInStyle(toc7StyleId);

            var toc8StyleId = Telerik.Windows.Documents.Flow.Model.Styles.BuiltInStyleNames.GetTocStyleIdByIndex(8);
            document.StyleRepository.AddBuiltInStyle(toc8StyleId);

            Telerik.Windows.Documents.Flow.Model.Fields.FieldInfo tocField = editor.InsertField("TOC \\f a \\s Chapter \\d -", "«result»");

            document.UpdateFields();

            var tocStyle1 = document.StyleRepository.GetStyle(toc1StyleId);
            tocStyle1.ParagraphProperties.SpacingBefore.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle1.ParagraphProperties.SpacingAfter.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle1.ParagraphProperties.LeftIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.4);
            tocStyle1.ParagraphProperties.HangingIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.4);
            tocStyle1.ParagraphProperties.RightIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.55);
            tocStyle1.CharacterProperties.FontWeight.LocalValue = Telerik.Documents.Core.Fonts.FontWeights.Bold;
            tocStyle1.CharacterProperties.FontFamily.LocalValue = new Telerik.Documents.Common.Model.ThemableFontFamily("Arial");
            tocStyle1.CharacterProperties.FontSize.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(11);
            tocStyle1.CharacterProperties.UnderlinePattern.LocalValue = Telerik.Windows.Documents.Flow.Model.Styles.UnderlinePattern.Dotted;

            var tocStyle2 = document.StyleRepository.GetStyle(toc2StyleId);
            tocStyle2.ParagraphProperties.SpacingBefore.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle2.ParagraphProperties.SpacingAfter.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle2.ParagraphProperties.LeftIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.4);
            tocStyle2.ParagraphProperties.HangingIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.4);
            tocStyle2.ParagraphProperties.RightIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.55);
            tocStyle2.CharacterProperties.FontFamily.LocalValue = new Telerik.Documents.Common.Model.ThemableFontFamily("Arial");

            var tocStyle3 = document.StyleRepository.GetStyle(toc3StyleId);
            tocStyle3.ParagraphProperties.SpacingBefore.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle3.ParagraphProperties.SpacingAfter.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle3.ParagraphProperties.LeftIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.94);
            tocStyle3.ParagraphProperties.HangingIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.85);
            tocStyle3.ParagraphProperties.RightIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.55);
            tocStyle3.ParagraphProperties.ContextualSpacing.LocalValue = true;
            tocStyle3.CharacterProperties.FontFamily.LocalValue = new Telerik.Documents.Common.Model.ThemableFontFamily("Arial");

            var tocStyle4 = document.StyleRepository.GetStyle(toc4StyleId);
            tocStyle4.ParagraphProperties.SpacingBefore.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle4.ParagraphProperties.SpacingAfter.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle4.ParagraphProperties.LeftIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(1.58);
            tocStyle4.ParagraphProperties.HangingIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.64);
            tocStyle4.ParagraphProperties.RightIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.55);
            tocStyle4.ParagraphProperties.ContextualSpacing.LocalValue = true;
            tocStyle4.CharacterProperties.FontFamily.LocalValue = new Telerik.Documents.Common.Model.ThemableFontFamily("Arial");

            var tocStyle5 = document.StyleRepository.GetStyle(toc5StyleId);
            tocStyle5.ParagraphProperties.SpacingBefore.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle5.ParagraphProperties.SpacingAfter.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle5.ParagraphProperties.LeftIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(2.3);
            tocStyle5.ParagraphProperties.HangingIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.85);
            tocStyle5.ParagraphProperties.RightIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.55);
            tocStyle5.ParagraphProperties.ContextualSpacing.LocalValue = true;
            tocStyle5.CharacterProperties.FontFamily.LocalValue = new Telerik.Documents.Common.Model.ThemableFontFamily("Arial");

            var tocStyle6 = document.StyleRepository.GetStyle(toc6StyleId);
            tocStyle6.ParagraphProperties.SpacingBefore.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle6.ParagraphProperties.SpacingAfter.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle6.ParagraphProperties.LeftIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(3.1);
            tocStyle6.ParagraphProperties.HangingIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.8);
            tocStyle6.ParagraphProperties.RightIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.55);
            tocStyle6.ParagraphProperties.ContextualSpacing.LocalValue = true;
            tocStyle6.CharacterProperties.FontFamily.LocalValue = new Telerik.Documents.Common.Model.ThemableFontFamily("Arial");

            var tocStyle7 = document.StyleRepository.GetStyle(toc7StyleId);
            tocStyle7.ParagraphProperties.SpacingBefore.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle7.ParagraphProperties.SpacingAfter.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle7.ParagraphProperties.LeftIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(1.2);
            tocStyle7.ParagraphProperties.HangingIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(1.2);
            tocStyle7.ParagraphProperties.RightIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.55);
            tocStyle7.CharacterProperties.FontWeight.LocalValue = Telerik.Documents.Core.Fonts.FontWeights.Bold;
            tocStyle7.CharacterProperties.FontFamily.LocalValue = new Telerik.Documents.Common.Model.ThemableFontFamily("Arial");

            var tocStyle8 = document.StyleRepository.GetStyle(toc8StyleId);
            tocStyle8.ParagraphProperties.SpacingBefore.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle8.ParagraphProperties.SpacingAfter.LocalValue = Telerik.Windows.Documents.Media.Unit.PointToDip(12);
            tocStyle8.ParagraphProperties.LeftIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(1.2);
            tocStyle8.ParagraphProperties.HangingIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(1.2);
            tocStyle8.ParagraphProperties.RightIndent.LocalValue = Telerik.Windows.Documents.Media.Unit.InchToDip(0.55);
            tocStyle8.CharacterProperties.FontFamily.LocalValue = new Telerik.Documents.Common.Model.ThemableFontFamily("Arial");

            document.UpdateFields();
ADMIN
Aleks
Posted on: 11 May 2023 10:13

Hi Adam,

I replicated your case and I can recommend you try the following workaround as the TCField does not expect to receive the TOC Style and can be reset manually if needed until we find a proper solution:

Style tcStyle = new Style("TCStyle", StyleType.Paragraph);
tcStyle.Name = "TCStyle";
tcStyle.CharacterProperties.FontFamily.LocalValue = new ThemableFontFamily("Arial");
document.StyleRepository.Add(tcStyle);

List<FieldCharacter> fieldCharacters = document.EnumerateChildrenOfType<FieldCharacter>().Where(f => f.FieldCharacterType == FieldCharacterType.Start).Where(f => f.FieldInfo.Field is TcField).ToList();
foreach (FieldCharacter fieldCharacter in fieldCharacters)
{
    fieldCharacter.Paragraph.StyleId = "TCStyle";
}                                                                                                                                                                                                                               //by default the TОCStyle is not added so in order to have it set to Arial you will have to iterate the whole document

Hope this helps.

For any future updates please refer to this public item.

Should you need any further support, I remain at your disposal.

Regards,
Aleks
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Adam-ACS
Posted on: 08 May 2023 23:11

That worked, thanks!  I was able to control the intending and line spacing by utilizing the TOC 1, TOC 2, etc styles.

One thing that I noticed, though, is that setting the font to Arial does change it for the "Text" portion of the line, but not the page number.  That is still stuck at Calibri (Body).

TC "Text" [Switches ] }

 

var toc1StyleId = Telerik.Windows.Documents.Flow.Model.Styles.BuiltInStyleNames.GetTocStyleIdByIndex(1);

document.StyleRepository.AddBuiltInStyle(toc1StyleId);

Telerik.Windows.Documents.Flow.Model.Fields.FieldInfo tocField = editor.InsertField("TC \"" + item.SectionNumber + "\t" + item.Name.ToUpper() + "\" \\f a \\l 1 \\h", "«result»");

editor.InsertLine(item.SectionNumber + "\t" + item.Name.ToUpper());

Telerik.Windows.Documents.Flow.Model.Fields.FieldInfo tocField = editor.InsertField("TOC \\f a \\s Chapter \\d -", "«result»");

document.UpdateFields();

tocStyle1.CharacterProperties.FontFamily.LocalValue = new Telerik.Documents.Common.Model.ThemableFontFamily("Arial");

 

 

Attached Files:
ADMIN
Vladislav
Posted on: 08 May 2023 13:33

Hi Adam,

If the code snippet provided by Silvio does not answer your question and you need any additional information, you can open a separate ticket where you can provide detailed information on your use case. Please, note that this thread is public, and any data you provide here will be visible to anyone.

Regards,
Vladislav
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Silvio
Posted on: 08 May 2023 06:43

For the fix to work, we need to overwrite the TOC style after the field is updated. Here's the example:

var document = new RadFlowDocument();

var heading1StyleId = BuiltInStyleNames.GetHeadingStyleIdByIndex(1);
document.StyleRepository.AddBuiltInStyle(heading1StyleId);

var editor = new RadFlowDocumentEditor(document);

var listHeading = document.Lists.Add(ListTemplateType.NumberedHierarchical);
var listToc = document.Lists.Add(ListTemplateType.NumberedHierarchical);
            
var toc1StyleId = BuiltInStyleNames.GetTocStyleIdByIndex(1);
document.StyleRepository.AddBuiltInStyle(toc1StyleId);

var tocField = editor.InsertField(@"TOC \o \h ""1-1""", "Pending Update");

FlowExtensibilityManager.NumberingFieldsProvider = new NumberingFieldsProvider();

var section = new Section(document);
document.Sections.Add(section);

for (int i = 0; i < 10; i++)
{
      var paragraph = section.Blocks.AddParagraph();

      paragraph.Inlines.AddRun($"TEST - Test {i}");
      paragraph.StyleId = heading1StyleId;
      paragraph.ListId = listHeading.Id;
      paragraph.ListLevel = 0;
                
      section.Blocks.AddParagraph();
}

document.UpdateFields();

// This part needs to be placed after the last call that updates the TOC field
var myTocStyle = document.StyleRepository.GetStyle(toc1StyleId);
myTocStyle.ParagraphProperties.ListLevel.LocalValue = 0;
myTocStyle.ParagraphProperties.BackgroundColor.LocalValue = new ThemableColor(Colors.Blue);
myTocStyle.ParagraphProperties.ListId.LocalValue = listToc.Id;
myTocStyle.ParagraphProperties.TextAlignment.LocalValue = Alignment.Center;

var fileName = "output.docx";

using Stream output = new FileStream("output.docx", FileMode.OpenOrCreate);
var provider = new DocxFormatProvider();
provider.Export(document, output);
Adam-ACS
Posted on: 06 May 2023 21:39
Can you provide an example of the workaround?