日期:2014-05-20  浏览次数:20715 次

把很多重复出现的代码,做个简单的封装,小白求教..
Java code
@SuppressWarnings("unused")
    public void PdfTab3() throws Exception
    {
        Document document = new Document(PageSize.A4, 80, 79, 20, 45);
        try
        {
            // 中文处理
            BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            // 其他所有文字字体
            Font normal = new Font(bfChinese, 14, Font.COURIER);
            // 粗体
            Font bold = new Font(bfChinese, 14, Font.BOLD);
            // 交易表1小字,四级
            Font fifth = new Font(bfChinese, 6, Font.NORMAL);
            // 模板抬头的字体
            Font titleChinese = new Font(bfChinese, 12, Font.NORMAL);
            // 一级
            Font first = new Font(bfChinese, 12, Font.COURIER);
            // 二级
            Font second = new Font(bfChinese, 8, Font.COURIER);
            // 三级
            Font third = new Font(bfChinese, 6, Font.BOLD);
            // 设置pdf生成路径
            PdfWriter.getInstance(document, new FileOutputStream("D:\\20121017-3.pdf"));
            // 打开文档
            document.open();
            // ------------------写入数据---------------------
            Paragraph title = new Paragraph("招标补遗表", titleChinese);
            title.setAlignment(Element.ALIGN_CENTER); // 设置居中
            title.setLeading(1f);// 设置行间距
            document.add(title);

            // 交易表2
            title = new Paragraph("                                                                                                                                                                                                                                                                                                                                          交易表3", fifth);
            title.setSpacingBefore(1f);
            title.setLeading(10f);
            document.add(title);

            float[] widths = { 10f, 25f, 10f, 25f };
            PdfPTable table = new PdfPTable(widths);
            table.setSpacingBefore(5f);
            table.setTotalWidth(500);
            table.setWidthPercentage(100);

            PdfPCell cell = null;
            // -----------表头----------------

            cell = new PdfPCell(new Paragraph("招标项目", third));
            cell.setFixedHeight(20);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("", fifth));
            cell.setColspan(3);
            cell.setFixedHeight(20);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("招 标 人", third));
            cell.setFixedHeight(20);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("", fifth));
            cell.setFixedHeight(20);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("招标编号", third));
            cell.setFixedHeight(20);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("", fifth));
            cell.setFixedHeight(20);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("联 系 人", third));
            cell.setFixedHeight(20);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("", fifth));
            cell.setFixedHeight(20);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("联系电话", third));
            cell.setFixedHeight(20);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("", fifth));
            cell.setFixedHeight(20);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("补遗类型", third));
            cell.setFixedHeight(20);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph("", fifth));
            cell.setColspan(3);
            cell.setFixedHeight(20);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("补遗主要内容:\n\n\n\n\n\n\n\n\n\n\n\n" + "                                                                                                                                                                                                                                                                                        招标人(章)\n\n\n" + "                                                                                                                                                                                                                                                                                        年     月     日\n\n", third));
            cell.setFixedHeight(120);
            cell.setColspan(4);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("\n\n\n\n\n" + "                                                                                           铁道部工程管理中心(章)\n\n" + "                                                                                                         年     月     日\n\n", third));
            cell.setFixedHeight(60);
            cell.setColspan(2);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("\n\n\n\n\n" + "                                                                    北京市建设工程发包承包交易中心(章)\n\n" + "                                                                                                    年     月     日\n\n", third));
            cell.setFixedHeight(60);
            cell.setColspan(2);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("附件:补遗原件 ", third));// 描述
            cell.setFixedHeight(10);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);// 设置内容左对齐
            cell.setVerticalAlignment(Element.ALIGN_LEFT); // 设置左对齐
            cell.setColspan(4);
            table.addCell(cell);

            document.add(table);
            // 必须用表格加以固定的技巧来实现
            float[] widthes = { 50f };// 设置表格的列宽和列数
            PdfPTable hiddenTable = new PdfPTable(widthes);// 建立一个pdf表格
            hiddenTable.setSpacingBefore(11f); // 设置表格上空间
            hiddenTable.setTotalWidth(500);// 设置表格的宽度
            hiddenTable.setWidthPercentage(100);// 设置表格宽度为%100
            hiddenTable.getDefaultCell().disableBorderSide(1);
            hiddenTable.getDefaultCell().disableBorderSide(2);
            hiddenTable.getDefaultCell().disableBorderSide(4);
            hiddenTable.getDefaultCell().disableBorderSide(8);

            cell = new PdfPCell(new Paragraph("", normal));// 描述
            cell.setHorizontalAlignment(Element.ALIGN_RIGHT);// 设置内容水平居中显示
            cell.setPaddingTop(0f); // 设置内容靠上位置
            cell.setPaddingBottom(0f);
            cell.setPaddingRight(20f);
            cell.setBorder(Rectangle.NO_BORDER);// 设置单元格无边框
            hiddenTable.addCell(cell);

            cell = new PdfPCell(new Paragraph("本表一式三份,由招标人填写,工管中心、交易中心、招标人各一份。", third));// 金额
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);// 设置内容水平居中显示
            cell.setPaddingTop(0f);
            cell.setPaddingRight(20f);
            cell.setBorder(Rectangle.NO_BORDER);
            hiddenTable.addCell(cell);
            document.add(hiddenTable);
            document.close();
        }
        catch (Exception e2)
        {
            System.out.println(e2.getMessage());
        }
    }