LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#.net通过libreOffice实现Office文件转换为pdf文件

admin
2020年4月16日 0:6 本文热度 4484
一、安装 libreOffice

点击 libreOffice官网 或 点晴网站(64位:地址1地址2,32位:地址1地址2) 下载安装 libreOffice,注意安装的时候,不要做任何改动,全部按照默认值安装即可。


二、创建一个新的项目 LibreOffice

创建一个新的项目,方便后面调用。

添加下面代码:

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
public class OfficeConvert
   {
       static string getLibreOfficePath()
       {
           switch (Environment.OSVersion.Platform)
           {
               case PlatformID.Unix:
                   return "/usr/bin/soffice";
               case PlatformID.Win32NT:
                   string binaryDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                   return binaryDirectory + "\\Windows\\program\\soffice.exe";
               default:
                   throw new PlatformNotSupportedException("你的系统暂不支持!");
           }
       }
 
       public static void ToPdf(string officePath, string outPutPath)
       {
           //获取libreoffice命令的路径
           string libreOfficePath = getLibreOfficePath();
            
           ProcessStartInfo procStartInfo = new ProcessStartInfo(libreOfficePath, string.Format("--convert-to pdf --outdir {0} --nologo {1}", outPutPath, officePath));
           procStartInfo.RedirectStandardOutput = true;                                         
           procStartInfo.UseShellExecute = false;
           procStartInfo.CreateNoWindow = true;
           procStartInfo.WorkingDirectory = Environment.CurrentDirectory;
 
           //开启线程
           Process process = new Process() { StartInfo = procStartInfo, };
           process.Start();
           process.WaitForExit();
 
           if (process.ExitCode != 0)
           {
               throw new LibreOfficeFailedException(process.ExitCode);
           }
       }
   }
 
   public class LibreOfficeFailedException : Exception
   {
       public LibreOfficeFailedException(int exitCode)
           base(string.Format("LibreOffice错误 {0}", exitCode))
       { }
   }


三、当将程序发布到iis时,需要将应用程序池中的高级设置


设置为true,这个问题坑了我一个星期,如果不设置,进程会一直运行,不退出。


该文章在 2020/5/18 15:36:36 编辑过

全部评论2

admin
2020年4月16日 9:18
libreoffice 将文档转换成pdf格式的命令
LibreOffice 在Linux下的命令:
/opt/libreoffice5.3/program/soffice --convert-to pdf:writer_pdf_Export /home/lims/file/1111111.xlsx --outdir /home/lims/file

/opt/libreoffice5.3/program/soffice --convert-to pdf:writer_pdf_Export --outdir /home/lims/file /home/lims/file/1111111.xlsx

LibreOffice 在Windows命令:
c:\Program Files\LibreOffice 5\program\soffice.exe --convert-to pdf:writer_pdf_Export --outdir e:\file e:\file\1111111.xlsx
admin
2020年4月16日 11:11
注意,如果要开启命令行转换文件模式,需要在系统环境变量的path中增加主程序:soffice.exe 所在目录:


新增加的path参数(一般是:C:\Program Files\LibreOffice 5\program),可能需要重启服务器才会生效。

检查参数是否生效的方法如下:
打开CMD,输入以下命令:
"C:\Program Files\LibreOffice 5\program\soffice.exe" --convert-to pdf:writer_pdf_Export --outdir C:\temp C:\temp\测试文件.xlsx
回车执行后,就会看到此目录下会增加一个自动转换出来的文件:C:\temp\测试文件.pdf


该评论在 2020/4/16 11:20:51 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved