相关引用对象在代码里了
相关依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<!-- 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>
<!-- ppt转pdf,excel转pdf需要用5.2.0以上版本 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<!-- excel转pdf字体使用 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
wordpdf_45">word转pdf
java"> word">import com.documents4j.api.DocumentType;
word">import com.documents4j.api.IConverter;
word">import com.documents4j.job.LocalConverter;
word">import java.io.*;
/**
* 实现word转pdf
*
* @param sourcePath 源文件地址 如 D:\\1.doc
* @param targetPath 目标文件地址 如 D:\\1.pdf
*/
word">public word">static word">void wordToPdf(String sourcePath, String targetPath) {
File inputWord = word">new File(sourcePath);
File outputFile = word">new File(targetPath);
word">try {
InputStream docxInputStream = word">new FileInputStream(inputWord);
OutputStream outputStream = word">new FileOutputStream(outputFile);
IConverter converter = LocalConverter.builder().build();
converter.convert(docxInputStream)
.as(DocumentType.DOCX)
.word">to(outputStream)
.as(DocumentType.PDF).schedule().get();
outputStream.close();
docxInputStream.close();
log.info("转换完毕 targetPath = {}", outputFile.getAbsolutePath()+",targetPath = " + outputFile.getAbsolutePath());
converter.shutDown();
} word">catch (Exception e) {
log.error("word转pdf失败:"+e.getMessage(), e);
}
}
pptpdf_85">ppt转pdf
java"> word">import com.itextpdf.text.*;
word">import com.itextpdf.text.pdf.PdfPCell;
word">import com.itextpdf.text.pdf.PdfPTable;
word">import com.itextpdf.text.pdf.PdfWriter;
word">import org.apache.poi.xslf.usermodel.*;
word">import java.awt.*;
word">import java.awt.Color;
word">import java.awt.image.BufferedImage;
word">import java.io.*;
/**
* ppt转为pdf
* @param sourcePath 源文件地址 如 D:\\1.pptx
* @param targetPath 目标文件地址 如 D:\\1.pdf
*/
word">public word">static word">void pptToPdf(String sourcePath,String targetPath) {
Document document = word">null;
XMLSlideShow slideShow = word">null;
FileOutputStream fileOutputStream = word">null;
PdfWriter pdfWriter = word">null;
ByteArrayOutputStream baos = word">null;
word">byte[] resBytes = word">null;
word">try {
baos=word">new ByteArrayOutputStream();
//使用输入流pptx文件
slideShow = word">new XMLSlideShow(word">new FileInputStream(word">new File(sourcePath)));
//获取幻灯片的尺寸
Dimension dimension = slideShow.getPageSize();
//创建一个写内容的容器
document = word">new Document(PageSize.A4, 72, 72, 72, 72);
//使用输出流写入
pdfWriter = PdfWriter.getInstance(document, baos);
//使用之前必须打开
document.word">open();
pdfWriter.word">open();
PdfPTable pdfPTable = word">new PdfPTable(1);
//获取幻灯片
java.util.List<XSLFSlide> slideList = slideShow.getSlides();
word">for (word">int i = 0, row = slideList.size(); i < row; i++) {
//获取每一页幻灯片
XSLFSlide slide = slideList.get(i);
word">for (XSLFShape shape : slide.getShapes()) {
//判断是否是文本
word">if(shape word">instanceof XSLFTextShape){
// 设置字体, 解决中文乱码
XSLFTextShape textShape = (XSLFTextShape) shape;
word">for (XSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
word">for (XSLFTextRun textRun : textParagraph.getTextRuns()) {
textRun.setFontFamily("宋体");
}
}
}
}
//根据幻灯片尺寸创建图形对象
BufferedImage bufferedImage = word">new BufferedImage((word">int)dimension.getWidth(), (word">int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = bufferedImage.createGraphics();
graphics2d.setPaint(Color.white);
graphics2d.setFont(word">new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));
//把内容写入图形对象
slide.draw(graphics2d);
graphics2d.dispose();
//封装到Image对象中
com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(bufferedImage, word">null);
image.scalePercent(50f);
// 写入单元格
pdfPTable.addCell(word">new PdfPCell(image, true));
document.add(image);
}
document.close();
pdfWriter.close();
resBytes = baos.toByteArray();
FileOutputStream outputStream = word">new FileOutputStream(targetPath);
outputStream.write(resBytes);
outputStream.flush();
outputStream.close();
} word">catch (Exception e) {
log.error("pptx转pdf异常:"+e.getMessage(),e);
} word">finally {
word">try {
word">if (baos != word">null) {
baos.close();
}
} word">catch (Exception e) {
log.error("pptx转pdf关闭io流异常:"+e.getMessage(),e);
}
}
}
excelpdf_179">excel转pdf
java"> word">import com.itextpdf.text.*;
word">import com.itextpdf.text.Font;
word">import com.itextpdf.text.pdf.BaseFont;
word">import com.itextpdf.text.pdf.PdfPCell;
word">import com.itextpdf.text.pdf.PdfPTable;
word">import com.itextpdf.text.pdf.PdfWriter;
word">import lombok.SneakyThrows;
word">import org.apache.poi.ss.usermodel.*;
/**
*
* @param sourcePath 源文件地址 如 D:\\1.xlsx
* @param targetPath 目标文件地址 如 D:\\1.pdf
*/
@SneakyThrows
word">private word">void excelToPdf(String sourcePath, String targetPath ){
word">try (Workbook workbook = WorkbookFactory.create(word">new File(sourcePath));
FileOutputStream fos = word">new FileOutputStream(targetPath )) {
// 获取第一个工作表
Sheet sheet = workbook.getSheetAt(0);
// 创建PDF文档对象
Document document = word">new Document(PageSize.A4, 50, 50, 50, 50);
// 创建PDF输出流
PdfWriter writer = PdfWriter.getInstance(document, fos);
// 打开PDF文档
document.word">open();
// 创建PDF表格对象
PdfPTable table = word">new PdfPTable(sheet.getRow(0).getLastCellNum());
// 设置表格宽度
table.setWidthPercentage(100);
// 设置表格标题,Example Table为表格标题
Paragraph title = word">new Paragraph("Example Table", word">new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 16, Font.BOLD));
title.setAlignment(Element.ALIGN_CENTER);
document.add(title);
// 添加表格内容
word">for (Row row : sheet) {
word">for (Cell cell : row) {
PdfPCell pdfCell = word">new PdfPCell(word">new Paragraph(cell.getStringCellValue(), word">new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12)));
pdfCell.setBorderWidth(1f);
pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(pdfCell);
}
}
// 添加表格到PDF文档
document.add(table);
// 关闭PDF文档
document.close();
} word">catch (IOException | DocumentException e) {
e.printStackTrace();
}
}
相关文件参考:
word转pdf : https://gitee.com/wu_ze_wen/word_trans_pdf?_from=gitee_search
ppt转pdf : https://blog.csdn.net/qq_30436011/article/details/127737553?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-127737553-blog-127973320.235%5Ev38%5Epc_relevant_anti_t3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-127737553-blog-127973320.235%5Ev38%5Epc_relevant_anti_t3&utm_relevant_index=4
excel转pdf :
https://blog.csdn.net/sqL520lT/article/details/131430166
https://www.cnblogs.com/iyyy/p/9346935.html