需要將不少doc文件轉爲pdf,WPS帶有這種功能,但是鼠標點擊次數太多以後整個人都變得很煩躁
用了一下午去搜這方面的工具軟件,找到若干。有一些免費,有一些試用的,但總歸就找到一個真正能用,雖說生成的文件名中有未授權字樣,但批量修改文件名簡單多了。
誰知道到了實驗室的電腦上因爲什麼打印機錯誤,還是不能用!
於是決定自己寫一個,
第二天上午開始搜資料,亂搜一陣,居然發現WPS有二次開發的功能,大喜
但是,沒有C++開發接口的資料,而且官方論壇的C++例子是針對老版本的。
於是參考別人寫的C#和VB的例子,在那摸索一陣,總算完事。
- void CTestDocDlg::OnBnClickedButton1()
- {
- _beginthreadex(NULL, 0, convertThread, this, 0, NULL);
- //StartConvert(m_FileSrc);
- }
- void CTestDocDlg::OnBnClickedButton2()
- {
- // TODO: 在此添加控件通知處理程序代碼
- TCHAR Buffer[MAX_PATH];
- BROWSEINFO bi;
- ZeroMemory(&bi, sizeof(BROWSEINFO));
- bi.hwndOwner = m_hWnd;
- bi.ulFlags = BIF_RETURNONLYFSDIRS ; //要求返回文件系統的目錄
- bi.pszDisplayName = Buffer; //此參數如爲NULL則不能顯示對話框
- bi.lpszTitle = _T("請選擇文件夾");
- bi.lpfn = NULL;
- bi.iImage=IDR_MAINFRAME;
- LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//調用顯示選擇對話框
- if(pIDList)
- {
- SHGetPathFromIDList(pIDList, Buffer);
- //取得文件夾路徑到Buffer裏
- UpdateData(FALSE);
- m_FileSrc = Buffer;//將文件夾路徑保存在一個CString對象裏
- if(m_FileSrc != "" && m_FileSrc.GetAt(m_FileSrc.GetLength() - 1) != '\\')
- m_FileSrc += "\\";
- m_destPath.SetWindowText(m_FileSrc);
- }
- else
- {
- }
- }
- int CTestDocDlg::StartConvert(CString path)
- {
- CFileFind fileFinder;
- CString filePth = path + _T("*.doc");
- BOOL bFinished = fileFinder.FindFile(filePth);
- // 先蒐集文件信息,保存起來,再集中處理!
- while(bFinished)
- {
- bFinished = fileFinder.FindNextFile();
- CString fileName = fileFinder.GetFileName();
- AddFileInfo(fileName.GetBuffer(0));
- //ConvertFile(path + fileName);
- }
- fileFinder.Close();
- std::vector<std::string>::iterator theIter;
- for(theIter = m_vecFileName.begin(); theIter != m_vecFileName.end(); theIter++)
- {
- ConvertFile(path + theIter->c_str());
- }
- return 0;
- }
- int CTestDocDlg::ConvertFile(CString szFileName)
- {
- CApplication app;
- app.CreateDispatch("WPS.APPLICATION");
- //app.SetVisible(TRUE);
- //app.doc
- app.put_Visible(FALSE);
- CDocuments docs = app.get_Documents();
- CDocument0 doc = docs.Open(szFileName, FALSE, TRUE, FALSE, NULL, NULL, TRUE, NULL, NULL, 0, 0, FALSE, FALSE, 0, FALSE);
- CString pdfName = szFileName;
- pdfName.Replace("doc", _T("pdf"));
- doc.ExportPdf(pdfName, NULL, NULL);
- //docs.Close(NULL, NULL, NULL);
- //doc.Close(NULL, NULL, NULL);
- COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),
- vtTrue((short)TRUE),
- vtFalse((short)FALSE);
- doc.Close(vtFalse, vtOptional, vtOptional);
- return 0;
- }
- unsigned int WINAPI CTestDocDlg::convertThread(void *pParam)
- {
- CoInitialize(NULL);
- ((CTestDocDlg *)pParam)->ReadConvert();
- ::CoUninitialize();
- return 0;
- }
- int CTestDocDlg::ReadConvert()
- {
- StartConvert(m_FileSrc);
- return 0;
- }
- void CTestDocDlg::AddFileInfo(CString strFileName)
- {
- m_vecFileName.push_back(strFileName.GetBuffer(0));
- }