簡體   English   中英

iText 5 - 如何在 Java 中使用 iText 5 設置不同大小的表格單元格

[英]iText 5 - How to set cells of a table in different sizes using iText 5 in Java

我正在編寫 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 個單元格)的表格。 設置圖像單元格的column-span等於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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM