加盟商资格面试代码字段修改及PDF转图片部分完成

This commit is contained in:
pserimal
2023-06-16 15:41:22 +08:00
parent d80c70deb7
commit 1836a50a3e
13 changed files with 135 additions and 67 deletions

View File

@@ -98,6 +98,12 @@
<artifactId>openpdf</artifactId>
<version>1.3.30</version>
</dependency>
<!-- PDFBox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.27</version>
</dependency>
</dependencies>
</project>

View File

@@ -5,10 +5,13 @@ import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.IOException;
import java.io.OutputStream;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* 使用 OpenPDF 封装的 pdf 工具类
@@ -120,7 +123,42 @@ public class PDFUtils {
throw new RuntimeException(ex);
}
}
}
/**
* PDF 转图片
* @param inputStream pdf 输入流
* @param dpi
*/
public static ByteArrayOutputStream pdf2Img(InputStream inputStream, float dpi) {
PDDocument document = null;
ByteArrayOutputStream outputStream = null;
try {
// 加载pdf文档
FileInputStream fileInputStream = new FileInputStream("passLetter.pdf");
document = PDDocument.load(fileInputStream);
// PDF文档渲染对象
PDFRenderer renderer = new PDFRenderer(document);
int pageCount = document.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
/*
* renderImage(i,1.9f)
* i: 指定页对象下标,从0开始,0即第一页
* 1.9f:DPI值(Dots Per Inch),官方描述比例因子,其中 1 = 72 DPI
*/
BufferedImage image = renderer.renderImage(i, dpi);
outputStream = new ByteArrayOutputStream();
FileOutputStream fileOutputStream = new FileOutputStream("pdf2img.png");
ImageIO.write(image, "PNG", outputStream);
byte[] byteArray = outputStream.toByteArray();
fileOutputStream.write(byteArray);
fileOutputStream.close();
return outputStream;
}
}catch (Exception e){
e.printStackTrace();
}
return outputStream;
}
}

View File

@@ -6,6 +6,7 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
@@ -49,7 +50,8 @@ public class PassLetterUtils {
return outputStream;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
}
finally {
try {
document.close();
outputStream.close();
@@ -73,8 +75,10 @@ public class PassLetterUtils {
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
PdfReader pdfReader = new PdfReader(inputStream);
PDFUtils.putParagraphAbsolutely(pdfReader, outputStream, x, y, content, 20, new Color(255, 82,25));
} catch (IOException e) {
String fontFamillyPath = PassLetterUtils.class.getResource("/static/simsun.ttc").getPath();
BaseFont font = BaseFont.createFont(fontFamillyPath + ",0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
PDFUtils.putParagraphAbsolutely(pdfReader, outputStream, x, y, content, font, 20, 1, new Color(255, 82,25));
} catch (Exception e) {
throw new RuntimeException(e);
}
}