그림과 텍스트를 pdf 파일에 쓰기

iText 패키지를 주로 사용하는데jar 패키지는 첨부 파일에 있습니다
iText는 현재 bmp 형식의 그림을 지원하지 않기 때문에 pdf에 삽입할 때 변환해야 합니다.
변환 코드
 

 
  
  
  
  
  1. package com.taiji.lbs.register.util;  
  2.  
  3. import java.awt.Image;  
  4. import java.awt.Toolkit;  
  5. import java.awt.image.BufferedImage;  
  6. import java.awt.image.MemoryImageSource;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10. import com.sun.image.codec.jpeg.JPEGCodec;  
  11. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  12.  
  13. public class BmpToJpg {  
  14.  
  15.     /**  
  16.      *   BMP -> JPG  
  17.      * @param file  
  18.      * @param dstFile  
  19.      */ 
  20.     public static void bmpTojpg(String file, String dstFile) {  
  21.         try {  
  22.             FileInputStream in = new FileInputStream(file);  
  23.             Image TheImage = read(in);  
  24.             int wideth = TheImage.getWidth(null);  
  25.             int height = TheImage.getHeight(null);  
  26.             BufferedImage tag = new BufferedImage(wideth, height,BufferedImage.TYPE_INT_RGB);  
  27.             tag.getGraphics().drawImage(TheImage, 00, wideth, height, null);  
  28.             FileOutputStream out = new FileOutputStream(dstFile);  
  29.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  30.             encoder.encode(tag);  
  31.             out.close();  
  32.         } catch (Exception e) {  
  33.             System.out.println(e);  
  34.         }  
  35.     }  
  36.  
  37.     public static int constructInt(byte[] in, int offset) {  
  38.         int ret = ((int) in[offset + 3] & 0xff);  
  39.         ret = (ret << 8) | ((int) in[offset + 2] & 0xff);  
  40.         ret = (ret << 8) | ((int) in[offset + 1] & 0xff);  
  41.         ret = (ret << 8) | ((int) in[offset + 0] & 0xff);  
  42.         return (ret);  
  43.     }  
  44.  
  45.     public static int constructInt3(byte[] in, int offset) {  
  46.         int ret = 0xff;  
  47.         ret = (ret << 8) | ((int) in[offset + 2] & 0xff);  
  48.         ret = (ret << 8) | ((int) in[offset + 1] & 0xff);  
  49.         ret = (ret << 8) | ((int) in[offset + 0] & 0xff);  
  50.         return (ret);  
  51.     }  
  52.  
  53.     public static long constructLong(byte[] in, int offset) {  
  54.         long ret = ((long) in[offset + 7] & 0xff);  
  55.         ret |= (ret << 8) | ((long) in[offset + 6] & 0xff);  
  56.         ret |= (ret << 8) | ((long) in[offset + 5] & 0xff);  
  57.         ret |= (ret << 8) | ((long) in[offset + 4] & 0xff);  
  58.         ret |= (ret << 8) | ((long) in[offset + 3] & 0xff);  
  59.         ret |= (ret << 8) | ((long) in[offset + 2] & 0xff);  
  60.         ret |= (ret << 8) | ((long) in[offset + 1] & 0xff);  
  61.         ret |= (ret << 8) | ((long) in[offset + 0] & 0xff);  
  62.         return (ret);  
  63.     }  
  64.  
  65.     public static double constructDouble(byte[] in, int offset) {  
  66.         long ret = constructLong(in, offset);  
  67.         return (Double.longBitsToDouble(ret));  
  68.     }  
  69.  
  70.     public static short constructShort(byte[] in, int offset) {  
  71.         short ret = (short) ((short) in[offset + 1] & 0xff);  
  72.         ret = (short) ((ret << 8) | (short) ((short) in[offset + 0] & 0xff));  
  73.         return (ret);  
  74.     }  
  75.  
  76.     static class BitmapHeader {  
  77.         public int iSize, ibiSize, iWidth, iHeight, iPlanes, iBitcount,  
  78.                 iCompression, iSizeimage, iXpm, iYpm, iClrused, iClrimp;  
  79.  
  80.         //  bmp  
  81.         public void read(FileInputStream fs) throws IOException {  
  82.             final int bflen = 14;  
  83.             byte bf[] = new byte[bflen];  
  84.             fs.read(bf, 0, bflen);  
  85.             final int bilen = 40;  
  86.             byte bi[] = new byte[bilen];  
  87.             fs.read(bi, 0, bilen);  
  88.             iSize = constructInt(bf, 2);  
  89.             ibiSize = constructInt(bi, 2);  
  90.             iWidth = constructInt(bi, 4);  
  91.             iHeight = constructInt(bi, 8);  
  92.             iPlanes = constructShort(bi, 12);  
  93.             iBitcount = constructShort(bi, 14);  
  94.             iCompression = constructInt(bi, 16);  
  95.             iSizeimage = constructInt(bi, 20);  
  96.             iXpm = constructInt(bi, 24);  
  97.             iYpm = constructInt(bi, 28);  
  98.             iClrused = constructInt(bi, 32);  
  99.             iClrimp = constructInt(bi, 36);  
  100.         }  
  101.     }  
  102.  
  103.     public static Image read(FileInputStream fs) {  
  104.         try {  
  105.             BitmapHeader bh = new BitmapHeader();  
  106.             bh.read(fs);  
  107.             if (bh.iBitcount == 24) {  
  108.                 return (readImage24(fs, bh));  
  109.             }  
  110.             if (bh.iBitcount == 32) {  
  111.                 return (readImage32(fs, bh));  
  112.             }  
  113.             fs.close();  
  114.         } catch (IOException e) {  
  115.             System.out.println(e);  
  116.         }  
  117.         return (null);  
  118.     }  
  119.  
  120.     // 24  
  121.     protected static Image readImage24(FileInputStream fs, BitmapHeader bh)  
  122.             throws IOException {  
  123.         Image image;  
  124.         if (bh.iSizeimage == 0) {  
  125.             bh.iSizeimage = ((((bh.iWidth * bh.iBitcount) + 31) & ~31) >> 3);  
  126.             bh.iSizeimage *= bh.iHeight;  
  127.         }  
  128.         int npad = (bh.iSizeimage / bh.iHeight) - bh.iWidth * 3;  
  129.         int ndata[] = new int[bh.iHeight * bh.iWidth];  
  130.         byte brgb[] = new byte[(bh.iWidth + npad) * 3 * bh.iHeight];  
  131.         fs.read(brgb, 0, (bh.iWidth + npad) * 3 * bh.iHeight);  
  132.         int nindex = 0;  
  133.         for (int j = 0; j < bh.iHeight; j++) {  
  134.             for (int i = 0; i < bh.iWidth; i++) {  
  135.                 ndata[bh.iWidth * (bh.iHeight - j - 1) + i] = constructInt3(  
  136.                         brgb, nindex);  
  137.                 nindex += 3;  
  138.             }  
  139.             nindex += npad;  
  140.         }  
  141.         image = Toolkit.getDefaultToolkit().createImage(  
  142.                 new MemoryImageSource(bh.iWidth, bh.iHeight, ndata, 0,  
  143.                         bh.iWidth));  
  144.         fs.close();  
  145.         return (image);  
  146.     }  
  147.  
  148.     // 32  
  149.     protected static Image readImage32(FileInputStream fs, BitmapHeader bh)  
  150.             throws IOException {  
  151.         Image image;  
  152.         int ndata[] = new int[bh.iHeight * bh.iWidth];  
  153.         byte brgb[] = new byte[bh.iWidth * 4 * bh.iHeight];  
  154.         fs.read(brgb, 0, bh.iWidth * 4 * bh.iHeight);  
  155.         int nindex = 0;  
  156.         for (int j = 0; j < bh.iHeight; j++) {  
  157.             for (int i = 0; i < bh.iWidth; i++) {  
  158.                 ndata[bh.iWidth * (bh.iHeight - j - 1) + i] = constructInt3(  
  159.                         brgb, nindex);  
  160.                 nindex += 4;  
  161.             }  
  162.         }  
  163.         image = Toolkit.getDefaultToolkit().createImage(  
  164.                 new MemoryImageSource(bh.iWidth, bh.iHeight, ndata, 0,  
  165.                         bh.iWidth));  
  166.         fs.close();  
  167.         return (image);  
  168.     }  
  169.  
  170.     public static void main(String[] args) {  
  171.         String srcfile = "c:\\726.bmp";  
  172.         String dstFile = "c:\\726.jpg";  
  173.         bmpTojpg(srcfile, dstFile);  
  174.     }  
  175.  
  176. }  

코드 삽입을 보도록 하겠습니다.
 

 
  
  
  
  
  1. package com.taiji.lbs.register.util;  
  2.  
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.File;  
  5. import java.io.FileOutputStream;  
  6. import java.io.InputStream;  
  7. import java.util.List;  
  8.  
  9. import com.lowagie.text.Document;  
  10. import com.lowagie.text.DocumentException;  
  11. import com.lowagie.text.Image;  
  12. import com.lowagie.text.Paragraph;  
  13. import com.lowagie.text.pdf.BaseFont;  
  14. import com.lowagie.text.pdf.PdfWriter;  
  15. import com.taiji.core.util.ApplicationPath;  
  16. import com.taiji.core.util.PaginationSupport;  
  17. import com.taiji.lbs.register.hibernate.model.Picture;  
  18. import com.taiji.lbs.register.hibernate.model.RegisterInfo;  
  19.  
  20. public class CreatePDF {  
  21.     /**  
  22.      *  pdf  
  23.      *   
  24.      * @param list  
  25.      * @throws DocumentException   
  26.      * @throws Exception   
  27.      */ 
  28.     public static void createPDF(PaginationSupport list) throws Exception, DocumentException{  
  29.         // pdf  
  30.         List pdfList = list.getItems();  
  31.         String picName = "";  
  32.         String picNameDst = "";// bmp jpg  
  33.         String str = "";  
  34.         String rootPath = ApplicationPath.getRootPath().replaceAll("\\\\","\\\\\\\\");//     
  35.         //  
  36.          Document doc = new Document();  
  37.          PdfWriter.getInstance(doc, new FileOutputStream("c:/hello.pdf"));  
  38.         //   
  39.         doc.open();  
  40.         // , windos  
  41.         BaseFont basefont;  
  42.         com.lowagie.text.Font   FontChinese ;  
  43.         basefont = BaseFont.createFont("c:\\windows\\fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);  
  44.         FontChinese   =   new   com.lowagie.text.Font(basefont);     
  45.         for (Object object : pdfList) {  
  46.             Picture pic = ((RegisterInfo)object).getPicture();  
  47.             if (pic != null) {  
  48.                 picName = String.valueOf(pic.getId() + ".bmp");  
  49.                 picNameDst = String.valueOf(pic.getId() + ".jpg");  
  50.                 File filePic = new File(rootPath + "\\photo\\" + picName);  
  51.                 FileOutputStream output;  
  52.                 output = new FileOutputStream(filePic);  
  53.                 byte buffer[] = null;  
  54.                 if (pic.getPhoto() != null) {  
  55.                     buffer = pic.getPhoto();  
  56.                     InputStream in = new ByteArrayInputStream(buffer);  
  57.                     int len;  
  58.                     while ((len = in.read(buffer)) > 0) {  
  59.                         output.write(buffer, 0, len);  
  60.                     }  
  61.                     output.close();  
  62.                     in.close();  
  63.                 } else {  
  64.                     picName = null;  
  65.                 }  
  66.             }  
  67.             BmpToJpg.bmpTojpg(rootPath + "\\photo\\" + picName, rootPath + "\\photo\\" + picNameDst);  
  68.             //  
  69.             Image jpg = Image.getInstance(rootPath + "\\photo\\" + picNameDst);  
  70.             jpg.setAlignment(Image.ALIGN_LEFT);  
  71.             doc.add(jpg);  
  72.             //  
  73.             str = ((RegisterInfo)object).getId()+":"+((RegisterInfo)object).getIdNum();  
  74.             Paragraph tt = new Paragraph(str, FontChinese);  
  75.             tt.setAlignment(Paragraph.ALIGN_CENTER);  
  76.             doc.add(tt);  
  77.         }  
  78.         //      
  79.         doc.close();  
  80.     }  
  81. }  

이렇게 하면 그림 형식의 변환과 삽입이 완성되었다.
===============================================================
이전의 그 예는 그림과 텍스트가 쉽게 분리된다. 더욱 보기 좋게 그림과 텍스트를 한 표에 넣기 위해 일부 코드를 다음과 같이 수정한다.

 
  
  
  
  
  1. BmpToJpg.bmpTojpg(rootPath + "\\photo\\" + picName, rootPath + "\\photo\\" + picNameDst);  
  2.             //  
  3.             Image jpg = Image.getInstance(rootPath + "\\photo\\" + picNameDst);  
  4.             jpg.setAlignment(Image.ALIGN_LEFT);  
  5.             //doc.add(jpg);  
  6.             //  
  7.             str = ((RegisterInfo)object).getChineseName()+" "+((RegisterInfo)object).getEnglishName()+" "+((RegisterInfo)object).getNationality()+" "+((RegisterInfo)object).getIdNum()+" "+((RegisterInfo)object).getDiplomaticTitle();  
  8.             Paragraph tt = new Paragraph(str, FontChinese);  
  9.             tt.setAlignment(Paragraph.ALIGN_RIGHT);  
  10.             //doc.add(tt);  
  11.               
  12.               
  13.             // 1  
  14.             PdfPTable table = new PdfPTable(1);  
  15.             //  
  16.             PdfPCell cell = new PdfPCell(jpg);  
  17.             //  
  18.             table.addCell(cell);  
  19.             //  
  20.             PdfPCell cellText = new PdfPCell(tt);  
  21.             //  
  22.             table.addCell(cellText);  
  23.             //  
  24.             doc.add(table); 

 
본문 은 '조레이 의 블로그 학습 진보' 블로그 에서 나온 것 이니, 반드시 이 출처 를 보존해 주십시오http://sucre.blog.51cto.com/1084905/554921

좋은 웹페이지 즐겨찾기