.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

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 云南 代理服务器_今日更新快速ip代理服务地址 免费国外代理服务器 2013.4.17

    云南 代理服务器_今日更新快速ip代理服务地址 免费国外代理服务器 2013.4.1761.175.223.134:3128@HTTP;浙江省台州市电信61.175.223.142:3128@HTTP;浙江省台州市电信60.190.129.52:3128@HTTP;浙江省嘉兴市电信代理ip61.166.55.153:11808@HTTP;云南省昭通市电信115.124.73.166:8080@HTTP;印度尼西亚雅加达市180.250.79.122:8080@HTTP;印度…

  • IM开发宝典:史上最全,微信各种功能参数和逻辑规则资料汇总「建议收藏」

    1、引言IM应用的初学者们,在补全了各种基础技术知识后(如果您仍不具备这些知识,建议马上阅读《新手入门一篇就够:从零开发移动端IM》),在动手编码实践时,很多时候纠结的并不是功能该如何实现,而是这个功能该实现成什么样(没有经验,我特玛能找谁问问?)。比如,最常见的纠结有以下这些:1)离线聊天消息该保存多久?2)好友请求应该保存多久?3)短视频消息中的视频时长设为多大合适?4…

  • pycharm2021.12.12 激活(JetBrains全家桶)

    (pycharm2021.12.12 激活)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.cn/100143.html4D5UJRVIF9-eyJsaWNlbnNlSWQi…

  • rider2021 激活码【2021免费激活】

    (rider2021 激活码)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~https://javaforall.cn/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~BC…

  • 函数指针,指针函数,返回值为指针的函数「建议收藏」

    函数指针,指针函数,返回值为指针的函数「建议收藏」在学习C语言时,一个比较容易混淆的概念就是函数指针、指针函数、返回值为指针的函数。本文将对这三个概念进行区分。指针函数就是返回值为指针的函数,两个本质上是同一事物,只是叫法不同。函数指针就是一个指向函数的指针,本质上是一个指针,只是这个指针指向的对象是函数,而不是一般意义上的存储对象。

  • java四舍五入成整数的方法

    java四舍五入成整数的方法    在java的Math类中,提供了许许多多的和数学计算有关的方法,其中也包括取整的,关于取整的有向下取整的floor(doubled)返回值double,rint(doubled),round(doubled)和round(floatf)。   但是,其中和四舍五入相近的方法只有rint和round方法,如果单独使用这两个方法的话,所得到的结果和我们预期的结果不一样,    比如r…

发表回复

您的电子邮箱地址不会被公开。

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