iText PDF文档应包含3列

时间:2016-12-05 03:54:50

标签: java itext documents

我的问题是如何做以下事情。 我想把我的pdf分成三列。

因此,如果我添加一个段落,它将只会转到第一列,如果第一列已经完成/已满,将继续到第二列。我希望你理解我的问题。我的英语不好。

我正在使用iText PDF。

例如,我有一个看起来像这样的表:

9:00AM Meal 1  
Chicken

现在,如果该表有1000多列,我希望它继续在第一页的下一列。下面是我的代码表,请指导我如何做,因为我只是一个初学者。

try {
    Document document = new Document(PageSize.A4, 25, 25, 25, 25);


    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\PURCHASEORDER\\" + one + ".pdf"));
    document.open();


    Rectangle rect0 = writer.getPageSize();
    PdfPTable table0 = new PdfPTable(new float[]{6, 4});
    table0.setTotalWidth(527);
    table0.getDefaultCell().setBorder(Rectangle.NO_BORDER);
    table0.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
    table0.addCell(new Paragraph("****************D", FontFactory.getFont(FontFactory.TIMES_BOLD, 18, BaseColor.BLACK)));
    table0.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 14, BaseColor.BLACK)));
    table0.addCell(new Paragraph("**********************************", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, BaseColor.BLACK)));
    table0.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 14, BaseColor.BLACK)));
    table0.addCell(new Paragraph("**************************", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, BaseColor.BLACK)));
    table0.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 14, BaseColor.BLACK)));


    table0.setWidthPercentage(100);
    document.add(table0);


    try {
        Document document = new Document(PageSize.A4, 50, 25, 25, 25);


        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\PURCHASEORDER\\" + one + ".pdf"));
        document.open();


        Rectangle rect0 = writer.getPageSize();
        PdfPTable table0 = new PdfPTable(new float[]{6, 4});
        table0.setTotalWidth(527);
        table0.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        table0.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
        table0.addCell(new Paragraph("FABLUES PAZFOOD", FontFactory.getFont(FontFactory.TIMES_BOLD, 18, BaseColor.BLACK)));
        table0.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 14, BaseColor.BLACK)));
        table0.addCell(new Paragraph("#15 13th-15th Street, Nazareth, Cagayan de Oro", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, BaseColor.BLACK)));
        table0.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 14, BaseColor.BLACK)));
        table0.addCell(new Paragraph("Tel No: (088) 8571296/728-901", FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, BaseColor.BLACK)));
        table0.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 14, BaseColor.BLACK)));


        table0.setWidthPercentage(100);
        document.add(table0);


        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url + db, user, pass);
            Statement st = conn.createStatement();
            String zero = dates.getSelectedItem().toString();
            String sql = "select count(*) as hehe from purchaseorder where servedate = '" + zero + "' and time = 'AM'";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();

            Rectangle react = writer.getPageSize();
            PdfPTable table2 = new PdfPTable(new float[]{7, 3});
            table2.setTotalWidth(527);
            table2.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            PdfPCell cell = new PdfPCell(new Paragraph(""));
            cell.setColspan(8);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBackgroundColor(BaseColor.GRAY);
            table2.addCell(cell);
            table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);


            if (rs.next()) {

                String v1 = rs.getString("hehe");


                table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                table2.addCell(new Paragraph("KITCHEN AM- " + one + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 14, BaseColor.BLACK)));
                table2.addCell(new Paragraph("TOTAL Number: " + v1 + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 14, BaseColor.BLACK)));


            }
            table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 8, BaseColor.BLACK)));
            table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 8, BaseColor.BLACK)));


            table2.setWidthPercentage(100);
            document.add(table2);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }


        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url + db, user, pass);
            Statement st = conn.createStatement();
            String zero = dates.getSelectedItem().toString();
            String sql = " Select purchaseorder.po_id,concat(purchaseorder.servetime,' ',purchaseorder.time) as times, customer.customer from purchaseorder join customer on customer.customer_id = purchaseorder.customer_id where servedate = '" + zero + "' and purchaseorder.time = 'AM' order by servetime DESC";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();

            Rectangle react = writer.getPageSize();
            PdfPTable table2 = new PdfPTable(new float[]{2, 4, 2, 4, 4});
            table2.setTotalWidth(527);
            PdfPCell cell = new PdfPCell(new Paragraph(""));
            cell.setColspan(8);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBackgroundColor(BaseColor.GRAY);
            table2.addCell(cell);


            table2.getDefaultCell().setBorder(NO_BORDER);

            table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
            table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
            table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
            table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
            table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));


            while (rs.next()) {


                String v0 = rs.getString("po_id");

                String v1 = rs.getString("times");
                String v2 = rs.getString("customer");


                table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                table2.addCell(new Paragraph("" + v1 + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
                table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                table2.addCell(new Paragraph("" + v2 + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
                table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
                table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
                table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));


                String ttt = "select type,code, quantity, stockname from orderlistview where po_id = '" + v0 + "'";
                pst1 = conn.prepareStatement(ttt);
                rs2 = pst1.executeQuery();

                while (rs2.next()) {

                    String a1 = rs2.getString("code");
                    String a2 = rs2.getString("type");

                    String b00 = rs2.getString("quantity");
                    String b11 = rs2.getString("stockname");


                    table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                    table2.addCell(new Paragraph("", FontFactory.getFont(FontFactory.TIMES_BOLD, 8, BaseColor.BLACK)));
                    table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                    table2.addCell(new Paragraph("" + b00 + " " + b11 + "", FontFactory.getFont(FontFactory.TIMES_ROMAN, 8, BaseColor.BLACK)));
                    table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
                    table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
                    table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                    table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
                    table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
                    table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
                }
            }
            table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
            table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));
            table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN, 10, BaseColor.BLACK)));

            table2.setWidthPercentage(100);
            document.add(table2);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

        document.close();

        String pdfFile = "D:\\PURCHASEORDER\\" + one + ".pdf";
        File f = new File(pdfFile);
        if (pdfFile.toString().endsWith(".pdf")) {
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + pdfFile);
        } else {
            //For cross platform use
            Desktop desktop = Desktop.getDesktop();

            desktop.open(f);
        }

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

1 个答案:

答案 0 :(得分:0)

example显示了如何将页面水平划分为多个部分。在您的情况下,通过在示例中定义3个长形矩形而不是宽形来使其垂直。

官方example编辑了3个长形状的列。

    Error in feed_push_parser(buf) : 
  parse error: after key and value, inside map, I expect ',' or '}'
            "name": "Key block labelled "2"",       "members": true,        "t
                 (right here) ------^