Printing to Microsoft XPS Document Writer without showing File Save Dialog Box

Somehow commenting was not working on my blog, so a reader send me an email on how to print to XPS Document Writer in a server environment without popping up file save dialog box.

Printing to file is supported by GDI through the DOCINFO structure passed to StartDoc call. Here is a sample code:

void UILessXPSGeneration(const TCHAR * fileName)


HDC hDC = CreateDC(NULL, L"Microsoft XPS Document Writer", NULL, NULL);

DOCINFO docinfo;

memset(&docinfo, 0, sizeof(docinfo));

docinfo.cbSize = sizeof(docinfo);

docinfo.lpszOutput = fileName;

int result;

result = StartDoc(hDC, & docinfo);

result = StartPage(hDC);

const TCHAR * message = L"Hello XPS";

result = TextOut(hDC, 100, 100, message, (int) _tcslen(message));

result = EndPage(hDC);

result = EndDoc(hDC);



Here is the XPS FixedPage generated:

<FixedPage Width="816" Height="1056" xmlns="" xml:lang="en-US">

      <Path Data="F1 M 16,16 L 80.8,16 80.8,32 16,32 z" Fill="#ffffffff" />

      <Glyphs Fill="#ff000000" FontUri="/font_0.TTF" FontRenderingEmSize="14.3217"

            StyleSimulations="None" OriginX="16" OriginY="28.8"


            UnicodeString="Hello XPS" />