使用 Windows 打印 Spoler API s, 我们可以打印 XPS 文件 。
问题是打印 < 坚固> pooler 忽略 坚固 > 的 < 坚固 > 默认打印机设置 坚固 > 。
(我们放弃了尝试 < a href=" https://stackoverflow.com/ questions/10345179/resetdc-does- nothing-print-spooler-apis- for- windows" @em> apply 打印机设置 。默认打印机设置将只需要足够。)
例如... 打印输出总是以彩色和每页一张纸的形式出来, 不论控制面板设置的设置是什么: 黑& amp; 白/ 彩色、 双双色/ 非双面、 每页多页/ 每页单页 。
其他应用程序, 如 MS Word 和 Adobe 尊重默认打印机设置 。
我们在使用德尔斐 XE2和Windows 7 64比特
这个测试代码是自封的, 所以你可以把它粘贴进去测试它...
弹出一个组合框, 使用打印机名称 :
uses
Printers
ComboBox1.Items.Assign(Printer.Printers);
印刷程序:
uses
Winapi.WinSpool
procedure PrintXPS(PrinterName, FileNameXPS: string; ParentFormHandle: THandle = 0);
// Printer handle
procedure Printer_Open(out Printer: THandle; Defaults: PPrinterDefaultsW = nil);
begin
if not OpenPrinterW(PWideChar(PrinterName), Printer, Defaults) then
RaiseLastOSError;
end;
procedure Printer_Close(Printer: THandle);
begin
if not ClosePrinter(Printer) then
RaiseLastOSError;
end;
// Print jobs
function JobCreate(Printer: THandle; FileName: string): Cardinal;
var
lBufferSize: Cardinal;
lAddJobInfo: PAddJobInfo1W;
begin
// Create job
AddJobW(Printer, 1, nil, 0, lBufferSize);
GetMem(lAddJobInfo, lBufferSize);
try
if not AddJobW(Printer, 1, lAddJobInfo, lBufferSize, lBufferSize) then
RaiseLastOSError;
Result := lAddJobInfo.JobId;
// Copy the file into place
CopyFile(PWideChar(FileName), lAddJobInfo.Path, True);
finally
FreeMem(lAddJobInfo, lBufferSize);
end;
end;
procedure JobStart(Printer: THandle; JobID: Cardinal);
begin
if not ScheduleJob(Printer, JobID) then
RaiseLastOSError;
end;
var
PrinterA: THandle;
JobID: Cardinal;
begin
if not FileExists(FileNameXPS) then
raise Exception.Create( File not found: + FileNameXPS);
Printer_Open(PrinterA, nil);
try
JobID := JobCreate(PrinterA, FileNameXPS);
JobStart(PrinterA, JobID);
finally
Printer_Close(PrinterA);
end;
end;