Juan Foegen

I was trying my application on the latest Beta 2 release of Vista and found some weird features when writing out to XPS.

In my application the user has the option of going into a print layout window. This layout window allows the user to place the desired application related objects on pages for printing. When the user goes into the dialog, I go create a printerDC based on the default printer and hang on to that DC until the user closes the layout window or changes to a different printer. While in this dialog, the user can change how the objects are layed out and print at any time.

Inside I call startDoc with just the application name and when saving to an XPS the first time, the XPS file is generated successfully. If I write to a different XPS file after the first write, the XPS file generated is only 8K large instead of the 250K. This small file seems to have a preview in it, but not the main "image", because when you open it there is nothing in it. It seems like if I always go after a new printerDC before each startDoc call, then it will work, but I never seemed to require this for "normal" printers.

Also, if I try to print where I say I want to replace an existing XPS document, StartDoc returns a -2, but there is nothing in GetLastError.

Thought I would post the issue to see if others have seen similar problems.

Thanks,

Juan Foegen



Re: XML Paper Specification (XPS) XPS and StartDoc

Karthikeyan Mahadevan

I think you have logged on as normal user. Try doing this as the Inbuilt administrator.

regards,

Karthikeyan.





Re: XML Paper Specification (XPS) XPS and StartDoc

Bhuvaneshwari K

Hi karthik,

I have to use XPS printer to get an XPS file out of a notepad file.I have to give the output file location programatically to supress the 'SavaAsDialog' .I am programming in C#.I used gdi32.dll and startdoc as unmanaged code.But I am not able to get the printer handle for XPS printer.

DOCINFO di = new DOCINFO();

di.pDocFileName = inputfile.txt ; di.outputfile = output.xps;

PrintDocument pd = new PrintDocument();

Intptr hdevmode =pd.PrinterSettings.GetHdevMode();

int dc = CreateDC("Microsoft XPS Document Writer","Microsoft XPS Document Writer",di.outputfile,hdevmode);

int result = startdoc(dc,di);

In the above code CreateDC returns 0 for XPS printer.If it is some other printer like 'Microsoft office Document Image Writer' it works correctly. (DOCINFO is an unmanaged type defined at the beginning)

Any pointers would be of great help.This is a blocking issue for us for the past one week.

thanks and regards

K Bhuvaneshwari





Re: XML Paper Specification (XPS) XPS and StartDoc

Karthikeyan Mahadevan

Hi,

I  tried the same using Win32 Programming,

#include <Windows.h>

 int _tmain(int argc, _TCHAR* argv[])

{

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

DOCINFO *pDoc = new DOCINFO;

pDoc->cbSize = sizeof(DOCINFO);

memset(pDoc,0,sizeof(DOCINFO));

pDoc->lpszDocName = TEXT("e:\\h.txt");

pDoc->lpszOutput = TEXT("e:\\output.xps");

StartDoc(hDC,pDoc);

StartPage(hDC);

BOOL IsTextOut = TextOut(hDC, 1000,1000,TEXT("TEST APPLICATION"),17);

EndPage(hDC);

EndDoc(hDC);

DeleteDC(hDC);

return

}

The code works properly.

regards,

Karthikeyan.





Re: XML Paper Specification (XPS) XPS and StartDoc

Karthikeyan Mahadevan

I think I cornered the situation. The problem seems to be in hdevmode that is passed in  CreateDC. When I made this to NULL the dc is getting created. Also try to use GettlastError to find the reason of failure.

///////////////////////////////////////////////////////////////////////////////////////////////////////////
IntPtr dc1 = CreateDC(null, "Microsoft XPS Document Writer", null, (IntPtr)0);
//IntPtr dc1 = CreateDC("Microsoft XPS Document Writer", "Microsoft XPS Document Writer", outputfile, hdevmode);
int h = GetLastError();

///////////////////////////////////////////////////////////////////////////////////////////////////////////





Re: XML Paper Specification (XPS) XPS and StartDoc

Juan Foegen

I am always using a HDEVMode because I am using the information for things like portrait and landscape. That would really point to a problem in the XPS Document Writer if it cannot handle the information given in the HDEVMODE structure.

I was hoping I could get that to work seamlessly without doing something specific for the XPS writer, like I do for PDF after installing its printer driver.

Thanks for keeping us updated,

Juan Foegen





Re: XML Paper Specification (XPS) XPS and StartDoc

LalitSRana

Hi Karthik and Juan,
Karthik the Win32 code, which you have written is not converting the content of
pDoc->lpszDocName = TEXT("e:\\h.txt");

to

pDoc->lpszOutput = TEXT("e:\\output.xps");

what it does is it creates, output.xps and writes "TEST APPLICATION" because of the following
BOOL IsTextOut = TextOut(hDC, 1000,1000,TEXT("TEST APPLICATION"),17);

