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

java 同时被 2 个专栏收录
5 篇文章 1 订阅
1 篇文章 0 订阅

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>

获取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;
    }

简单的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在一下有解释
    }

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;
}

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

填充数据有两种填充方式

  1. 书签填充
    在这里插入图片描述
    此时书签就会填充在鼠标光点所在位置
    2.MergeFile域填充
    在这里插入图片描述
    选中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);
    }

生成后的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);
    }

效果图
在这里插入图片描述
往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);
    }
 //添加页码
    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.RIGHT
        footerpara.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);
        /*添加页码*/
    }

效果图
在这里插入图片描述

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>

获取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;
    }

转换方法

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();
        }
    }

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;
    }

导入jar包

<!-- https://mvnrepository.com/artifact/com.aspose/aspose-slides -->
		<dependency>
			<groupId>com.aspose</groupId>
			<artifactId>aspose.slides</artifactId>
			<version>15.9.0</version>
		</dependency>

执行方法

 private static void ppt2pdf(String from, String to) {
        // 验证License
        if (!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();
        }
    }

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

  • 2
    点赞
  • 2
    评论
  • 11
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值