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. 书签填充
    aspose使用合集java(Word、Excel、PPT转PDF)-编程知识网
    aspose使用合集java(Word、Excel、PPT转PDF)-编程知识网
    2.MergeFile域填充
    aspose使用合集java(Word、Excel、PPT转PDF)-编程知识网
    aspose使用合集java(Word、Excel、PPT转PDF)-编程知识网
    aspose使用合集java(Word、Excel、PPT转PDF)-编程知识网

以下以书签为例

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文档填充了刚刚的文本字段
aspose使用合集java(Word、Excel、PPT转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

效果图
aspose使用合集java(Word、Excel、PPT转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的路径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

效果图
aspose使用合集java(Word、Excel、PPT转PDF)-编程知识网

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