pdf 생성 라이브러리 itextpdf jar

 

import java.io.File;

import org.terracotta.agent.repkg.de.schlichtherle.io.FileOutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TestPdfCreate {

 public void pdfCreate() throws Exception {
  String[] nameArr = {"Mr.Lee", "Mr.Kim", "Mr.Bae", "Mr.Hong"};
  String[] ageArr = {"41", "25", "16", "30"};
  String fileName = "";
  String dir = "E:/Test/Pdf";
  fileName = "testFile.pdf";
  
  File directory = new File(dir);
  if(!directory.exists()) directory.mkdirs();
  File file = new File(dir+"/"+fileName);
  file.createNewFile();
  
  Document document = new Document();
  FileOutputStream fos = new FileOutputStream(file);
  PdfWriter.getInstance(document, fos);
  
  document.open();
  PdfPTable table = new PdfPTable(2);
  
  for(int i = 0 ; i < nameArr.length ; i++){
   table.addCell("name : " + nameArr[i]);
   table.addCell("age : " + ageArr[i]);
  }
  
  document.add(table);
  document.close();
 }
 
 public static void main(String[] args) {
  try {
   TestPdfCreate pbv = new TestPdfCreate();
   pbv.pdfCreate();
   System.out.println("file success");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

반응형

+ Recent posts