package testServlet; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.PdfPageEventHelper; import com.itextpdf.text.pdf.PdfWriter; public class hello extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); try { // 出力先を指定し、文書をPDFとして出力 Document doc = new Document(PageSize.A4, 50, 50, 50, 50); PdfWriter pdfwriter = PdfWriter.getInstance(doc, byteOut); Rectangle pageSize = doc.getPageSize(); pdfwriter.setBoxSize( "art", new Rectangle(36, 50, pageSize.getWidth() - 50, pageSize .getHeight() - 36)); // 日本語フォントの設定 Font font = new Font(BaseFont.createFont("HeiseiKakuGo-W5", "UniJIS-UCS2-H", BaseFont.NOT_EMBEDDED),18); Font font_red = new Font(BaseFont.createFont("HeiseiKakuGo-W5", "UniJIS-UCS2-H", BaseFont.NOT_EMBEDDED), 18); font_red.setColor(BaseColor.RED); // ヘッダー・フッター設定 pdfwriter.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); //フッター(ページ)表示 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(); // 出力するPDFに説明を付与 doc.addAuthor("作成者"); doc.addSubject("iTextサンプル"); // 文書に要素を追加 doc.add(new Paragraph("おはよう", font)); doc.add(new Paragraph("こんにちは", font_red)); // 出力終了 doc.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } // ブラウザへの出力 response.setContentType("application/pdf"); response.setContentLength(byteOut.size()); OutputStream out = response.getOutputStream(); out.write(byteOut.toByteArray()); out.close(); } } |