C# wps转pdf(word、ppt、excel),在线预览pdf

wps转pdf

  注:我是在wps试用期专业版,windows10系统  vs2019 webform(.net framework4.5)测试。

  前提:需要下载安装wps专业版、企业版。

  项目中需要引用wps的com组件

    com组件Upgrade WPS Spreadsheets 3.0 Object Library (Beta)  ,对应“Excel”,C:\WINDOWS\assembly\GAC_32\Kingsoft.Office.Interop.Etapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Etapi.dll

    com组件 Upgrade WPS Presentation 3.0 Object Library (Beta),对应“PowerPoint”,C:\WINDOWS\assembly\GAC_32\Kingsoft.Office.Interop.Wppapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Wppapi.dll

    com组件Upgrade Kingsoft WPS 3.0 Object Library (Beta),对应“Word”,C:\WINDOWS\assembly\GAC_32\Kingsoft.Office.Interop.Wpsapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Wpsapi.dll

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
public  static  class  WpsToPdf
{
     /// <summary>
     /// word转pdf
     /// </summary>
     /// <param name="source">源<see cref="string"/>.</param>
     /// <param name="newFilePath">新文件路径<see cref="string"/>.</param>
     /// <returns>The <see cref="bool"/>.</returns>
     public  static  bool  WordWpsToPdf( string  source,  string  newFilePath)
     {
         if  (source ==  null throw  new  ArgumentNullException(nameof(source));
         if  (newFilePath ==  null throw  new  ArgumentNullException(nameof(newFilePath));
 
         var  type = Type.GetTypeFromProgID( "KWps.Application" );
         dynamic wps = Activator.CreateInstance(type);
         try
         {
             //用wps打开word不显示界面
             dynamic doc = wps.Documents.Open(source, Visible:  false );
 
             //转pdf
             doc.ExportAsFixedFormat(newFilePath, WdExportFormat.wdExportFormatPDF);
 
             //设置隐藏菜单栏和工具栏
             //wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);
             doc.Close();
         }
         catch  (Exception e)
         {
             //添加你的日志代码
             return  false ;
         }
         finally
         {
             wps.Quit();
             GC.Collect();
             GC.WaitForPendingFinalizers();
         }
 
         return  true ;
     }
 
     /// <summary>
     /// excel转pdf
     /// </summary>
     /// <param name="source">源<see cref="string"/>.</param>
     /// <param name="newFilePath">新文件路径<see cref="string"/>.</param>
     /// <returns>The <see cref="bool"/>.</returns>
     public  static  bool  ExcelToPdf( string  source,  string  newFilePath)
     {
         if  (source ==  null throw  new  ArgumentNullException(nameof(source));
         if  (newFilePath ==  null throw  new  ArgumentNullException(nameof(newFilePath));
 
         var  type = Type.GetTypeFromProgID( "KET.Application" );
         dynamic wps = Activator.CreateInstance(type);
         try
         {
             var  targetType = XlFixedFormatType.xlTypePDF;
             var  missing = Type.Missing;
             //转pdf
             var  doc = wps.Application.Workbooks.Open(source, missing, missing, missing, missing, missing,
                 missing, missing, missing, missing, missing, missing, missing, missing, missing);
             doc.ExportAsFixedFormat(targetType, newFilePath, XlFixedFormatQuality.xlQualityStandard,  true false ,
                 missing, missing, missing, missing);
             doc.Close();
         }
         catch  (Exception e)
         {
             //添加你的日志代码
             return  false ;
         }
         finally
         {
             wps.Quit();
             GC.Collect();
             GC.WaitForPendingFinalizers();
         }
         return  true ;
     }
 
     /// <summary>
     /// ppt转pdf
     /// </summary>
     /// <param name="source">源<see cref="string"/>.</param>
     /// <param name="newFilePath">新文件路径<see cref="string"/>.</param>
     /// <returns>The <see cref="bool"/>.</returns>
     public  static  bool  PptToPdf( string  source,  string  newFilePath)
     {
         var  type = Type.GetTypeFromProgID( "KWPP.Application" );
         dynamic wps = Activator.CreateInstance(type);
         try
         {
             //转pdf
             var  doc = wps.Presentations.Open(source, MsoTriState.msoCTrue, MsoTriState.msoCTrue,
                 MsoTriState.msoCTrue);
             doc.SaveAs(newFilePath, PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoTrue);
             doc.Close();
         }
         catch  (Exception e)
         {
             //添加你的日志代码
             return  false ;
         }
         finally
         {
             wps.Quit();
             GC.Collect();
             GC.WaitForPendingFinalizers();
         }
         return  true ;
     }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值