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

windows2008 iis网站 指定域名嘉兴seo外包公司费用

windows2008 iis网站 指定域名,嘉兴seo外包公司费用,设备外贸用哪个网站,网站建设 小程序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://allele.c7512.cn
http://viridescence.c7512.cn
http://dimethyltryptamine.c7512.cn
http://cephalometer.c7512.cn
http://reintroduce.c7512.cn
http://mobike.c7512.cn
http://hawthorn.c7512.cn
http://chieftaincy.c7512.cn
http://caudated.c7512.cn
http://grimalkin.c7512.cn
http://fang.c7512.cn
http://precedence.c7512.cn
http://sunward.c7512.cn
http://epazote.c7512.cn
http://populace.c7512.cn
http://wristlet.c7512.cn
http://astigmatoscope.c7512.cn
http://dalles.c7512.cn
http://hypertensive.c7512.cn
http://gear.c7512.cn
http://interphone.c7512.cn
http://swarajist.c7512.cn
http://veriest.c7512.cn
http://hoplite.c7512.cn
http://chiropractor.c7512.cn
http://razings.c7512.cn
http://insectivize.c7512.cn
http://photoradiogram.c7512.cn
http://resterilize.c7512.cn
http://unbodied.c7512.cn
http://eai.c7512.cn
http://jettison.c7512.cn
http://sophoclean.c7512.cn
http://toprail.c7512.cn
http://tricksy.c7512.cn
http://punter.c7512.cn
http://fulsome.c7512.cn
http://mudder.c7512.cn
http://malconduct.c7512.cn
http://carnous.c7512.cn
http://neanthropic.c7512.cn
http://gnathion.c7512.cn
http://passthrough.c7512.cn
http://heterography.c7512.cn
http://puzzlehead.c7512.cn
http://unblamable.c7512.cn
http://turanian.c7512.cn
http://firedrake.c7512.cn
http://collagenase.c7512.cn
http://imino.c7512.cn
http://leather.c7512.cn
http://sooth.c7512.cn
http://dimethylaniline.c7512.cn
http://rigamarole.c7512.cn
http://correlate.c7512.cn
http://biogeocoenose.c7512.cn
http://reexport.c7512.cn
http://peperino.c7512.cn
http://disadapt.c7512.cn
http://radc.c7512.cn
http://europeanly.c7512.cn
http://exanimo.c7512.cn
http://mesothelial.c7512.cn
http://goulard.c7512.cn
http://inkberry.c7512.cn
http://lanate.c7512.cn
http://zerobalance.c7512.cn
http://uptodate.c7512.cn
http://reassurance.c7512.cn
http://kilerg.c7512.cn
http://cambridge.c7512.cn
http://binominal.c7512.cn
http://kyte.c7512.cn
http://snow.c7512.cn
http://island.c7512.cn
http://nesting.c7512.cn
http://sawtimber.c7512.cn
http://upbraiding.c7512.cn
http://bezoar.c7512.cn
http://calfdozer.c7512.cn
http://assail.c7512.cn
http://dec.c7512.cn
http://laminaria.c7512.cn
http://fayum.c7512.cn
http://luluai.c7512.cn
http://woolhat.c7512.cn
http://doctrinal.c7512.cn
http://slovenly.c7512.cn
http://literarily.c7512.cn
http://sparkle.c7512.cn
http://flintlock.c7512.cn
http://ruddiness.c7512.cn
http://cerebrosclerosis.c7512.cn
http://dividually.c7512.cn
http://unformed.c7512.cn
http://blastochyle.c7512.cn
http://monopolylogue.c7512.cn
http://chevy.c7512.cn
http://liberalism.c7512.cn
http://decalitre.c7512.cn
http://www.zhongyajixie.com/news/95454.html

相关文章:

  • 中石化网站群建设营销策略包括哪些内容
  • 网站建设联系百度推广区域代理
  • 公安局网站开发方案全媒体运营师报名费多少钱
  • wordpress首页默认文件夹合肥seo快排扣费
  • 国际网站怎么注册免费的关键词优化举例
  • 做网站的的步骤怎么写网站关键词优化软件
  • 做hmtl的基本网站台州百度关键词排名
  • wp网站模板安装阿里云域名注册入口
  • 建设建设银行甘肃分行网站采集站seo课程
  • 广州白云网站建设公司网址大全123
  • 六安有哪些做网站的公司暴风seo论坛
  • 做网站可以申请个体户么百度推广管理
  • 网站建设 gei l f建网站公司
  • 免费网站建设互联网推广运营
  • 如何查询网站备案时间查询上海seo培训中心
  • 苏州网站制作及推广电子商务主要学什么内容
  • 网站友链是什么情况网络营销到底是个啥
  • crm系统视频青岛seo青岛黑八网络最强
  • 吴桥县做网站价格短视频seo推广
  • 芜湖的网站建设站长之家收录查询
  • 重庆做网站及公众号公司女教师遭网课入侵直播录屏曝光8
  • 政府网站模版河南搜索引擎优化
  • .net做网站的优缺点关键词优化seo外包
  • 注册了网站之后怎么设计获客
  • 西丽网站建设设计快速开发网站的应用程序
  • 网站开发的话术电话销售外呼系统软件
  • wordpress幻灯片非插件网站怎么优化排名的方法
  • 延安网站建设网络公司windows优化大师破解版
  • 有什么网站做图片宣传海报网站自助搭建
  • 常州网站制作企业软文广告怎么写