C#将Word转换成PDF方法汇总(基于Office和WPS)
作者:雲霏霏 时间:2021-09-12 22:52:58
有时候,我们需要在线上预览word文档,当然我们可以用NPOI抽出Word中的文字和表格,然后显示到网页上面,但是这样会丢失掉Word中原有的格式和图片。一个比较好的办法就是将word转换成pdf,然后让客户预览,下面来看一下基于Office和WPS的两种解决方案。
一、基于Office的解决方案
正如标题所说,基于Office就是要求服务器上面要安装的有Office。我们通过C#代码来调用COM接口,实现将Word转换成PDF。下面来看一下具体实现,首先引用Microsoft.Office.Interop.Word.dll,然后编写如下代码:
public bool WordToPDF(string sourcePath, string targetPath)
{
bool result = false;
Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
Document document = null;
try
{
application.Visible = false;
document = application.Documents.Open(sourcePath);
document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF);
result = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = false;
}
finally
{
document.Close();
}
return result;
}
二、基于WPS的解决方案
WPS最大的好处当然是免费,还有就是体积小。要实现Word到PDF的转换,当然,这个要求服务器上面必须安装的有WPS,我们调用的仍然是COM接口,然后编写如下代码:
public bool WordToPdfWithWPS(string sourcePath, string targetPath)
{
WPS.ApplicationClass app = new WPS.ApplicationClass();
WPS.Document doc = null;
try
{
doc = app.Documents.Open(sourcePath, true, true, false, null, null, false, "", null, 100, 0, true, true, 0, true);
doc.ExportPdf(targetPath, "", "");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
finally
{
doc.Close();
}
return true;
}
三、企业级解决方案
对于大型企业来说,往往有多台服务器,不可能在每台服务器上面安装office或者会WPS,或者说公司根本不想在服务器上面安装这些没用的软件。这个时候该怎么办呢?毕竟服务器上面安装这些软件就是一种资源的浪费。
当然,功能还是要实现的,那么该怎么解决呢?实际上,我们可以在一台服务器上面安装ofiice或者WPS软件,然后部署WCF服务或者remoting等WebService,其他的服务器可以调用这个服务来实现Word到PDF的转换。
标签:C#,Word,PDF
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Dubbo Consumer引用服务示例代码详解
2022-04-26 03:56:44
10种简单的Java性能优化
2023-06-20 20:43:41
![](https://img.aspxhome.com/file/2023/6/57526_0s.png)
Java的内部类总结
2022-08-13 01:33:41
Java单元测试工具之JUnit的使用
2022-09-05 13:20:54
![](https://img.aspxhome.com/file/2023/3/102003_0s.png)
详细讲述Java中的对象转型
2023-09-24 04:09:49
![](https://img.aspxhome.com/file/2023/5/103965_0s.png)
java list,set,map,数组间的相互转换详解
2023-04-11 13:02:04
Java关键字instanceof的两种用法实例
2023-07-31 06:24:50
java调用回调机制详解
2023-11-14 21:53:21
![](https://img.aspxhome.com/file/2023/2/59152_0s.jpg)
Java中多线程下载图片并压缩能提高效率吗
2023-08-06 07:40:10
![](https://img.aspxhome.com/file/2023/6/77076_0s.png)
Java中垃圾回收器GC对吞吐量的影响测试
2022-12-16 12:15:53
java中分组统计的三种实现方式
2023-05-29 06:04:13
详解用Eclipse如何创建Web项目
2023-11-11 05:41:01
![](https://img.aspxhome.com/file/2023/3/59393_0s.png)
SpringBoot 使用Prometheus采集自定义指标数据的方案
2023-04-25 02:50:39
![](https://img.aspxhome.com/file/2023/4/75494_0s.jpg)
Spring MVC 简单的hello world的实现
2023-06-18 02:56:41
![](https://img.aspxhome.com/file/2023/7/72597_0s.png)
关于mybatis if else if 条件判断SQL片段表达式取值和拼接问题
2023-02-04 18:51:21
![](https://img.aspxhome.com/file/2023/3/100063_0s.jpg)
Java 如何实现解压缩文件和文件夹
2023-07-17 23:10:22
Unity脚本自动添加头部注释的全过程
2021-06-09 08:38:20
![](https://img.aspxhome.com/file/2023/3/69813_0s.png)
SpringCloud之熔断器Hystrix的实现
2021-09-21 01:39:26
![](https://img.aspxhome.com/file/2023/1/110911_0s.png)
hadoop实现grep示例分享
2023-12-24 05:31:04
sharding-jdbc5.0.0实现分表实践
2023-12-07 10:12:26