简体   繁体   中英

iText 7.1.5 get real width of table from CSV

Currently I use iText 7.1.5 version and try to convert CSV file to PDF. When completing the table, I can't provide the correct page size to contain the entire table.

Before that version I used 7.1.1 and this code worked for me:

Table pdfTable = new Table(columnCount).setTextAlignment(TextAlignment.CENTER)
                .setVerticalAlignment(VerticalAlignment.MIDDLE)
                .setHorizontalAlignment(HorizontalAlignment.CENTER)
                // sets WIDTH property of table
                .useAllAvailableWidth();
        Cell cell = new Cell();

        cell.setBackgroundColor(ColorConstants.LIGHT_GRAY).setBold();
        pdfTable.addHeaderCell(cell);

        // headers and content
        for (int i = 0; i < columnCount; i++) {
            //...
            pdfTable.addHeaderCell(cell);
        }

        PageSize fittedPageSize = PDFUtility.getFittedPageSize(pdfTable.getWidth(), PageSize.A4.getWidth());
        pdfDocument.setDefaultPageSize(fittedPageSize);

        document.add(pdfTable);

Now the pdfTable.getWidth() returns 100% which is true because useAllAvailableWidth call.

Thanks in advance.

As answer to another question of mine, @Uladzimir Asipchuk gave me solution that can fit here too.

This is the answer link .

And my code:

`Table pdfTable = new Table(columnCount).setTextAlignment(TextAlignment.CENTER)
.setVerticalAlignment(VerticalAlignment.MIDDLE)
.setHorizontalAlignment(HorizontalAlignment.CENTER)
.useAllAvailableWidth();
// Add table content here
MinMaxWidth minMaxWidth = 
((TableRenderer)pdfTable.createRendererSubTree()
.setParent(document.getRenderer())).getMinMaxWidth();
float minWidth = minMaxWidth.getMaxWidth(); 
pdfDocument.setDefaultPageSize(PDFUtility
.getFittedPageSize(minWidth,PageSize.A4.getWidth()));
document.add(pdfTable);`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM