2

是否可以使用 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 文件正在工作。

4

1 回答 1

5

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

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

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

于 2018-01-26T05:23:24.717 回答