虚拟打印机,ADOBE PDF
// 打印PDF文件通过虚拟打印机
BOOL CADDINSGYD00FEFE::PrintCappFile2Pdf(IOb_ObjectPtr objSource)
{
_variant_t varTemp; // 临时对象
CString strFileName;
long nFileID;
CString strFileID;
CString strCode;
CString strVersion;
CString strPDFName;
THT5PATHSERVICE14Lib::IT5PathServicePtr pComPathServicePtr;
pComPathServicePtr.CreateInstance(__uuidof(THT5PATHSERVICE14Lib::T5PathService));
// 文件名称
varTemp = objSource->GetPropertyValueByName("FILENAME");
if(varTemp.vt != VT_NULL)
strFileName = (LPCTSTR)(_bstr_t)varTemp;
varTemp = objSource->GetPropertyValueByName("CODE");
if(varTemp.vt != VT_NULL)
strCode = (LPCTSTR)(_bstr_t)varTemp;
varTemp = objSource->GetPropertyValueByName("REVISION");
if(varTemp.vt != VT_NULL)
strVersion = (LPCTSTR)(_bstr_t)varTemp;
int iPos = strFileName.ReverseFind('.');
if(iPos != -1)
{
strFileName = strFileName.Left(iPos);
}
CString strTempPDFFile;
CString strTempTagetFile;
strTempPDFFile = (TCHAR*)pComPathServicePtr->GetTempPathA();
strTempTagetFile = (TCHAR*)pComPathServicePtr->GetTempPathA();
if(strTempPDFFile.Right(1) != _T("\\")) strTempPDFFile += _T("\\");
if(strTempTagetFile.Right(1) != _T("\\")) strTempTagetFile += _T("\\");
varTemp = objSource->GetPropertyValueByName("CAPPFILEID");
if(varTemp.vt != VT_NULL)
strFileID = (LPCTSTR)(_bstr_t)varTemp;
nFileID = atof(_bstr_t(strFileID));
strTempPDFFile = strTempPDFFile + _T(".pdf"); //_T("C:\\.pdf");//_T("C:\\只读_:") + strFileName + _T(".pdf");
strTempTagetFile = strTempTagetFile + strCode + _T("-") + strVersion + _T(".pdf");
strPDFName = strCode + _T("-") + strVersion + _T(".pdf");
CFileFind find;
if(find.FindFile(strTempPDFFile))
{
::DeleteFile(strTempPDFFile);
}
//获取系统当前使用的默认打印机的名称
m_strCutDefaultPrinterName = GetDefaultPrinterName(strMsg);
if (m_strCutDefaultPrinterName.IsEmpty()) {
if (strMsg.IsEmpty())
strMsg = _T("无法获取系统当前使用的默认打印机信息!");
strMsg += _T("\n不能进行提交操作!");
if (!strMsg.IsEmpty()) AfxMessageBox(strMsg);
return FALSE;
}
//检查系统中是否包含指定名称的打印机
if (!FindPrinter(PDF_PRINTER, strMsg)) {
if (strMsg.IsEmpty())
strMsg.Format(_T("无法查找到打印机:“%s”"), PDF_PRINTER);
strMsg += _T("\n不能进行提交操作!");
if (!strMsg.IsEmpty()) AfxMessageBox(strMsg);
return S_FALSE;
}
//将系统默认打印机设置为“Adobe PDF”
if (m_strCutDefaultPrinterName != PDF_PRINTER) {
if (!SetSystemDefaultPrinter(PDF_PRINTER, strMsg)) {
if (!strMsg.IsEmpty()) AfxMessageBox(strMsg);
return S_FALSE;
}
}
BSTR bstrPDFPath = strTempPDFFile.AllocSysString();
CString m_strT5AppHomePath;
m_strT5AppHomePath = (TCHAR*)pComPathServicePtr->GetHomePath();
if(m_strT5AppHomePath.Right(1) != _T("\\")) m_strT5AppHomePath += _T("\\");
。。。。。。。。。。。。。。。。。。。
//::WinExec(CStringA(strCmdLine),SW_NORMAL);
//创建进程间通讯邮槽
HWND hWnd;
hWnd = FindWindow(G_MAINFRAME_CLASS_NAME_GUID, NULL);
G_lMsg = RegisterWindowMessage(G_REGISTER__EXECMDLINE);
//写入邮槽信息
WriteCAPPSlot(G_MAINFRAME_CLASS_NAME_GUID, strCmdLine);
::SendMessage(hWnd, G_lMsg, NULL, NULL);
//循环等待打印结束,等待时间最长为分钟
BOOL bFindFile = FALSE;
int i = 1;
for (i = 0; i < 300; i++) {
if (find.FindFile(strTempPDFFile)) {
bFindFile = TRUE;
break;
} else
Sleep(1000);
}
//恢复系统默认打印机
if (m_strCutDefaultPrinterName != PDF_PRINTER) {
if (!SetSystemDefaultPrinter(m_strCutDefaultPrinterName, strMsg)) {
if (!strMsg.IsEmpty()) AfxMessageBox(strMsg);
return S_FALSE;
}
}
if (!bFindFile) {
m_strLastError = _T("生成PDF文件失败!");
return S_FALSE;
}
//复制虚拟打印产生的PDF文件,并改名
::CopyFile(strTempPDFFile, strTempTagetFile, FALSE);
::DeleteFile(strTempPDFFile);
// 转化成字节流传送至A4
PDF2ByteSend(strPDFName, strTempTagetFile);
return TRUE;
}
// 打印相关函数
// ==============================================================================================================
//获取系统当前使用的默认打印机的名称
CString CADDINSGYD00FEFE::GetDefaultPrinterName(CString& strMsg)
{
strMsg.Empty();
CString strDefaultPrinterName(_T(""));
try {
//设置打印参数
PRINTDLG pd; //该结构包含打印对话框中的所有信息
LPDEVMODE lpDevMode;
if(AfxGetApp()->GetPrinterDeviceDefaults(&pd)) { //获得默认的打印机的信息
lpDevMode=(LPDEVMODE)GlobalLock(pd.hDevMode);
if(lpDevMode) {
strDefaultPrinterName = lpDevMode->dmDeviceName;
}
GlobalUnlock(pd.hDevMode);
}
}
catch (_com_error& e)
{
strMsg.Format(_T("获取系统当前使用的默认打印机信息出错:%s"), e.ErrorMessage());
return _T("");
}
catch (CException* e)
{
TCHAR ch[1024];
e->GetErrorMessage(ch, 1024);
strMsg.Format(_T("获取系统当前使用的默认打印机信息出错:%s"), ch);
return _T("");
}
return strDefaultPrinterName;
}
//检查系统中是否包含指定名称的打印机
BOOL CADDINSGYD00FEFE::FindPrinter(const CString& strPrinterName, CString& strMsg)
{
try {
DWORD dwSize = 0, dwPrinters = 0;
::EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 5, NULL, 0, &dwSize, &dwPrinters);
BYTE *pBuffer = new BYTE[dwSize];
::EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL, NULL, 5, pBuffer, dwSize, &dwSize, &dwPrinters);
CString sPrinter(_T(""));
if (dwPrinters != 0) {
PRINTER_INFO_5* pPrnInfo=(PRINTER_INFO_5*)pBuffer;
int i=0;
for (i=0; i<dwPrinters; i++) {
//获取打印机名称
sPrinter.Format(_T("%s"), pPrnInfo->pPrinterName);
if (sPrinter.CompareNoCase(strPrinterName) == 0) {
delete []pBuffer;
return TRUE;
}
//打印机指针后移
pPrnInfo++;
}
}
delete []pBuffer;
}
catch (_com_error& e)
{
strMsg.Format(_T("获取系统所有打印机信息出错:%s"), e.ErrorMessage());
return FALSE;
}
catch (CException* e)
{
TCHAR ch[1024];
e->GetErrorMessage(ch, 1024);
strMsg.Format(_T("获取系统所有打印机信息出错:%s"), ch);
return FALSE;
}
return FALSE;
}
//设置系统默认打印机
BOOL CADDINSGYD00FEFE::SetSystemDefaultPrinter(const CString& strPrinterName, CString& strMsg)
{
BOOL bFlag = FALSE;
LONG lResult = 0;
DWORD dwNeeded = 0;
LPTSTR pBuffer = NULL;
HANDLE hPrinter = NULL;
OSVERSIONINFO stOsvInfo = {0};
PRINTER_INFO_2* pstPrintInfo2 = NULL;
try {
LPTSTR pPrinterName = (LPTSTR)(LPCTSTR)strPrinterName;
stOsvInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&stOsvInfo);
if(!pPrinterName)
return FALSE;
if(stOsvInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) //Win9x
{
// Open this printer so we can get information about it
bFlag = OpenPrinter(pPrinterName, &hPrinter, NULL);
if(!bFlag || hPrinter==NULL)
return FALSE;
GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
if(dwNeeded == 0) {
ClosePrinter(hPrinter);
return FALSE;
}
pstPrintInfo2 = (PRINTER_INFO_2 *)GlobalAlloc(GPTR, dwNeeded);
if(pstPrintInfo2 == NULL) {
ClosePrinter(hPrinter);
return FALSE;
}
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pstPrintInfo2, dwNeeded, &dwNeeded);
if(!bFlag) {
ClosePrinter(hPrinter);
GlobalFree(pstPrintInfo2);
return FALSE;
}
// Set default printer attribute for this printer...
pstPrintInfo2->Attributes |= PRINTER_ATTRIBUTE_DEFAULT;
bFlag = SetPrinter(hPrinter, 2, (LPBYTE)pstPrintInfo2, 0);
if(!bFlag) {
ClosePrinter(hPrinter);
GlobalFree(pstPrintInfo2);
return FALSE;
}
// lResult = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0L, (LPARAM)(LPCTSTR)"windows", SMTO_NORMAL, 1000, NULL);
}
else if (stOsvInfo.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
#if(WINVER >= 0x0500)
if(stOsvInfo.dwMajorVersion >= 5) // Windows 2000 or later...
{
bFlag = SetDefaultPrinter(pPrinterName);
if (!bFlag)
return FALSE;
}
else // NT4.0 or earlier...
#endif
{
bFlag = OpenPrinter(pPrinterName, &hPrinter, NULL);
if(!bFlag || hPrinter==NULL)
return FALSE;
GetPrinter(hPrinter, 2, 0, 0, &dwNeeded);
if(dwNeeded == 0) {
ClosePrinter(hPrinter);
return FALSE;
}
pstPrintInfo2 = (PRINTER_INFO_2*)GlobalAlloc(GPTR, dwNeeded);
if(pstPrintInfo2 == NULL) {
ClosePrinter(hPrinter);
return FALSE;
}
bFlag = GetPrinter(hPrinter, 2, (LPBYTE)pstPrintInfo2, dwNeeded, &dwNeeded);
if((!bFlag) || (!pstPrintInfo2->pDriverName) || (!pstPrintInfo2->pPortName)) {
ClosePrinter(hPrinter);
GlobalFree(pstPrintInfo2);
return FALSE;
}
pBuffer = (LPTSTR)GlobalAlloc(GPTR, lstrlen(pPrinterName)+lstrlen(pstPrintInfo2->pDriverName)+lstrlen(pstPrintInfo2->pPortName)+3);
if(pBuffer == NULL) {
ClosePrinter(hPrinter);
GlobalFree(pstPrintInfo2);
return FALSE;
}
// Build string in form "printername,drivername,portname"
lstrcpy(pBuffer, pPrinterName); lstrcat(pBuffer, ",");
lstrcat(pBuffer, pstPrintInfo2->pDriverName); lstrcat(pBuffer, ",");
lstrcat(pBuffer, pstPrintInfo2->pPortName);
bFlag = WriteProfileString("windows", "device", pBuffer);
if(!bFlag) {
ClosePrinter(hPrinter);
GlobalFree(pstPrintInfo2);
GlobalFree(pBuffer);
return FALSE;
}
}
//向所有的应用程序广播消息
lResult = SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0L, 0L, SMTO_NORMAL, 1000, NULL);
}
if(hPrinter) {
ClosePrinter(hPrinter);
}
if(pstPrintInfo2) {
GlobalFree(pstPrintInfo2);
}
if(pBuffer) {
GlobalFree(pBuffer);
}
}
catch (_com_error& e)
{
strMsg.Format(_T("设置系统默认打印机操作出错:%s"), e.ErrorMessage());
return FALSE;
}
catch (CException* e)
{
TCHAR ch[1024];
e->GetErrorMessage(ch, 1024);
strMsg.Format(_T("设置系统默认打印机操作出错:%s"), ch);
return FALSE;
}
return TRUE;
}