使用iText生成PDF的一些實踐和總結


產品中要求有將表單內容、流轉意見等內容放入PDF的功能。由於表單的內容不固定,所以采用PDF模板填充的方式。

填充PDF模板的主要代碼:

/**
     * 填充PDF模板
     * 
     * @throws IOException
     * @throws DocumentException
     */
    public static void fillTemplatePDF(Session session,lotus.domino.Document doc,java.io.InputStream isData,String gwType)
            throws IOException, DocumentException, Exception {
        PdfReader reader = new PdfReader(isData);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(String
                .format(null, TMP_RESULT_MAIN, null)));
        fill(session,doc, stamper.getAcroFields(),gwType);
        stamper.setFormFlattening(true);
        stamper.close();
    }
    /**
     * 將模板中的表單字段賦值
     */
    public static void fill(Session session,lotus.domino.Document doc, AcroFields form,String gwType)
            throws IOException, DocumentException, Exception {
        String strFieldName = "";
        //使用中文字體 
        BaseFont bf = BaseFont.createFont("STSongStd-Light",  "UniGB-UCS2-H",
                false);
//        BaseFont bf = BaseFont.createFont( "simsun.ttc,0", BaseFont.IDENTITY_H,
//                BaseFont.NOT_EMBEDDED);
        for (Iterator it = form.getFields().keySet().iterator(); it.hasNext();) {
            strFieldName = it.next().toString();
            form.setFieldProperty(strFieldName, "textfont", bf, null);
            if (doc.hasItem(strFieldName)) {
                Item item = doc.getFirstItem(strFieldName);
                Vector allvalues = doc.getItemValue(strFieldName);
                String result = "";
                if (allvalues.size() > 0) {
                    int itemType = item.getType();
                    if(itemType == Item.AUTHORS || itemType==Item.NAMES || itemType==Item.READERS){
                        Name tmpName = null;
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < allvalues.size(); i++) {
                            tmpName = session.createName((String) allvalues
                                    .get(i));
                            sb.append(tmpName.getCommon()).append(",");
                        }

                        result = sb.substring(0, sb.lastIndexOf(","));
                    } else {
                        
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < allvalues.size(); i++) {
                            if(((String) allvalues.get(i)).indexOf("<tr><td>") != -1){
                                
                            }else{
                            sb.append(allvalues.get(i)).append(",");
                            }
                        }
                        result = sb.substring(0, sb.lastIndexOf(","));
                    }
                    form.setField(strFieldName, result);
                }
            }
        }
        form.setField("Title", gwType);  //設置PDF文檔的Title域
    }

但有一個問題是:表單內容中如果有圖片,那如何插入到PDF模板中的域中呢?

 form.setField(strFieldName, result);

這個方法是只能將字符串放入域中。希望高手能給指點一下。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM