2016-07-07 11 views
0

Ich bin in der Lage, PDF zu erstellen, aber was ich brauche ist, ich möchte Werte anzeigen, die in Array abgerufen und dort in der ersten Spalte angezeigt werden unter dem anderen.Ich bin in der Lage, diese Werte auch zu drucken, aber nicht in der Lage, diese Werte untereinander zu setzen. Ich bin in diesem schlecht stecken. So kann mir jemand helfen, dieses Problem zu lösen.Unterdessen ist mein Code zum Generieren von PDF gefolgt von Bild von wie es aussieht.Wie mehrere Werte untereinander in einer einzelnen Spalte angezeigt werden mit itext und java struts2

public String execute() throws Exception { 
     // TODO Auto-generated method stub 
     HttpServletResponse response = ServletActionContext.getResponse(); 
     float temp = Integer 
       .parseInt(invoiceVar.getNetAmount().split("\\.")[0]) 
       + Integer.parseInt(invoiceVar.getServiceTax().split("\\.")[0]) 
       + Integer.parseInt(invoiceVar.getSbcTax().split("\\.")[0]); 
     // Double d = new Double(temp); 
     long n = (long) temp; 
     System.out.println(n); 
     float[] columnWidths = { 2, 5, 5, 5 }; 
     String docname = "invoice" 
       + invoiceVar.getCompanyName().substring(0, 2); 
     // + invoiceVar.getInvoiceNumber().substring(0, 2); 
     Document document = new Document(PageSize.A4, 50, 50, 50, 50); 
     Paragraph addLicense = null; 

     Paragraph someSectionText = new Paragraph(
       "Invoice: " 
         + invoiceVar.getInvoiceNumber() 
         + "                 Date: " 
         + invoiceVar.getInvoiceEventsDate() + "\n\n"); 
     Paragraph someSectionText2 = new Paragraph("To,"); 
     Paragraph someSectionText3 = new Paragraph("   " 
       + invoiceVar.getCompanyName()); 

     System.out.println("Into Action Class..."); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

     PdfWriter writer = PdfWriter.getInstance(document, baos); 
     PdfPTable table = new PdfPTable(4); 
     table.setSpacingBefore(25); 
     table.setWidths(columnWidths); 
     table.setWidthPercentage(100); 
     table.setSpacingAfter(25); 

     PdfPCell c1 = new PdfPCell(new Phrase("Details ")); 
     c1.setHorizontalAlignment(Element.ALIGN_CENTER); 
     table.addCell(c1); 

     c1 = new PdfPCell(new Phrase(" ")); 
     c1.setHorizontalAlignment(Element.ALIGN_CENTER); 
     table.addCell(c1); 

     c1 = new PdfPCell(new Phrase("Description")); 
     c1.setHorizontalAlignment(Element.ALIGN_CENTER); 
     table.addCell(c1); 

     c1 = new PdfPCell(new Phrase("Amount Details")); 
     c1.setHorizontalAlignment(Element.ALIGN_CENTER); 
     table.addCell(c1); 
     table.setHeaderRows(1); 

     table.addCell("\n "); 
     table.addCell("\n"); 
     for (int i = 0; i < invoiceVar.getLicName().length; i++) { 
      addLicense=new Paragraph(invoiceVar.getLicName()[i]); 
      table.addCell(addLicense); 
       } 

     table.addCell("\n"); 

     table.addCell("Pan Number: "); 
     table.addCell(invoiceVar.getPanNumber()); 
     table.addCell("Net Amount"); 
     table.addCell(invoiceVar.getNetAmount()); 

     table.addCell("Service Tax Number: "); 
     table.addCell(invoiceVar.getServiceTaxNumber()); 
     table.addCell("Service Tax @ 0.14 %"); 
     table.addCell(invoiceVar.getServiceTax()); 
     table.addCell(""); 
     table.addCell(""); 
     table.addCell("Sbc Tax @0.50%"); 
     table.addCell(invoiceVar.getSbcTax()); 
     table.addCell(""); 
     table.addCell(""); 
     table.addCell("Total Amount"); 
     table.addCell(invoiceVar.getInvoiceTotalAmount()); 

     Paragraph amountPara = new Paragraph("\nTotal amount in words is : " 
       + Conversion.evaluate(n) + " Only."); 
     Paragraph someSectionText4 = new Paragraph(
       "\nReceiver's Signature:                  Proprietor: "); 

     document.open(); 
     document.add(someSectionText); 
     document.add(someSectionText2); 
     document.add(someSectionText3); 
     document.add(table); 
     document.add(amountPara); 
     document.add(someSectionText4); 
     document.close(); 
     System.out.println(invoiceVar.getEventName()); 
     Database database = new Database(); 
     Connection con = database.Get_Connection(); 

     PreparedStatement ps = con 
       .prepareStatement("UPDATE PDFSTATUS SET I_PDF=? WHERE EVENT_NAME=? AND COMPANY_NAME=?"); 
     ps.setString(1, "one"); 
     ps.setString(2, invoiceVar.getInvoiceNumber()); 
     ps.setString(3, invoiceVar.getCompanyName()); 
     ps.executeUpdate(); 
     System.out.println("I_PDF RECORDS UPDATED !!"); 
     ServletOutputStream outputStream = response.getOutputStream(); 
     baos.writeTo(outputStream); 
     response.setHeader("Content-Disposition", 
       "attachment; filename=\"InvoicePDF.pdf\""); 
     response.setContentType("application/pdf"); 
     outputStream.flush(); 
     outputStream.close(); 

     return "success"; 
    } 

} 

Hier zeigt invoiceVar.getLicName()[i] alle im Array gespeicherten Werte an. Error Hier Dcp Office, Liquor License und Rangabhoomi sind Werte, die aus dem obigen Array angezeigt werden, aber sie werden in neuen Zellen angezeigt, aber ich möchte diese Werte in der gleichen Spalte untereinander.

+0

Möchten Sie die Werte in der gleichen Zelle oder in einer anderen Zeile unterhalb der aktuellen Zelle? Für die gleiche Zelle fügen Sie einfach Ihren Text zu dem Absatz hinzu, in den Sie die Zelle einfügen. Für eine weitere Zeile fügen Sie einfach ein paar leere Zellen hinzu, bevor Sie Ihre Hauptzelle hinzufügen. –

+0

Ich habe es getan, wie Sie sagen @ChrisHaas Sir.Sie können meinen Code überprüfen, aber es funktioniert nicht. Ich möchte Werte unter dem anderen in der gleichen Spalte –

Antwort

0

Ich habe diesen Code versucht und es hat für mich funktioniert. Ich hoffe, es wird auch anderen helfen.

for(int i=0;i<invoiceVar.getLicName().length;i++) 
     { 
     table.addCell(" "); 
     table.addCell(""); 
     table.addCell(invoiceVar.getLicName()[i]); 
     table.addCell(""); 
     } 
     table.addCell("Pan Number: "); 
     table.addCell(invoiceVar.getPanNumber()); 
     table.addCell("Net Amount"); 
     table.addCell(invoiceVar.getNetAmount()); 

     table.addCell("Service Tax Number: "); 
     table.addCell(invoiceVar.getServiceTaxNumber()); 
     table.addCell("Service Tax @ 0.14 %"); 
     table.addCell(invoiceVar.getServiceTax()); 
     table.addCell(""); 
     table.addCell(""); 
     table.addCell("Sbc Tax @0.50%"); 
     table.addCell(invoiceVar.getSbcTax()); 
     table.addCell(""); 
     table.addCell(""); 
     table.addCell("Total Amount"); 
     table.addCell(invoiceVar.getInvoiceTotalAmount());