首页 > 编程语言 >aspose word与pdf互转

aspose word与pdf互转

时间:2023-06-05 22:35:30浏览次数:50  
标签:outputStream word String import System File new 互转 aspose

aspose word与pdf互转

package com.example.core.mydemo.aspose;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class WordToPdf {
    public static File docToPdf(String docPath, String pdfPath) {
        System.out.println("WORD转化PDF开始>>>>");  //转化用时
        File pdfFile = new File(pdfPath);
        try {
            long old = System.currentTimeMillis();
            String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
            ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
            License license = new License();
            license.setLicense(is);
            Document document = new Document(docPath);
            FileOutputStream outputStream = new FileOutputStream(pdfFile);
            document.save(outputStream, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            outputStream.close();
            is.close();
            System.out.println("WORD转化PDF共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
        } catch (Exception e) {
            System.out.println("转化失败");
            e.printStackTrace();
        }
        return pdfFile;
    }

    public static File docToPdf(InputStream docPathInputStream, String pdfPath) {
        File pdfFile = new File(pdfPath);
        try {
            long old = System.currentTimeMillis();
            String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
            ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
            License license = new License();
            license.setLicense(is);
            Document document = new Document(docPathInputStream);
            FileOutputStream outputStream = new FileOutputStream(pdfFile);
            document.save(outputStream, SaveFormat.PDF);
            long now = System.currentTimeMillis();
            outputStream.close();
            is.close();
            System.out.println("WORD转化PDF共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
        } catch (Exception e) {
            System.out.println("转化失败");
            e.printStackTrace();
        }

        return pdfFile;
    }

    public static void main(String[] args) {
        String docPath = "F:\\Users\\test\\Desktop\\IT项目.docx";
        String pdfPath = "F:\\Users\\test\\Desktop\\IT项目-convert1.pdf";
        docToPdf(docPath,pdfPath);

    }

}
package com.example.core.mydemo.aspose;

import com.aspose.pdf.DocSaveOptions;
import com.aspose.pdf.License;
import com.aspose.pdf.SaveFormat;
import com.aspose.pdf.Document;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class PdfToWord {
    public static File pdfToDoc(String pdfPath, String docPath) {
        System.out.println("PDF转化WORD开始>>>>");  //转化用时
        File pdfFile = new File(docPath);
        try {
            long old = System.currentTimeMillis();
            String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
            ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
            License license = new License();
            license.setLicense(is);

            Document document = new Document(pdfPath);
            FileOutputStream outputStream = new FileOutputStream(pdfFile);
            document.save(outputStream, SaveFormat.Doc);

//            DocSaveOptions saveOptions = new DocSaveOptions();
//            saveOptions.setMode(DocSaveOptions.RecognitionMode.Flow);
//            saveOptions.setFormat(DocSaveOptions.DocFormat.DocX);
//            document.save(outputStream, saveOptions);

            outputStream.close();
            is.close();
            long now = System.currentTimeMillis();
            System.out.println("PDF转化WORD共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
        } catch (Exception e) {
            System.out.println("转化失败");
            e.printStackTrace();
        }
        return pdfFile;
    }

    public static File pdfToDoc(InputStream pdfPathInputStream, String docPath) {
        File pdfFile = new File(docPath);
        try {
            long old = System.currentTimeMillis();
            String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
            ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
            License license = new License();
            license.setLicense(is);
            Document document = new Document(pdfPathInputStream);
            FileOutputStream outputStream = new FileOutputStream(pdfFile);
            document.save(outputStream, SaveFormat.Doc);
            outputStream.close();
            is.close();
            long now = System.currentTimeMillis();
            System.out.println("PDF转化WORD共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
        } catch (Exception e) {
            System.out.println("转化失败");
            e.printStackTrace();
        }
        return pdfFile;
    }

    public static void main(String[] args) {
        String pdfPath = "F:\\Users\\test\\Desktop\\IT项目.pdf";
        /**
         * 测试发现:只能生成doc,而不能生成docx,文件打不开。
         */
        String docPath = "F:\\Users\\test\\Desktop\\IT项目-convert.doc";
        pdfToDoc(pdfPath,docPath);

    }

}

百度云:
链接:https://pan.baidu.com/s/1uskoVYZvxHnc6COWW_eAJg
提取码:qmz7

标签:outputStream,word,String,import,System,File,new,互转,aspose
From: https://www.cnblogs.com/oktokeep/p/17459112.html

相关文章

  • mysql版本:'for the right syntax to use near 'identified by 'password' with grant
    查询mysql具体版本SELECT@@VERSION问题分析:mysql版本8.0.13,在给新用户授权时,发生了变化:1064-YouhaveanerrorinyourSQLsyntax;checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousenear'identifiedby'password'withgrant......
  • 如何将word图片粘贴到CKEditor里面
    ​ 1.编辑器修改(可选)1.1在 ueditor/config.json 中添加代码块    /* 上传word配置 */    "wordActionName":"wordupload",/* 执行上传视频的action名称 */    "wordFieldName":"upfile",/* 提交的视频表单名称 */    "wordPathFormat":"/p......
  • 前端实现导出word文档docx格式
    说明前端实现导出word文档,我们需要用到docxtemplater这个库使用的是vue2.6和vue-cli5还需要准备一个word模板,更多模板变量请去docxtemplater官网获取准备word模板安装需要用到的库//安装docxtemplaternpminstalldocxtemplaterpizzip--save//安装jszip-utilsn......
  • 如何将word图片粘贴到KindEditor里面
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@     page contentType="text/html;cha......
  • 如何将word图片粘贴到wangEditor里面
    ​ 如何做到ueditor批量上传word图片?1、前端引用代码<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>......
  • 调整word序号的格式
     点击定义新编号格式是设置左顶格或右顶格。 右键字体设置大小......
  • java中Date类型和时间戳、Date和String互转代码
    /***10位时间戳转Date类型*@paramtimeStamp*@return*/publicstaticDatestamp2Date(StringtimeStamp){SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");Datedate=null;try{......
  • Aspose.cell 常用封包
    导出成List[AttributeUsage(AttributeTargets.Property,AllowMultiple=false)]publicclassExcelColumnAttribute:Attribute{publicExcelColumnAttribute(intcolumnIndex){ColumnIndex=columnIndex;}......
  • c# xml、json相互转换
    stringjsonString="{\"Table\":{\"Row\":[{\"Name\":\"张三\",\"Age\":\"20\"},{\"Name\":\"李四\",\"Age\":\"25\"}]}}";......
  • Word复制过去源格式也不对问题
    我在做毕业论文的时候,各种材料文档要集成在一个文档。结果复制过去,不是表格断了就是字间距宽了,OpenType选项变灰等字体问题,等等。这其实是word文档的兼容模式,老文档字间距紧凑,而新文档间距拉长。结果可能造成表格断开到第二页。如果要转换成最新的版本,方法是:菜单栏的“文件”(“......