.net pdf转word_pdf to word

.net pdf转word_pdf to wordAsposewordpdf相互转换工具类文件的详细路径:pdfToDoc(StringpdfPath,StringdocPath)输入流:pdfToDoc(InputStreampdfPathInputStream,StringdocPath)//Anhighlightedblockpackagecom.example.wordpdf.utils;importcom.aspose.pdf.License;importcom.aspose.pdf.SaveF

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

Aspose word pdf 相互转换

在这里插入图片描述

工具类

文件的详细路径:
pdfToDoc(String pdfPath, String docPath)
输入流:
pdfToDoc(InputStream pdfPathInputStream, String docPath)

// An highlighted block
package com.example.wordpdf.utils;

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;

/** * @ProjectName: word-pdf * @Package: com.example.wordpdf.utils * @ClassName: PdfToWord * @Author: jibl * @Description: * @Date: 2021/11/23 15:50 * @Version: 1.0 */
public class PdfToWord { 
   
    public static File pdfToDoc(String pdfPath, 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(pdfPath);
            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 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;
    }
}

Jetbrains全家桶1年46,售后保障稳定

// An highlighted block
package com.example.wordpdf.utils;

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;

/** * @ProjectName: word-pdf * @Package: com.example.wordpdf.utils * @ClassName: WordToPdf * @Author: jibl * @Description: * @Date: 2021/11/23 15:50 * @Version: 1.0 */
public class WordToPdf { 
   
    public static File docToPdf(String docPath, 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(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;
    }
}

controller

// An highlighted block
package com.example.wordpdf.controller;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.example.wordpdf.utils.PdfToWord;
import com.example.wordpdf.utils.WordToPdf;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;

/** * @ProjectName: word-pdf * @Package: com.example.wordpdf.controller * @ClassName: FileController * @Author: jibl * @Description: * @Date: 2021/11/23 14:32 * @Version: 1.0 */
@Controller
@RequestMapping("/file")
public class FileController { 
   

    public static void main(String[] args)  { 
   
      File file =  WordToPdf.docToPdf("D:/upload/test.doc", "D:/upload/test666.pdf");
    }

    @RequestMapping("/docToPdf")
    public void importPdfFile(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException { 
   
        System.out.println("doc =====> PDF");
        InputStream inputStream = file.getInputStream();
        File converFile = WordToPdf.docToPdf(inputStream,"D:/upload/"+new Date().getTime() +".pdf");
        inputStream.close();
        System.out.println("====转化成功!====");
        response.setContentType("application/x-download");
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String("ggg.pdf".getBytes(),"iso-8859-1"));
        InputStream is = new FileInputStream(converFile);
        ServletOutputStream oupstream = response.getOutputStream();
        byte[] buffer = new byte[512]; // 缓冲区
        int bytesToRead = -1;
        // 通过循环将读入的文件的内容输出到浏览器中
        while ((bytesToRead = is.read(buffer)) != -1) { 
   
            oupstream.write(buffer, 0, bytesToRead);
        }
        oupstream.close();
        is.close();

    }

    @RequestMapping("/pdfToDoc")
    public void importDocFile(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException { 
   
        System.out.println("doc =====> PDF");
        InputStream inputStream = file.getInputStream();
        File converFile = PdfToWord.pdfToDoc(inputStream,"D:/upload/"+new Date().getTime() +".doc");
        inputStream.close();
        System.out.println("====转化成功!====");
        response.setContentType("application/x-download");
        response.setHeader("Content-Disposition", "attachment;filename="
                + new String("ggg.pdf".getBytes(),"iso-8859-1"));
        InputStream is = new FileInputStream(converFile);
        ServletOutputStream oupstream = response.getOutputStream();
        byte[] buffer = new byte[512]; // 缓冲区
        int bytesToRead = -1;
        // 通过循环将读入的文件的内容输出到浏览器中
        while ((bytesToRead = is.read(buffer)) != -1) { 
   
            oupstream.write(buffer, 0, bytesToRead);
        }
        oupstream.close();
        is.close();

    }

}

html

// An highlighted block
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>PDF-WORD转化</title>
    <script src="/jquery-3.2.1/jquery-3.2.1.js"></script>
    <link type="text/css" rel="stylesheet"
          href="/bootstrap-3.4.1/css/bootstrap.css"/>
    <script src="/bootstrap-3.4.1/js/bootstrap.js"></script>
    <style>
        * { 
   
            margin: 0px;
            padding: 0px;
        }

