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);
}
}