2017-07-25 69 views
0
Document document = new Document(); 
PdfWriter.getInstance(document, new FileOutputStream("VendorOrder.pdf")); 
document.open(); 
Image img = Image.getInstance("ReportHeader.png"); 
img.setAlignment(Image.MIDDLE); 
document.add(img); 

String dateOrdered,vendName,vendTotalOrd,vendBalance,vendRebate; 
String sql = "select * from VendorOrder where NO=?"; 
pst = sqliteconn.prepareStatement(sql); 
pst.setString(1, getVendorField.getText()); 
rs = pst.executeQuery(); 
if(rs.next()) { 
    dateOrdered = rs.getString("DATE"); 
    vendName = rs.getString("VENDOR"); 
    vendTotalOrd = rs.getString("TOTAL"); 
    vendBalance = rs.getString("BALANCE"); 
    vendRebate = rs.getString("REBATE"); 
    rs.close(); 
    pst.close(); 

    document.add(new Paragraph("\n")); 

    PdfPTable nameTable = new PdfPTable(2); 
    PdfPCell cell = new PdfPCell(new Paragraph("CUSTOMER DETAILS")); 
    cell.setColspan(3); 
    cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
    cell.setBackgroundColor(BaseColor.CYAN); 
    nameTable.addCell(cell); 
    nameTable.setWidths(new int[]{2, 1}); 
    nameTable.addCell("NAME: \n" + vendName); 
    nameTable.addCell("DATE ORDERED: "+ dateOrdered); 
    document.add(nameTable); 

    PdfPTable vendTable = new PdfPTable(3); 
    PdfPCell cell1 = new PdfPCell(new Paragraph("TOTAL PURCHASED")); 
    cell1.setBackgroundColor(BaseColor.LIGHT_GRAY); 
    vendTable.addCell(cell1); 
    PdfPCell cell2 = new PdfPCell(new Paragraph("BALANCE LEFT")); 
    cell2.setBackgroundColor(BaseColor.LIGHT_GRAY); 
    vendTable.addCell(cell2); 
    PdfPCell cell3 = new PdfPCell(new Paragraph("TOTAL REBATE")); 
    cell3.setBackgroundColor(BaseColor.LIGHT_GRAY); 
    vendTable.addCell(cell3); 
    vendTable.addCell(vendTotalOrd); 
    vendTable.addCell(vendBalance); 
    vendTable.addCell(vendRebate); 
    document.add(vendTable); 
} 

document.add(new Paragraph("----------------------------------------------------------------------------------------------------------------------------------")); 
document.add(new Paragraph("\n")); 

PdfPTable ordTable = new PdfPTable(4); 
PdfPCell cell = new PdfPCell(new Paragraph("PURCHASED ITEMS")); 
cell.setColspan(4); 
cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell.setBackgroundColor(BaseColor.CYAN); 
ordTable.addCell(cell); 
PdfPCell cell1 = new PdfPCell(new Paragraph("NEWPAPER")); 
cell1.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell1.setBackgroundColor(BaseColor.LIGHT_GRAY); 
ordTable.addCell(cell1); 
PdfPCell cell2 = new PdfPCell(new Paragraph("PRICE")); 
cell2.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell2.setBackgroundColor(BaseColor.LIGHT_GRAY); 
ordTable.addCell(cell2); 
PdfPCell cell3 = new PdfPCell(new Paragraph("# OF ORDERS")); 
cell3.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell3.setBackgroundColor(BaseColor.LIGHT_GRAY); 
ordTable.addCell(cell3); 
PdfPCell cell4 = new PdfPCell(new Paragraph("SUBTOTAL")); 
cell4.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell4.setBackgroundColor(BaseColor.LIGHT_GRAY); 
ordTable.addCell(cell4); 
//document.add(ordTable); 

String newsName,newsPrice,newsOrders,newsSubt; 
String sql1 = "select NEWSPAPER,PRICE,ORDERS,SUBTOTAL from NewspaperOrder where NO=?"; 
pst = sqliteconn.prepareStatement(sql1); 
pst.setString(1, getVendorField.getText()); 
rs=pst.executeQuery(); 
while(rs.next()){ 
    newsName = rs.getString("NEWSPAPER"); 
    newsPrice = rs.getString("PRICE"); 
    newsOrders = rs.getString("ORDERS"); 
    newsSubt = rs.getString("SUBTOTAL"); 
    rs.close(); 
    pst.close(); 

    //System.out.println(newsName + " " + newsPrice + " " + newsOrders + " " + newsSubt); 

    ordTable.addCell(newsName); 
    ordTable.addCell(newsPrice); 
    ordTable.addCell(newsOrders); 
    ordTable.addCell(newsSubt); 

} 
document.add(ordTable); 
document.close(); 

JOptionPane.showMessageDialog(null, "SAVED"); 

我试图从VendorOrderNewspaperOrder数据库表打印一堆信息iText的PDF。我试图从VendorOrder db表中打印一组数据,并且我已经成功将它们打印到PDF,而在NewspaperOrder db表中,我尝试打印多于一组数据,并且它仅显示来自NewspaperOrder db的第一组数据表。在NewspaperOrder分贝表打印数据库表iText的PDF报告的Java

PDF

基地,应该打印完所有具有“11”的顺序ID的数据。

NewspaperOrder db table

我看到这个类似的问题和解决方案在某种程度上类似于我做了什么。我找不到错误,因为代码不会抛出任何异常。

+0

将rs.close()和pst.close()语句移出循环... –

+0

ohh,谢谢!这解决了这个问题:) :)哈哈 – Mits

回答

3

假设你可以自由选择库,我会去iText7,因为它处理表格要好得多。

下面的代码是一个小样本来展示如何完成它。 在你的例子中,让我感到震惊的是代码重复的数量。 我建议尽量减少。

public void go() throws FileNotFoundException { 
    // some data source 
    Collection<Record> db = Arrays.asList(new Record("01-01-2017", "Oracle", "1024", "0", "24"), 
              new Record("02-02-2017", "Google", "2048", "32", "0"), 
              new Record("03-03-2017", "Microsoft", "512", "16", "0")); 

    // get output file 
    File out = getOutputFile(); 
    PdfDocument pdfDocument = new PdfDocument(new PdfWriter(out)); 
    Document layoutDocument = new Document(pdfDocument); 

    // set up table 
    Table table = new Table(UnitValue.createPercentArray(new float[]{10f,20f,10f,10f,10f})); 

    // add header 
    table.addCell(new Cell().add(new Paragraph("Date"))); 
    table.addCell(new Cell().add(new Paragraph("Vendor"))); 
    table.addCell(new Cell().add(new Paragraph("Total"))); 
    table.addCell(new Cell().add(new Paragraph("Balance"))); 
    table.addCell(new Cell().add(new Paragraph("Rebate"))); 

    // iterate over records 
    for(Record r : db) 
    { 
     table.addCell(new Cell().add(new Paragraph(r.date.toString()))); 
     table.addCell(new Cell().add(new Paragraph(r.vendor))); 
     table.addCell(new Cell().add(new Paragraph(r.total + ""))); 
     table.addCell(new Cell().add(new Paragraph(r.balance + ""))); 
     table.addCell(new Cell().add(new Paragraph(r.rebate + ""))); 
    } 

    // add table to document 
    layoutDocument.add(table); 

    // close 
    layoutDocument.close(); 
} 

这产生一个pdf文档,其中包含上述记录的表格。