Cannot print directly to XPS printer.

  • Thread starter Thread starter Ian Boyd
  • Start date Start date
I

Ian Boyd

Using the code from

HOWTO: Send Raw Data to a Printer by Using the Win32 API
http://support.microsoft.com/kb/q138594/

i can print fine to every "real" printer. But when i print to the "Microsoft
XPS Document Writer" printer, no save dialog ever appears.

To sum up:
OpenPrinter()
StartDocPrinter()
StartPagePrinter()
WritePrinter()
EndPagePrinter()
EndDocPrinter()
CloserPrinter()

There are no errors, the spooler appears and disappears, the bytes written
equal the bytes supposed to be written. Just in the end no Save Dialog
appears.

Where is my print job going?


Code:

procedure PrintStrToPrinter(const TextToPrint: string; const PrinterName:
string);
var
hPrinter: THandle;
DocInfo: TDocInfo1;
dwBytesWritten : DWORD;
dwJobID: DWORD;
begin
{
HOWTO: Send Raw Data to a Printer by Using the Win32 API
http://support.microsoft.com/kb/q138594/
}
if TextToPrint = '' then
raise Exception.Create('[PrintStrToPrinter] To text specified');

if not WinSpool.OpenPrinter(PChar(PrinterName), hPrinter, nil) then
raise Exception.Create('[PrintStrToPrinter] Cannot find the
printer "'+PrinterName+'".');
try
// Fill in the structure with info about this "document"
ZeroMemory(@DocInfo, SizeOf(DocInfo));
DocInfo.pDocName := 'My Document';
DocInfo.pOutputFile := nil;
DocInfo.pDatatype := 'RAW';

// Inform the spooler the document is beginning
dwJobID := StartDocPrinter(hPrinter, 1, @docInfo);
if dwJobID = 0 then
RaiseLastWin32Error;
try
if not StartPagePrinter (hPrinter) then
RaiseLastWin32Error;
try
if not WritePrinter(hPrinter, PChar(TextToPrint),
Length(TextToPrint), dwBytesWritten) then
RaiseLastWin32Error;
finally
EndPagePrinter(hPrinter);
end;
finally
EndDocPrinter(hPrinter);
end;
finally
WinSpool.ClosePrinter(hPrinter);
end;
end;


PrintStrToPrinter('adfasfasdf', 'Microsoft XPS Document Writer');
 
Well this probably is because you send "RAW" data directly to the printer,
and RAW can be any PDL. But the XPS driver will probably only understands
XPS, and it will probably just ignore your "unknown: adfasfasdf" PDL. The
XPS driver will probably, if any, only accept XPS data when you write
directly to it.

If you want to render text on the XPS driver, you should use GDI. You might
be able to send plain text to the driver if you specify "TEXT" as the
datatype. The print processor attached to the driver will then "convert" the
plaintext for you by rendering the job via GDI to the driver.

Btw, you should also call AbortPrinter in case of any exception, so that the
spoolfile will be deleted.
 
Back
Top