.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)
上一篇 2025年7月23日 上午9:43
下一篇 2025年7月23日 上午10:15


相关推荐

  • ft232芯片怎么样_引脚悬空是什么电平

    ft232芯片怎么样_引脚悬空是什么电平概述:FF4232H芯片一款专门用于USB到RS232/RS485/RS422之间的电平转换芯片,数据收发和协议转换工作全由芯片独立完成,无需人工干预,不用编写芯片的固件,给设计者带来了极大的便利。利用该芯片只需要加少量的外围电路就可以实现相应的转换。FT4232H采用64-LDLeadFreeLQFPorQFN封装工艺。一、FT4232H功能和特性1、单芯片到4路串口的转换,整个接口协…

    2022年8月10日
    7
  • Xen VMM

    Xen VMMXen虚拟机即XenVMM(VirtualMachineMonitor),是剑桥大学计算机实验室开发的一个开源项目,它能够使用户创建更多的虚拟机,而每一个虚拟机都是运行在同一个操作系统上的实例。

    2026年2月15日
    4
  • Python实现久坐提醒小助手程序「建议收藏」

    Python实现久坐提醒小助手程序「建议收藏」不论是日常的工作还是学习,现代年轻人在电脑屏幕时长数据能让人惊掉下巴,继而引发一系列身体不适的现象。小李也是久坐族中的一员,为了时刻提醒自己起来活动活动,我开发了一款基于PythonGU…

    2026年4月14日
    5
  • pycharm配置路径_如何在pycharm添加解释器

    pycharm配置路径_如何在pycharm添加解释器步骤一:pycharm–>settingforNewProjects步骤二:settingsforNewprojects–>projectInterpreter–>showAll–>Add

    2022年8月25日
    35
  • MyBatis面试题总结「建议收藏」

    MyBatis面试题总结「建议收藏」啃下MyBatis源码-MyBatis面试题总结1.概念/使用方法向的问题1.1什么是Mybatis?1.2为什么说Mybatis是半ORM框架?/与Hibernate有哪些不同?1.3Mybaits的优点?1.4MyBatis框架的缺点?1.5#{}和${}的区别?1.6怎么解决实体类中的属性名和表中的字段名不一样的问题?1.7在mapper中如何传递多个参数?…

    2022年6月11日
    27
  • Dynamics CRM IFD部署之后遇到的登录问题

    Dynamics CRM IFD部署之后遇到的登录问题DynamicsCRMI 部署之后遇到的登录问题证书问题共用证书域控问题跨域登录证书问题共用证书因为公司只给了一个证书 且 UAT 环境和生产环境都进行了 IFD 部署 当在同一个浏览器同时登录 UAT 和生产环境就会报错 URL 解码的错误信息为 错误详细信息 ID1073 尝试使用 ProtectedDat 解密 Cookie 时出现 Cryptographi 有关详细信息 请参见内部异常 如果使用的是 IIS7 5 则这可能是由于应用程序池的 loadUserP

    2026年3月17日
    1

发表回复

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

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