if I comment this statement, I get a blank output.xps of 5kb.

Can you please tell why it is not creating a copy of h.txt to output.xps if it being printed successfully.

Regards,
Lalit







Re: XML Paper Specification (XPS) XPS and StartDoc

Juan Foegen

I actually do not supply the file and the driver prompts for the filename. My point is if a NULL hDevMode is required to make it work correctly, then an XPS document driver cannot be treated as just another printer.

Thanks,

Juan





Re: XML Paper Specification (XPS) XPS and StartDoc

LalitSRana

That is correct Juan,

But can you please tell me, if your code puts the text of the "lpszDocName" to the newly created xps file "lpszOutput", whether you are coding it or entering it when prompted.

If I take Karthik's code, then should not I get whatever is there in h.txt in output.xps.

#include <Windows.h>

int _tmain(int argc, _TCHAR* argv[])

{

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

DOCINFO *pDoc = new DOCINFO;

pDoc->cbSize = sizeof(DOCINFO);

memset(pDoc,0,sizeof(DOCINFO));

pDoc->lpszDocName = TEXT("e:\\h.txt");

pDoc->lpszOutput = TEXT("e:\\output.xps");

StartDoc(hDC,pDoc);

StartPage(hDC);


// Commenting the following

// BOOL IsTextOut = TextOut(hDC, 1000,1000,TEXT("TEST APPLICATION"),17);

EndPage(hDC);

EndDoc(hDC);

DeleteDC(hDC);

return

}







Re: XML Paper Specification (XPS) XPS and StartDoc

Karthikeyan Mahadevan

Hi Lalit,

if you see the definition for the DOCINFO'S lpszDocName,

lpszDocName
Pointer to a null-terminated string that specifies the name of the document. And not the input file.

So I doubt whether we will be able to print the e:\\h.txt, actually I forgot to rename that . http://msdn2.microsoft.com/en-gb/library/ms535517.aspx HAs a example that uses the lpszDocName.

On more place to refer is that of Feng yuans blog, http://blogs.msdn.com/fyuan/archive/2005/09/16/469076.aspx.

Please let me know if I am wrong.

regards,

Karthikeyan.





Re: XML Paper Specification (XPS) XPS and StartDoc

Jo0815

lpszDocName is just the name how it got displayed in the print-spooler, and not an input-file or whatever... only the GDI-calls on the printer's DC like TextOut will create output then...





Re: XML Paper Specification (XPS) XPS and StartDoc

LalitSRana

Thanks Karthik and Jo0815,

Actually, I was thinking I can use that parameter for the input file.

Now I have taken another approach.

My Requirement :- Want to convert any given file format to an XPS file

Approach:-
I am using ShellExecuteEx to print my file to "XPS" format using MXDW. But it pops a dialog for the output file, can I givet the file name programmatically (putting local port as a filename is not helping).
Can you please suggest how I can remedy this, may be an entirely different approach.

Regards,






Re: XML Paper Specification (XPS) XPS and StartDoc

Bhuvaneshwari K

hi

I too have the same problem as Lalit.I want to convert a given file to XPS without user intervention.

Any guidance for the same would be of great help.





Re: XML Paper Specification (XPS) XPS and StartDoc

CTA

lalit and Bhuvaneswary,

I understand what you are asking, I don't think this reply will solve, but it could shed some light, I hope.

To understand Lalit issue, is simply calling ShellExecute which sends DDE command to Print. (%s \p).Now you dont' have any control over the printing process, because end app implements the printing such as gets printer dc and calls :

start doc-startpage-draw it on dc-endpage-startpage-endpage...enddoc calls.

If you want to completely take over printing process you can directly write content to the printer. that can be possible by calling like this:

DOC_INFO_1 di;

di.pDocName = TEXT("Job Name") ; // that appears in the SM.

di.pOutputFile = _T("c:\\myoutput.xps");

di.pDatatype = NULL;

StartDocPrinter(hPrinter, 1 (LPBYTE)&di);

StartPagePrinter(hPrinter);

// you are writing print content.

EndPagePrinter(hPrinter);

EndDocPrinter(hPrinter);

Note: To really solve your issue, you need to write a port monitor, however i 'm just thinking other venue like you.






Re: XML Paper Specification (XPS) XPS and StartDoc

Bhuvaneshwari K

hi charles,

Thanks for replying.Ya what you mentioned is right.But i actually need to convert one .txt file( any printable file for that matter;say office files or PDF files) to its equivalent .xps file.As far as i know the XPS printer is not exposing any API ,which can be used directly to do this function.

I need to know a way to specify the input file(say .txt file) and output file(.xps file) location to the XPS printer programmatically ,so that the 'SaveAs' dialog box will not pop-up.

Will writing a separate port monitor be a solution for this problem