package com.pdf.util; import java.awt.Image; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.PageSize; import com.lowagie.text.pdf.PdfWriter; import com.sun.image.codec.jpeg.ImageFormatException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; import com.sun.pdfview.PDFFile; import com.sun.pdfview.PDFPage; @SuppressWarnings("restriction") public class ImagesPDFUtil { /** * 图片转换为PDF * @param imagePath * @param mOutputPdfFileName * @return */ public File jpg2Pdf(String imagePath, String mOutputPdfFileName) { Document doc = new Document(PageSize.A4, 20, 20, 20, 20); try { PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream( mOutputPdfFileName)); doc.open(); doc.newPage(); com.lowagie.text.Image png1 = com.lowagie.text.Image .getInstance(imagePath); float heigth = png1.getHeight(); float width = png1.getWidth(); int percent = this.getPercent2(heigth, width); png1.setAlignment(com.lowagie.text.Image.MIDDLE); png1.setAlignment(com.lowagie.text.Image.TEXTWRAP); png1.scalePercent(percent + 3); doc.add(png1); doc.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } File mOutputPdfFile = new File(mOutputPdfFileName); if (!mOutputPdfFile.exists()) { mOutputPdfFile.deleteOnExit(); return null; } System.out.println(mOutputPdfFile); return mOutputPdfFile; } /** * 图片处理 * @param h * @param w * @return */ private int getPercent2(float h, float w) { int p = 0; float p2 = 0.0f; p2 = 530 / w * 100; p = Math.round(p2); return p; } /** * pdf文件转换为jpg文件 * * @param pdfFile * @param jpgFile * @throws IOException */ public void pdfjpg(String pdfFile, String jpgFile){ try { File file = new File(pdfFile); RandomAccessFile raf = new RandomAccessFile(file, "r"); System.out.println(raf); FileChannel channel = raf.getChannel(); ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); PDFFile pdffile = new PDFFile(buf); System.out.println("页数: " + pdffile.getNumPages()); int with = 0; int height = 0; for (int i = 1; i <= pdffile.getNumPages(); i++) { // draw the first page to an image PDFPage page = pdffile.getPage(i); with = (int) page.getBBox().getWidth(); height += (int) page.getBBox().getHeight(); } Rectangle rect = new Rectangle(0, 0, with, height); BufferedImage tag = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB); for (int j = 1; j <= pdffile.getNumPages(); j++) { PDFPage page = pdffile.getPage(j); Rectangle rect1 = new Rectangle(0, 0, (int) page.getBBox() .getWidth(), (int) page.getBBox().getHeight()); Image img = page.getImage(rect1.width, rect1.height, // width & // height rect1, // clip rect null, // null for the ImageObserver true, // fill background with white true // block until drawing is done ); tag.getGraphics().drawImage(img, 0, rect1.height * (j - 1), rect1.width, rect1.height, null); } FileOutputStream out = new FileOutputStream(jpgFile); // 输出到文件流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); // JPEG编码 out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ImageFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // new ImagesPDFUtil().jpg2Pdf("E:\\verifies\\4jyb.jpg", "E:\\verifies\\4jyb.pdf"); // new ImagesPDFUtil().pdfjpg("E:\\verifies\\4jyb.pdf","E:\\verifies\\111.jpg"); } }

Sisphyus LV1
2022年2月17日
我的最代码 LV20
2022年1月14日
xuexizhuanyong23 LV16
2021年9月24日
xmjying LV13
2020年3月19日
lsongssl LV2
2019年6月14日
DU-ZB LV13
2019年3月27日
麻辣小龙虾 LV11
2019年3月12日
zhangshu6682 LV7
2019年1月6日
tanyunqing LV8
2018年7月11日
gggbbbb LV1
2018年6月12日