在Java开发中,有时我们需要将Word文档转换为PDF格式。Aspose.Words for Java是一个非常强大的工具库,可以帮助我们轻松完成这项任务。本文将介绍一个Java工具类,它使用Aspose.Words for Java库来将Word文档转换为PDF,并去除水印。
准备工作
首先,你需要确保已经下载并添加了Aspose.Words for Java库到你的项目中。你可以从Aspose官方网站下载该库,并将其添加到你的项目依赖中。此外,你还需要一个有效的许可证字符串,以便能够使用Aspose.Words for Java的所有功能。
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0-jdk16</version>
</dependency>
工具类代码
下面是一个完整的Java工具类,用于将Word文档转换为PDF并去除水印:
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.aspose.pdf.Document as PdfDocument; // 注意这里的Document是com.aspose.pdf包下的
import java.io.*;
public class WordToPdfConverter {
/**
* 许可证字符串(可以放到resource下的xml文件中也可)
*/
private static final String LICENSE = "<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>";
/**
* 设置 license 去除水印
*/
private static void setLicense() {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(LICENSE.getBytes());
License license = new License();
try {
license.setLicense(byteArrayInputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* word 转 pdf 生成至指定路径,pdf为空则上传至word同级目录
*
* @param wordPath word文件路径
* @param pdfPath pdf文件路径
* @return 生成的PDF页数
*/
public static long wordConvertPdfFile(String wordPath, String pdfPath) {
FileOutputStream fileOutputStream = null;
long pageCount = 0; // 默认为0页
try {
pdfPath = pdfPath == null ? getPdfFilePath(wordPath) : pdfPath;
setLicense();
File file = new File(pdfPath);
fileOutputStream = new FileOutputStream(file);
Document doc = new Document(wordPath);
doc.save(fileOutputStream, SaveFormat.PDF);
PdfDocument pdfDocument = new PdfDocument(pdfPath);
pageCount = pdfDocument.getPages().size();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
assert fileOutputStream != null;
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return pageCount;
}
/**
* word 转 pdf 生成byte字节流
*
* @param wordPath word所在的目录地址
* @return 生成的PDF字节流
*/
public static byte[] wordConvertPdfByte(String wordPath) {
ByteArrayOutputStream fileOutputStream = null;
try {
setLicense();
fileOutputStream = new ByteArrayOutputStream();
Document doc = new Document(wordPath);
doc.save(fileOutputStream, SaveFormat.PDF);
return fileOutputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
assert fileOutputStream != null;
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 获取 生成的 pdf 文件路径,默认与源文件同一目录
*
* @param wordPath word文件路径
* @return 生成的 pdf 文件路径
*/
private static String getPdfFilePath(String wordPath) {
int lastIndexOfPoint = wordPath.lastIndexOf(".");
String pdfFilePath = "";
if (lastIndexOfPoint > -1) {
pdfFilePath = wordPath.substring(0, lastIndexOfPoint);
}
return pdfFilePath + ".pdf";
}
}
标签:Word,水印,Java,wordPath,pdf,PDF,new,fileOutputStream
From: https://blog.csdn.net/m0_65292294/article/details/145175201