Java pdf和jpg互轉(zhuǎn)案例
pdfbox: jpg轉(zhuǎn)pdf:
/** * 使用pdfbox將jpg轉(zhuǎn)成pdf * @param jpgStream jpg輸入流 * @param pdfPath pdf文件存儲(chǔ)路徑 * @throws IOException IOException */ public static void jpgToPdf(InputStream jpgStream, String pdfPath) throws IOException { PDDocument pdDocument = new PDDocument(); BufferedImage image = ImageIO.read(jpgStream); PDPage pdPage = new PDPage(new PDRectangle(image.getWidth(), image.getHeight())); pdDocument.addPage(pdPage); PDImageXObject pdImageXObject = LosslessFactory.createFromImage(pdDocument, image); PDPageContentStream contentStream = new PDPageContentStream(pdDocument, pdPage); contentStream.drawImage(pdImageXObject, 0, 0, image.getWidth(), image.getHeight()); contentStream.close(); pdDocument.save(pdfPath); pdDocument.close(); }
pdfbox: pdf轉(zhuǎn)jpg:
static void pdfbox() throws IOException { long start = System.currentTimeMillis(); //pdf路徑 URL url = new URL('file:///D:/1.pdf'); InputStream stream = URLUtil.getStream(url); // 加載解析PDF文件 PDDocument doc = PDDocument.load(stream); PDFRenderer pdfRenderer = new PDFRenderer(doc); PDPageTree pages = doc.getPages(); int pageCount = pages.getCount(); for (int i = 0; i < pageCount; i++) { BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(bim, 'jpg', os); byte[] datas = os.toByteArray();// InputStream is = new ByteArrayInputStream(datas); //jpg文件轉(zhuǎn)出路徑 FileUtil.writeBytes(datas, new File('d:/jpg/' + i + '.jpg')); } long end = System.currentTimeMillis(); long time = (end - start) / 1000; System.out.println(StrUtil.format('pdf轉(zhuǎn)jpg耗時(shí): {}s', time)); }
icepdf: pdf轉(zhuǎn)jpg
Document document = new Document();document.setUrl(new URL(pdfUrl));int pageNum = document.getNumberOfPages();for (int i = 0; i < pageNum; i++) { // 目前僅支持1對(duì)1的pdf->jpg if (i != 0) { continue; } // 3、pdf -> jpg BufferedImage bim = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, scale); os = new ByteArrayOutputStream(); ImageIO.write(bim, 'jpg', os); // 4、jpg -> fdfs byte[] datas = os.toByteArray(); InputStream is = new ByteArrayInputStream(datas);
補(bǔ)充知識(shí):Java實(shí)現(xiàn)對(duì)png圖片文件電子簽名操作
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
/** * 根據(jù)圖片像素位置添加用戶電子簽名 * @param imagePath 要操作的圖片路徑 * @param signImagePath 電子簽名圖片路徑 * @param outImagePath 合成后輸出圖片路徑 * @param width 像素位寬度 * @param height 像素位高度 */public static void syntheticPicture(String imagePath, String signImagePath,Integer width,Integer height, String outImagePath ) { try { BufferedImage big = ImageIO.read(new File(imagePath)); BufferedImage small = ImageIO.read(new File(signImagePath)); Graphics2D g = big.createGraphics(); //根據(jù)圖片像素位置粘貼帶電子簽名 g.drawImage(small, width, height, small.getWidth(), small.getHeight(), null); g.dispose(); ImageIO.write(big, outImagePath .split('.')[1], new File(outImagePath )); } catch (Exception e) { throw new RuntimeException(e); }}
以上這篇Java pdf和jpg互轉(zhuǎn)案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. WML的簡(jiǎn)單例子及編輯、測(cè)試方法第1/2頁(yè)2. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂(lè)代碼3. XML基本概念XPath、XSLT與XQuery函數(shù)介紹4. el-input無(wú)法輸入的問(wèn)題和表單驗(yàn)證失敗問(wèn)題解決5. 關(guān)于html嵌入xml數(shù)據(jù)島如何穿過(guò)樹(shù)形結(jié)構(gòu)關(guān)系的問(wèn)題6. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)7. 不要在HTML中濫用div8. vue實(shí)現(xiàn)復(fù)制文字復(fù)制圖片實(shí)例詳解9. XML入門(mén)的常見(jiàn)問(wèn)題(三)10. XML入門(mén)的常見(jiàn)問(wèn)題(四)
