一、前言
通过itext包com.lowagie.text.pdf.PdfReader/PdfWriter、com.lowagie.text.Image及com.lowagie.text.Document等定义PDFPrintTool工具类,主要实现了多个PDF文件合并为一个完整pdf文档、将图片转换为PDF文档的代码示例。
二、代码示例
package pdf;@b@@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@@b@import com.lowagie.text.Document;@b@import com.lowagie.text.Image;@b@import com.lowagie.text.PageSize;@b@import com.lowagie.text.Paragraph;@b@import com.lowagie.text.Rectangle;@b@import com.lowagie.text.pdf.PdfCopy;@b@import com.lowagie.text.pdf.PdfImportedPage;@b@import com.lowagie.text.pdf.PdfReader;@b@import com.lowagie.text.pdf.PdfWriter;@b@@b@public class PDFPrintTool{@b@ @b@ /**@b@ * 合并PDF@b@ * @param files@b@ * @param newfile@b@ * @return@b@ */@b@ public static boolean mergePdfFiles(String[] files, String newfile) {@b@ boolean retValue = false;@b@ Document document = null;@b@ FileOutputStream fos = null;@b@ PdfReader pr = null;@b@ try {@b@ document = new Document(new PdfReader(files[0]).getPageSize(1));@b@ fos = new FileOutputStream(newfile);@b@ PdfCopy copy = new PdfCopy(document, fos);@b@ document.open();@b@ for (int i = 0; i < files.length; i++) {@b@ PdfReader reader = new PdfReader(files[i]);@b@ int n = reader.getNumberOfPages();@b@ for (int j = 1; j <= n; j++) {@b@ document.newPage();@b@ PdfImportedPage page = copy.getImportedPage(reader, j);@b@ copy.addPage(page);@b@ }@b@ reader.close();@b@ }@b@ retValue = true;@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ } finally {@b@ if(document != null) document.close();@b@ try {@b@ fos.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@ return retValue;@b@ } @b@ @b@ /**@b@ * 图片转PDF@b@ * @param imgPath 图片路径@b@ * @param pdfPath 生成PDF路径@b@ * @param size PDF大小 (PageSize.A3.rotate(),PageSize.A3, PageSize.A4.rotate(), PageSize.A4)@b@ * @param pdfWidth 宽度@b@ * @param height 高度@b@ * @param type 是否加空白页 (0为不加空白页,1为添加空白页)@b@ * @throws Exception@b@ */@b@ public static void convertImagesToPDF(String[] imgPath,String pdfPath, Rectangle size, int pdfWidth, int height, int type) throws Exception {@b@ float scale=Float.parseFloat("0.656");@b@ FileOutputStream out = new FileOutputStream(pdfPath);@b@ Document document = new Document(size);@b@ document.setMargins(0, 0, 0, 0);@b@ try{@b@ PdfWriter.getInstance(document, out);@b@ document.open();@b@ document.newPage();@b@ Image image;@b@ for(String img:imgPath){@b@ int i = 0;@b@ image= Image.getInstance(img);@b@ @b@ if(image.getWidth()>image.getHeight()){@b@ scale = pdfWidth/image.getWidth();@b@ } else {@b@ scale = height/image.getHeight(); @b@ }@b@ image.scalePercent(scale*100);@b@ image.setAlignment(Image.LEFT);@b@ document.add(image);@b@ if (type == 1) {@b@ document.newPage();@b@ //Image image = Image.getInstance(null);@b@ Paragraph text=new Paragraph("空白页"); @b@ document.add(text);@b@ }@b@ i++;@b@ }@b@ } catch (Exception e){@b@ e.printStackTrace();@b@ if(out != null) out.close();@b@ } finally {@b@ out.flush();@b@ document.close();@b@ out.close();@b@ }@b@ }@b@ @b@ public static void main(String[] args) throws Exception {@b@ //合并pdf@b@ mergePdfFiles(new String[]{"C:/temp/BAMySQL1111.pdf","C:/temp/MySQL-Cluster222.pdf"},"C:/temp/mysql-all333.pdf");@b@ //图片转换pdf@b@ convertImagesToPDF(new String[]{"C:/temp/txmn1027_0.jpg"},"C:/temp/txmln1027_0.pdf",PageSize.A4,604,824,0);@b@ } @b@ @b@}
效果如下图所示