WPF:打印的FlowDocument上的字体不会更改

Sno*_*oop 9 c# wpf

我正在尝试打印富文本框的内容.我通过以下方式做到这一点:

  1. TextRange从中获得一个FlowDocument.
  2. FlowDocument使用更小的字体创建一个新的TextRange.
  3. 将此新内容发送FlowDocument到打印机.

我的问题是,字体似乎没有改变.我希望它下降到8号.相反,它仍保持固定的大小.这是我的代码:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

我想补充一点,当流文档位于富文本框内时,更改字体的效果非常好.但是,当我以编程方式执行此操作时(如上所示),我遇到了问题.

小智 2

我尝试你的代码,发现当我删除这一行然后将 r.FontSize 更改为 50 时,它似乎有效。

pg.LineHeight = 0;
Run Code Online (Sandbox Code Playgroud)