aspose使用合集java(Word、Excel、PPT转PDF)

  • aspose使用合集java(Word、Excel、PPT转PDF
    • 文档所需jar包
    • Word转为PDF
    • 获取license
    • 简单的Word转为PDF
    • Word转为PDF的同时往Word上填充数据
  • Excel转为PDF
    • PPT转为PDF

aspose使用合集java(Word、Excel、PPT转PDF

Aspose.word for Java是一个类库,它使应用程序能够执行大量的文档处理任务。Aspose.word支持DOC、DOCX、RTF、HTML、OpenDocument、PDF、XPS、EPUB等格式。Aspose不使用Microsoft Word就可以生成、修改、转换、渲染和打印文档

文档所需jar包

百度云链接:https://pan.baidu.com/s/10DtYyLCBhB1xKKfKBTLWBg
提取码:xg5y
将以上的jar导入项目中

Word转为PDF

maven项目导入

<repositories><repository><id>AsposeJavaAPI</id><name>Aspose Java API</name><url>https://repository.aspose.com/repo/</url></repository></repositories>
<dependency><groupId>com.aspose</groupId><artifactId>aspose-words</artifactId><version>18.8</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

获取license

private static boolean getLicense() {boolean result = false;try {// 凭证String license ="<License>\n" +"  <Data>\n" +"    <Products>\n" +"      <Product>Aspose.Total for Java</Product>\n" +"      <Product>Aspose.Words for Java</Product>\n" +"    </Products>\n" +"    <EditionType>Enterprise</EditionType>\n" +"    <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +"    <LicenseExpiry>20991231</LicenseExpiry>\n" +"    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +"  </Data>\n" +"  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +"</License>";InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));License asposeLic = new License();asposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}
  • 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

简单的Word转为PDF

 public static void main(String[] args) {//获取文档流String fromPath = "";//所需要转为PDF的Word文档InputStream stream = new FileInputStream(fromPath);Document convertDoc = new Document(stream);String toPath = "";//转为PDF的路径convertDoc.save(toPath , SaveFormat.PDF);//saveFormat此处的saveFormat在一下有解释}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

saveFormat :根据所需选择转换格式

public final class SaveFormat {public static final int UNKNOWN = 0;public static final int DOC = 10;public static final int DOT = 11;public static final int DOCX = 20;public static final int DOCM = 21;public static final int DOTX = 22;public static final int DOTM = 23;public static final int FLAT_OPC = 24;public static final int FLAT_OPC_MACRO_ENABLED = 25;public static final int FLAT_OPC_TEMPLATE = 26;public static final int FLAT_OPC_TEMPLATE_MACRO_ENABLED = 27;public static final int RTF = 30;public static final int WORD_ML = 31;public static final int PDF = 40;public static final int XPS = 41;public static final int XAML_FIXED = 42;public static final int SVG = 44;public static final int HTML_FIXED = 45;public static final int OPEN_XPS = 46;public static final int PS = 47;public static final int PCL = 48;public static final int HTML = 50;public static final int MHTML = 51;public static final int EPUB = 52;public static final int ODT = 60;public static final int OTT = 61;public static final int TEXT = 70;public static final int XAML_FLOW = 71;public static final int XAML_FLOW_PACK = 72;public static final int TIFF = 100;public static final int PNG = 101;public static final int BMP = 102;public static final int EMF = 103;public static final int JPEG = 104;public static final int GIF = 105;public static final int length = 35;
}
  • 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

Word转为PDF的同时往Word上填充数据

填充数据有两种填充方式

  1. 书签填充


    2.MergeFile域填充


以下以书签为例

public static void main(String[] args) {if (!getLicense()) {return;}//获取文档流String fromPath = "C:\Users\laoyouji\Desktop\Mark.doc";//所需要转为PDF的Word文档InputStream stream = new FileInputStream(fromPath);Document convertDoc = new Document(stream);String toPath = "C:\Users\laoyouji\Desktop\Mark.pdf";//转为PDF的路径        //get("mark")此处的mark为加入Word的书签//setText("填充的数据")是你需要的赋值convertDoc.getRange().getBookmarks().get("mark").setText("填充的数据");convertDoc.save(toPath , SaveFormat.PDF);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

生成后的PDF文档填充了刚刚的文本字段

往Word中填充图片

public static void main(String[] args) throws Exception {if (!getLicense()) {return;}//获取文档流String fromPath = "C:/Users/17734/Desktop/Mark.doc";//所需要转为PDF的Word文档InputStream stream = new FileInputStream(fromPath);Document convertDoc = new Document(stream);String toPath = "C:/Users/17734/Desktop/Mark.pdf";//转为PDF的路径//get("mark")此处的mark为加入Word的书签//setText("填充的数据")是你需要的赋值try {DocumentBuilder builder = new DocumentBuilder(convertDoc);String picturePath = "C:\\Users\\17734\\Desktop\\阿泽.jpg";//所需插入图片的路径double width = 100;//插入图片的宽度double height = 100;//插入图片的高度Shape shape = builder.insertImage(picturePath, width, height);shape.setWrapType(WrapType.NONE);shape.setLeft(100);//此处的100是离文档左边的距离shape.setTop(100);//此处的100是离文档顶部的距离} catch (Exception e) {System.out.println("------图片不存在------");}convertDoc.save(toPath , SaveFormat.PDF);}
  • 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

效果图

往Word中加入页码

public static void main(String[] args) throws Exception {if (!getLicense()) {return;}//获取文档流String fromPath = "C:/Users/17734/Desktop/Mark.doc";//所需要转为PDF的Word文档InputStream stream = new FileInputStream(fromPath);Document convertDoc = new Document(stream);String toPath = "C:/Users/17734/Desktop/Mark.pdf";//转为PDF的路径pagination(convertDoc,ParagraphAlignment.RIGHT);convertDoc.save(toPath , SaveFormat.PDF);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
 //添加页码private static void pagination(Document convertDoc, Integer position) throws Exception {/*添加页码*/HeaderFooter footer = new HeaderFooter(convertDoc, HeaderFooterType.FOOTER_PRIMARY);convertDoc.getFirstSection().getHeadersFooters().add(footer);//页脚段落Paragraph footerpara = new Paragraph(convertDoc);//ParagraphAlignment.RIGHTfooterpara.getParagraphFormat().setAlignment(position);Run footerparaRun = new Run(convertDoc, "共 ");footerparaRun.getFont().setName("方正仿宋简体");footerparaRun.getFont().setSize(12.0);footerpara.appendChild(footerparaRun);footerpara.appendField(FieldType.FIELD_NUM_PAGES, false);//总页码footerparaRun = new Run(convertDoc, " 页,第 ");footerparaRun.getFont().setName("方正仿宋简体");footerparaRun.getFont().setSize(12.0);footerpara.appendChild(footerparaRun);footerpara.appendField(FieldType.FIELD_PAGE, false);//当前页码footerparaRun = new Run(convertDoc, " 页");footerparaRun.getFont().setName("方正仿宋简体");footerparaRun.getFont().setSize(12.0);footerpara.appendChild(footerparaRun);footer.appendChild(footerpara);/*添加页码*/}
  • 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

效果图

Excel转为PDF

导入依赖

<!-- https://mvnrepository.com/artifact/com.aspose/aspose-cells --><dependency><groupId>com.aspose</groupId><artifactId>aspose-cells</artifactId><version>8.5.2</version></dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

获取license

 private static boolean getLicense3() {boolean result = false;try {// 凭证String license ="<License>\n" +"  <Data>\n" +"    <Products>\n" +"      <Product>Aspose.Total for Java</Product>\n" +"      <Product>Aspose.Words for Java</Product>\n" +"    </Products>\n" +"    <EditionType>Enterprise</EditionType>\n" +"    <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +"    <LicenseExpiry>20991231</LicenseExpiry>\n" +"    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +"  </Data>\n" +"  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +"</License>";InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));com.aspose.cells.License asposeLic = new com.aspose.cells.License();asposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}
  • 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

转换方法

private static void excel2pdf(String from,String to) {if (!getLicense3()) { // 验证License 若不验证则转化出的pdf文档会有水印产生return;}try {File pdfFile = new File(to);// 输出路径Workbook wb = new Workbook(from);// 原始excel路径FileOutputStream fileOS = new FileOutputStream(pdfFile);wb.save(fileOS, com.aspose.cells.SaveFormat.PDF);fileOS.close();} catch (Exception e) {e.printStackTrace();}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

PPT转为PDF

获取license

private static boolean getLicense2() {boolean result = false;try {String license ="<License>\n" +"  <Data>\n" +"    <Products>\n" +"      <Product>Aspose.Total for Java</Product>\n" +"      <Product>Aspose.Words for Java</Product>\n" +"    </Products>\n" +"    <EditionType>Enterprise</EditionType>\n" +"    <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +"    <LicenseExpiry>20991231</LicenseExpiry>\n" +"    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +"  </Data>\n" +"  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +"</License>";InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));com.aspose.slides.License aposeLic = new com.aspose.slides.License();aposeLic.setLicense(is);result = true;} catch (Exception e) {e.printStackTrace();}return result;}
  • 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

导入jar包

<!-- https://mvnrepository.com/artifact/com.aspose/aspose-slides --><dependency><groupId>com.aspose</groupId><artifactId>aspose.slides</artifactId><version>15.9.0</version></dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

执行方法

 private static void ppt2pdf(String from, String to) {// 验证Licenseif (!getLicense2()) {return;}try {File file = new File(to);// 输出pdf路径Presentation pres = new Presentation(from);//输入ppt路径FileOutputStream fileOS = new FileOutputStream(file);//IFontsManager fontsManager = pres.getFontsManager();pres.save(fileOS, SaveFormat.Pdf);fileOS.close();} catch (Exception e) {e.printStackTrace();}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

总结:
aspose 自己项目中使用到的一些功能,如果有错误,望指正!!!
生活很难,请你开心一点~
来自:老友j

转自:https://blog.csdn.net/weixin_44794260/article/details/106586011

aspose使用合集java(Word、Excel、PPT转PDF)相关推荐

  1. java word,excel,ppt转pdf

    准备工作 1.下载 jacob.jar  链接:https://pan.baidu.com/s/1TWIGyX9A3xQ6AG9Y3mVlVg  提取码:abcd 2.下载安装wps WPS Offi ...

  2. php word excel转pdf文件怎么打开,php office文件(word/excel/ppt)转pdf文件,pptpdf

    php office文件(word/excel/ppt)转pdf文件,pptpdf 把代码放到了github上,点击进入 前阶段有个项目用到了线上预览功能, 关于预览office文件实现核心就是,把o ...

  3. php word/excel/ppt 转pdf

    转载至:https://blog.csdn.net/sangjinchao/article/details/78053545 把代码放到了github上,点击进入 前阶段有个项目用到了线上预览功能, ...

  4. java word excel ppt 图片转pdf

    第一步将jar导入mvn库 下载地址 0积分 https://download.csdn.net/download/qq_35908944/18549670 mvn install:install-f ...

  5. Java准确获取Word/Excel/PPT/PDF的页数(附Word页数读不准的处理办法)

    Java准确获取Word/Excel/PPT/PDF的页数(附Word页数读不准的处理办法) 1.需求背景 2.环境准备工作 2.1 JACOB介绍及安装 2.2 Microsoft Office W ...

  6. Java使用Openoffice将word、ppt转换为PDF

    最近项目中要实现WORD的文件预览功能,我们可以通过将WORD转换成PDF或者HTML,然后通过浏览器预览. OpenOffice OpenOffice.org 是一套跨平台的办公室软件套件,能在 W ...

  7. vba 保存word里面的图片_笔记7 【office精华课】一套课程学会Word+Excel+PPT(一)【Word】(2020年第37周 周五)...

    [office精华课] <一套课程学会Word+Excel+PPT> 课程目录:(总时长合计:28:56:25) =================================== [ ...

  8. Python办公自动化(八)|使用Python转换PDF,Word/Excel/PPT/md/HTML都能转

    Word转PDF Word转PDF应该是最常见的需求了,毕竟使用PDF格式可以更方便展示文档,虽然在Word中可以直接导出为PDF格式,但是使用Python可以批量转换,更加高效. 目前在Python ...

  9. Win10安装了Office右键没有新建Word,excel,PPT等选项解决方法

    大多用户在新建一个Office文档的时候都会快速在桌面上右键新建一个Word或Excel.PPT等,而不是先打开软件去新建,但是用户反馈在安装了office之后,右键新建中并没有Word,excel, ...

最新文章

  1. Python--matplotlib 绘图可视化练手--折线图/条形图
  2. linux中sh基本语法
  3. Python 列表List的定义及操作
  4. SSRF,以weblogic为案例
  5. phpcmsV9 邮箱注册:邮箱验证(不改代码、含演示截图) - 配置篇
  6. 带通滤波中零相位和最小相位_相位器在Perl 6中的工作方式
  7. 并查集——关押罪犯(洛谷 P1525)
  8. 【华为云技术分享】【开发记录】Linux服务器维护常用命令(二)
  9. 送给大学生新生的建议
  10. CAN FD实战之CAN FD应用领域及CAN迁移至CAN FD的策略
  11. 梦幻西游三维版获取服务器信息,梦幻西游三维版服务器等级提升
  12. 计算机和影视结合专业,计算机专业专业建设总结与典型案例2.5微电影拍摄与后期制作(影视拍摄与后期制作技术)课....
  13. 利用区块链技术解决科研问题的前景
  14. 7-4 计算职工工资(15分)
  15. 互联网医院源码|互联网医院软件体现智慧医疗的优势
  16. 命令提示符中的一些基本命令
  17. CentOS7系统安装
  18. Python数据结构之算法引入
  19. 加拿大海运专线要多少天?有哪些物流方式?
  20. 一个极简单的基于STM32的精确PWM脉冲计数

热门文章

  1. 简单c语言练习题(第三期)
  2. 开题报告中拟解决的主要问题怎么写?
  3. 【毕业设计】深度学习+python+opencv实现动物识别 - 图像识别
  4. 项目管理(项目管理中的重要角色项目经理)
  5. Postgresql源码(5)缓冲区管理
  6. 粤嵌开发板之手机WIFI摄像头
  7. 沟通修炼 I型沟通->U型沟通
  8. Hive实现32位UUID
  9. Zemax操作--4(公差问题)
  10. PHPWord 导出生成word