PDF组件练习 iText
一、用pdf组件iText输出文本
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//创建文档对象,a4大小Document document=new Document(PageSize.A4);//创建一个字节数组输出流ByteArrayOutputStream stream=new ByteArrayOutputStream();try {//创建一个pdf输出流PdfWriter writer=PdfWriter.getInstance(document, stream);//打开文档document.open();//向pdf中写入文字document.add(new Paragraph("Hello world , Hello iText!"));//关闭文档document.close();} catch (Exception e) {e.printStackTrace();}//设置响应文档类型 为pdfresponse.setContentType("application/pdf");//设置响应数据大小response.setContentLength(stream.size());//为输出流的大小//获得响应数据ServletOutputStream out=response.getOutputStream();//得到response中的输出流//将pdf数据流写入响应数据中stream.writeTo(out);out.flush();out.close();}