Java 소스 오픈 도구 iText PDF 단순 인스턴스 생성

iText 다운로드 페이지:http://sourceforge.net/projects/itext/files/
1. 간단한 PDF 파일 만들기

package console.pdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

/**
 *  iText PDF 
 */
public class CreatePDF {

  public static void main(String[] args) {
    CreatePDF p001 = new CreatePDF();

    String filename = "P001.pdf";
    p001.createPDF(filename);
  }

  public void createPDF(String filename) {
    // step 1
    Document document = new Document(PageSize.A4);
    // step 2
    try {
      PdfWriter.getInstance(document, new FileOutputStream(filename));
      
      document.addTitle("ID.NET");
      document.addAuthor("dotuian"); 
      document.addSubject("This is the subject of the PDF file."); 
      document.addKeywords("This is the keyword of the PDF file.");
      
      // step 3
      document.open();
      // step 4
      document.add(new Paragraph("Hello World!"));

      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    } finally {
      // step 5
      document.close();
    }
  }

  
  
  
}

2. PDF 파일에 테이블 추가

package console.pdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

/**
 *  iText PDF 
 *  PDF 
 */
public class TableOfPDF {

  public static void main(String[] args) {
    TableOfPDF p001 = new TableOfPDF();

    String filename = "P002.pdf";
    p001.createPDF(filename);
  }

  public void createPDF(String filename) {
    // step 1
    Document document = new Document(PageSize.A4);
    // step 2
    try {
      PdfWriter.getInstance(document, new FileOutputStream(filename));

      document.addTitle("ID.NET");
      document.addAuthor("dotuian");
      document.addSubject("This is the subject of the PDF file.");
      document.addKeywords("This is the keyword of the PDF file.");

      // step 3
      document.open();
      // step 4
      PdfPTable table = createTable1();
      document.add(table);
      
      table = createTable2();
      table.setSpacingBefore(5);
      table.setSpacingAfter(5);
      document.add(table);
      
      table = createTable3();
      document.add(table);
      
      table = createTable4();
      table.setSpacingBefore(5);
      table.setSpacingAfter(5);
      document.add(table);
      
      table = createTable5();
      document.add(table);
      
      table = createTable6();
      document.add(table);

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    } finally {
      // step 5
      document.close();
    }
  }

  /**
   * Creates a table; widths are set with setWidths().
   * 
   * @return a PdfPTable
   * @throws DocumentException
   */
  public static PdfPTable createTable1() throws DocumentException {
    PdfPTable table = new PdfPTable(3);
    table.setWidthPercentage(288 / 5.23f);
    table.setWidths(new int[] { 2, 1, 1 });
    
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Table 1"));
    cell.setColspan(3);
    table.addCell(cell);
    
    cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
    cell.setRowspan(2);
    table.addCell(cell);
    table.addCell("row 1; cell 1");
    table.addCell("row 1; cell 2");
    table.addCell("row 2; cell 1");
    table.addCell("row 2; cell 2");
    return table;
  }

  /**
   * Creates a table; widths are set with setWidths().
   * 
   * @return a PdfPTable
   * @throws DocumentException
   */
  public static PdfPTable createTable2() throws DocumentException {
    PdfPTable table = new PdfPTable(3);
    table.setTotalWidth(288);
    table.setLockedWidth(true);
    table.setWidths(new float[] { 2, 1, 1 });
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Table 2"));
    cell.setColspan(3);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
    cell.setRowspan(2);
    table.addCell(cell);
    table.addCell("row 1; cell 1");
    table.addCell("row 1; cell 2");
    table.addCell("row 2; cell 1");
    table.addCell("row 2; cell 2");
    return table;
  }

  /**
   * Creates a table; widths are set in the constructor.
   * 
   * @return a PdfPTable
   * @throws DocumentException
   */
  public static PdfPTable createTable3() throws DocumentException {
    PdfPTable table = new PdfPTable(new float[] { 2, 1, 1 });
    table.setWidthPercentage(55.067f);
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Table 3"));
    cell.setColspan(3);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
    cell.setRowspan(2);
    table.addCell(cell);
    table.addCell("row 1; cell 1");
    table.addCell("row 1; cell 2");
    table.addCell("row 2; cell 1");
    table.addCell("row 2; cell 2");
    return table;
  }

