* itext 라이브러리 사용
- http://itextpdf.com
- API : http://developers.itextpdf.com/examples-itext5
- 한글 처리를 위해서는 폰트 정보가 필요함
* pom.xml 에 라이브러리 추가
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-pdfa</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-xtra</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.9</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
* 예제 코드
// step 1
Document document = new Document();
// step 2
PdfWriter writer =
PdfWriter.getInstance(document, new FileOutputStream("d:/sample.pdf"));
// step 3
document.open();
// WEB-INF/font 디렉토리에 배치, 리소스 설정
BaseFont baseFont = BaseFont.createFont("c:/windows/fonts/H2PORL.TTF"
, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(baseFont, 12);
// step 4
PdfPTable table = new PdfPTable(4);
// title
Chunk chunk = new Chunk("한줄메모장", font);
Paragraph ph = new Paragraph(chunk);
ph.setAlignment(Element.ALIGN_CENTER);
document.add(ph);
document.add(Chunk.NEWLINE);
document.add(Chunk.NEWLINE);
// table header
PdfPCell cell1 = new PdfPCell(new Phrase("번호", font));
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase("이름", font));
cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Phrase("메모", font));
cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell3);
PdfPCell cell4 = new PdfPCell(new Phrase("날짜", font));
cell4.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell4);
table.setHeaderRows(1);
// many data rows:
List<MemoDTO> list = memoDao.getMemoList();
for (int i = 1; i < list.size(); i++) {
MemoDTO dto = list.get(i);
table.addCell("" + dto.getIdx());
PdfPCell cellWriter = new PdfPCell(new Phrase(dto.getWriter(), font));
table.addCell(cellWriter);
PdfPCell cellMemo = new PdfPCell(new Phrase(dto.getMemo(), font));
table.addCell(cellMemo);
table.addCell(dto.getWrite_date());
}
document.add(table);
// step 5
document.close();
댓글 ( 4)
댓글 남기기