發表於 程式分享

使用iText將word內多筆資料以功能變數convert 成 pdf

1.word設定如下

foreach

2.程式碼如下

 1) 將資料取至List

    private boolean retriveListData_13(int applySeqno_in, int kind_in, OLServiceEntry olServiceEntry, 
            List clientASOs) {
        //1.取得資料    
        OLClientSign olClientSign = olServiceEntry.retriveOLClientSign(applySeqno_in, OLClientSignKind.Kind_8.getId());
        
        //2.對應資料
        HashMap answerMap = retriveAnswerMap((olClientSign == null) ? "" : olClientSign.getAnswer());
        
        //3.取筆數
        String asoAddrow = (answerMap.get("asoAddrow") != null) ? answerMap.get("asoAddrow").trim() : "";
        int iAsoAddrow = (new Integer(asoAddrow)).intValue();
        
        if (iAsoAddrow >= 1) {
            if (clientASOs == null) {
                logger.error("List clientASOs is null");
                return false;
            }
            
            //3.取資料
            for (int iAsoIdx = 1; iAsoIdx <= iAsoAddrow; iAsoIdx++) {
                String kindKey = "kind_" + iAsoIdx;
                String kind = (answerMap.get(kindKey) != null) ? answerMap.get(kindKey).trim() : "";
                int iKind = (new Integer(kind)).intValue();
                
                String declareNameKey = "declareName_" + iAsoIdx;
                String declareName = (answerMap.get(declareNameKey) != null) ? answerMap.get(declareNameKey).trim() : "";
                
                String declareIdKey = "declareId_" + iAsoIdx;
                String declareId = (answerMap.get(declareIdKey) != null) ? answerMap.get(declareIdKey).trim() : "";
                
                String kindDesc = kind + "-";
                for (ASOKind asoKind : ASOKind.values()) {
                    if (iKind == asoKind.getId()) {
                        kindDesc += asoKind.getDesc();
                        break;
                    }
                }
                clientASOs.add(new OLClientASOItem(declareId, kindDesc, declareName));
            }
        }
        
        return true;
    }

2) convert to pdf

                    if (retriveListData_13(applySeqno_in, kind_in, olServiceEntry, clientASOs) == false) {
                        logger.error("retrive List(ASO) Data error - applySeqno_in: ' + applySeqno_in + ', kind_in: " + kind_in + ", version_in: " + version_in);
                        response.getWriter().println("retrive List(ASO) Data error");
                        return null;
                    }
                    
                    context.put("clientASOs", clientASOs);
發表於 程式分享

使用word書籤 ,convert word to pdf

1.word / 書籤 => 設定書籤名稱
pic1
2.範例程式碼

   private ByteArrayOutputStream gen2ImgFile(int applySeqno_in, String kindDesc, OLServiceEntry olServiceEntry,
            StringBuffer docFileName, OLClientSeal olClientSeal1, OLClientSeal olClientSeal2) {
        //0.Start
        InputStream in = OLPdfClientShowAction.class.getResourceAsStream(templateImgFileName);          
        IXDocReport report = null;
        IContext context = null;
        try {
            //0.Init
            report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Velocity);
            context = report.createContext();
            
            OLPdfProject project = new OLPdfProject(kindDesc + "_" + applySeqno_in);
            context.put("project", project);
            context.put("client.title", kindDesc);
            FieldsMetadata metadata = report.createFieldsMetadata();    
            
            //1.第1張圖
            if (olClientSeal1 != null) {
                logger.info("fileName-1: " + olClientSeal1.getDocFileName());
                String iPageTag1 = "pic1";
                    
                //1-1.註冊圖片
                metadata.addFieldAsImage(iPageTag1);
                    
                //1-2.設定圖片 
                byte[] docSrc1 = olClientSeal1.getDocSrc();
                byte[] decodeBytes1 = docSrc1;
                if (olClientSeal1.getSrcKind() == 1) {  //base64
                    String base64Str1 = new String(docSrc1);            
                    String tmpBase64Str1 = base64Str1.substring(base64Str1.indexOf(",") + 1);
                    decodeBytes1 = DatatypeConverter.parseBase64Binary(tmpBase64Str1);
                }
                        
                IImageProvider pic1 = new ByteArrayImageProvider(decodeBytes1);
                pic1.setWidth(300f);
                pic1.setResize(true);
                        
                context.put(iPageTag1, pic1);
            }
                    
            //2.第2張圖
            if (olClientSeal2 != null) {
                logger.info("fileName-2: " + olClientSeal2.getDocFileName());
                String iPageTag2 = "pic2";
                
                //2-1.註冊圖片
                metadata.addFieldAsImage(iPageTag2);
                
                //2-2.設定圖片      
                byte[] docSrc2 = olClientSeal2.getDocSrc();
                byte[] decodeBytes2 = docSrc2;
                if (olClientSeal2.getSrcKind() == 1) {  //base64
                    String base64Str2 = new String(docSrc2);            
                    String tmpBase64Str2 = base64Str2.substring(base64Str2.indexOf(",") + 1);
                    decodeBytes2 = DatatypeConverter.parseBase64Binary(tmpBase64Str2);
                }
                
                IImageProvider pic2 = new ByteArrayImageProvider(decodeBytes2);
                pic2.setWidth(300f);
                pic2.setResize(true);
                    
                context.put(iPageTag2, pic2);
            }
            
            //3.Generate report by merging Java model with the Docx
            ByteArrayOutputStream outPdf = new ByteArrayOutputStream();
            PdfOptions pdfOptions = PdfOptions.create();
            FontFactory.registerDirectory(application.getRealPath("/") + "WEB-INF" + File.separator 
                        + "classes" + File.separator + "fonts" + File.separator);
            Options options = Options.getTo(ConverterTypeTo.PDF).via(ConverterTypeVia.XWPF).subOptions(pdfOptions);                 
            report.convert(context, options, outPdf);
                
            //8.顯示檔案
            if (docFileName != null)
                docFileName.append(java.net.URLEncoder.encode(OLPdfKind.Kind_2.getDesc() + "_" + applySeqno_in + ".pdf", "UTF-8"));
            return outPdf;
        } catch (IOException e) {
            logger.error("gen2ImgFile() IOException error : " + e.getMessage());
            return null;
        } catch (XDocReportException e) {
            logger.error("gen2ImgFile() XDocReportException error : " + e.getMessage());
            return null;
        }       
    }