iText 5-如何使用Java中的iText 5将表格的单元格设置为不同大小 [英] iText 5 - How to set cells of a table in different sizes using iText 5 in Java

查看:165
本文介绍了iText 5-如何使用Java中的iText 5将表格的单元格设置为不同大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写Java代码以生成PDF模板.在pdf的标题部分中,我创建了一个pdfTable,该表具有7个单元格,其中包括一个图像单元格(徽标),一个文本字段(ID号)和剩余的5个单元格以填充实际的ID号.

在输出中,我应该获得更大的图像单元格(代表徽标),并且ID号单元格的大小必须小于图像单元格.例如,如下图(预期结果).

但是,当生成模板时,我无法如上图所示(预期结果)进行填充.

生成PDF时,所有单元格都采用图像单元格的大小.

我尝试了不同的方法,例如设置列宽度,setFixedHeight(),setRowSpan(),setColumnSpan()方法等.但是没有任何效果.下图显示了我的输出(当前输出).

下面是我编写的代码.

public class NbaBafTemplateGenerator {

        private void createNbaBafTemplate(File outPutfileName, NbaBafTemplateData formData,String logoName) {

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outPutfileName));
                document.open();
                PdfPTable table = null;

    // Passing the data as a single String                      

              //IdNumber varible is of type String and has 5 characters of the number.
                table = NbaBafTemplatePage.createHeaderTable(logoName + ",Id Number: , " + 
                formData.getIdNumber(), 7, "", "","1.5f, 1f, 0.2f, 0.2f, 0.2f, 0.2f, 0.2f"); 
                document.add(table); 

    }// END OF CLASS NbaBafTemplateGenerator.



    //Class NbaBafTemplatePage  Begins.
    public class NbaBafTemplatePage extends PdfPageEventHelper {

        public static PdfPTable createHeaderTable(String text, int columnCount, String colour, String align, String colSize)
            throws DocumentException, IOException {

                    PdfPTable table = null;
                    table = new PdfPTable(columnCount); // 7 columns.
                    table.setWidthPercentage(100); // Width 100%
                    table.setSpacingBefore(0f); /
                    table.setSpacingAfter(10f);

              //Assigning column widths based on input width params.                
                float[] tablecolumnWidths = {
                        Float.parseFloat(colSize.split(",")[0]),
                        Float.parseFloat(colSize.split(",")[1]),
                        Float.parseFloat(colSize.split(",")[2]),
                        Float.parseFloat(colSize.split(",")[3]),
                        Float.parseFloat(colSize.split(",")[4]),
                        Float.parseFloat(colSize.split(",")[5]),
                        Float.parseFloat(colSize.split(",")[6])};   

            PdfPCell imgCell = new PdfPCell(createImageCell(text.split(",")[0]));

                    //imgCell.setColspan(3);
                    //imgCell.setRowspan(3);
                    imgCell.setBorder(PdfPCell.NO_BORDER);
                    imgCell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    imgCell.setVerticalAlignment(Element.ALIGN_LEFT);
                    table.addCell(imgCell);


            PdfPCell idCell = new PdfPCell(new Paragraph(text.split(",")[1]));

                    idCell.setBorderColor(BaseColor.BLACK);
                    idCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    idCell.setPaddingLeft(10);
                    idCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    idCell.setVerticalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(idCell);

            PdfPCell cellC0 = new PdfPCell(new Paragraph(text.split(",")[2]));

                cellC0.setBorderColor(BaseColor.BLACK); 
                cellC0.setHorizontalAlignment(Element.ALIGN_CENTER);
                cellC0.setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addCell(cellC0);

           PdfPCell cellC1 = new PdfPCell(new Paragraph(text.split(",")[3]));

                cellC1.setBorderColor(BaseColor.BLACK); 
                cellC1.setHorizontalAlignment(Element.ALIGN_CENTER);
                cellC1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addCell(cellC1);

           PdfPCell cellC2 = new PdfPCell(new Paragraph(text.split(",")[4]));

                cellC2.setBorderColor(BaseColor.BLACK); 
                cellC2.setHorizontalAlignment(Element.ALIGN_CENTER);
                cellC2.setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addCell(cellC2);

           PdfPCell cellC3 = new PdfPCell(new Paragraph(text.split(",")[5]));

                cellC3.setBorderColor(BaseColor.BLACK); 
                cellC3.setHorizontalAlignment(Element.ALIGN_CENTER);
                cellC3.setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addCell(cellC3);

           PdfPCell cellC4 = new PdfPCell(new Paragraph(text.split(",")[6]));
           cellC4.setBorderColor(BaseColor.BLACK); 
           cellC4.setHorizontalAlignment(Element.ALIGN_CENTER);
           cellC4.setVerticalAlignment(Element.ALIGN_MIDDLE);
           table.addCell(cellC4);

    return table;
    }//END OF METHOD createHeaderTable.

        public static PdfPCell createImageCell(String path) throws DocumentException, IOException {
        Image img = Image.getInstance(path);
        PdfPCell cell = new PdfPCell(img, true);

        return cell;
    }
  }

我正在使用Java和iText 5.x版本.

任何人都可以让我知道如何生成具有不同单元格大小的pdf表.

解决方案

您可以创建一个包含2行(13个单元格)的表.将图像单元格的列跨度设置为等于2.将第一行的其余单元格保留为空白,将其边界设置为0(不可见),并根据所需的对齐方式调整其高度(第一行的单元格). 然后将剩余的6个单元格添加到表中作为第二行.根据您的要求调整列宽.希望这可以帮助.

    PdfPTable table = new PdfPTable(7);         
    table.setWidthPercentage(100);
    PdfPCell imageCell = new PdfPCell(image);       
    imageCell.setBorder(0);
    imageCell.setRowspan(2);        
    table.addCell(imageCell);

    for(int i=0; i<6;i++) {
        PdfPCell blankCell = new PdfPCell();
        blankCell.setBorder(0);
        blankCell.setFixedHeight(20f);
        table.addCell(blankCell);
    }

    PdfPCell cell22 = new PdfPCell(new Phrase("ID Number"));
    table.addCell(cell22);

    PdfPCell cell23 = new PdfPCell(new Phrase("9"));                
    table.addCell(cell23);

    PdfPCell cell24 = new PdfPCell(new Phrase("6"));        
    table.addCell(cell24);

    PdfPCell cell25 = new PdfPCell(new Phrase("0"));        
    table.addCell(cell25);

    PdfPCell cell26 = new PdfPCell(new Phrase("5"));        
    table.addCell(cell26);

    PdfPCell cell27 = new PdfPCell(new Phrase("1"));        
    table.addCell(cell27);      

    document.add(table);

I am writing a Java code to generate a PDF template. In the pdf's header section, I have created a pdfTable which has 7 cells including an image cell (Logo), a text field (Id Number) and remaining 5 cells to populate the actual Id Number.

In the output, I should get a bigger image cell (representing the Logo) and Id number cells must be smaller in size than the image cell. Example, as in the below image (Expected Result).

However when the template is generated, I am unable populate as expected as shown in the above image (Expected Result).

When the PDF is generated, all the cells are taking the size of the image cell.

I have tried with different approaches like setting column widhts, setFixedHeight(), setRowSpan(), setColumnSpan() methods etc. But nothing worked. Below image shows my output (Current Output).

Below is code which I have written.

public class NbaBafTemplateGenerator {

