ぬるすぺいす遍在

仕事がらみや物書きの勉強したことや、日々雑感

【Webアプリ】 iTextによるPDF出力

あとで編集

 

・PDF作成サーブレット (データ取得部分は省略)

 

 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

 

ByteArrayOutputStream bout = null;

PdfWriter writer = null;

Document doc = null;

BookDAO dao = null;

int lineCursor = 1;

int shoukei = 0;

int soukei = 0;

 

try {

dao = new BookDAO();

ArrayList list = dao.getBookListForPDF();

 

bout = new ByteArrayOutputStream();

doc = new Document(PageSize.A4, 50, 50, 50, 50);

writer = PdfWriter.getInstance(doc, bout);

Rectangle pageSize = doc.getPageSize();

writer.setBoxSize("art",

new Rectangle(36,50,

pageSize.getWidth() - 50,

pageSize.getHeight() - 36)

);

 

// ヘッダー・フッター設定

writer.setPageEvent(new PdfPageEventHelper() {

//ヘッダーのフォント

Font font_header = new Font(BaseFont.createFont(

"HeiseiKakuGo-W5", "UniJIS-UCS2-H",

BaseFont.NOT_EMBEDDED), 10, Font.ITALIC);

 

//フッターのフォント

Font font_page = new Font(BaseFont.createFont(

"HeiseiKakuGo-W5", "UniJIS-UCS2-H",

BaseFont.NOT_EMBEDDED), 9, Font.NORMAL);

 

@Override

public void onEndPage(PdfWriter writer, Document document) {

Rectangle rect = writer.getBoxSize("art");

 

//ヘッダー表示

Phrase header = new Phrase("書籍マスター一覧", font_header);

 

ColumnText.showTextAligned(writer.getDirectContent(),

Element.ALIGN_LEFT, header, rect.getLeft(),

rect.getTop(), 0);

SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy'年'MM'月'dd'日'");

Phrase headerDate = new Phrase(sdf1.format(new Date()), font_header);

 

ColumnText.showTextAligned(writer.getDirectContent(),

Element.ALIGN_RIGHT, headerDate, rect.getRight(),

rect.getTop(), 0);

 

//フッター(ページ)表示

Phrase page = new Phrase(String.format("- %d -",

writer.getPageNumber()), font_page);

 

ColumnText.showTextAligned(writer.getDirectContent(),

Element.ALIGN_CENTER, page,

(rect.getLeft() + rect.getRight()) / 2,

rect.getBottom(), 0);

 

}

 

});

 

doc.open();

 

/* 文字 */

BaseFont bf;

Font font;

// bf = BaseFont.createFont("HeiseiKakuGo-W5","UniJIS-UCS2-H",BaseFont.EMBEDDED);

// font = new Font(bf, 10);

 

bf = BaseFont.createFont("KozMinPro-Regular","UniJIS-UCS2-H",BaseFont.EMBEDDED);

font = new Font(bf, 10);

 

// 出力するPDFに説明を付与

doc.addAuthor("ウニウニコンピュータ");

doc.addSubject("書籍マスタ一覧");

 

// テーブルオブジェクトの作成

PdfPTable table = new PdfPTable(3); // 3列のテーブル

float[] columnWidths = {3, 14, 4}; // 列の幅

table.setWidths(columnWidths);

 

for( BookBean item : list ) {

 

//以下、すべてのif文を通すこと。else if でくくってはいけない

if(lineCursor % 52 == 1) {

// ヘッダ

PdfPCell hcell1 = new PdfPCell(new Paragraph("書籍番号", font));

PdfPCell hcell2 = new PdfPCell(new Paragraph("書籍名", font));

PdfPCell hcell3 = new PdfPCell(new Paragraph("金額", font));

hcell1.setHorizontalAlignment(Element.ALIGN_CENTER);

hcell2.setHorizontalAlignment(Element.ALIGN_CENTER);

hcell3.setHorizontalAlignment(Element.ALIGN_CENTER);

table.addCell(hcell1);

table.addCell(hcell2);

table.addCell(hcell3);

lineCursor++;

}

if(lineCursor % 52 != 0 && lineCursor % 52 != 1) {

//通常の行

PdfPCell cell1 = new PdfPCell(new Paragraph(item.getBookid(), font));

PdfPCell cell2 = new PdfPCell(new Paragraph(item.getBooknm(), font));

int tmpvalue = Integer.parseInt(item.getBookprice());

PdfPCell cell3 = new PdfPCell(new Paragraph(

String.format("%1$,3d", tmpvalue), font)

);

cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);

table.addCell(cell1);

table.addCell(cell2);

table.addCell(cell3);

lineCursor++;

shoukei += Integer.parseInt(item.getBookprice());

soukei += Integer.parseInt(item.getBookprice());

}

if(lineCursor % 52 == 0) {

// 小計フッタ

Paragraph s_footer1 = new Paragraph("小計", font);

s_footer1.setAlignment(Element.ALIGN_CENTER);

PdfPCell fscell1 = new PdfPCell(s_footer1);

PdfPCell fscell2 = new PdfPCell(new Paragraph(String.format("%1$,3d", shoukei), font));

fscell1.setColspan(2);

fscell2.setHorizontalAlignment(Element.ALIGN_RIGHT);

table.addCell(fscell1);

table.addCell(fscell2);

shoukei = 0;

lineCursor++;

}

}

 

// 小計フッタ(ラスト)

Paragraph s_footer1 = new Paragraph("小計", font);

s_footer1.setAlignment(Element.ALIGN_CENTER);

PdfPCell fscell1 = new PdfPCell(s_footer1);

PdfPCell fscell2 = new PdfPCell(new Paragraph(String.format("%1$,3d", shoukei), font));

fscell1.setColspan(2);

fscell2.setHorizontalAlignment(Element.ALIGN_RIGHT);

table.addCell(fscell1);

table.addCell(fscell2);

// 総計フッタ

Paragraph footer1 = new Paragraph("総計", font);

footer1.setAlignment(Element.ALIGN_CENTER);

PdfPCell fcell1 = new PdfPCell(footer1);

PdfPCell fcell2 = new PdfPCell(new Paragraph(String.format("%1$,3d", soukei), font));

fcell1.setColspan(2);

fcell2.setHorizontalAlignment(Element.ALIGN_RIGHT);

table.addCell(fcell1);

table.addCell(fcell2);

 

// ドキュメントにテーブルを追加

Paragraph para1 = new Paragraph();

para1.add(table);

doc.add(para1);

 

} catch (NamingException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (DocumentException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

doc.close();

try {

dao.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

 

response.setContentType("application/pdf");

response.setContentLength(bout.size());

OutputStream out = response.getOutputStream();

out.write(bout.toByteArray());

out.close();