如何使用itext和java struts2在单列中一个接一个地显示多个值

时间:2016-07-07 12:16:13

标签: java itext

我能够创建pdf,但我需要的是我想要显示正在数组中获取的值并在那里以单列一个显示在另一个下面。我能够打印这些值但是不能将这些值设置为低于另一个。我陷入了困境。所以任何人都可以帮助我解决这个问题。下面是我生成pdf的代码,然后是图像,看看它是什么样的。

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";
    }

}

此处invoiceVar.getLicName()[i]显示存储在数组中的所有值。Error 此处Dcp OfficeLiquor LicenseRangabhoomi是从上面的数组中显示的值,但是它们会显示在新的单元格中,但我希望这些值在同一列中排在另一列之下。

1 个答案:

答案 0 :(得分:0)

我尝试了这段代码,它对我有用。我希望它也可以帮助其他人。

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());