NiXPS
2009: Out with the old, in with the new!
The Gregorian Calendar we all love and use (note: shameless Western bias), is yet again forcing us within a few days to add another year to the counter, running the total up to 2009!
Traditionally people tend to look back, evaluate and hope for the best in the future. My - this is very appropriate these days.
2008 was an interesting year for NiXPS. We released tons of new software, travelled around the world to give demo's and we had the chance to meet lot of interesting people and, most importantly, customers!
It is great to see that XPS is slowly making progress, and more an more people are building fantastic products and applications around it. We have yet to see a big uptake of the format on the desktop, but I'm convinced that this will happen, sooner or later.
This brings us to 2009. In more ways than one do we throw out the old and welcome the new!
Not only a new year, but also a new team. NiXPS found a new motivated team of investors, who took the commitment to support NiXPS in continuing and expanding its business. This is fantastic news, and holds great promise for the years to come.
2009 will probably be the year that Windows 7 will be released, which will strengthen the XPS ecosystem, as Windows 7 build upon the XPS foundation laid down with Windows Vista.
There's a good chance that 2009 will also be the year of the formal ECMA approved XPS specification, the people over at TC46 are very busy with that.
NiXPS will continue to improve its current products, and release some more. We have a lot of interesting projects scheduled for 2009, I hope to able to tell you more about this very soon.
And I also hope for the world at large that we we will learn from the crisis that is upon is in 2008, and make a better future for all of us in 2009.
Warm wishes from the NiXPS team to all of you, and may 2009 turn out be a '
Grand Cru' for you!
Convert any File to XPS!
People have been asking about how to convert all sorts of formats to XPS and now we're pleased to announce there is a way to do so. All you need to do is install a printer driver and make one call to our library.
Any file format that can be printed by a program on your system will be able to generate an XPS file without any user interaction and our library will trigger the necessary calls to do so.
Let me explain what you need to do.
Our next version of the library comes with a printer driver. You need to install this printer driver on the system where you want to generate the XPS files. Download and unzip our library. In the Control Panel for your Printer you "Add a printer".
Add a local printer. When you get the following dialog:
choose to create a new local port for it. Pressing the Next button will prompt you for a suitable name. Choose something like "C:/NiXPS/output.xps"
The prompt appears to choose a printer driver.
Choose Have Disk... and when the dialog appears to insert the manufacturer's installation disk, click on Browse...
Now browse to the place where you just downloaded our library and in the folder driver choose the xdsmpl.inf file.
Click OK and Next and give the printer a name:
You can accept the default to not share the printer. Print a test page or skip that and just click Finish and you're all set:
What you have just done is install a printer driver like the (on Vista) preinstalled Microsoft XPS Document Writer that can print to XPS files. The difference however is that this one is mute. It won't come up and ask where to save the file and it won't open the XPS file afterward.
That way it can be used to generate XPS documents from whatever files are printable on your system. In other words if you have a file format you want to convert to XPS and you have an application that can print the file format you now have a solution to convert that format to XPS.
From our library all it takes is one call:
static public UInt32 convertToXPSViaPrinter(string pSourceFile,
string pDestinationFile,
string pPrinterOutputFile,
string pXPSPrinterDriverName,
UInt32 pTimeOut)
You give it paths to the source file and the resulting destination file, you give it the location of the printers output file which is the one you set up while you were creating the local port and you give it the printer name you just installed. All of that and a timeout in seconds so that when things go horribly wrong your system does not hang. The method returns whether it was successful or not. There is an example that uses this in the sample project in the SDK.
That's it. It should work on Windows XP and Windows Vista, both for 32 and 64 bit applications.
Let us know what you think.
kristof
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 hereThe 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:
- You can use the XPS as the printing output of your application. Giving your end-user a higher quality print-out.
- Allowing your user to save the output as PDF and/or XPS, which are ideal formats for sharing.