Java iText操作PDF文档

欢迎进入Java社区论坛,与200万技术人员互动交流 >>进入

  1.Action view plain //在线打印PDF String path = servlet.getServletContext()。getRealPath(””); //得到项目根目录CreatePdf checkPdf = new CreatePdf();String filepath = checkPdf.printImportAndPtExpert(path,”expertBase”,importList,putongtList);request.setAttribute(”filepath”, path+File.separator+”community”+File.separator+”pdfile”+File.separator+filepath);return mapping.findForward(”downLoad”);

  2.公用类CreatPDF view plain import java.io.File;import java.io.FileOutputStream;import java.util.List;

  import com.lowagie.text.Cell;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Element;import com.lowagie.text.Font;import com.lowagie.text.FontFactory;import com.lowagie.text.HeaderFooter;import com.lowagie.text.Image;import com.lowagie.text.PageSize;import com.lowagie.text.Paragraph;import com.lowagie.text.Phrase;import com.lowagie.text.Rectangle;import com.lowagie.text.Table;import com.lowagie.text.Watermark;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.pdf.PdfPageEventHelper;import com.lowagie.text.pdf.PdfWriter;

  public class CopyOfCreatePdf extends PdfPageEventHelper {

  /** * 打印专家评审基地(重点基地和普通基地)

  * * @param path * 获得项目根目录 String path = * servlet.getServletContext()。getRealPath(””);* @param filename * 文件名* @param importList * 重点基地集合* @param putongList * 普通基地集合* @return * @throws Exception */ @SuppressWarnings(”unchecked”)

  public String printImportAndPtExpert(String path, String filename,List importList, List putongList) throws Exception { // 创建报表大小,不能够指定页边距。

  // Document document = new Document(PageSize.A4, 85, 71, 72, 72);Document document = new Document(PageSize.A4, 30, 30, 72, 72);try { BaseFont bfFS = BaseFont.createFont(path + File.separator + “WEB-INF” + File.separator + “classes” + File.separator + “fonts” + File.separator + “SIMFANG.TTF”,BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);// 华文细黑BaseFont bfHWXH = BaseFont.createFont(path + File.separator + “WEB-INF” + File.separator + “classes” + File.separator + “fonts” + File.separator + “STXIHEI.TTF”,BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);// 黑体BaseFont bfHT = BaseFont.createFont(path + File.separator + “WEB-INF” + File.separator + “classes” + File.separator + “fonts” + File.separator + “SIMHEI.TTF”,BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

  Font htFontB = new Font(bfHT, 14, Font.BOLD);

  PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(path + File.separator + “community” + File.separator + “pdfile” + File.separator + filename + “.pdf”));

  // 插入水印if (new File(path + File.separator + “image006.gif”) != null) { Watermark watermark = new Watermark(Image.getInstance(path + File.separator + “image006.gif”), 0f, 0f);document.add(watermark);} else { System.out.println(”添加水印失败”);} com.lowagie.text.Font ST3 = FontFactory.getFont(”STSong-Light”,”UniGB-UCS2-H”);ST3.setSize(10f);HeaderFooter footer = new HeaderFooter(new Phrase(”第”, ST3),new Phrase(”页”, ST3));footer.setBorder(Rectangle.NO_BORDER);footer.setAlignment(Element.ALIGN_CENTER);document.setFooter(footer);// 加密writer.setEncryption(PdfWriter.STRENGTH128BITS, null,”hujichen0928″, PdfWriter.AllowPrinting | PdfWriter.AllowCopy);// 添加元信息

  document.addAuthor(”北京科学技术委员会”);document.addSubject(”评审基地项目列表”);document.open();

  com.lowagie.text.Font ST = FontFactory.getFont(”STSong-Light”,”UniGB-UCS2-H”);com.lowagie.text.Font ST22 = FontFactory.getFont(”STSong-Light”,”UniGB-UCS2-H”);

  BaseFont baseFont2 = ST22.getBaseFont();Font ST2 = new Font(baseFont2, 14, Font.BOLD);com.lowagie.text.Font cnFont = new Font(bfHWXH);ST.setSize(10);ST2.setSize(12);

  Table proinfoTab = new Table(8);proinfoTab.setWidth(100.0f);float[] twidth = { 6f, 8f, 14f, 14f, 14f, 6f, 6f, 28f };proinfoTab.setWidths(twidth);proinfoTab.setCellspacing(4);// 设置表框属性Cell proinfoCell = new Cell(new Paragraph(”2011年北京市第五批基地申报专家评审表”,ST2));proinfoCell.setUseAscender(true);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setColspan(8);proinfoTab.addCell(proinfoCell);

  proinfoCell = new Cell(new Paragraph(”重点评审项目”, ST2));proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoCell.setColspan(8);proinfoTab.addCell(proinfoCell);

  proinfoCell = new Cell(new Paragraph(”序号”, ST2));proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  proinfoCell = new Cell(new Paragraph(”区县”, ST2));proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  proinfoCell = new Cell(new Paragraph(”基地名称”, ST2));proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  proinfoCell = new Cell(new Paragraph(”单位名称”, ST2));proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  proinfoCell = new Cell(new Paragraph(”基地类别”, ST2));proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  proinfoCell = new Cell(new Paragraph(”分值”, ST2));proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  proinfoCell = new Cell(new Paragraph(”等级”, ST2));proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  proinfoCell = new Cell(new Paragraph(”专家评语”, ST2));proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  if (importList.size() > 0 && importList != null) { for (int i = 0; i < importList.size(); i++) {

  BBasePoint bbp = (BBasePoint) importList.get(i);BBaseDocument doc = bbp.getBBaseDocument();

  // 序号proinfoCell = new Cell(new Paragraph(String.valueOf(i + 1),ST));proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  // 区县if (doc.getDocCorpdiv() == null) { proinfoCell = new Cell(new Paragraph(”无”, ST));} else { proinfoCell = new Cell(new Paragraph(doc。getDocCorpdiv(), ST));} proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  // 基地名称if (doc.getBaseName() == null) { proinfoCell = new Cell(new Paragraph(”无”, ST));} else { proinfoCell = new Cell(new Paragraph(doc.getBaseName(),ST));} proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  // 单位名称if (doc.getDocCorpName() == null) { proinfoCell = new Cell(new Paragraph(”无”, ST));} else { proinfoCell = new Cell(new Paragraph(doc。getDocCorpName(), ST));} proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  // 基地类型if (doc.getDocCorpKind()。equals(”0″)) { proinfoCell = new Cell(new Paragraph(”科普教育基地”, ST));} else if (doc.getDocCorpKind()。equals(”1″)) { proinfoCell = new Cell(new Paragraph(”科普培训基地”, ST));} else if (doc.getDocCorpKind()。equals(”2″)) { proinfoCell = new Cell(new Paragraph(”科普传媒基地”, ST));} else if (doc.getDocCorpKind()。equals(”3″)) { proinfoCell = new Cell(new Paragraph(”科普研发基地”, ST));} else { proinfoCell = new Cell(new Paragraph(”无基地类型”, ST));} proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  // 基地评分if (bbp.getZongpoint() == null) { proinfoCell = new Cell(new Paragraph(”0″, ST));} else { proinfoCell = new Cell(new Paragraph(String。valueOf(Math.round(bbp.getZongpoint())), ST)); // 基地分数} proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  // 分数等级if (bbp.getGrade() == null) { proinfoCell = new Cell(new Paragraph(”无”, ST));} else { proinfoCell = new Cell(new Paragraph(bbp.getGrade(), ST));} proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_CENTER);proinfoTab.addCell(proinfoCell);

  // 专家评语String opinion = bbp.getOpinion();if (opinion != null) { opinion = opinion.replaceAll(”<p>”, “\r\n”);opinion = opinion.replaceAll(”</p>”, “\r\n”);opinion = opinion.replaceAll(” “, ” “);} else { opinion = “无”;} proinfoCell = new Cell(new Paragraph(opinion, ST));proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.LEFT);proinfoCell.setHorizontalAlignment(Cell.LEFT);proinfoTab.addCell(proinfoCell);}

  proinfoCell = new Cell(new Paragraph(”专 家 签 字:\n\n” + ” 年 月 日\n”,ST));proinfoCell.setUseAscender(true);proinfoCell.setVerticalAlignment(Cell.ALIGN_MIDDLE);proinfoCell.setHorizontalAlignment(Cell.ALIGN_LEFT);proinfoCell.setColspan(8);proinfoTab.addCell(proinfoCell);

  document.add(proinfoTab);} catch (DocumentException de) { System.err.println(de.getMessage());de.printStackTrace();} document.close();return filename + “.pdf”;}

  3.downLoad.jsp view plain <%@ page contentType=”text/html;charset=UTF-8″ import=”com.jspsmart.upload.*” %> <% Object obj = request.getAttribute(”filepath”);String filepath = “”;if(obj != null){ filepath = (String)obj;}else{ filepath = request.getParameter(”filepath”);} System.out.println(”**********”+filepath);//filepathfilepath = filepath.replaceAll(”/”,”\\”);// 新建一个SmartUpload对象SmartUpload su = new SmartUpload();// 初始化su.initialize(pageContext);su.setContentDisposition(null);// 下载文件su.downloadFile(filepath);out.clear();%>

  下面的是在网上看到的一个例子,与大家分享view plain package com.thuram.test;

  import java.awt.Color;import java.io.FileOutputStream;

[1][2]

总有一天,我会丢下我所有的疲倦和理想,

Java iText操作PDF文档

相关文章:

你感兴趣的文章:

标签云: