tnblog
首页
视频
资源
登录

基于open office 把各种类型转为pdf在线预览

6417人阅读 2019/7/3 9:27 总访问:51114 评论:1 收藏:0 手机
分类: .net

                 

  1. //需要在服务器安装open office,引用cli_basetypes,cli_cppuhelper,cli_oootypes,cli_ure,cli_uretypes
  2. public bool ConvertToPdf(string inputFile, string outputFile)
  3.         {
  4.             if (ConvertExtensionToFilterType(Path.GetExtension(inputFile)) == null) {
  5.                 throw new InvalidProgramException("Unknown file  = " + inputFile);
  6.             }
  7.             StartOpenOffice();
  8.             //Get a ComponentContext
  9.             var xLocalContext = Bootstrap.bootstrap();
  10.            
  11.             //Get MultiServiceFactory
  12.             var xRemoteFactory = (XMultiServiceFactory) xLocalContext.getServiceManager();
  13.             //Get a CompontLoader
  14.             var aLoader = (XComponentLoader)xRemoteFactory.createInstance("com.sun.star.frame.Desktop");
  15.             //var aa = (XComponentLoader)xRemoteFactory.createInstance("com.sun.star.style.CharacterProperties");
  16.             //Load the sourcefile 
  17.            
  18.             XComponent xComponent = null;
  19.             try
  20.             {
  21.                 xComponent = InitDocument(aLoader, PathConverter(inputFile), "_blank");
  22.                 
  23.                 //Wait for loading
  24.                 while (xComponent == null)
  25.                 {
  26.                     Thread.Sleep(1000);
  27.                 }
  28.                 // save/export the document
  29.                
  30.                return SaveDocument(xComponent, inputFile, PathConverter(outputFile));
  31.             }
  32.             finally
  33.             {
  34.                 if (xComponent != null) xComponent.dispose();
  35.             }
  36.         }
  37.         private static void StartOpenOffice()
  38.         {
  39.             var ps = Process.GetProcessesByName("soffice.exe");
  40.             if (ps.Length != 0)
  41.                 throw new InvalidProgramException("soffice not installed!");
  42.             if (ps.Length > 0)
  43.                 return;
  44.             var p = new Process
  45.             {
  46.                 StartInfo =
  47.                 {
  48.                     Arguments = "-headless -nofirststartwizard",
  49.                     FileName = "soffice.exe",
  50.                     CreateNoWindow = true
  51.                 }
  52.             };
  53.             var result = p.Start();
  54.             if (result == false)
  55.                 throw new InvalidProgramException("soffice failed to start.");
  56.         }
  57.         private static XComponent InitDocument(XComponentLoader aLoader, string file, string target)
  58.         {
  59.             var openProps = new PropertyValue[1];
  60.             openProps[0] = new PropertyValue { Name = "Hidden", Value = new Any(true) };
  61.             var xComponent = aLoader.loadComponentFromURL( file, target, 0, openProps);
  62.             
  63.             return xComponent;
  64.         }
  65.         private static bool SaveDocument(XComponent xComponent, string sourceFile, string destinationFile)
  66.         {
  67.             try {
  68.                 var propertyValues = new PropertyValue[2];
  69.                 
  70.                 // Setting the flag for overwriting
  71.                 propertyValues[1] = new PropertyValue { Name = "Overwrite", Value = new Any(true) };
  72.                 //// Setting the filter name
  73.                 propertyValues[0] = new PropertyValue
  74.                 {
  75.                     Name = "FilterName",
  76.                     Value = new Any(ConvertExtensionToFilterType(Path.GetExtension(sourceFile)))
  77.                 };
  78.                
  79.                 //propertyValues[2] = new PropertyValue
  80.                 //{ 
  81.                 //    Name = "CharFontStyleName",
  82.                 //    Value = new Any("华文琥珀")
  83.                 //};
  84.                 ((XStorable)xComponent).storeToURL(destinationFile, propertyValues);
  85.                 return true;
  86.             } catch {
  87.                 return false;
  88.             }
  89.            
  90.         }
  91.         private static string PathConverter(string file)
  92.         {
  93.             if (string.IsNullOrEmpty(file))
  94.                 throw new NullReferenceException("Null or empty path passed to OpenOffice");
  95.             return String.Format("file:///{0}", file.Replace(@"\""/"));
  96.         }
  97.         public static string ConvertExtensionToFilterType(string extension)
  98.         {
  99.             switch (extension)
  100.             {
  101.                 case ".doc":
  102.                 case ".docx":
  103.                case ".txt":
  104.                 case ".rtf":
  105.                 case ".html":
  106.                 case ".htm":
  107.                 case ".xml":
  108.                 case ".odt":
  109.                 case ".wps":
  110.                 case ".wpd":
  111.                     return "writer_pdf_Export";
  112.                 case ".xls":
  113.                 case ".xlsb":
  114.                 case ".xlsx":
  115.                 case ".ods":
  116.                     return "calc_pdf_Export";
  117.                 case ".ppt":
  118.                 case ".pptx":
  119.                 case ".odp":
  120.                     return "impress_pdf_Export";
  121.                 default:
  122.                     return null;
  123.             }
  124.         }

 

评价

剑轩

2019/7/3 17:36:20

都是些高大上的问题!

基于office 把wordwordxpptpptxxlsxlsx转为pdf在线预览

//,要在服务器安装office,需要引用usingMicrosoft.Office.Interop.Word;usingMicrosoft.Office.Interop.PowerPoint;usingMi...

基于接口的Sort排序方法原理

集合提供了Sort,我们可以实现IComparer接口,来实现自定义的对象排序方法:例如:根据年龄来排序publicclassMySort:ICompa...

基于angular的HttpClient封装_HttpClient

基于angular的HttpClient封装_HttpClient,直接复制源码即可用包含常用的get、post、patch、delete和put请求;import{HttpC...

Elasticsearch 基于词项和基于全文的搜索

Elasticsearch 基于词项和基于全文的搜索[TOC] 基于 Term 的查询Term 的重要性Term 是表达语意的最小单位。搜索和利用语言...

Centos7安装k8s(基于kubeadm)

Centos7安装k8s(基于kubeadm)[TOC] 下载相关包 链接:https://pan.baidu.com/s/1H9MuZsf28f_3uyVHBUS8jQ提取码:w9tv ...

.net core调用基于GPT-3.5的ChatGPT接口

基于GPT-3.5的接口OpenAI 已经开放和官网版本的ChatGPT相同的模型 gpt-3.5-turbo GPT-3.5和3.0调用方式几乎一样,修改一下...

Pytorch 基于经典网络架构训练图像分类模型

Pytorch 基于经典网络架构训练图像分类模型[TOC] 数据预处理部分:数据增强:torchvision中transforms模块自带功能,比较...

基于Ollama创建Gemma定制化AI模型

基于Ollama创建Gemma定制化AI模型[TOC] 什么是Gemma?Gemma模型使用了和Gemini同源的技术,总共有20亿参数和70亿参数两种...

Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览office文档

Aspose.Total是Aspose公司旗下的最全的一套office文档管理方案,通过它,我们可以有计划地操纵一些商业中最流行的文件格式...

Iframe展示pdf

<iframeclass="scroll-style-8"id="filesldsptPDF"scrolling="yes"src="~/Scripts/...

使用Aspose.Words进行word转换pdfword转换htmldoc转换成docxword转换markdown

要先下载Aspose.Words依赖然后转换各种类型一句代码搞定:varwrdf=newAspose.Words.Document(dir+"template.doc"...

pdf转htmlpdf转word等

方法一:使用Aspose.Words转这种方法效果不是太好,毕竟Aspose.Words,主要是做word相关转换的。比如word转pdf,word转html...

html完美转换成pdf doctron

doctron的基础使用 看文档https://github.com/lampnick/doctron/blob/master/README_ZH.md

word在线预览pdf在线预览,excel在线预览,word转图片,pdf图片。预览组件kkFileView。vue中使用Base64

在线文件预览组件推荐kkFileViewdocker下的安装拉取镜像 docker pull keking/kkfileview 运行镜像 docker run -it -p 80...

vue生成pdf下载。vue把当前页面生成pdf下载。vue根据页面dom id生成pdf下载。vue pdf导出

思路: 通过 html2canvas 将 HTML 页面转换成图片,然后再通过 jspdf 将图片的 base64 生成为 pdf 文件。[TOC]下载相关依赖...
老子许灵灵,写字第一名
排名
45
文章
7
粉丝
3
评论
2
基于open office 把各种类型转为pdf在线预览
剑轩 : 都是些高大上的问题!
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术