字体大小随打印wpf而变化

时间:2016-04-27 12:20:36

标签: c# wpf ihtmldocument2

我有一个非常基本的WPF表单,我想打印

<Grid x:Name="grid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Text="Name" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center"  Grid.Column="0" Grid.Row="0"/>
        <TextBox Text="Name" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1" Grid.Row="0" Height="60" x:Name="NameTb" Width="200" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
        <TextBlock Text="ID" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center"  Grid.Column="0" Grid.Row="1"/>
        <TextBox Text="ID" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1" x:Name="IdTb" Height="60" Width="200" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
        <TextBlock Text="Description" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center"  Grid.Column="0" Grid.Row="2"/>
        <TextBox Text="Description" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1" Grid.Row="2" x:Name="DscTb" Height="60" Width="200" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"/>
        <Button Content="Print" Grid.Column="1" Grid.Row="3" Width="150" Height="30" Click="Print_Click"/>
    </Grid>

代码隐藏是:

private void Print_Click(object sender,RoutedEventArgs e)
{
    WebBrowser wb = new WebBrowser();
    bool PrintFired = true;
    StringBuilder printString = new StringBuilder("<head><meta http-equiv='Content-Type' content='text/html;charset=UTF-8'></head>");
    printString.Append("<table border='0' margin-right ='20'><tr><td><b><font size='3'>" +"Name" + "</font>></b></td><td>&nbsp;&nbsp;</td><td>");
    //printString.Append(explanation.EngagementName);
    printString.Append("<font size='3'>" + NameTb.Text + "</font>");
    printString.Append("</td></tr><tr><td></td></tr>");
    printString.Append("<tr><td><b><font size='3'>" + "ID" + "</font></b></td><td>&nbsp;&nbsp;</td><td>");
    printString.Append("<font size='3'>" + IdTb.Text + "</font>");
    printString.Append("</td></tr><tr><td></td></tr>");
    printString.Append("<tr><td valign ='top'><b><font size='3'>" + "Description" + "</font></b></td><td>&nbsp;&nbsp;</td><td>");
    printString.Append("<font size='3'>" + DscTb.Text + "</font>");           
    printString.Append("</td></tr></table>");
    wb.NavigateToString(printString.ToString());
    var d = new System.Windows.Threading.DispatcherTimer();
    d.Interval = TimeSpan.FromMilliseconds(250);
    d.Tick += new EventHandler((sender1, args) =>
    {
        if (PrintFired == true && ((mshtml.IHTMLDocument2)(wb.Document)).readyState != "loading")
        {
            PrintFired = false;
            mshtml.IHTMLDocument2 doc = wb.Document as mshtml.IHTMLDocument2;
            doc.execCommand("Print", true, null);
            d.Stop();
        }
    });
    d.Start();        
}

问题是当我在描述字段(或任何其他字段)中输入大量文本时,字体大小会减少。

PS-您不必打印此文件,您可以将其另存为PDF格式。

1 个答案:

答案 0 :(得分:1)

字体大小的更改是由缩放以适合页面大小引起的。如果要显示这样的长字母序列,则需要包装单元格的内容。

尝试以下列方式修改html:

"<table border='0' style=\"table-layout: fixed; width: 100%;\" margin-right='20'>"
.....
"<td style=\"word-wrap: break-word\" ><font size='3'>" + DscTb.Text + "</font></td>"

结果:

result