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.
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.
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);
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.
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");
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.
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);