J'essaie d'imprimer le contenu d'une boîte de texte enrichi. Je le fais de la manière suivante :
- Obtenir un
TextRange
de laFlowDocument
. - Créer un nouveau
FlowDocument
avec une police plus petite en utilisant leTextRange
. - Envoyez cette nouvelle
FlowDocument
à l'imprimante.
Mon problème, c'est que la police ne semble pas changer. Je voudrais qu'elle passe à la taille 8. Au lieu de cela, elle reste à une taille fixe. Voici mon code :
private void button_Print_Click(object sender, RoutedEventArgs e)
{
IDocumentPaginatorSource ps = null;
FlowDocument fd = new FlowDocument();
PrintDialog pd = new PrintDialog();
Paragraph pg = new Paragraph();
Style style = new Style(typeof(Paragraph));
Run r = null;
string text = string.Empty;
// get the text
text = new TextRange(
this.richTextBox_Info.Document.ContentStart,
this.richTextBox_Info.Document.ContentEnd).Text;
// configure the style of the flow document
style.Setters.Add(new Setter(Block.MarginProperty, new Thickness(0)));
fd.Resources.Add(typeof(Paragraph), style);
// style the paragraph
pg.LineHeight = 0;
pg.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
pg.FontFamily = new FontFamily("Courier New");
pg.TextAlignment = TextAlignment.Left;
pg.FontSize = 8;
// create the paragraph
r = new Run(text);
r.FontFamily = new FontFamily("Courier New");
r.FontSize = 8;
pg.Inlines.Add(r);
// add the paragraph to the document
fd.Blocks.Add(pg);
ps = fd;
// format the page
fd.PagePadding = new Thickness(50);
fd.ColumnGap = 0;
fd.ColumnWidth = pd.PrintableAreaWidth;
// print the document
if (pd.ShowDialog().Value == true)
{
pd.PrintDocument(ps.DocumentPaginator, "Information Box");
}
}
J'aimerais ajouter que la modification de la police fonctionne parfaitement pour le document de flux lorsqu'il se trouve à l'intérieur de la boîte de texte enrichi. Cependant, lorsque je le fais de manière programmatique (comme indiqué ci-dessus), je rencontre des problèmes.