- maven项目引包
<!--word转pdf-->
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version>
</dependency>
<!--pdf转图片-->
<dependency>
<groupId>org.icepdf</groupId>
<artifactId>icepdf-core</artifactId>
<version>4.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.icepdf/icepdf-viewer -->
<dependency>
<groupId>org.icepdf</groupId>
<artifactId>icepdf-viewer</artifactId>
<version>4.1.1-taverna</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.icepdf.os/icepdf-core -->
<dependency>
<groupId>org.icepdf.os</groupId>
<artifactId>icepdf-core</artifactId>
<version>6.2.2</version>
</dependency>
2.代码
//PDF转图片(帮助文档)
public List<String> getDataPDF(String imagePath) {
File newFile = new File(manual);
if (!newFile.exists()) {
newFile.mkdirs();
}
List<String> strList = new ArrayList<>();
try {
//word转PDF pdf文件名
String pdfName = newFile.getName().substring(0, newFile.getName().lastIndexOf(".")) + ".pdf";
File outputFile = new File(imagePath + pdfName);
InputStream doc = new FileInputStream(newFile);
OutputStream outputStream = new FileOutputStream(outputFile);
IConverter converter = LocalConverter.builder().build();
converter.convert(doc).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
outputStream.close();
//PDF转图片
List<String> imageList = PDFUtils.PDFImges(imagePath + pdfName, imagePath);
for (String str : imageList
) {
strList.add(httpPath + imagePath.replaceAll(imagePath.split("/")[0], "") + str);
}
} catch (Exception e) {
e.printStackTrace();
}
return strList;
}
/**
* PDF转图片
*
* @param
* @return null
* @Author HB
* @Date 2021/4/13 09:56
**/
public static List<String> PDFImges(String pdfPath, String imagePath) {
List<String> imageList = new ArrayList<>();
Document document = new Document();
try {
document.setFile(pdfPath);
} catch (Exception ex) {
}
//缩放比例
float scale = 2.5f;
//旋转角度
float rotation = 0f;
for (int i = 0; i < document.getNumberOfPages(); i++) {
BufferedImage image = null;
try {
image = (BufferedImage)
document.getPageImage(i, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, scale);
RenderedImage rendImage = image;
try {
int a=i+100;
File file = new File(imagePath + "/help_" +a+ ".png");
ImageIO.write(rendImage, "png", file);
imageList.add(file.getName());
} catch (IOException e) {
e.printStackTrace();
}
image.flush();
} catch (InterruptedException e) {
e.printStackTrace();
}
document.dispose();
}
return imageList;
}
3.友情提示
不能直接拿来就用,要稍作修改,即可使用!!!欢迎留言探讨。