zoukankan      html  css  js  c++  java
  • 将doc文件批量转为pdf文件

    需要将不少doc文件转为pdf,WPS带有这种功能,但是鼠标点击次数太多以后整个人都变得很烦躁

    用了一下午去搜这方面的工具软件,找到若干。有一些免费,有一些试用的,但总归就找到一个真正能用,虽说生成的文件名中有未授权字样,但批量修改文件名简单多了。

    谁知道到了实验室的电脑上因为什么打印机错误,还是不能用!

    于是决定自己写一个,

    第二天上午开始搜资料,乱搜一阵,居然发现WPS有二次开发的功能,大喜

    但是,没有C++开发接口的资料,而且官方论坛的C++例子是针对老版本的。

    于是参考别人写的C#和VB的例子,在那摸索一阵,总算完事。

      

    1. void CTestDocDlg::OnBnClickedButton1()  
    2. {  
    3.     _beginthreadex(NULL, 0, convertThread, this, 0, NULL);  
    4.     //StartConvert(m_FileSrc);  
    5. }  
    6.   
    7.   
    8. void CTestDocDlg::OnBnClickedButton2()  
    9. {  
    10.     // TODO: 在此添加控件通知处理程序代码  
    11.   
    12.     TCHAR Buffer[MAX_PATH];  
    13.     BROWSEINFO bi;  
    14.     ZeroMemory(&bi, sizeof(BROWSEINFO));  
    15.     bi.hwndOwner = m_hWnd;  
    16.     bi.ulFlags = BIF_RETURNONLYFSDIRS ;    //要求返回文件系统的目录  
    17.   
    18.     bi.pszDisplayName = Buffer;            //此参数如为NULL则不能显示对话框  
    19.     bi.lpszTitle = _T("请选择文件夹");  
    20.     bi.lpfn = NULL;  
    21.     bi.iImage=IDR_MAINFRAME;  
    22.       
    23.     LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框  
    24.     if(pIDList)  
    25.     {  
    26.         SHGetPathFromIDList(pIDList, Buffer);  
    27.         //取得文件夹路径到Buffer里  
    28.         UpdateData(FALSE);  
    29.   
    30.         m_FileSrc = Buffer;//将文件夹路径保存在一个CString对象里  
    31.         if(m_FileSrc != "" && m_FileSrc.GetAt(m_FileSrc.GetLength() - 1) != '\')  
    32.             m_FileSrc += "\";  
    33.         m_destPath.SetWindowText(m_FileSrc);  
    34.   
    35.   
    36.     }  
    37.     else  
    38.     {  
    39.   
    40.     }  
    41. }  
    42.   
    43. int CTestDocDlg::StartConvert(CString path)  
    44. {  
    45.     CFileFind fileFinder;  
    46.     CString filePth = path + _T("*.doc");  
    47.   
    48.     BOOL bFinished = fileFinder.FindFile(filePth);  
    49.   
    50.     // 先搜集文件信息,保存起来,再集中处理!  
    51.     while(bFinished)  
    52.     {  
    53.         bFinished = fileFinder.FindNextFile();  
    54.         CString fileName = fileFinder.GetFileName();  
    55.         AddFileInfo(fileName.GetBuffer(0));  
    56.         //ConvertFile(path + fileName);   
    57.     }  
    58.     fileFinder.Close();  
    59.   
    60.     std::vector<std::string>::iterator theIter;  
    61.     for(theIter  = m_vecFileName.begin(); theIter != m_vecFileName.end(); theIter++)  
    62.     {  
    63.         ConvertFile(path + theIter->c_str());  
    64.     }  
    65.   
    66.     return 0;  
    67. }  
    68.   
    69. int CTestDocDlg::ConvertFile(CString szFileName)  
    70. {  
    71.     CApplication app;  
    72.     app.CreateDispatch("WPS.APPLICATION");  
    73.     //app.SetVisible(TRUE);  
    74.     //app.doc  
    75.     app.put_Visible(FALSE);  
    76.     CDocuments docs = app.get_Documents();  
    77.     CDocument0 doc = docs.Open(szFileName, FALSE, TRUE, FALSE, NULL, NULL, TRUE, NULL, NULL, 0, 0, FALSE, FALSE, 0, FALSE);  
    78.     CString pdfName = szFileName;  
    79.     pdfName.Replace("doc", _T("pdf"));  
    80.     doc.ExportPdf(pdfName, NULL, NULL);  
    81.     //docs.Close(NULL, NULL, NULL);  
    82.     //doc.Close(NULL, NULL, NULL);  
    83.     COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR),  
    84.         vtTrue((short)TRUE),  
    85.         vtFalse((short)FALSE);  
    86.     doc.Close(vtFalse, vtOptional, vtOptional);  
    87.     return 0;  
    88. }  
    89.   
    90. unsigned int WINAPI CTestDocDlg::convertThread(void *pParam)  
    91. {  
    92.     CoInitialize(NULL);  
    93.     ((CTestDocDlg *)pParam)->ReadConvert();  
    94.     ::CoUninitialize();  
    95.     return 0;  
    96. }  
    97.   
    98. int CTestDocDlg::ReadConvert()  
    99. {  
    100.     StartConvert(m_FileSrc);  
    101.     return 0;  
    102. }  
    103.   
    104. void CTestDocDlg::AddFileInfo(CString strFileName)  
    105. {  
    106.     m_vecFileName.push_back(strFileName.GetBuffer(0));  
    107. }  

    后面才知道原来C++版本的WPS二次开发,接口也是参考WORD的!!!

    参考http://stackoverflow.com/questions/145573/creating-opening-and-printing-a-word-file-from-c

     

    2014/03/22 10:32

    总算是解决了文档关闭问题(转PDF)
    不关闭的话,doc文件一直被占用,
    但调用close函数总是失败,后面参考
    http://support.microsoft.com/kb/220911/en-us
    重新构造了close函数参数,总算是解决了
     
    2014/03/22 11:05
    把转换代码放在线程中打开文件总是失败,
    后面线程总算是解决了,换了个初始化COM的函数,另外每个线程都要进行初始化!
    原始是因为COM组件和线程的关联原因
  • 相关阅读:
    XtraBackup2.3.3安装配置使用(innobakupex)
    MySQL主从配置问题整理
    saltstack之(十二)配置管理mount
    常用HTTP状态码和CURL 000问题
    RHEL6解决无法使用YUM源问题
    zabbix监控MySQL
    ELK-Python(三)
    解决eclipse项目下出现deployment descriptor和jax-ws web services
    【未来畅想】未来的电信通讯行业,账号密码将取代sim卡
    最新samba.tar.gz安装方法
  • 原文地址:https://www.cnblogs.com/web100/p/doc-to-pdf.html
Copyright © 2011-2022 走看看