1

我正在使用com.itextpdf.text.*以下生成pdf文件是我的代码,它创建带有突出显示的标题和标题和行的pdf文件,我想要做的是,创建一个顶部带有图像的pdf文件和带有备用颜色的行,如何做到这一点使用com.itextpdf.text.*

response.setHeader("Content-disposition", "attachment; filename=\"" + reportName + ".pdf\"");
        response.setContentType("application/pdf");
        PdfWriter.getInstance(document,response.getOutputStream());

        try {
            document.open();
            addTitlePage(document, reportName);

            //float[] colsWidth = {1.5f,3f,4f,4f,2f};

            List<Float> colsWidth = new ArrayList<Float>();
            int iterator = 1;
           while (iterator <= headerMap.size()) {
               if(iterator==1){
                   colsWidth.add(1.5f); 
               }else{
                colsWidth.add(3f); 
               }
                iterator++;
            }
           float[] floatArray = ArrayUtils.toPrimitive(colsWidth.toArray(new Float[0]), 0.0F);

           PdfPTable table = new PdfPTable(floatArray);
            table.setWidthPercentage(98);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);

            PdfPCell c1 = new PdfPCell();
            for (Iterator it = headerMap.keySet().iterator(); it.hasNext();) {
                String headerName = (String) headerMap.get(it.next());
                c1 = new PdfPCell(new Phrase(headerName, headerFont));
                c1.setBackgroundColor(BaseColor.LIGHT_GRAY);
                table.addCell(c1);
            }
            table.setHeaderRows(1);
            table = custDAO.creadPDFTable(query, table);
            document.add(table);
            document.addAuthor(userViewModel.getUsername());
            document.addCreationDate();
            document.addCreator("POC");
            document.close();
            response.flushBuffer();

public PdfPTable creadPDFTable(String query,PdfPTable table){
        int numberOfColumns=0,sno=1;
        Connection connection = getConnection();
        if (connection != null) {
            try {
                PreparedStatement reportTablePS = connection.prepareStatement(query);
                ResultSet reportTable_rst = reportTablePS.executeQuery();
                ResultSetMetaData reportTable_rsmd = reportTable_rst.getMetaData();
                numberOfColumns = reportTable_rsmd.getColumnCount();
                while (reportTable_rst.next()) {
                    table.addCell(new PdfPCell(new Paragraph(String.valueOf(sno), textFont)));
                            for (int columnIterator = 1; columnIterator <= numberOfColumns; columnIterator++) {
                                 String column = reportTable_rst.getString(columnIterator);
                                 table.addCell(new PdfPCell(new Paragraph(column, textFont)));
                            }
                            sno++;
            }
            } catch (Exception ex) {
                ex.printStackTrace();
            }finally {
                try {
                    closeConnection(connection, null, null);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        return table;
    }

private static void addTitlePage(Document document, String reportName) throws DocumentException {
        Paragraph preface = new Paragraph();
        addEmptyLine(preface, 1);
        /**
         * Lets write a big header
         */
        Paragraph paragraph = new Paragraph(reportName, titleFont);
        paragraph.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph);

        /**
         * Add one empty line
         */
        addEmptyLine(preface, 1);
        document.add(preface);
    }
    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }

当我使用以下内容时,我得到以下异常'getoutputstream()已经调用了这个响应'

我想用它来插入图像。

            Image image = Image.getInstance(path+"images/abi.png");
            image.setAbsolutePosition(40f, 770f);
            image.scaleAbsolute(70f, 50f);
            document.add(image);

那么如何去做呢?

更新 :

我想创建一个像这样的 pdf 文件我只想在顶部添加图像和像这样使用交替颜色的行。

4

2 回答 2

1

您可以使用 XSLFO 和 Apache FOP。它对我有用。为了添加图像,我在 XSL 中进行了更改。

如需参考,请访问 http://www.codeproject.com/Articles/37663/PDF-Generation-using-XSLFO-and-FOP

于 2012-09-18T09:57:24.903 回答
1

http://what-when-how.com/itext-5/decorating-tables-using-table-and-cell-events-itext-5/


(来源:what-when-how.com


(来源:what-when-how.com

于 2012-09-18T09:53:40.690 回答