pdfbox: jpg转pdf:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/** * 使用pdfbox将jpg转成pdf * @param jpgStream jpg输入流 * @param pdfPath pdf文件存储路径 * @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转jpg:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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文件转出路径 FileUtil.writeBytes(datas, new File( "d:/jpg/" + i + ".jpg" )); } long end = System.currentTimeMillis(); long time = (end - start) / 1000 ; System.out.println(StrUtil.format( "pdf转jpg耗时: {}s" , time)); } |
icepdf: pdf转jpg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
Document document = new Document(); document.setUrl( new URL(pdfUrl)); int pageNum = document.getNumberOfPages(); for ( int i = 0 ; i < pageNum; i++) { // 目前仅支持1对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); |
我就废话不多说了,大家还是直接看代码吧~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/** * 根据图片像素位置添加用户电子签名 * @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(); //根据图片像素位置粘贴带电子签名 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互转案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xqnode/article/details/80525995