java PDF 이미지 변환 방법
1. 먼저 마븐을 이용하여 필요한jar 패키지 도입
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
2. 이것은 본인이 직접 쓴 도구 종류입니다. 두 가지 방법이 있습니다. 하나는 PDF 총 페이지 번호를 얻는 것입니다. 하나는 전송된 페이지를 통해 대응하는 pdf를 지정된 형식의 그림으로 바꾸고 흐르는 방식으로 클라이언트에게 응답하는 것입니다.
public class PDFToImgUtil {
private static Logger logger = LoggerFactory.getLogger(PDFToImgUtil.class);
/**
* PDF
* @throws IOException
*/
public static int getPDFNum(String fileUrl) throws IOException {
PDDocument pdDocument = null;
int pages = 0;
try {
pdDocument = getPDDocument(fileUrl);
pages = pdDocument.getNumberOfPages();
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(),e);
} finally {
if (pdDocument != null) {
pdDocument.close();
}
}
return pages;
}
/**
* PDF
* @throws IOException
* imgType: jpg,png
*/
public static void PDFToImg(OutputStream sos,String fileUrl,int page,String imgType) throws IOException {
PDDocument pdDocument = null;
/* dpi , */
int dpi = 100;
try {
pdDocument = getPDDocument(fileUrl);
PDFRenderer renderer = new PDFRenderer(pdDocument);
int pages = pdDocument.getNumberOfPages();
if (page <= pages && page > 0) {
BufferedImage image = renderer.renderImageWithDPI(page,dpi);
ImageIO.write(image, imgType, sos);
}
} catch (Exception e) {
e.printStackTrace();
logger.error(e.getMessage(),e);
} finally {
if (pdDocument != null) {
pdDocument.close();
}
}
}
private static PDDocument getPDDocument(String fileUrl) throws IOException {
File file = new File(fileUrl);
FileInputStream inputStream = new FileInputStream(file);
return PDDocument.load(inputStream);
}
}
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.