我正在编写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表。
我正在编写Java代码以生成PDF模板。在pdf的标题部分中,我创建了一个pdfTable,该表具有7个单元格,包括图像单元格(徽标),文本字段(ID号)和其余5个...
您可以创建一个包含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"));
cell23.setFixedHeight(10f);
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);