java完成Img与PDF彼此转化ITeye - 乐橙lc8

java完成Img与PDF彼此转化ITeye

2019-01-11 19:23:43 | 作者: 昌翰 | 标签: 文件,方针,彼此 | 浏览: 168

java完结Img与PDF彼此转化

http://blog.csdn.net/hubiao_0618/article/details/29226883?utm_source=tuicool utm_medium=referral

https://github.com/itext/itextpdf 完结

 

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import com.Utils.ImgFileTool;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
 * @author hubiao
 * @dateTime 2014-06-07
 * 本东西对完结对IMG与PDF彼此转化。
 * 运转测验需求导入以下2个jar包
 * itext-2.0.2.jar 
 * PDFRenderer.jar
@SuppressWarnings("unused")
public class ImgPdfUtils {
 public static void main(String[] args) throws Exception {
 //PDF包提取 pdf
 //pdfExtraction();
 //pdf转jpg
 //pdfToJpg("E:\\java\\材料pdf\\1.pdf","E:\\java\\材料pdf\\1.jpg",1);
 //将多个jpg直接兼并成pdf包
 //extractionPdf("F:\\temp\\Project\\数据\\dfdsfds\\巴黎公社活动家传略_img","F:\\temp\\Project\\数据\\dfdsfds\\巴黎公社活动家传略_img.pdf");
 //jpg转pdf
 //jpgToPdf();
 //文件排序
 //listOrder();
 ImgFileTool.imgMerageToPdf(new File("F:\\temp\\Project\\数据\\dfdsfds\\巴黎公社活动家传略_img").listFiles(),new File("F:\\temp\\Project\\数据\\dfdsfds\\","巴黎公社活动家传略.pdf"));
 private static void listOrder() {
 File[] listFiles = new File("F:\\temp\\Project\\数据\\dfdsfds\\巴黎公社活动家传略_img").listFiles();
 TreeMap Integer, File tree = new TreeMap Integer, File 
 for(File f : listFiles)
 tree.put(Integer.parseInt(f.getName().replaceAll(".jpg$", "")), f);
 for(Entry Integer, File eif : tree.entrySet())
 System.out.println(eif.getKey()+"="+eif.getValue().toString());
 /** 
 * @param list 图片调集
 * @param file 保存途径
 * @return true,兼并完结
 * 假如文件名不是1.jpg,2.jpg,3.jpg,4.jpg这样的。则需求自己重写TreeMap的排序方法!
 public static boolean imgMerageToPdf(File[] list, File file)throws Exception {
 //1:对图片文件经过TreeMap以称号进行天然排序
 Map Integer,File mif = new TreeMap Integer,File 
 for(File f : list)
 mif.put(Integer.parseInt(f.getName().replaceAll(".jpg$", "")), f);
 //2:获取第一个Img的宽、高做为PDF文档规范
 ByteArrayOutputStream baos = new ByteArrayOutputStream(2048*3);
 InputStream is = new FileInputStream(mif.get(1));
 for(int len;(len=is.read())!=-1;)
 baos.write(len);
 baos.flush();
 Image image = Image.getInstance(baos.toByteArray());
 float width = image.width();
 float height = image.height();
 baos.close();
 //3:经过宽高 ,实例化PDF文档方针。
 Document document = new Document(new Rectangle(width,height));
 PdfWriter pdfWr = PdfWriter.getInstance(document, new FileOutputStream(file));
 document.open();
 //4:获取每一个图片文件,转为IMG方针。装载到Document方针中
 for(Entry Integer,File eif : mif.entrySet())
 //4.1:读取到内存中
 baos = new ByteArrayOutputStream(2048*3);
 is = new FileInputStream(eif.getValue());
 for(int len;(len=is.read())!=-1;)
 baos.write(len);
 baos.flush();
 //4.2经过byte字节生成IMG方针
 image = Image.getInstance(baos.toByteArray());
 Image.getInstance(baos.toByteArray());
 image.setAbsolutePosition(0.0f, 0.0f);
 //4.3:添加到document中
 document.add(image);
 document.newPage();
 baos.close();
 //5:开释资源
 document.close();
 pdfWr.close();
 return true;
 * @param source 源文件
 * @param target 方针文件
 * @param x 读取源文件中的第几页
 private static void pdfToJpg(String source,String target,int x) throws Exception {
 //创立从中读取和向其间写入(可选)的随机拜访文件流,R表明对其仅仅拜访形式
 RandomAccessFile rea = new RandomAccessFile(new File(source), "r");
 //将流读取到内存中,然后还映射一个PDF方针
 FileChannel channel = rea.getChannel();
 ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,0, channel.size());
 PDFFile pdfFile = new PDFFile(buf);
 PDFPage page = pdfFile.getPage(x); 
 // get the width and height for the doc at the default zoom 
 java.awt.Rectangle rect = new java.awt.Rectangle(0, 0, (int) page.getBBox() 
 .getWidth(), (int) page.getBBox().getHeight()); 
 // generate the image 
 java.awt.Image img = page.getImage(rect.width, rect.height, // width 
 rect, // clip rect
 null, // null for the ImageObserver
 true, // fill background with white
 true // block until drawing is done
 BufferedImage tag = new BufferedImage(rect.width, rect.height, 
 BufferedImage.TYPE_INT_RGB); 
 tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height, 
 null); 
 FileOutputStream out = new FileOutputStream(target); // 输出到文件流 
 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
 encoder.encode(tag); // JPEG编码 
 out.close(); 
 * @param source 源PDF文件途径
 * @param target 保存PDF文件途径
 * @param pageNum 提取PDF中第pageNum页
 * @throws Exception 
 private static void pdfExtraction(String source,String target,int pageNum) throws Exception{
 //1:创立PDF读取方针
 PdfReader pr = new PdfReader(source);
 System.out.println("this document "+pr.getNumberOfPages()+" page");
 //2:将第page页转为提取,创立document方针
 Document doc = new Document(pr.getPageSize(pageNum));
 //3:经过PdfCopy转其独自存储
 PdfCopy copy = new PdfCopy(doc, new FileOutputStream(new File(target)));
 doc.open();
 doc.newPage();
 //4:获取第1页,装载到document中。
 PdfImportedPage page = copy.getImportedPage(pr,pageNum);
 copy.addPage(page); 
 //5:开释资源
 copy.close();
 doc.close();
 pr.close();
 * @param pdfFile 源PDF文件
 * @param imgFile 图片文件
 private static void jpgToPdf(File pdfFile,File imgFile) throws Exception {
 //文件转img
 InputStream is = new FileInputStream(pdfFile);
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 for(int i;(i=is.read())!=-1;)
 baos.write(i);
 baos.flush();
 //获得图画的宽和高。
 Image img = Image.getInstance(baos.toByteArray());
 float width = img.width();
 float height = img.height();
 img.setAbsolutePosition(0.0F, 0.0F);//撤销偏移
 System.out.println("width = "+width+"\theight"+height);
 //img转pdf
 Document doc = new Document(new Rectangle(width,height));
 PdfWriter pw = PdfWriter.getInstance(doc,new FileOutputStream(imgFile));
 doc.open();
 doc.add(img);
 //开释资源
 System.out.println(doc.newPage());
 pw.flush();
 baos.close();
 doc.close();
 pw.close();

 

 

 

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表乐橙lc8立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章