다음을 통해 공유


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);

}

UILessXPSGeneration(L"c:\\temp\\file1.zip");

Here is the XPS FixedPage generated:

<FixedPage Width="816" Height="1056" xmlns="https://schemas.microsoft.com/xps/2005/06" 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"

            Indices="43,71;72,55;79;79;82,55;3;59;51,66;54"

            UnicodeString="Hello XPS" />

</FixedPage>

Comments

  • Anonymous
    September 16, 2005

    Thanks ;-)

  • Anonymous
    January 09, 2006
    The comment has been removed

  • Anonymous
    June 09, 2006

    Navigation

  • Anonymous
    September 26, 2006
    This works for me at least for the short term.


    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
                   (ByVal hWndParent As Long, ByVal hWndChildAfter As Long, _
                   ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                   (ByVal hwnd As Long, ByVal wMsg As Long, _
                   ByVal wParam As Long, ByVal lParam As Any) As Long
                   
    Private Declare Function PostMessage Lib "user32" _
       Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long


    Private Declare Function FindWindow Lib "user32" _
       Alias "FindWindowA" (ByVal szClass$, ByVal szTitle$) As Long
       Private Const WM_CLOSE = &H10
       Private Const BM_CLICK As Long = &HF5&
       Private Const WM_KEYDOWN = &H100
       Private Const WM_KEYUP = &H101
       Private Const WM_CHAR = &H102
       Private Const WM_SETTEXT As Long = &HC
    Private Sub Command1_Click()
        WindowHandle = FindWindow(vbNullString, "Save the file as")
           cboBoxHandle = FindWindowEx(WindowHandle, 0, "ComboBoxEx32", "")
           retVal = SendMessage(cboBoxHandle, WM_SETTEXT, ByVal CLng(0), "c:w.xps")
           ButtonHandle = FindWindowEx(WindowHandle, 0, "Button", "&Save")
           retVal = SendMessage(ButtonHandle, BM_CLICK, 0, "0")End Sub

  • Anonymous
    March 11, 2007
    mmm.. nice design, I must say..

  • Anonymous
    March 14, 2007
    Du musst ein Fachmann sein - wirklich guter Aufstellungsort, den du hast!

  • Anonymous
    March 18, 2007
    luogo grande:) nessun osservazioni!

  • Anonymous
    October 20, 2007
    How can i do that using any .NET language ?

  • Anonymous
    February 01, 2008
    I got a mail overnight asking about ways to automatically generate XPS from applications, specifically

  • Anonymous
    February 01, 2008
    PingBack from http://msdnrss.thecoderblogs.com/2008/02/01/generating-xps-automagically/

  • Anonymous
    February 15, 2009
    hi feng, great work.. i tested it in my application and it worked fine..but i am confused about my problem. actually we have an application in MFC..which supports printing like conventional applications.. now i want to create XPS documents on some event say behind some button click "generate XPS file" which asks the file name and i create an XPS file of that name..now the problem is how my current DC(holding all my drawings) will be mapped to this new dc i have just created for writing XPS file..??I mean we already have written a wrapper over windows GDI library which does all drawings for my application..now i want all my current drawingns to be written to the XPS file..actually i am not much familiar with windows graphics programming and GDI concepts..please help..thanks

  • Anonymous
    March 24, 2009
    What language is this sample written in? Is it possible to make use of MXDW in a .NET application? If yes, then how? Thanks.

  • Anonymous
    August 06, 2009
    Where are the XPS spool files stored on the system?  When I print in WinXP to the XPS Document write the C:WINDOWSsystem32spoolPRINTERS contains a small .SHD file and a zero byte .SPL file.

  • Anonymous
    December 29, 2009
    The comment has been removed