NiXPS
Tuesday, December 02, 2008
  WPF to PDF
Update 09/23/2010: We have released a 100% managed SDK called NiPDF v1.0 which makes it even easier to go from WPF to PDF. Take a look at the code sample here

The Windows Presentation Foundation (WPF short) is the UI programming framework that Microsoft introduced with the release of .NET v2.0. It is a very comprehensive, and powerful UI model that seems to be picking up steam in developer cycles.

Creating an XPS file from a WPF visual is very easy; if you combine this with our NiXPS SDK to convert XPS to PDF, you can very quickly add a nice PDF output feature to your WPF application.

As a matter of fact I've gave it a try, and the following code snippit draws a WPF visual, converts it to XPS and then uses the NiXPS SDK to write out a pdf:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;
using System.IO.Packaging;
using System.IO;
using System.Windows.Xps.Packaging;
using System.Printing;
using System.Windows.Xps;

namespace NiXPSObjects
{

class WPF2PDF
{

static Visual CreateVisual()
{
const double Inch = 96;
DrawingVisual visual = new DrawingVisual();
DrawingContext dc = visual.RenderOpen();
Pen bluePen = new Pen(Brushes.Blue, 1);
dc.DrawRectangle(Brushes.Yellow, bluePen, new Rect(Inch / 2, Inch / 2, Inch * 1.5, Inch * 1.5));
Brush pinkBrush = new SolidColorBrush(Color.FromArgb(128, 255, 0, 255));
Pen blackPen = new Pen(Brushes.Black, 1);
dc.DrawEllipse(pinkBrush, blackPen, new Point(Inch * 2.25, Inch * 2), Inch * 1.25, Inch);
dc.Close();
return visual;
}

public static void Main(string[] args)
{
try
{
// create XPS file based on a WPF Visual, and store it in a memorystream
MemoryStream lMemoryStream = new MemoryStream();
Package package = Package.Open(lMemoryStream, FileMode.Create);
XpsDocument doc = new XpsDocument(package);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
writer.Write(CreateVisual());
doc.Close();
package.Close();

// now open this XPS stream in the NiXPS SDK, and export it as pdf
NOPackage lPackage = NOPackage.readPackageFromBuffer("file.xps", lMemoryStream.GetBuffer(), (uint) lMemoryStream.Length);
NOProgressReporter lReporter = new NOProgressReporter();
lPackage.getDocument(0).exportToPDF("file.pdf", lReporter);
NOPackage.destroyPackage(ref lPackage);
} catch (NOException e)
{
System.Console.Out.WriteLine( "EXCEPTION: 0x" + string.Format("{0:X}", e.getError()) );
}
}
}
}


As a WPF developer you are getting 2 important benefits here:

  1. You can use the XPS as the printing output of your application. Giving your end-user a higher quality print-out.
  2. Allowing your user to save the output as PDF and/or XPS, which are ideal formats for sharing.
 
Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives
September 2006 / October 2006 / November 2006 / December 2006 / January 2007 / February 2007 / March 2007 / April 2007 / May 2007 / June 2007 / July 2007 / August 2007 / September 2007 / October 2007 / November 2007 / December 2007 / January 2008 / February 2008 / March 2008 / April 2008 / May 2008 / June 2008 / July 2008 / August 2008 / September 2008 / October 2008 / November 2008 / December 2008 / January 2009 / February 2009 / March 2009 / April 2009 / May 2009 / June 2009 / July 2009 / August 2009 / September 2009 / October 2009 / November 2009 / December 2009 / January 2010 / February 2010 / March 2010 / April 2010 / May 2010 / June 2010 / July 2010 / September 2010 / October 2010 / November 2010 / January 2011 /

NiXPS home
XPS info from the creators
    follow me on Twitter
    Add to Technorati Favorites

    Subscribe to
    Posts [Atom]