Java tutorial
/** * PureInfo Command * @(#)PatentPrintPdfAction.java 1.0 2006-9-19 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.srm.patent.action; import java.awt.Color; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.struts.action.ActionForward; import org.jfree.ui.Align; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.ElementTags; import com.lowagie.text.Font; import com.lowagie.text.PageSize; import com.lowagie.text.Paragraph; import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; import com.pureinfo.ark.content.ArkContentHelper; import com.pureinfo.ark.interaction.ActionBase; import com.pureinfo.force.ForceConstants; import com.pureinfo.force.exception.PureException; import com.pureinfo.force.fileserver.FileFactory; import com.pureinfo.force.io.FileUtil; import com.pureinfo.srm.auth.Pair; import com.pureinfo.srm.org.model.Organization; import com.pureinfo.srm.patent.domain.IPatentMgr; import com.pureinfo.srm.patent.model.Patent; /** * <P> * Created on 2006-9-19 11:22:02 <BR> * Last modified on 2006-9-19 * </P> * * @author Administrator * @version 1.0, 2006-9-19 * @since Command 1.0 */ public class PatentPrintPdfAction extends ActionBase { /** * @see com.pureinfo.ark.interaction.ActionBase#executeAction() */ public ActionForward executeAction() throws PureException { int nYear = request.getRequiredInt("year", ""); Rectangle rectPageSize = new Rectangle(PageSize.A4); rectPageSize.setBackgroundColor(Color.WHITE); rectPageSize.setBorderColor(Color.BLACK); rectPageSize = rectPageSize.rotate(); Document doc = new Document(rectPageSize, 10, 10, 10, 10); doc.addTitle(nYear + ""); doc.addAuthor("PureInfo"); try { BaseFont bfontTitle = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font fontTitle = new Font(bfontTitle, 18, Font.NORMAL); Paragraph paraTitle = new Paragraph(nYear + "", fontTitle); paraTitle.setAlignment(ElementTags.ALIGN_CENTER); BaseFont bfontContent = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font fontContent = new Font(bfontContent, 10, Font.NORMAL); FileFactory fileFactory = FileFactory.getInstance(); String sPath = fileFactory.lookupPathConfigByFlag(FileFactory.FLAG_DOWNLOADTEMP, true).getLocalPath(); FileUtil.insurePathExists(sPath); PdfWriter.getInstance(doc, new FileOutputStream(new File(sPath, "patent.pdf"))); doc.open(); doc.add(paraTitle); doc.add(new Paragraph(" ")); String[] arrTitles = new String[] { "", "", "", "", "", "", "", "", "" }; PdfPTable pTable = new PdfPTable(arrTitles.length); pTable.setWidths(new int[] { 5, 10, 9, 9, 20, 15, 12, 10, 10 }); for (int i = 0; i < arrTitles.length; i++) { PdfPCell pCell = new PdfPCell(); Paragraph para = new Paragraph(arrTitles[i], fontContent); para.setAlignment(ElementTags.ALIGN_CENTER); pCell.addElement(para); pTable.addCell(pCell); } /** * */ IPatentMgr mgr = (IPatentMgr) ArkContentHelper.getContentMgrOf(Patent.class); List patents = mgr.findAllAuthorizedOf(nYear); int i = 0; for (Iterator iter = patents.iterator(); iter.hasNext(); i++) { Patent patent = (Patent) iter.next(); this.addPatentCell(pTable, String.valueOf(i + 1), fontContent); this.addPatentCell(pTable, patent.getPatentSid(), fontContent); this.addPatentCell(pTable, ForceConstants.DATE_FORMAT.format(patent.getApplyDate()), fontContent); this.addPatentCell(pTable, patent.getWarrantDate() == null ? "" : ForceConstants.DATE_FORMAT.format(patent.getWarrantDate()), fontContent); this.addPatentCell(pTable, patent.getName(), fontContent); this.addPatentCell(pTable, patent.getAllAuthosName(), fontContent); this.addPatentCell(pTable, patent.getRightPerson(), fontContent); this.addPatentCell(pTable, getCollegeName(patent), fontContent); this.addPatentCell(pTable, patent.getPatentTypeName(), fontContent); } doc.add(pTable); doc.close(); } catch (DocumentException ex) { // TODO Auto-generated catch block ex.printStackTrace(System.err); } catch (IOException ex) { // TODO Auto-generated catch block ex.printStackTrace(System.err); } List list = new ArrayList(); list.add(new Pair("/download/patent.pdf", "")); request.setAttribute("forward", list); return mapping.findForward("success"); } private String getCollegeName(Patent _patent) throws PureException { Organization college = _patent.getCollege(); return college == null ? "" : college.getName(); } private void addPatentCell(PdfPTable _pTable, String _sValue, Font _font) { PdfPCell pCell = new PdfPCell(); Paragraph para = new Paragraph(_sValue, _font); para.setAlignment(Align.CENTER); pCell.addElement(para); _pTable.addCell(pCell); // return _pTable; } }