Vivek Saurav
Vivek Saurav

Reputation: 2275

Font size changes in print wpf

I have a very basic WPF form which i want to print

<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>

And the code-behind is :

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

The problem is when i input large amount of text into the description field(or any other field) the font sizes reduces in print.

PS- you don't have to print this, you can save it as a PDF.

Upvotes: 0

Views: 675

Answers (1)

lukbl
lukbl

Reputation: 1773

Change in font size is caused by scaling to fit page size. You need to wrap content of cell if you want to display such long letter sequences.

Try modifying your html in a following way:

"<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:

result

Upvotes: 1

Related Questions