- package com.tool.itext;
-
- import java.io.FileOutputStream;
-
- import com.itextpdf.text.BaseColor;
- import com.itextpdf.text.Document;
- import com.itextpdf.text.Element;
- import com.itextpdf.text.Font;
- import com.itextpdf.text.Image;
- import com.itextpdf.text.PageSize;
- import com.itextpdf.text.Paragraph;
- import com.itextpdf.text.pdf.BaseFont;
- import com.itextpdf.text.pdf.PdfContentByte;
- import com.itextpdf.text.pdf.PdfPTable;
- import com.itextpdf.text.pdf.PdfReader;
- import com.itextpdf.text.pdf.PdfStamper;
- import com.itextpdf.text.pdf.PdfWriter;
-
- public class PdfUtil {
- // 表头
- public static final String[] tableHeader = { "出借人(id)", "借款金额", "借款期限",
- "年利率", "借款开始日", "借款到期日", "截止还款日", "还款本息" };
-
- // 数据表字段数
- private static final int colNumber = 8;
-
- // 表格的设置
- private static final int spacing = 1;
-
- // 表格的设置
- private static final int padding = 1;
-
- public static void main(String[] args) {
- // 创建文Pdf文挡50, 50, 50,50左右上下距离
- Document document = new Document(PageSize.A4);
- try {
- // 使用PDFWriter进行写文件操作
- PdfWriter.getInstance(document,
- new FileOutputStream("d:\\学生信息.pdf"));
- document.open();
- // 中文字体
- BaseFont bfChinese = BaseFont.createFont("STSong-Light",
- "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
- Font fontChinese = new Font(bfChinese, 12, Font.BOLD);
- // 创建有colNumber(8)列的表格
- PdfPTable datatable = new PdfPTable(colNumber);
- // 定义表格的宽度
- int[] cellsWidth = { 5, 4, 4, 4, 5, 5, 5, 5 };
- datatable.setWidths(cellsWidth);
- // 表格的宽度百分比
- datatable.setWidthPercentage(100);
- datatable.getDefaultCell().setPadding(padding);
- datatable.getDefaultCell().setBorderWidth(spacing);
- // 设置表格的底色
- datatable.getDefaultCell().setBackgroundColor(BaseColor.WHITE);
- datatable.getDefaultCell().setHorizontalAlignment(
- Element.ALIGN_CENTER);
- // 添加表头元素
- for (int i = 0; i < colNumber; i++) {
- datatable.addCell(new Paragraph(tableHeader[i], fontChinese));
- }
- // 添加子元素
- for (int i = 0; i < colNumber; i++) {
- datatable.addCell(new Paragraph(tableHeader[i], fontChinese));
- }
- Paragraph pf = new Paragraph("借款协议", fontChinese);
- pf.setAlignment(Element.ALIGN_CENTER);
- pf.setSpacingAfter(10f);
- document.add(pf);
- pf = new Paragraph(
- "借款协议号:389 借款人:lianhuan515 出借人:详见本协议第一条\n签订日期:2013-05-20 \n借款人通过成如下协议: \n第一条:详情如下表所示: ",
- fontChinese);
- pf.setSpacingAfter(20f);
- document.add(pf);
- document.add(datatable);
- pf = new Paragraph(
- "第二条:还款 \n", fontChinese);
- document.add(pf);
- document.close();
- PdfReader reader = new PdfReader("d:\\学生信息.pdf", "PassWord".getBytes());// 选择需要印章的pdf
- PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(
- "d:\\学生信息1.pdf"));// 加完印章后的pdf
- PdfContentByte over = stamp.getOverContent(1);// 设置在第几页打印印章
- Image image = Image
- .getInstance("D:\\workspace\\old\\WebContent\\images\\zhang.jpg");// 选择透明图片
- image.setAlignment(1);
- image.scaleAbsolute(220, 220);// 控制图片大小
- image.setAbsolutePosition(200, 500);// 控制图片位置
- over.addImage(image);
- stamp.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
-
-
- }
- }
[html] view plaincopy
- @RequestMapping(value = "/fileDownload/pdf/{id}.do", method = RequestMethod.GET)
- public void pdfDownload(HttpServletRequest request,
- HttpServletResponse response, @PathVariable Long id) {
- /** 获得输出流 **/
- OutputStream outputStream = null;
- InputStream in = null;
- try {
- /** 文件名,可以从数据库中取 **/
- String fileName = id + ".pdf";
-
- /** 获得输入流 **/
- in = new BufferedInputStream(request
- .getSession()
- .getServletContext()
- .getResourceAsStream(
- Constants.FILEPATH + File.separator + fileName));
-
- /** 存放文件的字节数组,available()返回字节数 **/
- byte[] buffer = new byte[in.available()];
-
- /** 从流中将文件读取到字节数组中 **/
- in.read(buffer);
-
- /** 转换编码 **/
- fileName = URLEncoder.encode(fileName, "UTF-8");
-
- /** 重置response **/
- response.reset();
-
- /** 设置HTML头部信息 **/
- response.setHeader("Content-Disposition", "attachment; filename=\""
- + fileName + "\"");
- response.addHeader("Content-Length", "" + buffer.length);
- response.setContentType("application/octet-stream;charset=UTF-8");
-
- outputStream = new BufferedOutputStream(response.getOutputStream());
-
- /** 从字节数组中将文件写到输出流中 **/
- outputStream.write(buffer);
-
- /** 清空输出流 **/
- outputStream.flush();
-
- /*** 关闭输入流 */
-
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- in.close();
- outputStream.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
- }