        html, body { 
   
            margin: 0px;
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
<div class="container" style="padding: 20px;height:95%">
        <div class="panel panel-info">
            <div class="panel-heading">
                <h3 class="panel-title">WORD/PDF转化</h3>
            </div>
            <div class="panel-body">
                <div class="row" style="margin-top: 10px;">
                    <div class="col-sm-3">

                    </div>
                    <div class="col-sm-4">
                        <input type='text' class="form-control" style="width:100%"
                               name='filename' id='filename' autocomplete="off"
                               readonly="readonly" />
                    </div>
                    <div class="col-sm-2">
                        <input onclick="upload()" value="选择文件上传" type="button"
                               class="btn btn-primary" />
                    </div>
                    <input type="file" id="file" accept=".doc,.pdf" name="file"
                           style="display: none" />
                </div>
                <div class="row" style="margin-top: 10px;margin-right: 20px;text-align: right">
                    <button type="button" class="btn btn-primary" onclick="docToPdf()">Word转PDF</button>
                    <button type="button" class="btn btn-info"  onclick="pdfToDoc()">PDF转Word</button>
                </div>
            </div>
         </div>

</div>
</body>
<script>
    function upload() { 
   
        $("#file").click();
        $('#file').change(
            function(e) { 
   
                fileName = e.target.files[0];// 上传文件对象
                $("#filename")
                    .val(
                        fileName.name.substring(fileName.name
                            .lastIndexOf('.')));
                if (fileName !== undefined) { 
   
                    var file_typename = fileName.name.substring(fileName.name
                        .lastIndexOf('.'));
                    if (file_typename === '.doc' || file_typename === '.pdf') { 
   
                        $("#filename").css("display", "block");
                        $("#filename").val(fileName.name);
                        // UpladFile(fileName);
                    } else { 
   
                        $("#filename").val("")
                        alert("请选择正确的文件类型!")
                    }
                } else { 
   
                    $("#filename").val("")
                    alert("请选择正确的文件!")
                }
            })
    }
    function docToPdf() { 
   
        var formData = new FormData();
        formData.append("file", fileName); // form中放入文件对象
        var xhr = new XMLHttpRequest();
        xhr.open('POST', "http://localhost:8066/file/docToPdf", true);    // 也可以使用POST方式,根据接口
        // xhr.setRequestHeader("Content-Type","multipart/form-data");
        xhr.responseType = "blob";  // 返回类型blob
        // 定义请求完成的处理函数,请求前也可以增加加载框/禁用下载按钮逻辑
        xhr.onload = function () { 
   
            // 请求完成
            if (this.status === 200) { 
   
                // 返回200
                var blob = this.response;
                var reader = new FileReader();
                reader.readAsDataURL(blob);  // 转换为base64,可以直接放入a表情href
                reader.onload = function (e) { 
   
                    // 转换完成,创建一个a标签用于下载
                    var a = document.createElement('a');
                    a.download = new Date().getTime()+'.pdf';
                    a.href = e.target.result;
                    a.click();
                }
            }
        };
        // 发送ajax请求
        xhr.send(formData)
    }
    function pdfToDoc() { 
   
        var formData = new FormData();
        formData.append("file", fileName); // form中放入文件对象
        var xhr = new XMLHttpRequest();
        xhr.open('POST', "http://localhost:8066/file/pdfToDoc", true);    // 也可以使用POST方式,根据接口
        // xhr.setRequestHeader("Content-Type","multipart/form-data");
        xhr.responseType = "blob";  // 返回类型blob
        // 定义请求完成的处理函数,请求前也可以增加加载框/禁用下载按钮逻辑
        xhr.onload = function () { 
   
            // 请求完成
            if (this.status === 200) { 
   
                // 返回200
                var blob = this.response;
                var reader = new FileReader();
                reader.readAsDataURL(blob);  // 转换为base64,可以直接放入a表情href
                reader.onload = function (e) { 
   
                    // 转换完成,创建一个a标签用于下载
                    var a = document.createElement('a');
                    a.download = new Date().getTime()+'.doc';
                    a.href = e.target.result;
                    a.click();
                }
            }
        };
        // 发送ajax请求
        xhr.send(formData)
    }
</script>
</html>

pom

        <!--word转pdf-->
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-word</artifactId>
            <version>15.8.0</version>
        </dependency>
        <!--pdf转word-->
        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-pdf</artifactId>
            <version>15.8.0</version>
        </dependency>

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/213435.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 最新版本git下载安装&配置教程「建议收藏」

    最新版本git下载安装&配置教程「建议收藏」原文地址https://blog.csdn.net/zx1996119/article/details/80814752下载地址:https://git-scm.com/downloads如图:这里下载完成后是酱子的如图(windows64位的)1.双击打开后,点击Next2.再次点击Next(这里我改了安装的路径)3.下图方框…

    2022年5月1日
    60
  • pycharm 重装后双击无反应的一种解决方法

    pycharm 重装后双击无反应的一种解决方法问题描述之前安装的2018版本的pycharm,更新失败之后自动删除,所以重装了一个2020.1,然后发现双击无反应。解决方法用下面方法解决后,记录一下过程:在“添加和删除程序”中发现了pycharm2018版本的程序名,但目录文件已被删除。于是通过注册表编辑器(cmd输入regedit),在编辑→查找中查找pycharm发现了pycharm2018项,将其删除后添加或删除程序里…

    2022年8月26日
    7
  • Java中double转BigDecimal的注意事项

    Java中double转BigDecimal的注意事项先上结论:不要直接用double变量作为构造BigDecimal的参数。 线上有这么一段Java代码逻辑:1,接口传来一个JSON串,里面有个数字:57.3。2,解析JSON并把这个数字保存在一个float变量。3,把这个float变量赋值给一个BigDecimal对象,用的是BigDecimal的double参数的构造:  newBigDecimal(double…

    2022年5月2日
    43
  • 什么是代理服务器(Proxy)

    什么是代理服务器(Proxy)以类似代理人的身份去取得用户所需要的数据就是了!但是由于它的『代理』能力,使得我们可以透过代理服务器来达成防火墙功能与用户浏览数据的分析! 此外,也可以藉由代理服务器来达成节省带宽的目的,以及加快内部网络对因特网的WWW访问速度  17.1.1什么是代理服务器 我们或许会帮忙家人去办理一些杂务吧!举个例子来说,例如缴费或者是申办提款卡等等的,由于你并不是『

    2022年5月29日
    41
  • TCP报文段详解

    TCP协议tcp报文段源端口&amp;amp;目的端口2字节(tcp的分用功能以端口实现)序号4字节tcp三次握手中的seq,表示tcp数据段发送的第一个字节的序号,范围[0,2^32-1],即mod2^32;例如,seq=201,携带的数据有100,那么最后一个字节的序号就为300,那么下一个报文段就应该从401开始,下一个序列的首地址.tcp是面向字节…

    2022年4月7日
    61
  • java经典源码_java经典源代码[通俗易懂]

    java经典源码_java经典源代码[通俗易懂]Java100个经典小程序_计算机软件及应用_IT/计算机_专业资料。Java100个经典小程序【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字……()A、编写源代码B、编写HTML文件调用该小程序,以.html为扩展名存入相同文件夹C、编译过程D、解释执行85.Java的字符类型采用的是Unicode编码方案……15个经典…

    2022年7月9日
    21

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号