五、封装PDF工具类
import java.awt.Color;
import java.awt.Point;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.MalformedURLException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;酸菜鱼做法
import javax.rvlet.ServletOutputStream;
import javax.rvlet.http.HttpServletRespon;
import s.logging.Log;
import s.logging.LogFactory;
import Anchor;
import Annotation;
import BadElementException;
泾县中学import Cell;
import Chapter;
import Chunk;
import Document;
import DocumentException;
import Element;
import Font;
import HeaderFooter;
import Image;
import ListItem;
import PageSize;
import Paragraph;
import Phra;
import Rectangle;
import Section;
import Table;
import pdf.PdfWriter;
/**
* @author Jack Chang
* @version 創建時間:Jan 19, 2010 4:49:26 PM 類說明
*/
public class PdfUtil {
private static final Log log = Log(PdfUtil.class);// 日志
/**
* 导出PDF文档(最基本方法)到物理地址
*
* @param 需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象。
* @param pdfPath
* 导出pdf文档路径
*/
public static void exportPdf(String content, String pdfPath) {
Document document = null;
OutputStream out = null;
try {
// 1.建立Document对象的实例
document = new Document();
// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
out = new FileOutputStream(pdfPath);
Instance(document, out);
// 3.打开文档
document.open();
// 4.向文档中添加内容
document.add(new Paragraph(content)); // 会中文乱码
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
document.clo();// 5.关闭文档
try {
out.clo();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 导出一段话的pdf到物理地址
*
* @param para
* @param pdfPath
*/
public static void exportPdf(Paragraph para, String pdfPath,
boolean vertical) {
Document doc = null;
OutputStream out = null;安徽省地质调查院
try {
// 1.建立Document对象的实例
doc = buildDoc(vertical);
// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
out = new FileOutputStream(pdfPath);
Instance(doc, out);
// 3.打开文档
doc.open();
// 4.向文档中添加内容
doc.add(para);
} catch (Exception e) {
e.printStackTrace();
log.debug("生成pdf出错");
} finally {
clo(doc, out);// 5.关闭
}
}
/**
* 导出PDF到物理地址
*
* @param doc
* @param element
* @param pdfPath
*/
public static void exportPdf(Document doc, Element element, String pdfPath) {
OutputStream out = null;
try {
// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
out = new FileOutputStream(pdfPath);
Instance(doc, out);
// 3.打开文档
doc.open();
// 4.向文档中添加内容
doc.add(element);
} catch (Exception e) {
e.printStackTrace();
log.debug("生成pdf出错");
} finally {
clo(doc, out);// 5.关闭
}
}
/**
* 导出pdf到物理地址
*
* @param doc
* @param pdfBean
*/
public static void exprotPdf(Document doc, PdfBean pdfBean) {
OutputStream out = null;
try {
// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
out = new FileName());
PdfWriter writer = gerPdfWriter(doc, out);
doc = tPdfProperty(doc, pdfBean); // 设置属性
// 加密处理
if (pdfBean.isEncryptFlag()) {
List permissionList = PermissionList();
if (permissionList != null && permissionList.size() > 0) {
int allPermission = 0;
for (Iterator it = permissionList.iterator(); it.hasNext();) {
int permission = (Integer) it.next();
allPermission = allPermission | permission; // 有或运算来累加权限
}
writer = encrypt(writer, UrPsw(), pdfBean健康食品有哪些
.getOwnerPsw(), allPermission);
}
}
// 3.打开文档
doc.open();
// 4.向文档中添加内容
List elementList = ElementList();
if(elementList != null && !elementList.isEmpty()){
for(Iterator it = elementList.iterator(); it.hasNext();){
Element element = (Element) it.next();
doc.add(element);
}
}
} catch (Exception e) {
e.printStackTrace();
log.debug("生成pdf出错");
} finally {
clo(doc, out);// 5.关闭
}
}
手势语言/**
* 导出pdf到url*
* @param doc
* @param pdfBean
*/
public static void exprotPdfToUrl(Document doc, PdfBean pdfBean, HttpServletRespon respon) {
ByteArrayOutputStream bao = null;
ServletOutputStream out = null;
try {
// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
bao = new ByteArrayOutputStream();
PdfWriter writer = gerPdfWriter(doc, bao);
doc = tPdfProperty(doc, pdfBean); // 设置属性
// 加密处理
if (pdfBean.isEncryptFlag()) {
List permissionList = PermissionList();
if (permissionList != null && permissionList.size() > 0) {
int allPermission = 0;
for (Iterator it = permissionList.iterator(); it.hasNext();) {荒野四大美女
int permission = (Integer) it.next();
allPermission = allPermission | permission; // 有或运算来累加权限
}
writer = encrypt(writer, UrPsw(), pdfBean
.getOwnerPsw(), allPermission);
}
}
// 3.打开文档
doc.open();
// 4.向文档中添加内容
List elementList = ElementList();
if(elementList != null && !elementList.isEmpty()){
for(Iterator it = elementList.iterator(); it.hasNext();){
Element element = (Element) it.next();
doc.add(element);
}
}
doc.clo(); //关闭document
//respon设置
respon.tContentType("application/pdf");
/*=====添加下面一段,点击提示“另存为”,否则直接在浏览器中打开 start ===== */
// respon.tHeader("Content-Disposition", "attachment;filename=\"temp.pdf\"");
// respon.tHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
// respon.tHeader("Pragma", "public");
/
/ respon.tDateHeader("Expires", (System.currentTimeMillis() + 1000));
/*====添加下面一段,点击提示“另存为”,否则直接在浏览器中打开 end ======*/
respon.tContentLength(bao.size());
out = OutputStream();
bao.writeTo(out);
out.flush();
} catch (Exception e) {市场研究
e.printStackTrace();
log.debug("页面输出pdf出错");
} finally {
try {
bao.clo();
out.clo();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 块,PDF中,最小的单位
* @param content
* @param font
* @return
*/
public static Chunk getChunk(String content, Font font){
Chunk chunk = new Chunk(content, font);
return chunk;
分道扬镳的近义词}
/**
* 短句
* @param content
* @param font
* @return
*/
public static Phra getPhra(String content, Font font){
Phra phra = new Phra(content, font);
return phra;
}