.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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • srvctl命令_srvctl命令

    srvctl命令_srvctl命令添加删除ASMCommand:srvctladdasm[-llsnr_name][-pspfile][-dasm_diskstring]Command:srvctlremoveasm-l[-f]Example:srvctladdasm-lLISTENER_ASM01-p+diskg_data/spfile.oraExample:srvctlremoveasm-lLISTENER_ASM01-f启动停

    2022年9月11日
    0
  • mysql如何使用前缀索引_MySQL的前缀索引你是如何使用的[通俗易懂]

    mysql如何使用前缀索引_MySQL的前缀索引你是如何使用的[通俗易懂]灵魂3连问:什么是前缀索引?前缀索引也叫局部索引,比如给身份证的前10位添加索引,类似这种给某列部分信息添加索引的方式叫做前缀索引。为什么要用前缀索引?前缀索引能有效减小索引文件的大小,让每个索引页可以保存更多的索引值,从而提高了索引查询的速度。但前缀索引也有它的缺点,不能在orderby或者groupby中触发前缀索引,也不能把它们用于覆盖索引。什么情况下适合使用前缀索引?当字符…

    2022年5月18日
    52
  • bitblt函数 透明底图_手绘楼盘摆位图怎么画

    bitblt函数 透明底图_手绘楼盘摆位图怎么画CDC::BitBlt函数,应用在GDI的绘图中,有时候我们需要显示的位图有一部分是透明的,例如在棋类游戏中,棋盘图片和棋子图片融合的时候,棋子边缘应该是透明。    在图1中,棋盘和棋子图片融合在一起,需要设置棋子图片边缘的“品红色”为透明。     1、BitBlt函数说明:        BOOLBitBlt(intx,int

    2022年10月19日
    0
  • qt显示视频的控件_qt 控件

    qt显示视频的控件_qt 控件一、前言在平时的写作过程中,经常需要将一些操作动作和效果图截图成gif格式,使得涵盖的信息更全面更生动,有时候可以将整个操作过程和运行效果录制成MP4,但是文件体积比较大,而且很多网站不便于上传,基本上都支持gif动图,一般一个5秒左右的gif,800*600分辨率,可以很好的控制在500KB内,这样就比较完美的支持各大网站上传动图。最开始使用的是ScreenGif.exe,用了很久…

    2022年9月20日
    0
  • JQuery Div scrollTop ScrollHeight

    JQuery Div scrollTop ScrollHeightjQuery里和滚动条有关的概念很多,但是有三个属性和滚动条的拖动有关,就是:scrollTop、scrollLeft、scrollHeight。其中scrollHeight属性,互联网上几乎搜素不到关于它的应用技巧,而我正好需要用到它。我们现在只探讨和垂直滚动有关的scrollTop、scrollHeight属性。一、滚动条有关属性的正确理解:假设有以下Html代码:

    2022年7月23日
    4
  • harmonyos系统与安卓区别(uAndroid)

    目录一、前言二、HarmonyOS与Android的对比2.1HarmonyOS并不是Android的替代品2.2系统定位2.3内核对比2.4运行速度三、方舟编译器一、前言这段时间我在寻思这学习一下鸿蒙,第一是因为在着手做一个自己的开源项目,技术选型的时候想到了鸿蒙;第二是我个人非常看好鸿蒙系统的未来,清除明白华为和一些民族企业担负的责任和国人的期待,虽然带着一些民族感情;鸿蒙刚发布的时候自己是非常激动的,但是后来项目太忙一直没有认真的去了解过,这次打算花一部

    2022年4月13日
    157

发表回复

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

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