2016-12-05 65 views
0

我的问题是如何做以下事情。 我想将我的PDF分成三列。iText PDF文件应该包含3列

所以,如果我添加一个段落,它只会去第一列,如果第一列已经完整/完整,它将继续到第二列。我希望你能理解我的问题。我英语不好。

我正在使用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); 
    } 
} 

回答

0

这个example显示了如何水平地将一个页面分成多个部分。在你的情况下,通过在示例中定义3个长形矩形而不是宽形来进行垂直。

官方example编辑制作3长形柱。

private void createPdf(String dest){ 

     try{ 
      // step 1 
      Document document = new Document(); 
      // step 2 
      PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); 
      // step 3 
      document.open(); 
      // step 4 
      // show the area for the column as a rectangle with red borders 
      Rectangle rect = new Rectangle(10, 10, 160, 800); 
      addColumn(writer, rect, false); 
      rect = new Rectangle(170, 10, 330, 800); 
      addColumn(writer, rect, true); 
      rect = new Rectangle(340, 10, 490, 800); 
      addColumn(writer, rect, true); 
      // step 5 
      document.close(); 
     } catch (Exception ex){ 
      System.out.println(ex.getMessage()); 
     } 
    } 

    public void addColumn(PdfWriter writer, Rectangle rect, boolean useAscender) throws DocumentException { 
     rect.setBorder(Rectangle.BOX); 
     rect.setBorderWidth(0.5f); 
     rect.setBorderColor(BaseColor.RED); 
     PdfContentByte cb = writer.getDirectContent(); 
     cb.rectangle(rect); 
     Phrase p = new Phrase("This text is added at the top of the column."); 
     ColumnText ct = new ColumnText(cb); 
     ct.setSimpleColumn(rect); 
     ct.setUseAscender(useAscender); 
     ct.addText(p); 
     ct.go(); 
    } 
+0

好的我会尝试这一个 –

+0

我不明白。 t_T –

+0

你没有得到什么? – mkl

相关问题