网上找了好久的资料,发现aspose-word包可以用.支持 doc ,docx , et 可以转换PDF文档
官网API https://docs.aspose.com/display/wordsjava/Home
但是这个东西很耗内存,而且如果文档启用宏的话,进行切图会报错。这个没有处理。
还有切出来的每页图片内容跟源文档不一致,可能下一页的第一行到上一页的最后一行。
遇到某些文档报错,可能字体缺失,缺失哪种字体就下载放入 C:\Windows\Fonts 字体库
package wordCutImage;
import java.io.FileInputStream;
import java.io.InputStream;
import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.SaveFormat;
public class Test {
private static InputStream license;
private static InputStream fileInput;
/**
* 获取license
*
* @return
*/
public static boolean getLicense() {
boolean result = false;
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
license = new FileInputStream(loader.getResource("license.xml").getPath());// 凭证文件
fileInput = new FileInputStream(loader.getResource("test.doc").getPath()); // 文件路径
com.aspose.words.License license1 = new com.aspose.words.License();
license1.setLicense(license);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws Exception {
if (!getLicense()) {
return;
}
try {
long old = System.currentTimeMillis();
Document pdfDocument = new Document(fileInput);
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
options.setPrettyFormat(true);
options.setUseAntiAliasing(true);
options.setUseHighQualityRendering(true);
for (int i = 0; i < pdfDocument.getPageCount(); i++) {
String fileName = "第" + i + "张.jpg";
options.setPageIndex(i);
System.out.println(i);
pdfDocument.save("D:/wordTest/" + fileName, options);
}
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒 ");
} catch (Exception e) {
e.printStackTrace();
}
}
}
程序下载链接 https://download.csdn.net/download/hqbootstrap1/10491476