IText 5 Pdf Ширина столбца таблицы не работает - PullRequest
0 голосов
/ 18 ноября 2018

Я пытаюсь создать файл PDF, отформатированный для ярлыка с именем Avery 5395, который имеет 8 ярлыков на листе 8 1/2 x 11.Я создал таблицу PDF с измерениями из фактического листа и выписал ее.Проблема в том, что ширина столбца и общая ширина таблицы неверны.Вместо столбцов 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);
    }
}

}

1 Ответ

0 голосов
/ 19 ноября 2018

Нашел проблему!Оказывается, ширина столбца должна быть заблокирована после установки:

            theTable.setLockedWidth(true); // Without this the table width won't stick!
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...