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);
}
}
好的我會嘗試這一個 –
我不明白。 t_T –
你沒有得到什麼? – mkl