📄 itext api简单易用.txt
字号:
iText API简单易用。通过使用iText,你能创建自定义的PDF文档。iText库由下边的一些包组成:
com.lowagie.servlets
com.lowagie.text
com.lowagie.text.html
com.lowagie.text.markup
com.lowagie.text.pdf
com.lowagie.text.pdf.codec
com.lowagie.text.pdf.hyphenation
com.lowagie.text.pdf.wmf
com.lowagie.text.rtf
com.lowagie.text.xml
com.lowagie.tools
我们的例子使用了这些iText类:
com.lowagie.text.pdf.PdfWriter
com.lowagie.text.Document
com.lowagie.text.HeaderFooter
com.lowagie.text.Paragraph
com.lowagie.text.Phrase
com.lowagie.text.Table
com.lowagie.text.Cell
关键的类是Document和PdfWriter。在创建PDF文档时,你将经常使用这两个类。Document是PDF文档基于对象的描述。你可以通过调用Document类提供的方法往文档中加入内容。PdfWriter对象通过java.io.OutputStream对象与Document关联在一起。
generatePDFDocumentBytes方法负责创建PDF文档。在这个方法中三个最重要的对象是Document对象,ByteArrayOutputStream对象和PdfWriter对象。PdfWriter使用ByteArrayOutputStream关联Document。
Document doc = new Document();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter docWriter = null;
docWriter = PdfWriter.getInstance(doc, baosPDF);
// ...用add方法把内容添加到Document中。
doc.add(new Paragraph("This document wascreated by a class named: "+ this.getClass().getName()));
doc.add(new Paragraph("This document was created on "+ new java.util.Date()));
当你添加完内容后,要关闭Document和PdfWriter对象。
doc.close();
docWriter.close();
当关闭文档后,ByteArrayOutputStream对象返回到调用者。
return baosPDF;
ByteArrayOutputStream包含了PDF文档的所有字节。
HTTP响应头
在这个应用中,我们仅仅关注四个HTTP 响应头:Content-type,Content-disposition,Content-length,和Cache-control。如果你从没有使用过HTTP头,请参考HTTP 1.1规范。
%Content-type %
在servlet中,HttpServletResponse有一个表明响应所包含内容类型的参数。对PDF文件而言,内容类型是application/pdf。
如果servlet没有设置类型,web浏览器很难决定如何处理这个文件。
PDFServlet用下边的代码设置内容类型:
resp.setContentType("application/pdf");//resp response
%Content-disposition %
Content-disposition头提供给浏览器确定HTTP响应内容的信息。当浏览器读到这些头信息后,它能确定:
HTTP响应包含一个文件;
包含在响应中的文件名;
该文件是显示在浏览器主窗口中还是要用外部的应用查看;
RFC 2183中有对Content-disposition头完整的解释。
通过合适地设置Content-disposition的值,servlet能指示浏览器是“内嵌”显示文件还是把它当作附件处理。
////////////////////////////////////////
例1.内嵌显示一个文件
///////////////////////////////////////
Content-disposition: inline;filename=foobar.pdf
/////////////////////////////////////////
例2.往response里附加一个文件
/////////////////////////////////////////
Content-disposition: attachment;filename=foobar.pdf
下边的伪码说明了如何设置头信息:
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
{
// ...
resp.setHeader("Content-disposition","inline; filename=foobar.pdf" );
// ...
}
Cache-Control
根据你应用的特性不同,你可以让浏览器缓存或者不缓存你正在生成的PDF文件。服务器端应用可以有很多种HTTP 头来控制内容缓存。下边是一些例子:
Cache-Control: no-cache
Cache-Control: no-store
Cache-Control: must-revalidate
Cache-Control: max-age=30
Pragma: no-cache
Expires: 0
关于Cache-Control头的全面解释见HTTP 1.1规范。
PDFServlet把Cache-Control设置为max-age=30。这个头信息告诉浏览器缓存这个文件的最长时间为30秒。
?????????????????????
Content-length
?????????????????????
Content-length头必须设置成PDF文件中字节的数值。如果Content-length没有设置正确,浏览器可能不能正确地显示该文件。下边是例子代码:
ByteArrayOutputStream baos = getByteArrayOutputStream();
resp.setContentLength(baos.size());
把PDF文档送到Web浏览器
PDFServlet通过把字节流写到servlet的输出流的方式把PDF文档送到客户端。
它通过调用HttpServletResponse对象的getOutputStream方法来获得输出流。
getOutputStream方法返回一个javax.servlet.ServletOutputStream类型的对象。
ServletOutputStream sos;
sos = resp.getOutputStream();
baos.writeTo(sos);
sos.flush();
在把所有的数据写到流之后,调用flush()方法把所有的字节发送到客户端。
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -