public boolean fits() { final boolean simulate = true; // try adding into a single infinite column to calc height ColumnTextBuilder ctb = b.reuseColumnTextBuilder(); b.reuseRectangleBuilder(tempR).copyPositionsFrom(rectangle); tempR.setBottom(-10000f); tempR.setRight(horCenter()); applyPadding(tempR, true); ctb.setSimpleColumn(tempR); float yBefore = ctb.getYLine(); contentAdder.add(ctb, simulate); int status = ctb.go(simulate); if (ColumnText.hasMoreText(status)) { return false; } float yAfter = ctb.getYLine(); final float height = yBefore - yAfter; logger.trace("height: {}", height); // if(height > rectangle.getHeight()) { // return false; // } // now try adding into two actual columns final float columnHeight = height / 2 + additionalSpaceForColumn; if (!addToTwoColumns(simulate, columnHeight)) { return false; } addToTwoColumns(false, columnHeight); return true; }
private boolean addToTwoColumns(boolean simulate, float height) { ColumnTextBuilder ctb; int status; ctb = b.reuseColumnTextBuilder(); b.reuseRectangleBuilder(tempR).copyPositionsFrom(rectangle); final float horCenter = horCenter(); setColumn(height, horCenter, true, ctb, simulate); contentAdder.add(ctb, simulate); status = ctb.go(simulate); yLine = ctb.getYLine(); if (!ColumnText.hasMoreText(status) && simulate) { return true; } setColumn(height, horCenter, false, ctb, simulate); status = ctb.go(simulate); yLine = Math.min(yLine, ctb.getYLine()); // right column may not fit the rect, check if does fit the page if (simulate) { if (yLine < rectangle.getBottom()) { return false; } } return !ColumnText.hasMoreText(status); }