Blog / 阅读

将doc文件批量转为pdf文件

by admin on 2014-03-23 10:30:26 in ,



需要将不少doc文件转为pdf,WPS带有这种功能,但是鼠标点击次数太多以后整个人都变得很烦躁
用了一下午去搜这方面的工具软件,找到若干。有一些免费,有一些试用的,但总归就找到一个真正能用,虽说生成的文件名中有未授权字样,但批量修改文件名简单多了。
谁知道到了实验室的电脑上因为什么打印机错误,还是不能用!
于是决定自己写一个,
第二天上午开始搜资料,乱搜一阵,居然发现WPS有二次开发的功能,大喜
但是,没有C++开发接口的资料,而且官方论坛的C++例子是针对老版本的。
于是参考别人写的C#和VB的例子,在那摸索一阵,总算完事。

[cpp] view plaincopy
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));  
}  

后面才知道原来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组件和线程的关联原因
参考了http://support.microsoft.com/kb/238393/en-us

http://www.qtcn.org/bbs/simple/?t31216.html

本文转载自:http://blog.csdn.net/haithink/article/details/21804619


写评论

相关文章

上一篇:【Two Sigma】 数字单词混合字符串排序 Mix Sort

下一篇:DES加密算法详解- -

评论

写评论

* 必填.

分享

栏目

赞助商


热门文章

Tag 云