![](/img/trans.png)
[英]How to programmatically print without prompting for filename in MFC and CView using Print-To-PDF printer?
[英]Print a PDF file with MFC
在我的应用程序(MFC, C++)
我有一个按钮,可以在路径中创建PDF
文件。 现在我想创建另一个按钮,从路径开始打印pdf并选择一些选项,如方向和副本数量......但我无法做到这一点......
我看到CPrintDialog
显示了打印机的默认对话框,但我无法使用路径附加PDF文件。
我也看到了
ShellExecute(NULL, L"print", L"C:\\Documents\\1.pdf", NULL, NULL, SW_SHOWNORMAL);
这有效,但我不能选择任何参数......
如何使用CPrintDialog
打印路径中的现有PDF?
您必须使用ShellExecuteEx
和verb printto来更好地控制打印:
SHELLEXECUTEINFO ShellInfo;
ZeroMemory(&ShellInfo, sizeof(SHELLEXECUTEINFO));
ShellInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShellInfo.lpVerb = L"printto";
ShellInfo.lpFile = L"C:\\Documents\\1.pdf";
ShellInfo.lpParameters = szPrinter;
ShellInfo.nShow = SW_SHOWNORMAL;
ShellInfo.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_NOCLOSEPROCESS;
if(::ShellExecuteEx(&ShellInfo))
{
if((int)ShellInfo.hInstApp > 32)
{
if(ShellInfo.hProcess != NULL)
{
DWORD dwExitCode = STILL_ACTIVE;
while(dwExitCode == STILL_ACTIVE)
{
if(!::GetExitCodeProcess(ShellInfo.hProcess, &dwExitCode))
{
dwExitCode = 0;
}
}
::CloseHandle(ShellInfo.hProcess);
}
}
}
要获取打印机名称:
CPrintDialog dlg(TRUE);
if (dlg.DoModal() == IDOK)
{
CString sPrinterName = dlg.GetDeviceName();
}
我解决了一个解决方法。 而不是使用ShellExecute,我使用附加到CPrintDialog
类的hDC的CDC
对象绘制我想要打印的所有内容。 Rember根据打印机DPI管理绘图的大小,就像这里一样。
一个片段只有一个想法:
CPrintDialog printDialog(FALSE);
printDialog.GetDefaults();
printDialog.m_pd.Flags &= ~PD_RETURNDEFAULT;
DEVMODE* pDevMode = printDialog.GetDevMode();
pDevMode->dmFields = DM_ORIENTATION | DM_PAPERSIZE | DM_PRINTQUALITY ;
pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
pDevMode->dmPaperSize = DMPAPER_A4;
::GlobalUnlock(printDialog.m_pd.hDevMode);
if (printDialog.DoModal() == IDOK)
{
CDC* pDC = new CDC;
pDC->Attach(printDialog.m_pd.hDC);
pDCPDF->StartDoc(_T(""));
pDCPDF->StartPage();
// ...
//draw what you want
// ...
pDCPDF->EndPage();
pDCPDF->EndDoc(); //this starts the printer
pDCPDF->DeleteDC();
}
希望很快达到15的声誉投票其他问题的答案。 感谢Andrew Komiagin的回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.