Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java IText 5 Pdf表格列宽不工作_Java_Pdf_Itext - Fatal编程技术网

Java IText 5 Pdf表格列宽不工作

Java IText 5 Pdf表格列宽不工作,java,pdf,itext,Java,Pdf,Itext,我正在尝试创建一个PDF文件,格式为Avery name标签5395,它在一张8 1/2 x 11的工作表上有8个标签。我建立了一个PdfPTable,其中包含了实际工作表中的测量值,并将其写出来。问题是列宽和总表宽是错误的。而不是第3.5列“它们约为2.8”。我的代码如下。我错过了什么 public class TryNameLabelPdf { public static void main(String[] args) { PdfPTable theTable; Document theD

我正在尝试创建一个PDF文件,格式为Avery name标签5395,它在一张8 1/2 x 11的工作表上有8个标签。我建立了一个PdfPTable,其中包含了实际工作表中的测量值,并将其写出来。问题是列宽和总表宽是错误的。而不是第3.5列“它们约为2.8”。我的代码如下。我错过了什么

public class TryNameLabelPdf {
public static void main(String[] args) {
PdfPTable theTable;
Document theDoc;
    try {
        // Avery 5395 Name Badges
        theDoc = new Document();
        Rectangle pageSize = new Rectangle(612f,792f); // 8.5*72 = 612, 11*72= 792
        theDoc.setPageSize(pageSize);
        theDoc.setMargins(49.5f, 49.5f,41.06f, 41.06f);  // left, right, top, bottom
        theTable = new PdfPTable(2);
        float[] columnWidths = {252f, 252f}; // 3.5*72 = 252
        theTable.setWidths(columnWidths);        
        theTable.setTotalWidth(504f); // 2*252 = 504
        PdfWriter.getInstance(theDoc, new FileOutputStream("MyTestTable.pdf"));
        theDoc.open();
        PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1.1"));
        cell1.setFixedHeight(174);
        PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 1.2"));
        cell2.setFixedHeight(174);
        PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 2.1"));
        cell3.setFixedHeight(174);
        PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 2.2"));
        cell4.setFixedHeight(174);
        PdfPCell cell5 = new PdfPCell(new Paragraph("Cell 3.1"));
        cell5.setFixedHeight(174);
        PdfPCell cell6 = new PdfPCell(new Paragraph("Cell 3.2"));
        cell6.setFixedHeight(174);
        PdfPCell cell7 = new PdfPCell(new Paragraph("Cell 4.1"));
        cell7.setFixedHeight(174);
        PdfPCell cell8 = new PdfPCell(new Paragraph("Cell 4.2"));
        cell8.setFixedHeight(174);
        theTable.addCell(cell1);
        theTable.addCell(cell2);
        theTable.addCell(cell3);
        theTable.addCell(cell4);
        theTable.addCell(cell5);
        theTable.addCell(cell6);
        theTable.addCell(cell7);
        theTable.addCell(cell8);
        theDoc.add(theTable);
        theDoc.close();
    } catch (DocumentException ex) {
        Logger.getLogger(TryNameLabelPdf.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(TryNameLabelPdf.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}找到了问题!设置后需要锁定列宽:

            theTable.setLockedWidth(true); // Without this the table width won't stick!