WPF: Printing using page templates

J

Julia M

Hi,

I'm desperatly trying to find code samples how to print stuff
_without_ using DrawText, DrawLine, adding child elements a.s.o (the
only samples that seem to be out there) but nice and managable page
templates.

e.g. I created a page that looks like this

<Page x:Class="WpfApplication1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="I am a template">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" Margin="0,0,0,10" >
<TextBlock Text="I am a header " />
</Grid>
<Grid Grid.Row="1" >
<TextBlock Grid.Row="1" Text="I am a table"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
<ItemsControl ItemsSource="{Binding}" />
</Grid>
<Grid Grid.Row="2" Margin="0,10,0,0" >
<TextBlock Text="I am a footer" />
</Grid>
</Grid>
</Page>


In real life, these forms a much more complex than this and I really
_really_ don't want to create them using code - you wouldn't want to,
either, trust me ;-)

Trouble is, I can't figure out how to "push" the contents out to a
printer.

I tried:

PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() != true) return;
Page1 p = new Page1();
dialog.PrintVisual(p,"This is a test");

but the page is just blank.

After some fiddling around I coaxed a filled page from the printer.
Like this

PrintDialog dialog = new PrintDialog();
if (dialog.ShowDialog() != true) return;
Page1 p = new Page1();
w.Height = dialog.PrintableAreaHeight;
w.Width = dialog.PrintableAreaWidth;
p.Height = w.Height;
p.Width =w.Width;
p.UpdateLayout();
w.Content = p;
w.ShowDialog();
w.Close();
dialog.PrintVisual(w,"This is another test");

I'm pretty sure this is not the way it's done and it sucks, too.

For one, the bottom part of the page is now missing its footer. Simply
because the screen has a diff resolution than the printer. i.e. a page
the height of some 1100 px will just cut off the bottom 50 or even
300px. Depending on whether your screen is a 1680x1050 or 1024x768.

Any suggestions on how to do this a better way?

TIA
 
Joined
Sep 21, 2009
Messages
2
Reaction score
0
Hi I would suggest you to convert your xaml page to XPS file then to print it.. you'll find a lot of src code concerning convertion to XPS then it's just a formality to print an XPS file.

Hamza
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top