您的位置:首页 > 其它

使用Itext生成PDF绝对定位流入web网页

2015-01-27 19:49 351 查看
public void PDF(){
<span style="white-space:pre">	</span>ByteArrayOutputStream stream = new ByteArrayOutputStream();
<span style="white-space:pre">	</span>//自定义界面大小,上下左右边距
<span style="white-space:pre">	</span>Document document = new Document(new Rectangle(50,50), 0, 0, 0, 0);
<span style="white-space:pre">	</span>PdfWriter writer = PdfWriter.getInstance(document, stream);
<span style="white-space:pre">	</span>document.open(); // 打开 Document 文档
<span style="white-space:pre">	</span>PdfContentByte cb = writer.getDirectContent();
<span style="white-space:pre">	</span>BaseFont bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",false);//设置字体
<span style="white-space:pre">	</span>cb.beginText();
<span style="white-space:pre">	</span>cb.setFontAndSize(bf, 12);//字体和子号
<span style="white-space:pre">	</span>cb.setTextMatrix(50,50);//字体显示位置
<span style="white-space:pre">	</span>cb.showText();//内容
<span style="white-space:pre">	</span>cb.endText();
<span style="white-space:pre">	</span>document.close();//关闭文档
<span style="white-space:pre">	</span>//设置响应文档类型为pdf 
<span style="white-space:pre">	</span>getResponse().setContentType("application/pdf"); 
<span style="white-space:pre">	</span>//设置响应数据大小 
<span style="white-space:pre">	</span>getResponse().setContentLength(stream.size()); 
<span style="white-space:pre">	</span>//将pdf数据流写入响应数据流中 
<span style="white-space:pre">	</span>ServletOutputStream out = getResponse().getOutputStream();
<span style="white-space:pre">	</span>stream.writeTo(out); 
<span style="white-space:pre">	</span>out.flush(); 
<span style="white-space:pre">	</span>out.close();
下载ITEXT包
下载ITEXT依赖中文包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: