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

时间:2016-08-03 14:06:06

标签: c# wpf

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

  1. TextRange获取FlowDocument
  2. 使用FlowDocument
  3. 创建一个较小字体的新TextRange
  4. 将此新FlowDocument发送给打印机。
  5. 我的问题是,字体似乎没有变化。我希望它下降到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");
        }
    }
    

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

1 个答案:

答案 0 :(得分:2)

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

pg.LineHeight = 0;