WPF Printing Overview

WPF Printing Overview

 

Introduction


The techniques covered here are all available in a downloadable project over at MSDN Samples:

This sample shows five aspects of printing, in relation to WPF.
With WPF, it has never been so easy to print your entire application, or build and format pages for printing.
 
 

1. Using DrawingContext



The DrawingContext (of a DrawingVisual) allows you to build up the print page, like a canvas.

This method allows you to build up the page in code with many methods, like adding shapes, images and text.

In the accompanying MSDN sample, we test the following:

Below is a snippet from the project:
 
DrawingVisual dv = new DrawingVisual();
 
var dc = dv.RenderOpen();
 
var rect = new Rect(new System.Windows.Point(20, 20), new System.Windows.Size(350, 240));
dc.DrawRoundedRectangle(System.Windows.Media.Brushes.Yellow, new Pen(Brushes.Purple, 2), rect, 20, 20);
 

2. Printing WPF Controls



WPF is specially good at printing, because you can print the rendered WPF controls, exactly as they are.

You can also save to file or apply the image to another surface (technique sometimes used in reflections)

In the example (shown above) there is a rotating Viewport3D, an Image and a TextBox for you to play with. Whatever state the controls are in, they will be printed.

This method uses PrintVisual, which takes any Visual as it's parameter

 

3. Printing Flow Documents



Flow documents are a great way to display formatted content, and they are easy to print.

The FlowDocumentReader is a rich user control, and printing pages can be controlled with the DocumentPaginator.

In this example, you can resize the window or change the layout style. If there is more than one page whenyou print, they will print on separate pages.

PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
    printDialog.PrintDocument(((IDocumentPaginatorSource)flowDocument).DocumentPaginator, "This is a Flow Document");

 

4. Making and Printing XPS Documents



XPS documents are a popular printing file format, and they are easy to make out of WPF controls.

Here we have an InkCanvas which you can draw on, enlarge the window, draw more and print.

This example shows how to use the XpsDocumentWriter to build the file.

The XPS file is then sent to the printer jobs queue.

FixedDocument doc = new FixedDocument();
 
PageContent pageContent = new PageContent();
((System.Windows.Markup.IAddChild)pageContent).AddChild(page);
doc.Pages.Add(pageContent); 
 
XpsDocument xpsd = new XpsDocument(filename, FileAccess.Write);
XpsDocument.CreateXpsDocumentWriter(xpsd).Write(doc);            
xpsd.Close();
 
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
    printDialog.PrintQueue.AddJob("MyInkCanvas print job", filename, true);

 

5. Capturing and Saving Video Stills



This example shows how you can "scrub" through a video and pull out frames to file and printer.

A slider bar is joined to the MediaElement to allow scene selection.

When you click print, the selected video frame is converted into a Jpeg, saved and printed.

byte[] screenshot = GetScreenShot(player, 1, 90);
FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter binaryWriter = new BinaryWriter(fileStream);
binaryWriter.Write(screenshot);
binaryWriter.Close();
 
PrintDocument pd = new PrintDocument();
pd.PrintPage += (object printSender, PrintPageEventArgs printE) =>
{
    var img = System.Drawing.Image.FromFile(filename);
    printE.Graphics.DrawImageUnscaled(img, new System.Drawing.Point(0, 0));
};
 
PrintDialog dialog = new PrintDialog();
dialog.ShowDialog();
pd.Print(); 
 

6. Landscape Printing


In this example, there are two printing buttons. the first prints the parent grid, which prints the buttons, scrollviewer and it's contents. This shows as a cut off print.
The second example prints the TextBlock itself, which is inside a ScrollViewer, so is not actually constrained at all. This version prints perfectly stretched, showing all of the text.

private void Button_Click2(object sender, RoutedEventArgs e)
{
    PrintDialog printDialog = new PrintDialog();
    printDialog.PrintTicket.PageOrientation = PageOrientation.Landscape;
    printDialog.PrintVisual(printBlock, "Landscape working TextBox print");
}





Note: XPS and JPG files generated from the project are saved in ".../MyDocuments/PrintingTest"

Don't forget to download this project from MSDN Samples:
http://code.msdn.microsoft.com/WPF-Printing-Overview-f28c541a

(...and please rate it if you like it ;)
Leave a Comment
  • Please add 1 and 5 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • XAML guy edited Revision 18. Comment: tweaks

  • XAML guy edited Revision 17. Comment: re-did image

  • XAML guy edited Revision 16. Comment: added image (with firefox cause ie didn't work!)

  • XAML guy edited Revision 15. Comment: added landscape

  • XAML guy edited Revision 14. Comment: tweaks

  • XAML guy edited Revision 13. Comment: fixed link

  • XAML guy edited Revision 12. Comment: added, tidied

  • XAML guy edited Revision 11. Comment: removed duplicated code?!?

  • XAML guy edited Revision 10. Comment: adding layout, extra and links

  • XAML guy edited Revision 9. Comment: added images

Page 1 of 2 (18 items) 12
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • XAML guy edited Original. Comment: fixed url

  • XAML guy edited Revision 1. Comment: toc

  • XAML guy edited Revision 2. Comment: fixing

  • XAML guy edited Revision 3. Comment: fiddling

  • XAML guy edited Revision 4. Comment: fiddling with font sizes

  • XAML guy edited Revision 5. Comment: still fiddling with fonts, editor looks different to result :/

  • XAML guy edited Revision 6. Comment: spellcheck

  • XAML guy edited Revision 7. Comment: sticky tags

  • XAML guy edited Revision 8. Comment: trying to fix line spacing

  • XAML guy edited Revision 9. Comment: added images

  • XAML guy edited Revision 10. Comment: adding layout, extra and links

  • XAML guy edited Revision 11. Comment: removed duplicated code?!?

  • XAML guy edited Revision 12. Comment: added, tidied

  • XAML guy edited Revision 13. Comment: fixed link

  • XAML guy edited Revision 14. Comment: tweaks

Page 1 of 2 (20 items) 12