java - iText 5 - 如何在 Java 中使用 iText 5 设置不同大小的表格单元格

标签 java itext pdf-generation

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

在输出中,我应该得到一个更大的图像单元格(代表 Logo ),并且 Id 数字单元格的大小必须小于图像单元格的大小。示例,如下图所示(预期结果)。

Expected Result

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

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

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

Current Output

下面是我编写的代码。

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

关于java - iText 5 - 如何在 Java 中使用 iText 5 设置不同大小的表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60114252/

相关文章:

pdf - PhantomJS PDF DPI 缩放问题

java - 有没有办法将 HTML 数据和图形数据转储到 pdf 中?

java - Lombok 与 hibernate

java - 在用户触摸屏幕的地方放置对话框

java - 使用 Java 将 HTML 转换为具有特殊字符的 PDF

java - 如何使用 Java 将 PDF 中表格的特定单元格内的文本设为粗体?

java - 为什么 Javascript(在 Rhino 中运行)无法访问 java.lang.reflect.Method 实例上的方法?

java - 无法使用注入(inject)器注入(inject) Play 的 WSClient 实例

java - 将 pdf 文档合并到单页 iText 中

java - 无法解析类型 java.awt.geom.AffineTransform。它是从所需的 .class 文件中间接引用的