WPF:打印FlowDocument时字体不会改变

9

我正在尝试打印富文本框的内容,我通过以下方式实现:

  1. FlowDocument 获取一个 TextRange
  2. 使用 TextRange 创建一个新的 FlowDocument,并使用较小的字体。
  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");
    }
}

我想补充一点,当流文档在富文本框中时,更改字体可以正常工作。但是,当我以编程方式执行它(如上所示),我会遇到问题。


当你说“打印”时,你是指打印在纸张上还是输出到XPS文件或者其他打印机? - 123 456 789 0
@III 使用 Microsoft 内置的 PDF 打印机将内容打印成 Adobe PDF。 - Snoop
如果使用大号字体会发生什么 - 能行吗?使用不同字体家族(例如 Times New Roman)的字体大小为8会怎么样? - Andrew Stephens
@StevieV 对我来说,上面的代码在 pg.LineHeight = 0; 行抛出了 ArgumentException 异常。 - Suresh
1个回答

2
我尝试了您的代码,发现当我删除这一行并将 r.FontSize 更改为 50 时,它似乎有效。
pg.LineHeight = 0;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接