This is a migrated thread and some comments may be shown as answers.

Problem with paragraph styles

4 Answers 810 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
Cleanwatts
Top achievements
Rank 1
Cleanwatts asked on 31 Mar 2016, 06:34 PM

I'm creating a document that consists of several tables with a simple title paragraph that is supposed to be "Heading2".

My code is something like this:

 

RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
var paragraph = section.Blocks.AddParagraph();
paragraph.StyleId = BuiltInStyleNames.GetHeadingStyleIdByIndex(2);
editor.MoveToParagraphStart();
editor.InsertText("Title");
var table = section.Blocks.AddTable();
FillTable(editor, table);

 

 

 

The table is filled in much the same way: paragraphs are added to the cells, and text is added to these paragraphs.

This block is inside a for loop and is executed as many times as needed. Everything is as it should be except for the styles of the title paragraphs: the first one is ok, but all the other ones have the same style as the last cell of the previous table. If I open the resulting document using Word, I see that the style is indeed applied to the paragraphs but the character formatting is not what it should be.

I think I could go around by specifying all character formatting manually for each of the title paragraphs, but that is what the styles are for.

Am I doing anything wrong?

4 Answers, 1 is accepted

Sort by
0
Cleanwatts
Top achievements
Rank 1
answered on 01 Apr 2016, 09:56 AM

I have already found the solution to my problem: I was creating a RadFlowDocumentEditor instance in the method that fills the document and sharing it with every other method (adding the title, filling the table).

A better representation of the code I was using is:

var editor = new RadFlowDocumentEditor(document);
var section = document.Sections.AddSection();
 
foreach (item)
{
    AddTitle(editor, section);
    var table = section.Blocks.AddTable();
    FillTable(editor, table);
}

If I don't share the RadFlowDocumentEditor instance between the methods (each method creates its own instance) than the problem goes away. Something like this:

var section = document.Sections.AddSection();
  
foreach (item)
{
    AddTitle(document, section);
    var table = section.Blocks.AddTable();
    FillTable(document, table);
}

It does seem like I'm creating the same object many times, using it for one operation and disposing of it almost instantly. Shouldn't I be able to share that object between the methods?

0
Tanya
Telerik team
answered on 05 Apr 2016, 10:18 AM
Hi,

The described issue seems pretty strange - I tested a similar scenario and it works as expected on our end. Could you please send us the code of the AddTitle() and FillTable() methods so we can test the exact scenario?

If you would like, you could open a support ticket where you can attach a sample project that shows the issue you are facing. It would be much helpful for us to check what may cause this behavior.

Regards,
Tanya
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Cleanwatts
Top achievements
Rank 1
answered on 08 Apr 2016, 05:49 PM

Hi,

The code I'm using in my project is far too complex to include here, but I've managed to reproduce the problem with some simple code:

public class DocumentCreator
{
    protected readonly ThemableFontFamily DefaultFontFamily = new ThemableFontFamily(new FontFamily("Calibri"));
    protected readonly ThemableColor DefaultForeground = new ThemableColor(Colors.Black);
    protected readonly double DefaultFontSize = 14.0;
 
    public void CreateDocument()
    {
        RadFlowDocument document = new RadFlowDocument();
 
        SetupStyles(document);
 
        CreateSection(document, "Document title");
 
        var docxProvider = new DocxFormatProvider();
        using (Stream stream = File.Create("Sample document.docx"))
        {
            docxProvider.Export(document, stream);
        }
    }
 
    public void SetupStyles(RadFlowDocument document)
    {
        var heading1 = document.StyleRepository.AddBuiltInStyle(BuiltInStyleNames.GetHeadingStyleIdByIndex(1));
        heading1.CharacterProperties.FontFamily.LocalValue = DefaultFontFamily;
        heading1.CharacterProperties.FontSize.LocalValue = 32.0;
        heading1.CharacterProperties.FontWeight.LocalValue = FontWeights.Bold;
        heading1.CharacterProperties.ForegroundColor.LocalValue = DefaultForeground;
 
        var heading2 = document.StyleRepository.AddBuiltInStyle(BuiltInStyleNames.GetHeadingStyleIdByIndex(2));
        heading2.CharacterProperties.FontFamily.LocalValue = DefaultFontFamily;
        heading2.CharacterProperties.FontSize.LocalValue = 24.0;
        heading2.CharacterProperties.FontWeight.LocalValue = FontWeights.Bold;
        heading2.CharacterProperties.ForegroundColor.LocalValue = DefaultForeground;
 
        document.DefaultStyle.CharacterProperties.FontFamily.LocalValue = DefaultFontFamily;
        document.DefaultStyle.CharacterProperties.FontSize.LocalValue = DefaultFontSize;
        document.DefaultStyle.CharacterProperties.FontWeight.LocalValue = FontWeights.Normal;
        document.DefaultStyle.CharacterProperties.ForegroundColor.LocalValue = DefaultForeground;
    }
 
