当前位置: 首页 > news >正文

网站系统建设招标公告百度搜索引擎地址

网站系统建设招标公告,百度搜索引擎地址,上海高端网页设计公司,开发网站要注意什么问题Java Office 一、文档格式转换 文档格式转换是office操作中经常需要进行一个操作,例如将docx文档转换成pdf格式。 java在这方面有许多的操作方式,大致可以分为内部调用(无需要安装额外软件),外部调用(需…

Java Office

一、文档格式转换

文档格式转换是office操作中经常需要进行一个操作,例如将docx文档转换成pdf格式。

java在这方面有许多的操作方式,大致可以分为内部调用(无需要安装额外软件),外部调用(需要安装额外软件)。

其中,内部调用的方式虽然简单,但是会遇到一些头痛的问题,例如:文档格式错误,字体错误,内容缺少。外部调用虽然麻烦,但可以在一定程度上解决这些问题。

在技术搭配上比较推荐:jodconverter+LibreOffice

jodconverter:jodconverter是一种Java OpenDocument转换器,能够转换不同格式的文档,依赖于Apache OpenOffice或 LibreOffice。

LibreOffice:LibreOffice 是一款功能强大的办公软件,默认使用开放文档格式 (OpenDocument Format , ODF), 并支持 docx, xlsx, pptx 等其他格式。

jodconverter是支持LibreOfficeApache OpenOffice两种开源的Office软件的,但是从稳定性、转换效果、简单性,更推荐LibreOffice。

1 LibreOffice安装

LibreOffice官网:https://www.libreoffice.org/

LibreOffice下载地址:https://www.libreoffice.org/download/download-libreoffice/

LibreOffice 7.5.6:https://www.libreoffice.org/donate/dl/win-x86_64/7.5.6/zh-CN/LibreOffice_7.5.6_Win_x86-64.msi

在这里插入图片描述

其中,官方一般会发布两个版本,即最新版本和稳定版本,这里推荐稳定版本,根据自己的操作系统的版本下载对应的安装包。

其中安装流程一直点下一步就好了,记住安装路径就行。

2 项目maven依赖

<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-local</artifactId><version>4.4.6</version>
</dependency>

3 代码逻辑与实现

  1. 创建OfficeManager
  2. 创建Converter
  3. 创建输入流与输出流
  4. 文档格式转换
  5. 关闭数据流和程序
3.1 创建OfficeManager
LocalOfficeManager.Builder builder = LocalOfficeManager.builder();
// 设置本地Office地址,推荐LibreOffice
builder.officeHome("D:/Program Files/LibreOffice");
// 部署主机,本地启动
builder.hostName("127.0.0.1");
// 部署端口,可以设置多个
builder.portNumbers(9000, 9001, 9002);
// 单任务过期时间 默认:120000 2分钟
builder.taskExecutionTimeout((long) (5 * 1000 * 60));
// 任务过期时间 默认:30000 3 秒
builder.taskQueueTimeout((long) (1000 * 60 * 60));
// 可以执行的最大任务数,默认200
builder.maxTasksPerProcess(1000);
// 构建
LocalOfficeManager manager = builder.build();
// 启动
manager.start();
3.2 创建Converter
LocalConverter converter = LocalConverter.builder().officeManager(manager).build();
3.3 创建输入流与输出流
// 测试word文档转pdf
// 创建输入流
FileInputStream input = new FileInputStream("E:/tmp/word/test.docx");
// 创建输出流
FileOutputStream output = new FileOutputStream("E:/tmp/word/test.pdf");
3.4 格式转换
// 进行格式转换
converter.convert(input).as(DefaultDocumentFormatRegistry.DOCX).to(output).as(DefaultDocumentFormatRegistry.PDF).execute();
3.5 关闭流
// 关闭流
output.close();
input.close();
manager.stop();

4 可支持的文档类型

public static final @NonNull DocumentFormat PDF = byExtension("pdf");
public static final @NonNull DocumentFormat SWF = byExtension("swf");
public static final @NonNull DocumentFormat HTML = byExtension("html");
public static final @NonNull DocumentFormat XHTML = byExtension("xhtml");
public static final @NonNull DocumentFormat ODT = byExtension("odt");
public static final @NonNull DocumentFormat OTT = byExtension("ott");
public static final @NonNull DocumentFormat FODT = byExtension("fodt");
public static final @NonNull DocumentFormat SXW = byExtension("sxw");
public static final @NonNull DocumentFormat DOC = byExtension("doc");
public static final @NonNull DocumentFormat DOCX = byExtension("docx");
public static final @NonNull DocumentFormat DOTX = byExtension("dotx");
public static final @NonNull DocumentFormat RTF = byExtension("rtf");
public static final @NonNull DocumentFormat WPD = byExtension("wpd");
public static final @NonNull DocumentFormat TXT = byExtension("txt");
public static final @NonNull DocumentFormat ODS = byExtension("ods");
public static final @NonNull DocumentFormat OTS = byExtension("ots");
public static final @NonNull DocumentFormat FODS = byExtension("fods");
public static final @NonNull DocumentFormat SXC = byExtension("sxc");
public static final @NonNull DocumentFormat XLS = byExtension("xls");
public static final @NonNull DocumentFormat XLSX = byExtension("xlsx");
public static final @NonNull DocumentFormat XLTX = byExtension("xltx");
public static final @NonNull DocumentFormat CSV = byExtension("csv");
public static final @NonNull DocumentFormat TSV = byExtension("tsv");
public static final @NonNull DocumentFormat ODP = byExtension("odp");
public static final @NonNull DocumentFormat OTP = byExtension("otp");
public static final @NonNull DocumentFormat FODP = byExtension("fodp");
public static final @NonNull DocumentFormat SXI = byExtension("sxi");
public static final @NonNull DocumentFormat PPT = byExtension("ppt");
public static final @NonNull DocumentFormat PPTX = byExtension("pptx");
public static final @NonNull DocumentFormat POTX = byExtension("potx");
public static final @NonNull DocumentFormat ODG = byExtension("odg");
public static final @NonNull DocumentFormat OTG = byExtension("otg");
public static final @NonNull DocumentFormat FODG = byExtension("fodg");
public static final @NonNull DocumentFormat SVG = byExtension("svg");
public static final @NonNull DocumentFormat VSD = byExtension("vsd");
public static final @NonNull DocumentFormat VSDX = byExtension("vsdx");
public static final @NonNull DocumentFormat PNG = byExtension("png");
public static final @NonNull DocumentFormat JPEG = byExtension("jpg");
public static final @NonNull DocumentFormat TIFF = byExtension("tif");
public static final @NonNull DocumentFormat GIF = byExtension("gif");
public static final @NonNull DocumentFormat BMP = byExtension("bmp");

5 完整代码

public static void main(String[] args) throws OfficeException, IOException {// =======================构建office管理器========================LocalOfficeManager.Builder builder = LocalOfficeManager.builder();// 设置本地Office地址,推荐LibreOfficebuilder.officeHome("D:/Program Files/LibreOffice");// 部署主机,本地启动builder.hostName("127.0.0.1");// 部署端口,可以设置多个builder.portNumbers(9000, 9001, 9002);// 单任务过期时间 默认:120000 2分钟builder.taskExecutionTimeout((long) (5 * 1000 * 60));// 任务过期时间 默认:30000 3 秒builder.taskQueueTimeout((long) (1000 * 60 * 60));// 可以执行的最大任务数,默认200builder.maxTasksPerProcess(1000);// 构建LocalOfficeManager manager = builder.build();// 启动manager.start();// ======================构建文档转换器======================LocalConverter converter = LocalConverter.builder().officeManager(manager).build();// ======================实现文档转换=======================// 测试word文档转pdf// 创建输入流FileInputStream input = new FileInputStream("E:/tmp/word/test.docx");// 创建输出流FileOutputStream output = new FileOutputStream("E:/tmp/word/test.pdf");// 进行格式转换converter.convert(input).as(DefaultDocumentFormatRegistry.DOCX).to(output).as(DefaultDocumentFormatRegistry.PDF).execute();// 关闭流output.close();input.close();manager.stop();
}

效果图

在这里插入图片描述

转换后

在这里插入图片描述

二、Spring Boot集成模式

jodconverter有对于Spring Boot的集成解决方案:jodconverter-spring-boot-starter

1 项目依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-spring-boot-starter</artifactId><version>4.4.6</version>
</dependency>

2 配置文件

jodconverter:local:office-home: D:/Program Files/LibreOfficeenabled: trueport-numbers:- 8100- 8101- 8102- 8103

3 测试单例

@SpringBootTest
class SpringBootOfficeApplicationTests {@Resourceprivate LocalConverter converter;@Testvoid contextLoads() throws IOException, OfficeException {// 测试word文档转pdf// 创建输入流FileInputStream input = new FileInputStream("E:/tmp/word/test.docx");// 创建输出流FileOutputStream output = new FileOutputStream("E:/tmp/word/test.pdf");// 进行格式转换converter.convert(input).as(DefaultDocumentFormatRegistry.DOCX).to(output).as(DefaultDocumentFormatRegistry.PDF).execute();output.close();input.close();}}

三、文档模板渲染输出

在基于java开发office项目中,需要完成一些文档数据的渲染和输出工作,例如将数据库中的数据渲染到表格中,然后输出pdf。

目前比较推荐的技术选型是

首先,刻画模板将模板需要填充部分特殊标记,然后转行为xml格式。

然后,利用模板引擎将数据与模板进行渲染。

最后,使用jodconverter转换为pdf输出。

目前比较推荐的模板引擎是freemarker

以word文档渲染后输出pdf为例

1 编写模板文件

在编写模板中,更推荐使用LibreOffice Writer是安装LibreOffice后自带的客户端。

在这里插入图片描述

在保存过程中,记得保存为:fodt格式的文件。

FODT文件是一种开放文档文本(Flat OpenDocument Text)文件的扩展名。OpenDocument是一种开放的文档标准,旨在提供一种用于创建和编辑文档的自由和开放的文件格式。FODT文件通常包含文本文档的内容,可以包括文字、格式设置、图像和其他与文档相关的元素。这个文件格式的一个常见用途是与LibreOffice和Apache OpenOffice等开源办公套件一起使用。

在这里插入图片描述

2 项目设计

2.1 项目依赖
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-local</artifactId><version>4.4.6</version>
</dependency>
<dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.32</version>
</dependency>
2.2 核心逻辑

freemarker工具类

public class FreemarkerUtils {public static final Configuration CONFIGURATION;public static final String TEMPLATE_DIRECTORY = "E:/tmp/word";static {// 初始化CONFIGURATION = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);// 编码CONFIGURATION.setDefaultEncoding("UTF-8");//模板文件夹路径try {// CONFIGURATION.setClassForTemplateLoading(FreemarkerUtils.class, path);CONFIGURATION.setDirectoryForTemplateLoading(new File(TEMPLATE_DIRECTORY));} catch (IOException e) {throw new RuntimeException(e);}}public static String rendering(String templateName, Map<String, Object> params) throws IOException, TemplateException {Writer writer = new StringWriter();Template template = CONFIGURATION.getTemplate(templateName);template.process(params, writer);writer.close();return writer.toString();}
}
// ======================实现文档转换=======================
// 测试word文档转pdf
Map<String, Object> map = new HashMap<>(3);
map.put("name", "张山");
map.put("age", 18);
map.put("text", "性格开朗,热情大方,富有正义感,勤奋好学,对工作认真负责。");
String dom = FreemarkerUtils.rendering("模板1.fodt", map);
// 创建输入流
ByteArrayInputStream input = new ByteArrayInputStream(dom.getBytes());
// 创建输出流
FileOutputStream output = new FileOutputStream("E:/tmp/word/模板1.pdf");
// 进行格式转换
converter.convert(input).as(DefaultDocumentFormatRegistry.DOCX).to(output).as(DefaultDocumentFormatRegistry.PDF).execute();
2.3 效果展示

在这里插入图片描述

四、实现文档预览

如果文件格式转换和文档渲染是基于文件操作的话,文档预览是需要引出图片操作了。

文档预览需要让文档转换为pdf后再转换成图片进行查看。

实现pdf转换成图片推荐:apache.pdfbox

1 项目依赖

<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox</artifactId><version>2.0.27</version>
</dependency>
<dependency><groupId>org.apache.pdfbox</groupId><artifactId>pdfbox-tools</artifactId><version>2.0.27</version>
</dependency>

2 具体代码

// 创建字节输出流
ByteArrayOutputStream output = new ByteArrayOutputStream();
// 进行格式转换
converter.convert(input).as(DefaultDocumentFormatRegistry.DOCX).to(output).as(DefaultDocumentFormatRegistry.PDF).execute();
// 创建文档
PDDocument document = PDDocument.load(output.toByteArray());
// 读取文档
PDFRenderer pdfRenderer = new PDFRenderer(document);
// 将文档每一张图片存入
for (int i = 0; i < document.getNumberOfPages(); i++) {BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(i, 600);ImageIO.write(bufferedImage, "PNG", new File("E:\\tmp\\word\\" + i + ".png"));
}

在这里插入图片描述


文章转载自:
http://sugarcane.hjyw.cn
http://phonoreceptor.hjyw.cn
http://mixtecan.hjyw.cn
http://paleomagnetism.hjyw.cn
http://superman.hjyw.cn
http://pieria.hjyw.cn
http://rammer.hjyw.cn
http://unimpressive.hjyw.cn
http://bilberry.hjyw.cn
http://regal.hjyw.cn
http://subprogram.hjyw.cn
http://subternatural.hjyw.cn
http://frontage.hjyw.cn
http://puka.hjyw.cn
http://dragbar.hjyw.cn
http://overabundance.hjyw.cn
http://pronominal.hjyw.cn
http://greed.hjyw.cn
http://shadow.hjyw.cn
http://belemnite.hjyw.cn
http://throng.hjyw.cn
http://beachmaster.hjyw.cn
http://heir.hjyw.cn
http://shoji.hjyw.cn
http://tellurize.hjyw.cn
http://ditty.hjyw.cn
http://anchorite.hjyw.cn
http://wrapper.hjyw.cn
http://carniferous.hjyw.cn
http://celestine.hjyw.cn
http://cloy.hjyw.cn
http://mycetophagous.hjyw.cn
http://prematurely.hjyw.cn
http://diovular.hjyw.cn
http://sardonic.hjyw.cn
http://providently.hjyw.cn
http://cyprus.hjyw.cn
http://cabotine.hjyw.cn
http://viviparism.hjyw.cn
http://bps.hjyw.cn
http://allegiant.hjyw.cn
http://spin.hjyw.cn
http://tradevman.hjyw.cn
http://bidding.hjyw.cn
http://desi.hjyw.cn
http://karakorum.hjyw.cn
http://naderism.hjyw.cn
http://knot.hjyw.cn
http://acclaim.hjyw.cn
http://untenable.hjyw.cn
http://tsimmes.hjyw.cn
http://palustral.hjyw.cn
http://affectionate.hjyw.cn
http://lurking.hjyw.cn
http://phenolate.hjyw.cn
http://stanch.hjyw.cn
http://anytime.hjyw.cn
http://puli.hjyw.cn
http://benefit.hjyw.cn
http://seroot.hjyw.cn
http://industrious.hjyw.cn
http://duchy.hjyw.cn
http://stedfast.hjyw.cn
http://scouse.hjyw.cn
http://negotiatory.hjyw.cn
http://douane.hjyw.cn
http://dunno.hjyw.cn
http://durmast.hjyw.cn
http://peyote.hjyw.cn
http://lumberyard.hjyw.cn
http://genospecies.hjyw.cn
http://dolores.hjyw.cn
http://recondition.hjyw.cn
http://niveous.hjyw.cn
http://parochiaid.hjyw.cn
http://tennantite.hjyw.cn
http://nasute.hjyw.cn
http://homochrome.hjyw.cn
http://anatomic.hjyw.cn
http://quarterfinal.hjyw.cn
http://bojardo.hjyw.cn
http://gigot.hjyw.cn
http://intragenic.hjyw.cn
http://pentylenetetrazol.hjyw.cn
http://posttyphoid.hjyw.cn
http://cyanosed.hjyw.cn
http://liquorish.hjyw.cn
http://indecorum.hjyw.cn
http://autoecious.hjyw.cn
http://jaywalk.hjyw.cn
http://involuntary.hjyw.cn
http://davao.hjyw.cn
http://durrellian.hjyw.cn
http://zimbabwean.hjyw.cn
http://spruik.hjyw.cn
http://pothunter.hjyw.cn
http://migration.hjyw.cn
http://trinal.hjyw.cn
http://salespeople.hjyw.cn
http://areosystyle.hjyw.cn
http://www.dt0577.cn/news/114820.html

相关文章:

  • 树莓派可以做网站空间吗广州新闻发布
  • 鞋子网站建设策划书百度客服人工
  • 高等院校网站建设方案合肥网络公司seo
  • 免费p站推广网站入口站长之家app下载
  • 做网站用注册公司吗深圳网络营销推广专员
  • 佛山做网站优化公司企业网络推广
  • 苏州做代驾司机哪个网站好免费seo优化工具
  • 网站 香港空间百度网首页官网
  • php网站后台模板下载微信广告怎么投放
  • 河南网站优化公司宁波网站推广优化公司电话
  • 网站设计技能网站seo重庆
  • 好看的网页布局北京seo费用是多少
  • 怎样做招嫖网站百度长尾关键词挖掘工具
  • 公司网址大全baidu优化
  • 网页设计赚钱网站临沂seo推广
  • 郑州中森网站建设新乡网站优化公司推荐
  • 十大免费行情软件网站下载北京核心词优化市场
  • 电影网站建设策划书企业网站模板源码
  • 外贸开源网站谷歌浏览器下载电脑版
  • 专做特卖的网站百度浏览器
  • 淘宝联盟推广网站怎么做网站seo外链建设
  • 变更icp备案网站信息查询抖音的商业营销手段
  • Html5移动网站百度搜索引擎优化方式
  • 怎么在国税网站上做实名认证淘宝seo优化排名
  • 网站任务界面站长工具ip地址查询域名
  • 怎样在微信上做网站seo优化需要做什么
  • 外贸cms 网站seo关键词外包公司
  • php动态网站开发人民邮电出版社做网站需要哪些技术
  • php视频网站开发实战站长网站提交
  • 丽水网站建设专业的公司付费推广方式有哪些