  /**
   * Creates a table; widths are set with special setWidthPercentage() method.
   * 
   * @return a PdfPTable
   * @throws DocumentException
   */
  public static PdfPTable createTable4() throws DocumentException {
    PdfPTable table = new PdfPTable(3);
    Rectangle rect = new Rectangle(523, 770);
    table.setWidthPercentage(new float[] { 144, 72, 72 }, rect);
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Table 4"));
    cell.setColspan(3);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
    cell.setRowspan(2);
    table.addCell(cell);
    table.addCell("row 1; cell 1");
    table.addCell("row 1; cell 2");
    table.addCell("row 2; cell 1");
    table.addCell("row 2; cell 2");
    return table;
  }

  /**
   * Creates a table; widths are set with setTotalWidth().
   * 
   * @return a PdfPTable
   * @throws DocumentException
   */
  public static PdfPTable createTable5() throws DocumentException {
    PdfPTable table = new PdfPTable(3);
    table.setTotalWidth(new float[] { 144, 72, 72 });
    table.setLockedWidth(true);
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Table 5"));
    cell.setColspan(3);
    table.addCell(cell);
    cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
    cell.setRowspan(2);
    table.addCell(cell);
    table.addCell("row 1; cell 1");
    table.addCell("row 1; cell 2");
    table.addCell("row 2; cell 1");
    table.addCell("row 2; cell 2");
    return table;
  }
  
  public static PdfPTable createTable6() throws DocumentException{
    PdfPTable table = new PdfPTable(10);
    table.setTotalWidth(595);
    //table.setLockedWidth(true);
    
    
    PdfPCell cell;
    cell = new PdfPCell(new Phrase("Table 6"));
    cell.setColspan(10);
    table.addCell(cell);
    
    for (int i = 1; i < 100; i++) {
      cell = new PdfPCell(new Phrase(String.valueOf(i)));
      cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
      table.addCell(cell);
    }
    
    
//    cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
//    cell.setRowspan(2);
//    table.addCell(cell);
//    table.addCell("row 1; cell 1");
//    table.addCell("row 1; cell 2");
//    table.addCell("row 2; cell 1");
//    table.addCell("row 2; cell 2");
    return table;
  }
  
  

}

3. PDF 파일에 그림을 추가하고 텍스트 위치를 지정합니다.

package console.pdf;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

/**
 *  iText PDF 
 *  PDF , PDF 
 */
public class BackgroundImageOfPDF {

  public static void main(String[] args) {
    BackgroundImageOfPDF p001 = new BackgroundImageOfPDF();

    String filename = "P003.pdf";
    p001.createPDF(filename);
  }

  public void createPDF(String filename) {
    // step 1
    Document document = new Document(PageSize.A4.rotate(),0,0,0,0);
    // step 2
    try {
      PdfWriter pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(filename));
      
      document.addTitle("ID.NET");
      document.addAuthor("dotuian"); 
      document.addSubject("This is the subject of the PDF file."); 
      document.addKeywords("This is the keyword of the PDF file.");
      
      // step 3
      document.open();
      // step 4
      Image image = Image.getInstance("bg.jpg");
      document.add(image);

      PdfContentByte pdfContentByte = pdfwriter.getDirectContent();
      pdfContentByte.beginText();
      BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,BaseFont.WINANSI,BaseFont.EMBEDDED);
      pdfContentByte.setFontAndSize(bf, 12);

      for (int i = 0; i <= 842; i = i + 50) {
        for (int j = 0; j <= 595; j = j + 20) {
          pdfContentByte.setTextMatrix(i, j);
          pdfContentByte.showText("(" + i + ":" + j + ")");
        }
      }
      
      pdfContentByte.endText();
      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (DocumentException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // step 5
      document.close();
    }
  }
}




좋은 웹페이지 즐겨찾기