添加依赖

com.aspose.words

aspose-words

20.10

com.aspose.cells

aspose-cells

8.5.2

代码实现

import com.aspose.cells.PdfSaveOptions;

import com.aspose.cells.Workbook;

import com.aspose.words.*;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.io.*;

/**

* Aspose工具类

*

* @author stsinghua

*/

public enum AsposeUtil {

INSTANCE;

private static final String DEF_CELLS = "cells";

private static final String DEF_WORDS = "words";

private static final Logger logger = LoggerFactory.getLogger(AsposeUtil.class);

/**

* 获取license

*/

public boolean getLicense(String type) {

boolean result = false;

try {

// 凭证

StringBuilder licenseStr = new StringBuilder();

licenseStr.append("\n");

licenseStr.append("\n");

licenseStr.append("\n");

licenseStr.append("Aspose.Total for Java\n");

licenseStr.append("Aspose.Words for Java\n");

licenseStr.append("\n");

licenseStr.append("Enterprise\n");

licenseStr.append("20991231\n");

licenseStr.append("20991231\n");

licenseStr.append("8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7\n");

licenseStr.append("\n");

licenseStr.append("sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=\n");

licenseStr.append("");

InputStream inputStream = new ByteArrayInputStream(licenseStr.toString().getBytes("UTF-8"));

if (DEF_WORDS.equalsIgnoreCase(type)) {

com.aspose.words.License asposeLic = new com.aspose.words.License();

asposeLic.setLicense(inputStream);

} else {

com.aspose.cells.License asposeLic = new com.aspose.cells.License();

asposeLic.setLicense(inputStream);

}

result = true;

} catch (Exception e) {

logger.error("set aspose license error,{}", e);

}

return result;

}

/**

* word 转 pdf.

*/

public void wordToPdf(String docFilePath, String pdfFilePath) {

wordToOther(docFilePath, pdfFilePath, com.aspose.words.SaveFormat.PDF);

}

/**

* xml 转 word.

*/

public void xmlToWord(String xmlFilePath, String docFilePath) {

wordToOther(xmlFilePath, docFilePath, com.aspose.words.SaveFormat.DOC);

}

/**

* docx 转 word.

*/

public void docxToWord(String docxFilePath, String docFilePath) {

wordToOther(docxFilePath, docFilePath, com.aspose.words.SaveFormat.DOC);

}

/**

* word 转 other.

*/

public void wordToOther(String filePath, String toFilePath, int type) {

if (!getLicense(DEF_WORDS)) {

return;

}

try {

Document document = new Document(filePath);

wordToOther(document, toFilePath, type);

} catch (Exception e) {

logger.error("wordToOther error,{}", e);

}

}

/**

* word 转 other.

*/

public void wordToOther(Document document, String docFilePath, int type) {

FileOutputStream fos = null;

if (!getLicense(DEF_WORDS)) {

return;

}

try {

document.acceptAllRevisions();

fos = new FileOutputStream(new File(docFilePath));

document.save(fos, type);

} catch (Exception e) {

logger.error("wordToWord error,{}", e);

} finally {

try {

if (fos != null) {

fos.close();

}

} catch (IOException e) {

logger.error("fos error,{}", e);

}

}

}

/**

* excel 转 pdf.

*/

public void excelToPdf(String excelFilePath, String pdfFilePath) {

FileOutputStream fos = null;

if (!getLicense(DEF_CELLS)) {

return;

}

try {

// pdf输出路径

File pdfFile = new File(pdfFilePath);

// excel路径

Workbook wb = new Workbook(excelFilePath);

wb.acceptAllRevisions();

PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();

pdfSaveOptions.setOnePagePerSheet(true);

fos = new FileOutputStream(pdfFile);

wb.save(fos, com.aspose.cells.SaveFormat.PDF);

} catch (Exception e) {

logger.error("excelToPdf error,{}", e);

} finally {

try {

if (fos != null) {

fos.close();

}

} catch (IOException e) {

logger.error("fos error,{}", e);

}

}

}

/**

* 判断书签是否存在

*/

public boolean isExistBookmark(Document dc, String bookmarkStr) {

Bookmark bookmark = dc.getRange().getBookmarks().get(bookmarkStr);

if (null != bookmark && bookmarkStr.equalsIgnoreCase(bookmark.getName())) {

return true;

}

return false;

}

/**

* 在书签处插入文本

*/

public void setTextAtBookMark(Document document, String bookMark, String text) throws Exception {

if (isExistBookmark(document, bookMark)) {

try {

document.getRange().getBookmarks().get(bookMark).setText(text);

} catch (Exception e) {

logger.error("setTextAtBookMark,书签{}赋值异常, error,{}", bookMark, e);

}

}

}

/**

* 在书签处插入图片

*/

public void insertPictureAtBookMark(Document document, String bookMark, String pictureFilePath) throws FileNotFoundException {

if (isExistBookmark(document, bookMark)) {

try {

document.getRange().getBookmarks().get(bookMark).setText("");

DocumentBuilder documentBuilder = new DocumentBuilder(document);

documentBuilder.moveToBookmark(bookMark);

documentBuilder.insertImage(pictureFilePath);

} catch (Exception e) {

logger.error("insertPictureAtBookMark,书签{}赋值异常, error,{}", bookMark, e);

}

}

}

/**

* 移除全部水印

*/

private void removeWatermark(Document doc) throws Exception {

for (Section sect : doc.getSections()) {

removeWatermarkFromHeader(sect, HeaderFooterType.HEADER_PRIMARY);

removeWatermarkFromHeader(sect, HeaderFooterType.HEADER_FIRST);

removeWatermarkFromHeader(sect, HeaderFooterType.HEADER_EVEN);

}

}

/**

* 移除指定Section的水印

*/

private void removeWatermarkFromHeader(Section sect, int headerType) throws Exception {

HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);

if (header != null) {

header.removeAllChildren();

}

}

}

附录

详情请参考官网:

aspose转pdf横版_doc转pdf之aspose相关推荐

  1. aspose转pdf横版_Aspose实现文件转PDF在线预览及水印功能

    文件转换及在线预览 因为项目是做OA这一块,有很多附件需要实现在线预览附件,在网上也看了很多相关的资料.主要实现方式就是 (openoffice+swftools+flexpaper)和(aspose ...

  2. aspose转pdf横版_Aspose实现Office转PDF (ASP.NET) 别人的

    0.添加Aspose的DLL 1.可以直接去官网下载,不过默认是带水印的,如需去除水印可以购买 2.当然也可以在国内的一些下载站下载 3.将Aspose.Cells.dll.Aspose.Words. ...

  3. python win32转pdf 横版_Python调用Win32com实现Office批量转PDF

    Python调用Win32com实现Office批量转PDF 需求 一直以来有将诸如Word之类的Office文档转为PDF的需求,此前的方法是挨个打开文档,手动另存为PDF,此方法费时费力,尤其在电 ...

  4. python win32转pdf 横版_讲真,别再用win32com包来实现Word文档转PDF了

    这几天有件大爽事. 之前一直放在Django原生的服务器的网站,终于放到Apache上了.并不是配置多难,也不是我懒,问题出在了用win32com实现word转pdf,我不能使用这个插件读取word, ...

  5. aspose转pdf横版_aspose实现Office转Pdf

    标签: aspose实现Office转Pdf关键代码: jar包: aspose-words-14.6.0.jar aspose-cells-10.8.jar aspose.slides-14.4.0 ...

  6. aspose转pdf横版_Aspose系列实现docx转PDF,PPT转PDF,EXCEL转PDF

    没有什么营养,就是调用一下这个组件.其实一开始用的是Microsoft.Office.Interop.Excel;Microsoft.Office.Interop.Word 但是在服务器要注意,服务器 ...

  7. python win32转pdf 横版_使用Python3将word文档和pdf电子书进行格式互转(兼容Windows/Linux)...

    一些重要文档格式之间的互转在目前显得尤为重要,pdf作为通用格式在现在各个平台上兼容性是最好的,所以写python脚本将这些word文档批量转换pdf是最好的解决方案. 由于windows系统对于wo ...

  8. python win32转pdf 横版_python实现word转pdf

    验证过ppt也可以合并成pdf文件,确实很高效,什么pdf转换器的再也不用了~ 需要下载模块pywin32,程序中导入模块为win32com. # -*- coding:utf-8 -*- impor ...

  9. python win32转pdf 横版_用Python将PPT转换PDF

    # 1). 导入需要的模块(打开应用程序的模块) import win32com.client import os def ppt2pdf(filename, output_filename): &q ...

最新文章

  1. 张晓帆:一个决定将47万奖金全部投入科研的博士生
  2. F - 等式(1/x + 1/y = 1/n)
  3. struts1跳入指定方法
  4. 一个富翁试图与陌生人做一笔生意用python_实验报告1 - 图文 -
  5. Xor Transformation
  6. 漫步数学分析一——实数轴
  7. Linux计划任务之_Crontab
  8. 吴恩达神经网络和深度学习-学习笔记-7-正则化regularization方法
  9. 3分钟tips:什么是特征向量?什么是特征值?
  10. 抖音直播电商带货项目商业运营计划书短视频创业规划方案
  11. 系统服务图形化安装卸载工具SRVINSTW汉化版
  12. 我安装archlinux的过程总结
  13. pycharm中python的默认安装路径_PyCharm下载和安装详细步骤
  14. 基于SSM的医院管理系统
  15. 腾讯云服务器带宽按使用流量计费规则(通俗易懂)
  16. Spring事务管理中异常回滚知识点总结
  17. DMA方式、中断方式的传输速率比较
  18. 隐藏删除 gitbook Published with GitBook 的方法
  19. 自媒体人如何搜集写作素材?建立自己的素材库
  20. 【游戏杂记】.xp3文件的解包

热门文章

  1. 已知一个IP地址计算子网掩码(C语言)
  2. 【Electron Playground 系列】窗口篇
  3. 办公本推荐计算机专业,适合设计师的轻薄办公笔记本电脑,我只推荐这几款
  4. 关于网上购物流程和如何维护网购权益
  5. 如何在Oracle官网上找到以前版本的JDK?
  6. 封神英雄榜java_封神英雄榜云霄仙子
  7. 打开计算机窗口抖动是什么原因,教你电脑屏幕抖动是什么原因及如何解决
  8. 折叠双屏+双模5G,摩托罗拉Razr 5G能否复刻刀锋经典?
  9. 重生之我是javaの神之修炼练气功法之javase基础语法
  10. 各种VBA excel 命令、属性、方法