使用Windows后台处理程序API

时间:2018-01-25 09:50:10

标签: c windows printing print-spooler-api

是否可以使用Windows Spooler API打印PDF文件。我尝试使用以下代码,但它无法正常工作......

int print_handle = 0;   
OpenPrinter(pPrinterName, &print_handle, NULL);
if (print_handle == 0)
{
    return 0;
}
docinfo1.pDocName = (LPTSTR)("My PDF");
docinfo1.pOutputFile = NULL;
docinfo1.pDatatype = (LPTSTR)("RAW");
temp = StartDocPrinter(print_handle, 1, &docinfo1);
temp = StartPagePrinter(print_handle);
temp = WritePrinter(print_handle, (LPBYTE)filebuff, filelen, &bytes_written);
EndPagePrinter(print_handle);
EndDocPrinter(print_handle);

WritePrinter函数返回SUCCESS并且没有打印任何内容。使用此API打印TXT和PRN文件正在运行。

1 个答案:

答案 0 :(得分:3)

Windows没有内置的打印PDF文件的功能。

您必须使用外部应用程序或库,例如Ghostscript

我应该注意,因为你的代码使用" RAW"数据类型(如official sample)它将写入的作业数据直接发送到打印机。 ASCII数据有效,因为几乎所有打印机都可以接收和打印ASCII。如果打印机将PDF理解为PDL(页面描述语言 - 其他示例是PCL或PostScript),它将打印您的PDF。但对于所有其他打印机,PDF需要转换为打印机能够理解的PDL。