文件的详细路径: 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;
}
}
// 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;
}
}
// 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();
}
}
// 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>
<!--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.cn/213435.html原文链接:https://javaforall.cn