完全可以,我使用的是pdflib,你可以在网上查一下

解决方案 »

  1.   

    import java.io.*;
    import java.util.*;
    import java.util.List;
    import java.awt.*;
    import com.lowagie.text.*;
    import com.lowagie.text.pdf.*;
    import com.lowagie.text.Image;
    import com.lowagie.text.Font;
    import java.text.SimpleDateFormat;/**
     * <p>Title: PDFFormatParser.java</p>
     * <p>Description: 查询结果另存为PDF文档</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author z
     * @version  */public class PDFFormatParser
    {
        private List dataList = null;
        //查询条件对象接口
        private SearchObject searchObject;
        private String modName;
        private USMMessageResources usmMessRes;
        //每行数据项最大长度
        private int itemsLength[] = null;    public PDFFormatParser()
        {
        }    public PDFFormatParser(List dataList,SearchObject searchObject,USMMessageResources usmMessRes
                               ,String modName)
        {
            this.dataList = dataList;
            this.searchObject = searchObject;
            this.usmMessRes = usmMessRes;
            this.modName = modName;
            setItemsLength();
        }    /**
         * 设置每个单元格的长度
         */
        public void setItemsLength()
        {
            //存储对应的数据项的长度
            String[] fieldsLength = ((DumpIF) dataList.get(0)).getFields();
            //现在对数据项个数为进行处理
            int[] itemsLength = new int[fieldsLength.length];
            if(fieldsLength.length == 9)
            {
                itemsLength = new int[]{10,30,35,20,35,25,25,30,25};
            }
            if(fieldsLength.length == 6)
            {
                itemsLength = new int[]{30,35,35,20,20,35};
            }
            if(fieldsLength.length == 7)
            {
                itemsLength = new int[]{30,30,30,30,30,30,30};
            }
            this.itemsLength = itemsLength;
        }    /**
         * 打印表头
         * @return String
         */
        public String printHead()
        {
            String head = "";
            if("IPDataList".equals(modName))
                head = "left.net.perf.search";
            else if("Application".equals(modName))
                head = "performance.protocol.analyse";
            else if("AppProtocol".equals(modName))
                head = "performance.application.layer";
            else if("OthProtocol".equals(modName))
                head = "performance.otherlayer.analyse";
            else
                UspLog.error("PDFFormatParse.des() modName is invalid!");        head = usmMessRes.getMessage(head);
            return head;
        }    /**
         * 打印查询条件
         * @return String
         */
        private String getCondition()
        {
            StringBuffer condition = new StringBuffer();
            condition.append(usmMessRes.getMessage("search.condition"));        String[] names = searchObject.getTitles();
            String[] fields = searchObject.getFields();
            for (int i = 0; i < names.length; i++) {            String field = fields[i];
                if ((field == null) || ("".equals(field))) {
                    continue;
                } else {
                    condition.append(usmMessRes.getMessage(names[i]) + "="
                            + usmMessRes.getMessage(field) + "    ");
                }        }
            return condition.toString();
        }    /**
         * 得到当前时间
         * @return String
         */
        public String getTime()
        {
            //获得服务器时间
            Date date = new Date();
            //时间统一格式
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        String serverTime = usmMessRes.getMessage("search.save.time")
                    + df.format(date);
            return serverTime;
        }
      

  2.   


        /**
         * 生成PDF文档
         * @param img Image[]
         * @param out OutputStream
         */
        public void getContent(Image[] img,OutputStream out)
        {
            Document document = new Document(PageSize.A4, 20, 20, 20, 20);
            try
            {
                //将document和out绑定
                PdfWriter.getInstance(document, out);
                document.open();            DumpIF data = (DumpIF) dataList.get(0);
                String[] names = data.getTitles();
                if ("IPDataList".equals(modName))
                {
                    names = getIPSeachTitle();
                }
                if ("Application".equals(modName))
                {
                    names = getApplicationTitle();
                }
                if ("AppProtocol".equals(modName))
                {
                    names = getAppProtocolTitle();
                }
                if ("OthProtocol".equals(modName))
                {
                    names = getOthProtocolTitle();
                }            //表格
                Table table = new Table(data.getFields().length);
                table.setWidths(itemsLength);
                table.setBorderWidth(1);
                table.setPadding(1);            //定义字体
                BaseFont bfChinese = null;
                try
                {
                    bfChinese = BaseFont.createFont("STSong-Light",
                        "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
                }
                catch (IOException ex1)
                {
                }
                catch (DocumentException ex1)
                {
                }
                Font FontChinese = new Font(bfChinese, 8, Font.NORMAL);
                Font titleFont = new Font(bfChinese,12,Font.NORMAL);            Paragraph title = new Paragraph(printHead(),titleFont);
                title.setAlignment(Element.ALIGN_CENTER);
                document.add(title);
                document.add(new Paragraph(getCondition(), titleFont));
                document.add(new Paragraph(""));
                document.add(new Paragraph(""));
                document.add(new Paragraph(""));
                if("Application".equals(modName) || "AppProtocol".equals(
                    modName) || "OthProtocol".equals(modName))
                {
                    for (int i = 0; img != null && i < img.length; i++)
                    {
                        if(i!=0 && i%2==0)
                        {
                            document.newPage();
                        }
                        img[i].setAlignment(Image.ALIGN_CENTER);
                        document.add(img[i]);
                        document.add(new Paragraph(""));
                        document.add(new Paragraph(""));
                    }
                }
                document.add(new Paragraph(""));
                for (int i = 0; i < names.length; i++)
                {
                    Cell cell = new Cell(new Paragraph(names[i], FontChinese));
                    cell.setBackgroundColor(Color.gray);
                    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
                    cell.setVerticalAlignment(Element.ALIGN_CENTER);
                    table.addCell(cell);
                }            //打印文件体
                if ("IPDataList".equals(modName))
                {
                    int k = 1;
                    for (int i = 0; i < dataList.size(); i++)
                    {
                        DumpIF datas = (DumpIF) dataList.get(i);
                        String[] fields = datas.getFields();
                        Cell cell0 = new Cell(new Paragraph(String.valueOf(k++),
                            FontChinese));
                        cell0.setHorizontalAlignment(Element.ALIGN_CENTER);
                        cell0.setVerticalAlignment(Element.ALIGN_CENTER);
                        table.addCell(cell0);
                        for (int j = 1; j < fields.length; j++)
                        {
                            Cell cell1 = new Cell(new Paragraph(fields[j] == null ?
                                "" : fields[j], FontChinese));
                            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                            cell1.setVerticalAlignment(Element.ALIGN_CENTER);
                            //cell0.setNoWrap(true);
                            table.addCell(cell1);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < dataList.size(); i++)
                    {
                        DumpIF datas = (DumpIF) dataList.get(i);
                        String[] fields = datas.getFields();
                        for (int j = 0; j < fields.length; j++)
                        {
                            Cell cell1 = new Cell(new Paragraph(fields[j] == null ?
                                "" : fields[j], FontChinese));
                            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                            cell1.setVerticalAlignment(Element.ALIGN_CENTER);
                            //cell0.setNoWrap(true);
                            table.addCell(cell1);
                        }
                    }
                }
                document.add(table);
            }
            catch (DocumentException de)
            {
                de.printStackTrace();
                System.err.println("A Document error:" + de.getMessage());
            }
            document.close();
        }  }