[转]doc,ppt,xls文件格式转PDF格式

http://blog.csdn.net/lee353086/article/details/7920355

确实好用。

需要注意的是#import文件路径要和自己电脑上的文件路径对应

  1. /*
  2. 功能:
  3. Office文件格式(docx、xlsx、pptx)转PDF格式文件
  4. Author:
  5. Kagula by 2012-08-29
  6. 使用前提
  7. [1]Office 2007(Word,Excel,PPT)
  8. [2]Office 2007导PDF插件
  9. 编译环境:
  10. [1]VS2008SP1
  11. [2]WinXPSP3
  12. */
  13.  
  14. #pragma warning(disable:4786)
  15.  
  16. #import "C:\Program Files\Common Files\Microsoft Shared\Office12\mso.dll" \
  17. rename("RGB","_OfficeRGB")
  18.  
  19. #import "C:\Program Files\Common Files\Microsoft Shared\VBA\VBA6\VBE6EXT.OLB" \
  20. rename("Reference", "ignorethis")
  21.  
  22. #import "C:\Program Files\Microsoft Office\Office12\msword.olb " \
  23. rename("FindText","_FindText")\
  24. rename("Rectangle","_Rectangle")\
  25. rename("ExitWindows","_ExitWindows")
  26.  
  27. #import "C:\Program Files\Microsoft Office\Office12\MSPPT.OLB"
  28.  
  29. #import "c:\Program Files\Microsoft Office\Office12\EXCEL.exe" \
  30. rename("DialogBox","_DialogBox") \
  31. rename("RGB","_RGB") \
  32. exclude("IFont","IPicture")
  33.  
  34. #include <string>
  35. #include <iostream>
  36.  
  37. int EXCEL2PDF(std::wstring inputFileName,std::wstring outputFileName)
  38. {
  39. HRESULT hr;
  40. ;
  41.  
  42. Excel::_ApplicationPtr pApplication = NULL;
  43. Excel::_WorkbookPtr pThisWorkbook = NULL ;
  44. BSTR szBstrInputFileName;
  45. BSTR szBstrOutputFileName;
  46.  
  47. szBstrInputFileName=SysAllocString(inputFileName.c_str());
  48. szBstrOutputFileName=SysAllocString(outputFileName.c_str());
  49.  
  50. if (FAILED(pApplication.CreateInstance(__uuidof(Excel::Application))))
  51. {
  52. wprintf(L"CreateInstance failed w/err 0x%08lx\n", hr);
  53. ;
  54. }
  55. try
  56. {
  57. pThisWorkbook = pApplication->GetWorkbooks()->Open(szBstrInputFileName);
  58.  
  59. pThisWorkbook->ExportAsFixedFormat(Excel::XlFixedFormatType::xlTypePDF,szBstrOutputFileName);
  60.  
  61. pThisWorkbook->Close();
  62. pThisWorkbook.Release();
  63. pThisWorkbook = NULL;
  64. }catch(...)
  65. {
  66. nR = ;
  67. }
  68.  
  69. pApplication-> Quit();
  70. pApplication.Release();
  71. pApplication= NULL;
  72.  
  73. return nR;
  74. }
  75.  
  76. int PPT2PDF(std::wstring inputFileName,std::wstring outputFileName)
  77. {
  78. PowerPoint::_ApplicationPtr spPpApp;
  79. BSTR szBstrInputFileName;
  80. BSTR szBstrOutputFileName;
  81. BSTR szBstrEmpty;
  82. HRESULT hr;
  83. PowerPoint::PresentationsPtr spPres;
  84. PowerPoint::_PresentationPtr pPre;
  85. ;
  86.  
  87. szBstrInputFileName=SysAllocString(inputFileName.c_str());
  88. szBstrOutputFileName=SysAllocString(outputFileName.c_str());
  89. szBstrEmpty=SysAllocString(L"");
  90.  
  91. if (FAILED(spPpApp.CreateInstance(__uuidof(PowerPoint::Application))))
  92. {
  93. wprintf(L"CreateInstance failed w/err 0x%08lx\n", hr);
  94. ;
  95. }
  96.  
  97. spPres = spPpApp->Presentations;
  98. if(spPres==NULL)
  99. {
  100. nR = ;
  101. goto _RELEASE_APP;
  102. }
  103. try{
  104. pPre = spPres->Open(szBstrInputFileName,
  105. Office::MsoTriState::msoTrue,Office::MsoTriState::msoFalse,Office::MsoTriState::msoFalse);
  106. if(pPre ==NULL)
  107. {
  108. nR = ;
  109. goto _RELEASE_APP;
  110. }
  111.  
  112. pPre->ExportAsFixedFormat(szBstrOutputFileName,PowerPoint::PpFixedFormatType::ppFixedFormatTypePDF,
  113. PowerPoint::PpFixedFormatIntent::ppFixedFormatIntentPrint,Office::MsoTriState::msoTriStateMixed,
  114. PowerPoint::PpPrintHandoutOrder::ppPrintHandoutHorizontalFirst,PowerPoint::PpPrintOutputType::ppPrintOutputSlides,
  115. Office::MsoTriState::msoFalse,NULL,PowerPoint::PpPrintRangeType::ppPrintAll,szBstrEmpty,
  116. VARIANT_TRUE,VARIANT_FALSE,VARIANT_TRUE,VARIANT_TRUE,VARIANT_FALSE);
  117.  
  118. pPre->Close();
  119. pPre.Release();
  120. pPre = NULL;
  121. }catch(...)
  122. {
  123. nR==;
  124. }
  125. _RELEASE_APP:
  126. spPpApp-> Quit();
  127. spPpApp.Release();
  128. spPpApp = NULL;
  129.  
  130. return nR;
  131. }
  132.  
  133. int Word2PDF(std::wstring inputFileName,std::wstring outputFileName)
  134. {
  135. ;
  136. Word::_ApplicationPtr pWordApp = NULL;
  137. Word::_DocumentPtr pDoc = NULL;
  138. HRESULT hr;
  139. BSTR szBstrOutputFileName;
  140.  
  141. szBstrOutputFileName=SysAllocString(outputFileName.c_str());
  142. hr = pWordApp.CreateInstance(__uuidof(Word::Application));
  143. if(hr!=S_OK)
  144. {
  145. ;
  146. }
  147.  
  148. Word::DocumentsPtr pDocs = NULL;
  149. pWordApp-> get_Documents(&pDocs);
  150. if(pDocs==NULL)
  151. {
  152. nR = ;
  153. goto _RELEASE_APP;
  154. }
  155.  
  156. try
  157. {
  158. pDoc = pDocs->Open(&(_variant_t(inputFileName.c_str())));
  159. if(pDoc==NULL)
  160. goto _RELEASE_APP;
  161.  
  162. pDoc->ExportAsFixedFormat(szBstrOutputFileName,Word::WdExportFormat::wdExportFormatPDF,VARIANT_FALSE,
  163. Word::WdExportOptimizeFor::wdExportOptimizeForPrint,Word::WdExportRange::wdExportAllDocument,,,
  164. Word::WdExportItem::wdExportDocumentContent,VARIANT_TRUE,VARIANT_TRUE,
  165. Word::WdExportCreateBookmarks::wdExportCreateNoBookmarks,VARIANT_TRUE,VARIANT_TRUE,VARIANT_FALSE);
  166.  
  167. pDoc-> Close();
  168. pDoc.Release();
  169. pDoc = NULL;
  170. }catch(...)
  171. {
  172. nR = ;
  173. }
  174.  
  175. _RELEASE_APP:
  176. pWordApp-> Quit();
  177. pWordApp.Release();
  178. pWordApp = NULL;
  179.  
  180. return nR;
  181. }
  182.  
  183. int _tmain(int argc, _TCHAR* argv[])
  184. {
  185. ;
  186.  
  187. CoInitialize(NULL);
  188.  
  189. std::wstring wsCmd;
  190. std::wstring wsS;
  191. std::wstring wsD;
  192.  
  193. )
  194. {
  195. std::cout<<"Command Usage: Office2PDF -[e|p|w] <source file name> <destination file name>"<<std::endl;
  196. std::cout<<" e.g.: Office2PDF -e myName.xlsx myName.pdf"<<std::endl;
  197. ;
  198. }
  199.  
  200. wsCmd = argv[];
  201. wsS = argv[];
  202. wsD = argv[];
  203.  
  204. if(wsCmd==L"-e")
  205. nR = EXCEL2PDF(wsS.c_str(),wsD.c_str());
  206. else if(wsCmd==L"-p")
  207. nR = PPT2PDF(wsS.c_str(),wsD.c_str());
  208. else if(wsCmd==L"-w")
  209. nR = Word2PDF(wsS.c_str(),wsD.c_str());
  210.  
  211. CoUninitialize();
  212.  
  213. )
  214. std::cout<<"Error:"<<nR<<std::endl;
  215.  
  216. return nR;
  217. }

c/c++常用代码---doc,ppt,xls文件格式转PDF格式[转]的更多相关文章

  1. 文档资源搜索小工具 - 支持PDF,DOC,PPT,XLS

    最近做了一个文档搜索小工具,当然不是网盘搜索工具,这个工具支持四种文件格式搜索(pdf,doc,ppt,xls),你只需要在搜索框中输入你想要搜索资源的关键词,点击搜索按钮即可获取相关资源,点击下载按 ...

  2. NSIS常用代码整理

    原文 NSIS常用代码整理 这是一些常用的NSIS代码,少轻狂特意整理出来,方便大家随时查看使用.不定期更新哦~~~ 1 ;获取操作系统盘符 2 ReadEnvStr $R0 SYSTEMDRIVE ...

  3. XSS(跨站脚本攻击) - 常用代码大全

    XSS(跨站脚本攻击) - 常用代码大全-版本一 1'"()&%<acx><ScRiPt >prompt(915149)</ScRiPt> < ...

  4. GCD 常用代码

    GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...

  5. 转--Android实用的代码片段 常用代码总结

    这篇文章主要介绍了Android实用的代码片段 常用代码总结,需要的朋友可以参考下     1:查看是否有存储卡插入 复制代码 代码如下: String status=Environment.getE ...

  6. 刀哥多线程之03GCD 常用代码

    GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...

  7. jquery常用代码集锦

    1. 如何修改jquery默认编码(例如默认GB2312改成 UTF-8 ) 1 2 3 4 5 $.ajaxSetup({     ajaxSettings : {         contentT ...

  8. IOS 使用webview 显示 doc/docx/xls/pdf等

    在一款项目里添加阅读各种文档功能 那么对在线的文档或者是下载后的文档 进行阅读,比如 doc/docx/xls/pdf等文件 有两种方法总结如下: 1. - (void)viewDidLoad { [ ...

  9. Mysql:常用代码

    C/S: Client Server B/S: Brower Server Php主要实现B/S .net IIS Jave TomCat LAMP:L Mysql:常用代码 Create table ...

随机推荐

  1. html5相关

    1.语义化标签: header\section\aside\footer 2.音视频播放: video\audio 其中video支持ogg\mpeg4\webm格式的视频: 3.canvas 一.画 ...

  2. js中字符串,数字之间转换的常用方法

    var number={ num:, num1:"2你好" }; //将数字转换为字符串 var str=number.num.toString();//十进制 );//二进制 ) ...

  3. WP8_读写XML

    /// <summary> /// WP手机,XML读写类 /// </summary> public class WPXmlRW { /// <summary> ...

  4. Linux:两台服务器之间添加信任关系,进行远程操作的时候不需要输入密码

    两台机器之间建立信任关系的步骤: 1. 在机器1上root用户执行ssh-keygen命令,生成建立安全信任关系的证书,直接Enter [root@CentOS64-x64 ~]# ssh-keyge ...

  5. Hbase rest方式获取指定key范围内的值

    代码如下: <?php class Monitor_Hbase{ private $rest_host = "http://10.99.90.39:8130/";//rest ...

  6. Vim中的正则表达式[转]

    来自:http://blog.csdn.net/endall/archive/2007/08/29/1764554.aspx Vim中的正则表达式功能很强大,如果能自由运用,则可以完成很多难以想象的操 ...

  7. Unieap3.5-Grid编辑列中数字与下拉改变

    Grid列表中字段改变事件 <cell label="单据金额" width='20%' name="NFEE_1" id="NFEE_1&qu ...

  8. svn服务器搭建与配置

    http://www.cnblogs.com/armyfai/p/3985660.html

  9. [原]sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)

    本文出自:http://blog.csdn.net/svitter 题意: f(x) = K, x = 1 f(x) = (a*f(x-1) + b)%m , x > 1 求出( A^(f(1) ...

  10. PeopleCode 处理压缩文件

     PeopleSoft中对文件附件的处理都是单个文件处理的,虽然在8.52版本新增了MAddAttachment(URLDestination, DirAndFilePrefix, Prompts, ...