views:

1258

answers:

1

For Windows XP / Windows Server 2003

I'm trying to print an EMF(or WMF) file format to the virtual printer "Microsoft XPS Document Writer" using Windows Picture and Fax Viewer (shimgvw.dll) from the command line. I want the resulting XPS to be in vector format, like the WMF/EMF is.

It works with all image formats except WMF and EMF. The XPS printer save dialog appears but the XPS file that is saved is empty.

I can do it if I load the WMF and click File>Print from the Windows Picture and FaxViewer GUI, but not from the command line. Also if I right click on the WMF file in Windows Explorer and click Print it will not allow the WMF file selected to print (it filters WMF and EMF files out of the list).

Code:
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = @"C:\WINDOWS\System32\rundll32.exe";
info.Arguments = @"C:\WINDOWS\System32\shimgvw.dll,ImageView_PrintTo /pt ""c:\vectorImage.emf"" ""Microsoft XPS Document Writer""";
Process.Start(info);

I've read a lot about the WMF exploit a while back and can only assume that direct printing of these files has been removed? But if I can print these WMF files form the GUI then I don't see how the exploit was avoided.

What I want to do is create an XPS vector file from a WMF/EMF file programatically with no user interaction required. I would imagine that I should be able to do this because all formats involved belong to Microsoft.

A: 

Take a look at the following example in C for how to get GDI commands into an XPS document: http://blogs.msdn.com/fyuan/archive/2005/09/16/469076.aspx

What you want to do, is create the printer DC, call StartDoc, call StartPage, then setup the viewport and window scale for placing the emf, call PlayEnhMetaFile(), EndPage, EndDoc.

The trick Feng Yuan is presenting is using the DocInfo argument to StartDoc to specify the output XPS file name, and what I'm suggesting is that you play the metafile contents into the printer DC.

I believe the EMF exploit was closed a while back by Microsoft Update. The problem you are probably seeing is due to the GDI and XPS print paths being separate and a lack of effort to bridge them in this corner case.

-Jason

Jason Harrison