Example usage for com.itextpdf.text.html.simpleparser TableWrapper setColWidths

List of usage examples for com.itextpdf.text.html.simpleparser TableWrapper setColWidths

Introduction

In this page you can find the example usage for com.itextpdf.text.html.simpleparser TableWrapper setColWidths.

Prototype

public void setColWidths(final float[] colWidths) 

Source Link

Document

Setter for the column widths

Usage

From source file:com.masscustsoft.service.ToPdf.java

License:Open Source License

/**
 * Gets the TableWrapper from the Stack and adds a new row.
 * @since 5.0.6//ww  w  .  j a  va 2  s.co  m
 */
public void processRow() {
    ArrayList<PdfPCell> row = new ArrayList<PdfPCell>();
    ArrayList<Float> cellWidths = new ArrayList<Float>();
    boolean percentage = false;
    float width;
    float totalWidth = 0;
    int zeroWidth = 0;
    TableWrapper table = null;
    while (true) {
        Element obj = stack.pop();
        if (obj instanceof CellWrapper) {
            CellWrapper cell = (CellWrapper) obj;
            width = cell.getWidth();
            cellWidths.add(new Float(width));
            percentage |= cell.isPercentage();
            if (width == 0) {
                zeroWidth++;
            } else {
                totalWidth += width;
            }
            row.add(cell.getCell());
        }
        if (obj instanceof TableWrapper) {
            table = (TableWrapper) obj;
            break;
        }
    }
    table.addRow(row);
    if (cellWidths.size() > 0) {
        // cells come off the stack in reverse, naturally
        totalWidth = 100 - totalWidth;
        Collections.reverse(cellWidths);
        float[] widths = new float[cellWidths.size()];
        boolean hasZero = false;
        for (int i = 0; i < widths.length; i++) {
            widths[i] = cellWidths.get(i).floatValue();
            if (widths[i] == 0 && percentage && zeroWidth > 0) {
                widths[i] = totalWidth / zeroWidth;
            }
            if (widths[i] == 0) {
                hasZero = true;
                break;
            }
        }
        if (!hasZero)
            table.setColWidths(widths);
    }
    stack.push(table);
}