繁体   English   中英

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

[英]WPF: The font will not change on a printed FlowDocument

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

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

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

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

pg.LineHeight = 0;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM