网上java word转pdf比较少,要么就是要一大把积分让你下载,不借助外部软件就是这个最简单,还有一个 Spire.Doc free版(class被加密,转pdf限制3页)
把word转为pdf分为几步?
第一步,下载jar包,引入 ,jar包下载地址:https://www.lanzous.com/i7zk9kb
第二部,贴上我下面这段代码
//引入 import java.io.*;
//引入 import com.aspose.words.*;
public static void wordToPdf(String docPath,String savePath){
try {
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);
com.aspose.words.Document document = new com.aspose.words.Document(docPath);
document.save(new FileOutputStream(new File(savePath)),SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception{
wordToPdf("C:\\Users\\Administrator\\Desktop\\aaa.docx","C:\\Users\\Administrator\\Desktop\\ddd.pdf");
}
把word转图片分为几步?
第一步,下载jar包,引入 ,jar包下载地址:https://www.lanzous.com/i7zk9kb
第二部,贴上我下面这段代码
/**
* 文档转图片
* @param inPath 传入文档地址
* @param outDir 输出的图片文件夹地址
*/
public static void doc2Img(String inPath, String outDir){
try {
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);
com.aspose.words.Document doc = new com.aspose.words.Document(inPath);
// 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
int pageCount = doc.getPageCount();
for (int i = 0; i < pageCount; i++) {
File file = new File(outDir+"/"+i+".JPEG");
FileOutputStream os = new FileOutputStream(file);
options.setPageIndex(i);
doc.save(os, options);
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
注:仅支持个人学习,商业用途请选择正版