package org.quetzaco.businesscomponents.dr.service.helper; import java.awt.Color; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.MissingResourceException; import org.quetzaco.businesscomponents.dms.service.DocumentServices; import org.quetzaco.businesscomponents.dms.service.DocumentServicesHome; import org.quetzaco.businesscomponents.dms.service.helper.BaseDmsServicesHelper; import org.quetzaco.commoncomponents.admin.model.BaseModel; import org.quetzaco.commoncomponents.admin.model.User; import org.quetzaco.commoncomponents.dms.model.Document; import org.quetzaco.commoncomponents.dr.model.DRRoute; import org.quetzaco.commoncomponents.global.constants.QuetzacoConstants; import org.quetzaco.commoncomponents.global.constants.QuetzacoJndiNames; import org.quetzaco.commoncomponents.global.model.RandomGUID; import org.quetzaco.commoncomponents.global.model.ResultData; import org.quetzaco.commoncomponents.global.model.ServicePool; import org.quetzaco.commoncomponents.global.util.ConfigManager; import org.quetzaco.commoncomponents.global.util.PDFUtil; import org.quetzaco.commoncomponents.global.util.QuetzacoServerLogger; import org.quetzaco.platformcomponents.dr.entity.helper.DRHelper; import org.quetzaco.wf.instance.WFNode; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.Phrase; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfStamper; import com.lowagie.text.pdf.PdfWriter; public class HQZLFrontPage { private static DocumentServices documentService = null; private final static int NORMAL_FONT_SIZE = 11; private final static Color NORMAL_FONT_COLOR = Color.black; private final static int NORMAL_FONT_ALIGN = PdfContentByte.ALIGN_LEFT; /* 1名称 2缓急 3密级 4 拟稿人 5 拟稿单位 6电话 7打印份数 9 标题 */ private final static String[] INDEX_KEYS = {"MC_ID", "HJ_ID", "MJ_ID", "NGR_ID", "NGDW_ID", "DH_ID", "DYFS_ID", "BT_ID"}; /* 1名称 2缓急 3密级 4 拟稿人 5 拟稿单位 6电话 7打印份数 9 标题 */ private final static int[][] INDEX_KEYS_DEFAULT_POSITION = {{90, 669}, {350, 669}, {440, 669}, {422, 359}, {422, 336}, {422, 314}, {422, 290}, {115, 168}}; // 附件1 附件2 附件3 附件4 附件5 private final static String[] ATTACHMENTS = {"FJ_1", "FJ_2", "FJ_3", "FJ_4", "FJ_5"}; // 附件1 附件2 附件3 附件4 附件5 private final static int[][] ATTACHMENTS_POSITION = {{116, 247}, {116, 235}, {116, 223}, {116, 212}, {116, 201}}; public static String makePublishPage(User contextUser, DRRoute drRoute) throws Exception { String hqzl_pdf_path = ConfigManager.getInstance().getPropertyResourceBundle(QuetzacoConstants.QUETZACO_CFG).getString("HQZL_PDF_PATH"); QuetzacoServerLogger.getInstance().debug(" hqzl_pdf_path " + hqzl_pdf_path); QuetzacoServerLogger.getInstance().debug(" drRoute.getObjectId() " + drRoute.getObjectId()); String templatePdf = DRHelper.getInstance().getCover(drRoute.getObjectId()); if ("".equals(templatePdf)) { return null; } String strTemplatePdf = hqzl_pdf_path +"/"+ DRHelper.getInstance().getCover(drRoute.getObjectId()); QuetzacoServerLogger.getInstance().debug(" strTemplatePdf " + strTemplatePdf); String cacheRoot = ConfigManager.getInstance().getPropertyResourceBundle(QuetzacoConstants.QUETZACO_CFG).getString("QUETZACO_CACHE_ROOT"); String strPrefacePdfFile = cacheRoot + "/" + new RandomGUID().getGUID() + ".pdf"; QuetzacoServerLogger.getInstance().debug("------------strPrefacePdfFile---" + strPrefacePdfFile); PdfStamper stamp = null; try { //读取临时的PDF 到内存中 PdfReader reader = new PdfReader(strTemplatePdf); //设置生成临时PDF的位置 stamp = new PdfStamper(reader, new FileOutputStream(strPrefacePdfFile)); // 取出第一页 PdfContentByte over = stamp.getOverContent(1); //操作PDF generateDocumentPublishInfoPart(contextUser,over, drRoute); } finally { stamp.close(); } String attachementPdf = getAttachementPath(contextUser, drRoute); QuetzacoServerLogger.getInstance().debug("------------attachement--" + attachementPdf); String destDoc_pdf = cacheRoot + "/" + new RandomGUID().getGUID() + ".pdf"; QuetzacoServerLogger.getInstance().debug("------------destDoc_pdf--" + destDoc_pdf); //将封页和附件内容合并 PDFUtil.mergePdf(strPrefacePdfFile, attachementPdf, destDoc_pdf); //写入随机数到每一页 String tmpPdf = cacheRoot + "/" + new RandomGUID().getGUID() + ".pdf"; PDFUtil.addWaterMark(new FileInputStream(destDoc_pdf), new FileOutputStream(tmpPdf), contextUser.getFullname(), contextUser); return tmpPdf; } //转换附件类型并获取路径 private static String getAttachementPath(User contextUser, DRRoute drRoute) throws Exception { documentService = ((DocumentServicesHome) ServicePool.getInstance().getServiceHome(DocumentServicesHome.class, QuetzacoJndiNames.DOCUMENT_SERVICE)).create(); QuetzacoServerLogger.getInstance().debug("------------getHqzlAttachementId---" + drRoute.getHqzlAttachementId()); Document document = new Document(drRoute.getHqzlAttachementId()); Document[] docs = {document}; ResultData rsDataBaseType = null; rsDataBaseType = documentService.viewDocumentDetails(contextUser, docs); if (rsDataBaseType.getStatus() == QuetzacoConstants.SUCCESS) { document = (Document) rsDataBaseType.getModelAt(0); } QuetzacoServerLogger.getInstance().debug(" document. " + document.getObjectName()); QuetzacoServerLogger.getInstance().debug(" document.getCurrentRevision().getInternalFileName() " + document.getCurrentRevision().getInternalFileName()); QuetzacoServerLogger.getInstance().debug(" document.getCurrentRevision().getInternalFileName() " + document.getCurrentRevision().getPath()); QuetzacoServerLogger.getInstance().debug(" document.getCurrentRevision().getInternalFileName() " + document.getCurrentRevision().getExtension()); String attachement = document.getCurrentRevision().getPath().replaceAll("~=~", "/") + document.getCurrentRevision().getInternalFileName(); QuetzacoServerLogger.getInstance().debug(" attachement " + attachement); boolean boo = BaseDmsServicesHelper.getInstance().ConvertFileToPDF(attachement); QuetzacoServerLogger.getInstance().debug(" boo " + boo); String attachementPdf = attachement.substring(0, attachement.lastIndexOf(".")) + ".pdf"; QuetzacoServerLogger.getInstance().debug(" attachementPdf " + attachementPdf); return attachementPdf; } //将流程信息一一读取出来 private static void generateDocumentPublishInfoPart(User contextUser, PdfContentByte over, DRRoute drRoute) throws IOException, DocumentException { String[] properties = new String[8]; properties[0] = drRoute.getHqzlDocumentNumberTitle() + drRoute.getDocumentSequeueNo() + drRoute.getTitle(); properties[1] = drRoute.getUrgency(); properties[2] = drRoute.getConfidentiality(); properties[3] = contextUser.getFirstName()+contextUser.getLastName(); properties[4] = drRoute.getHqzlUnit(); properties[5] = drRoute.getTelephone(); properties[6] = drRoute.getPrintNum() + ""; properties[7] = drRoute.getTitle(); for (int i = 0; i < properties.length; i++) { QuetzacoServerLogger.getInstance().debug(properties[i]); } List fj = drRoute.getAttachments(); QuetzacoServerLogger.getInstance().debug(" fj " + fj.size()); String[] attachements = new String[fj.size()>5?5:fj.size()]; Document[] arr = (Document[]) fj.toArray(new Document[fj.size()>5?5:fj.size()]); //写入最多5个附件 QuetzacoServerLogger.getInstance().debug(" arr " + arr); if (arr != null) { QuetzacoServerLogger.getInstance().debug(" arr length " + arr.length); } if (arr != null && attachements.length != 0) { for (int i = 0; i < arr.length && i < 5; i++) { attachements[i] = arr[i].getObjectName(); QuetzacoServerLogger.getInstance().debug(" arr[i].getObjectName() " + arr[i].getObjectName()); } } signPropertyText(over, properties); signAttachementText(over, attachements); } //写入属性 private static void signPropertyText(PdfContentByte over, String[] properties) throws IOException, DocumentException { for (int i = 0; i < properties.length; i++) { signTextWithDefault(over, properties[i], NORMAL_FONT_COLOR, NORMAL_FONT_ALIGN, 0f, NORMAL_FONT_SIZE, INDEX_KEYS[i], INDEX_KEYS_DEFAULT_POSITION[i][0], INDEX_KEYS_DEFAULT_POSITION[i][1]); } } //写入附件名 private static void signAttachementText(PdfContentByte over, String[] attachements) throws IOException, DocumentException { for (int i = 0; i < attachements.length; i++) { if (!"".equals(attachements[i])) { signTextWithDefault(over, attachements[i], NORMAL_FONT_COLOR, NORMAL_FONT_ALIGN, 0f, NORMAL_FONT_SIZE, ATTACHMENTS[i], ATTACHMENTS_POSITION[i][0], ATTACHMENTS_POSITION[i][1]); } } } /** * @param over 操作的PDF * @param showText 写入的文字 * @param color 字体颜色 NORMAL_FONT_COLOR * @param align 对齐方式 NORMAL_FONT_ALIGN * @param charSpacing 每个字之间的间隔 * @param fontSize 字体大小 * @param strConfigKey 在配置文件中的Key值 ,一般用于取出配置文件中的X轴和Y轴 对应的为 strConfigKey + "_X" strConfigKey + "_Y" * @param iDefaultX 默认的X轴位置 * @param iDefaultY 默认Y轴位置 * @throws IOException * @throws DocumentException */ private static void signTextWithDefault(PdfContentByte over, String showText, Color color, int align, float charSpacing, int fontSize, String strConfigKey, int iDefaultX, int iDefaultY) throws IOException, DocumentException { try { signText(over, showText, color, align, charSpacing, fontSize, Integer.parseInt(ConfigManager.getInstance().getPropertyResourceBundle(QuetzacoConstants.QUETZACO_CFG).getString(strConfigKey + "_X")), Integer.parseInt(ConfigManager.getInstance().getPropertyResourceBundle(QuetzacoConstants.QUETZACO_CFG).getString((strConfigKey + "_Y")))); } catch (MissingResourceException e) { QuetzacoServerLogger.getInstance().debug(e.getMessage()); signText(over, showText, color, align, charSpacing, fontSize, iDefaultX, iDefaultY); } catch (NumberFormatException e) { QuetzacoServerLogger.getInstance().debug(e.getMessage()); signText(over, showText, color, align, charSpacing, fontSize, iDefaultX, iDefaultY); } } private static void signText(PdfContentByte over, String showtext, Color color, int align, float charSpacing, int fontSize, float iX, float iY) throws IOException, DocumentException { try { QuetzacoServerLogger.getInstance().debug("---------------" + showtext + "-------x-------" + iX + "---------y---------" + iY); // BaseFont bfChinese =BaseFont.createFont( ConfigManager.getInstance().getPropertyResourceBundle( QuetzacoConstants.QUETZACO_CFG ).getString( "HQZL_FONT_PATH" ),BaseFont.IDENTITY_H,BaseFont.EMBEDDED); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); over.beginText(); over.setLineWidth(700); over.setFontAndSize(bfChinese, fontSize); over.setColorFill(color); over.setCharacterSpacing(charSpacing); if (showtext == null) showtext = ""; over.showTextAligned(align, showtext, iX, iY, 0); } finally { try { over.endText(); } catch (Exception e) { } } } //创建一个审批信息的文件,将流程审批信息写入 public static String createCommmentPDF(DRRoute drRoute)throws Exception{ String cacheRoot = ConfigManager.getInstance().getPropertyResourceBundle(QuetzacoConstants.QUETZACO_CFG).getString("QUETZACO_CACHE_ROOT"); String prefacePdfFile = cacheRoot + java.io.File.separatorChar+ new RandomGUID().getGUID() + ".pdf"; QuetzacoServerLogger.getInstance().debug(" DRViewCommentPanel "+drRoute.getWfInstance().getNodesMap()); List<Map.Entry<String, WFNode>> values = compareMap(drRoute.getWfInstance().getNodesMap()); List<String> list=new ArrayList<String>(); for (Map.Entry<String, WFNode> entry : values) { WFNode node = entry.getValue(); if ("START".equals(node.getActionToken())) { continue; } if (node.getAssignee().getObjectName() == null) { continue; } BaseModel model = new BaseModel(); list.add(node.getAssignee().getObjectName()+"&&"+node.getActionDate()); list.add(node.getComment()); QuetzacoServerLogger.getInstance().debug(" " +node.getAssignee().getObjectName()); QuetzacoServerLogger.getInstance().debug(" 审批&&"+node.getActionDate()); QuetzacoServerLogger.getInstance().debug(" 审批意见"+node.getComment()); } QuetzacoServerLogger.getInstance().debug(" prefacePdfFile "+prefacePdfFile); try { createPrefacePdf(prefacePdfFile,"",list); } catch (Exception e) { e.printStackTrace(); } return prefacePdfFile; } private static void createPrefacePdf(String file, String tableHeader, List<String> list)throws Exception, IOException { com.lowagie.text.Document document = new com.lowagie.text.Document(); PdfWriter.getInstance(document, new FileOutputStream(file)); document.open(); PdfPTable table = new PdfPTable(2); BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // BaseFont bfChinese =BaseFont.createFont( ConfigManager.getInstance().getPropertyResourceBundle( QuetzacoConstants.QUETZACO_CFG ).getString( "HQZL_FONT_PATH" ),"UniGB-UCS2-H",BaseFont.EMBEDDED); Font fontChinese = new Font(bfChinese, 12, Font.BOLD, Color.BLACK); PdfPCell name = new PdfPCell(new Phrase(tableHeader, fontChinese)); name.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER); name.setColspan(2); // table.addCell(name); Iterator iterator = list.iterator(); while(iterator.hasNext()){ String str = (String)iterator.next(); String[] strs = str.split("&&"); if(strs.length == 1){ PdfPCell column = new PdfPCell(new Phrase(strs[0], fontChinese)); column.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER); column.setColspan(2); table.addCell(column); }else if(strs.length == 2){ PdfPCell column1 = new PdfPCell(new Phrase(strs[0], fontChinese)); column1.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER); PdfPCell column2 = new PdfPCell(new Phrase(strs[1], fontChinese)); column2.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER); table.addCell(column1); table.addCell(column2); } } document.add(table); document.close(); } private static List<Map.Entry<String, WFNode>> compareMap(Map<String, WFNode> map){ List<Map.Entry<String, WFNode>> values = new ArrayList<Map.Entry<String, WFNode>>(map.entrySet()); Collections.sort(values, new Comparator<Map.Entry<String, WFNode>>() { public int compare(Map.Entry<String, WFNode> o1, Map.Entry<String, WFNode> o2) { if(o1.getValue().getActionDate()==null || o2.getValue().getActionDate()==null){ return 0; } if(o1.getValue().getActionDate().before(o2.getValue().getActionDate())){ return -1; }else{ return 1; } } }); System.out.println(values); return values; } }