본문 바로가기
Programming/java

itext 를 이용한 PDF 파일 만들기

by 막이 2013. 10. 22.

* 주의 2.x 버전과 현재 최신인 5.x 버전의 사용법이 다르다.

 

 

 

import  java.io.FileOutputStream;

import  java.io.IOException;

 

import  com.lowagie.text.*;            // 5.x 버전은  com.itextpdf.text 이다.

import  com.lowagie.text.pdf.*;

 

 

public class Pdf{

 

     public static void deleteFile(Requestinfo  info) throws  java.lang.Exception{

            String title = "PDF 제목";

            String filePath = "/home/WWWRoot/jsp/upload/" + title + ".pdf";

     } //deleteFile

 

     public void pdfText(RequestInfo info) throws DocumentException{

             Document  document = new Document(PageSize.A4, 20, 20, 30, 30) 

                                                                      // 페이지 사이즈와 좌우상하 여백

             try{

                   String  title = "PDF 제목";

 

                   java.io.File  folder = new java.io.File("/home/WWWRoot/jsp/upload/" + title + ".pdf";

                             if(! folder.exists()){

                                    folder.mkdir();

                             }

                   String  filePath = "/home/WWWRoot/jsp/upload/

                   

                   PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));

                   document.open();

 

                   BaseFont bf = BaseFont.createFont("HTGoThic-Medium", "UniKS-H", BaseFont.NOT_EMBEDDED);

                                                   // 5.x 와 사용법이 다르다. 5.x 버전은 폰트파일의 경로를 사용한다.

                   Font titleFont = new Font(bf, 16, Font.BOLD);

                   Font nomalFont = new Font(bf, 10);

                   Font nomalBoldFont = new Font(bf, 10, Font.BOLD);

                   Paragraph titleTxt = new Paragraph(title, titleFont);

                   titleTxt.setAlignment("center");

                   titleTxt.setSpacingAfter(30);

                   document.add(titleTxt);

 

                   //주간업무보고서         
                  PdfTableEvents event = new PdfTableEvents(1.2f,0,0,0); 
                  PdfPTable table = new PdfPTable(4);               //테이블의 열 수를 지정한다.
                  table.setHorizontalAlignment(Element.ALIGN_CENTER);
                  table.setTotalWidth(500f);    //table width
                  //table.setLockedWidth(true);
                  table.setTableEvent(event); //table border

 

                   // line1

                   PdfCell cell = new PdfCell(new Paragraph("이 름", nomalFont));

                   cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                   cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
                   cell.setFixedHeight(25);
                   table.addCell(cell); 

 

                   cell = new PdfPCell(new Paragraph(info.get("MEMBER_NM"),nomalFont));
                   cell.setVerticalAlignment(Element.ALIGN_MIDDLE);     
                   table.addCell(cell);


                   cell = new PdfPCell(new Paragraph("신청자",nomalFont));  
                   cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                   cell.setVerticalAlignment(Element.ALIGN_MIDDLE);    
                   table.addCell(cell);
                  

                   cell = new PdfPCell(new Paragraph(info.get("supply_man"),nomalFont));  
                   cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                   table.addCell(cell);

                

               document.add(table);

 

               }catch(Exection e){

                        document.close();

               }

 

 

            } //pdfText

  

//class

 

 

 

 

////////////////////////////////////////////////////////////////////////////////////////////////////////

 

 

 

package com.iwe.common.util.itext;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;


public class PdfTableEvents implements PdfPTableEvent{
 
 private int[] strokedBorderIdx;
 private float borderedWidth; 
 private int borderedColor1; 
 private int borderedColor2; 
 private int borderedColor3;   

 

 public PdfTableEvents(float borderWidth,int borderColor1,int borderColor2,int borderColor3){
     borderedWidth = borderWidth;
     borderedColor1 = borderColor1;
     borderedColor2 = borderColor2;
     borderedColor3 = borderColor3;     
 } 
 

 public int[] getStrokedBorderIdx(){ 
     return strokedBorderIdx;
 }
 
 public void setStrokedBorderIdx(int[] strokedBorderIdx){  
     this.strokedBorderIdx = strokedBorderIdx;
 }

  
 
 public void tableLayout(PdfPTable table,float[][] widths, float[] heights,int headerRows,

                                  int rowStart,PdfContentByte[] canvases){
              System.out.println("tableLayout----------------------------------------------------------");
              System.out.println("strokedBorderIdx=========================="+strokedBorderIdx);  
  

           float width[] = widths[0];
           float x = width[0];            //x축 좌표
           float y = heights[heights.length - 1];     //y축 좌표
           float w = width[width.length - 1] - width[0];  //테이블넓이
           float h = heights[0] - heights[heights.length - 1]; //테이블높이
           PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS];
           cb.saveState();
           cb.setLineWidth(borderedWidth); //border
           //cb.setRGBColorStroke(0,0,0); //border color
           cb.setRGBColorStroke(borderedColor1,borderedColor2,borderedColor3); //border color
           cb.rectangle(x,y,w,h);
           cb.stroke();
           //cb = canvases[PdfPTable.BASECANVAS];
           //cb.saveState();
  
          if(strokedBorderIdx != null){
                   for(int i = 0; i < strokedBorderIdx.length; i++){
                         float[] bp = getBorderPosition(widths,heights,strokedBorderIdx[i]);
                         cb.rectangle(bp[0],bp[1],bp[2],bp[3]);
                         cb.stroke();
                   //for
         //if
 } //tableLayout
 
 

/**
 *굵기를 설정 할 Rectangle의 좌표 및 사이즈를 얻어온다.
 **/
 private float[] getBorderPosition(float[][] widths,float[] heights,int index){
               System.out.println("getBorderPosition----------------------------------------------------");   
        float[] borderPosition = new float[4];
        float width[] = widths[0];
  
        float x = width[0];
        float y = heights[heights.length - 1];
        float w = width[width.length - 1] - width[0];
        float h = heights[index] - heights[heights.length - 1];
  
        borderPosition[0] = x;
        borderPosition[1] = y;
        borderPosition[2] = w;
        borderPosition[3] = h;
  
        return borderPosition;
  } //getBorderPosition

//class

 


'Programming > java' 카테고리의 다른 글

POI 셀병합 예제  (0) 2014.02.20
POI(엑셀라이브러리)  (0) 2014.02.20
도메인 명 알아내기  (0) 2013.06.04
java 한글깨짐 인코딩 테스트  (0) 2013.05.16
[PUSH] iOS 푸쉬 서비스  (0) 2013.03.20