java PDF 이미지 변환 방법

2512 단어 javaPDF그림
본고의 실례는 여러분에게 자바가 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);
 }
 
}
이상은 본문의 전체 내용입니다. 여러분의 학습에 도움이 되고 저희를 많이 응원해 주십시오.

좋은 웹페이지 즐겨찾기