    public void CreateSection(RadFlowDocument document, string title)
    {
        RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
 
        Section section = document.Sections.AddSection();
        section.SectionType = SectionType.Continuous;
 
        var paragraph = section.Blocks.AddParagraph();
        editor.MoveToParagraphStart(paragraph);
        paragraph.StyleId = BuiltInStyleNames.GetHeadingStyleIdByIndex(1);
        editor.InsertText(title);
 
        InsertTable(section, editor, "Table 1", new List<int> { 1, 2, 3, 4, 5 });
        InsertTable(section, editor, "Table 2", new List<int> { 2, 3, 4, 5, 6 });
        InsertTable(section, editor, "Table 3", new List<int> { 3, 4, 5, 6, 7 });
    }
 
    public void InsertTable(Section section, RadFlowDocumentEditor editor, string title, IList<int> values)
    {
        var paragraph = section.Blocks.AddParagraph();
        editor.MoveToParagraphStart(paragraph);
        paragraph.StyleId = BuiltInStyleNames.GetHeadingStyleIdByIndex(2);
        editor.InsertText(title);
 
        Table table = section.Blocks.AddTable();
 
        var row = table.Rows.AddTableRow();
 
        var cell = row.Cells.AddTableCell();
        cell.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Fixed, 100);
        SetCellText(editor, cell, "Column 1", bold: true, background: new ThemableColor(Colors.LightGray));
 
        cell = row.Cells.AddTableCell();
        cell.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Fixed, 100);
        SetCellText(editor, cell, "Column 2", bold: true, background: new ThemableColor(Colors.LightGray));
 
 
        foreach (var value in values)
        {
            row = table.Rows.AddTableRow();
 
            cell = row.Cells.AddTableCell();
            SetCellText(editor, cell, ((char)('@' + value)).ToString());
 
            cell = row.Cells.AddTableCell();
            SetCellText(editor, cell, value.ToString());
        }
    }
 
    protected void SetCellText(RadFlowDocumentEditor editor, TableCell cell, string text, string fontFamily = null,
                               double? fontSize = null, bool? bold = null, bool? italic = null, TableCellBorders borders = null,
                               ThemableColor background = null, ThemableColor foreground = null, Alignment? horizontalAlignment = null,
                               VerticalAlignment? verticalAlignment = null)
    {
        cell.IgnoreCellMarkerInRowHeightCalculation = true;
        cell.Padding = new Padding(1);
 
        var paragraph = cell.Blocks.AddParagraph();
        paragraph.StyleId = BuiltInStyleNames.NormalStyleId;
        paragraph.Properties.SpacingBefore.LocalValue = 0;
        paragraph.Properties.SpacingAfter.LocalValue = 0;
 
        editor.MoveToParagraphStart(paragraph);
 
        if (borders != null)
            cell.Borders = borders;
 
        if (background != null)
            cell.Shading.BackgroundColor = background;
 
        editor.CharacterFormatting.FontFamily.LocalValue = string.IsNullOrEmpty(fontFamily)
                                                               ? DefaultFontFamily
                                                               : new ThemableFontFamily(fontFamily);
 
        editor.CharacterFormatting.FontSize.LocalValue = fontSize ?? DefaultFontSize;
 
        editor.CharacterFormatting.FontWeight.LocalValue = (bold ?? false) ? FontWeights.Bold : FontWeights.Normal;
 
        editor.CharacterFormatting.FontStyle.LocalValue = (italic ?? false) ? FontStyles.Italic : FontStyles.Normal;
 
        editor.CharacterFormatting.ForegroundColor.LocalValue = foreground ?? DefaultForeground;
 
        cell.VerticalAlignment = verticalAlignment != null ? verticalAlignment.Value : VerticalAlignment.Center;
 
        paragraph.TextAlignment = horizontalAlignment != null ? horizontalAlignment.Value : Alignment.Center;
 
        editor.InsertText(string.IsNullOrEmpty(text) ? " " : text);
    }
}

 

0
Tanya
Telerik team
answered on 13 Apr 2016, 10:47 AM
Hello,

I checked the code and noticed hat in the SetCellText() method the local character properties of RadFlowDocumentEditor are modified, which reflects everything inserted through the editor as the local properties have higher priority when evaluating the style of the text.

If you would like to set different properties to a particular text, you could create a Run and apply the desired setting on it.
Run run = paragraph.Inlines.AddRun();
run.Text = string.IsNullOrEmpty(text) ? " " : text;
run.Properties.FontFamily.LocalValue = string.IsNullOrEmpty(fontFamily)
                                                        ? DefaultFontFamily
                                                        : new ThemableFontFamily(fontFamily);
run.Properties.FontSize.LocalValue = fontSize ?? DefaultFontSize;
run.Properties.FontWeight.LocalValue = (bold ?? false) ? FontWeights.Bold : FontWeights.Normal;
run.Properties.FontStyle.LocalValue = (italic ?? false) ? FontStyles.Italic : FontStyles.Normal;
run.Properties.ForegroundColor.LocalValue = foreground ?? DefaultForeground;

You could find more details on how the styles work in our documentation on that topic.

Regards,
Tanya
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
WordsProcessing
Asked by
Cleanwatts
Top achievements
Rank 1
Answers by
Cleanwatts
Top achievements
Rank 1
Tanya
Telerik team
Share this question
or