        private void createNbaBafTemplate(File outPutfileName, NbaBafTemplateData formData,String logoName) {

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outPutfileName));
                document.open();
                PdfPTable table = null;

    // Passing the data as a single String                      

              //IdNumber varible is of type String and has 5 characters of the number.
                table = NbaBafTemplatePage.createHeaderTable(logoName + ",Id Number: , " + 
                formData.getIdNumber(), 7, "", "","1.5f, 1f, 0.2f, 0.2f, 0.2f, 0.2f, 0.2f"); 
                document.add(table); 

    }// END OF CLASS NbaBafTemplateGenerator.



    //Class NbaBafTemplatePage  Begins.
    public class NbaBafTemplatePage extends PdfPageEventHelper {

        public static PdfPTable createHeaderTable(String text, int columnCount, String colour, String align, String colSize)
            throws DocumentException, IOException {

                    PdfPTable table = null;
                    table = new PdfPTable(columnCount); // 7 columns.
                    table.setWidthPercentage(100); // Width 100%
                    table.setSpacingBefore(0f); /
                    table.setSpacingAfter(10f);

              //Assigning column widths based on input width params.                
                float[] tablecolumnWidths = {
                        Float.parseFloat(colSize.split(",")[0]),
                        Float.parseFloat(colSize.split(",")[1]),
                        Float.parseFloat(colSize.split(",")[2]),
                        Float.parseFloat(colSize.split(",")[3]),
                        Float.parseFloat(colSize.split(",")[4]),
                        Float.parseFloat(colSize.split(",")[5]),
                        Float.parseFloat(colSize.split(",")[6])};   

            PdfPCell imgCell = new PdfPCell(createImageCell(text.split(",")[0]));

                    //imgCell.setColspan(3);
                    //imgCell.setRowspan(3);
                    imgCell.setBorder(PdfPCell.NO_BORDER);
                    imgCell.setHorizontalAlignment(Element.ALIGN_LEFT);
                    imgCell.setVerticalAlignment(Element.ALIGN_LEFT);
                    table.addCell(imgCell);


            PdfPCell idCell = new PdfPCell(new Paragraph(text.split(",")[1]));

                    idCell.setBorderColor(BaseColor.BLACK);
                    idCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    idCell.setPaddingLeft(10);
                    idCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
                    idCell.setVerticalAlignment(Element.ALIGN_RIGHT);
                    table.addCell(idCell);

            PdfPCell cellC0 = new PdfPCell(new Paragraph(text.split(",")[2]));

                cellC0.setBorderColor(BaseColor.BLACK); 
                cellC0.setHorizontalAlignment(Element.ALIGN_CENTER);
                cellC0.setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addCell(cellC0);

           PdfPCell cellC1 = new PdfPCell(new Paragraph(text.split(",")[3]));

                cellC1.setBorderColor(BaseColor.BLACK); 
                cellC1.setHorizontalAlignment(Element.ALIGN_CENTER);
                cellC1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addCell(cellC1);

           PdfPCell cellC2 = new PdfPCell(new Paragraph(text.split(",")[4]));

                cellC2.setBorderColor(BaseColor.BLACK); 
                cellC2.setHorizontalAlignment(Element.ALIGN_CENTER);
                cellC2.setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addCell(cellC2);

           PdfPCell cellC3 = new PdfPCell(new Paragraph(text.split(",")[5]));

                cellC3.setBorderColor(BaseColor.BLACK); 
                cellC3.setHorizontalAlignment(Element.ALIGN_CENTER);
                cellC3.setVerticalAlignment(Element.ALIGN_MIDDLE);
                table.addCell(cellC3);

           PdfPCell cellC4 = new PdfPCell(new Paragraph(text.split(",")[6]));
           cellC4.setBorderColor(BaseColor.BLACK); 
           cellC4.setHorizontalAlignment(Element.ALIGN_CENTER);
           cellC4.setVerticalAlignment(Element.ALIGN_MIDDLE);
           table.addCell(cellC4);

    return table;
    }//END OF METHOD createHeaderTable.

        public static PdfPCell createImageCell(String path) throws DocumentException, IOException {
        Image img = Image.getInstance(path);
        PdfPCell cell = new PdfPCell(img, true);

        return cell;
    }
  }

I am using Java and iText 5.x version.

Can anyone please let me know how to generate the pdf table with different cell sizes.

解决方案

You can create a table with 2 rows (13 cells). Set the column-span of image cell equal to 2. Keep the remaining cells of the first row as blank, set their borders as 0 (invisible), and adjust their heights (cells of the first row) as per your required alignment. Then add the remaining 6 cells to the table as a second row. Adjust column widths as per your requirement. Hope this helps.

    PdfPTable table = new PdfPTable(7);         
    table.setWidthPercentage(100);
    PdfPCell imageCell = new PdfPCell(image);       
    imageCell.setBorder(0);
    imageCell.setRowspan(2);        
    table.addCell(imageCell);

    for(int i=0; i<6;i++) {
        PdfPCell blankCell = new PdfPCell();
        blankCell.setBorder(0);
        blankCell.setFixedHeight(20f);
        table.addCell(blankCell);
    }

    PdfPCell cell22 = new PdfPCell(new Phrase("ID Number"));
    table.addCell(cell22);

    PdfPCell cell23 = new PdfPCell(new Phrase("9"));                
    table.addCell(cell23);

    PdfPCell cell24 = new PdfPCell(new Phrase("6"));        
    table.addCell(cell24);

    PdfPCell cell25 = new PdfPCell(new Phrase("0"));        
    table.addCell(cell25);

    PdfPCell cell26 = new PdfPCell(new Phrase("5"));        
    table.addCell(cell26);

    PdfPCell cell27 = new PdfPCell(new Phrase("1"));        
    table.addCell(cell27);      

    document.add(table);

这篇关于iText 5-如何使用Java中的iText 5将表格的单元格设置为不同大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