Quantcast
Channel: Telerik Forums RSS
Viewing all articles
Browse latest Browse all 84751

Problem with paragraph styles

$
0
0

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:

publicclassDocumentCreator
{
    protectedreadonlyThemableFontFamily DefaultFontFamily = newThemableFontFamily(newFontFamily("Calibri"));
    protectedreadonlyThemableColor DefaultForeground = newThemableColor(Colors.Black);
    protectedreadonlydoubleDefaultFontSize = 14.0;
 
    publicvoidCreateDocument()
    {
        RadFlowDocument document = newRadFlowDocument();
 
        SetupStyles(document);
 
        CreateSection(document, "Document title");
 
        var docxProvider = newDocxFormatProvider();
        using(Stream stream = File.Create("Sample document.docx"))
        {
            docxProvider.Export(document, stream);
        }
    }
 
    publicvoidSetupStyles(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;
    }
 
    publicvoidCreateSection(RadFlowDocument document, stringtitle)
    {
        RadFlowDocumentEditor editor = newRadFlowDocumentEditor(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", newList<int> { 1, 2, 3, 4, 5 });
        InsertTable(section, editor, "Table 2", newList<int> { 2, 3, 4, 5, 6 });
        InsertTable(section, editor, "Table 3", newList<int> { 3, 4, 5, 6, 7 });
    }
 
    publicvoidInsertTable(Section section, RadFlowDocumentEditor editor, stringtitle, 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 = newTableWidthUnit(TableWidthUnitType.Fixed, 100);
        SetCellText(editor, cell, "Column 1", bold: true, background: newThemableColor(Colors.LightGray));
 
        cell = row.Cells.AddTableCell();
        cell.PreferredWidth = newTableWidthUnit(TableWidthUnitType.Fixed, 100);
        SetCellText(editor, cell, "Column 2", bold: true, background: newThemableColor(Colors.LightGray));
 
 
        foreach(var value invalues)
        {
            row = table.Rows.AddTableRow();
 
            cell = row.Cells.AddTableCell();
            SetCellText(editor, cell, ((char)('@'+ value)).ToString());
 
            cell = row.Cells.AddTableCell();
            SetCellText(editor, cell, value.ToString());
        }
    }
 
    protectedvoidSetCellText(RadFlowDocumentEditor editor, TableCell cell, stringtext, stringfontFamily = 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 = newPadding(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
                                                               : newThemableFontFamily(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);
    }
}

 


Viewing all articles
Browse latest Browse all 84751

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>