Last active
October 6, 2018 10:09
-
-
Save jamespeckham/61aaf37a7b98287b67d5 to your computer and use it in GitHub Desktop.
Some basic Itext 2.1.7 PdfPCell, PdfPTable methods with various overload options and multiple columns, along with some methods for calculating averages/percentages, formatting numbers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.math.BigDecimal; | |
import java.math.RoundingMode; | |
import java.text.NumberFormat; | |
import java.util.List; | |
import com.lowagie.text.DocumentException; | |
import com.lowagie.text.Element; | |
import com.lowagie.text.Font; | |
import com.lowagie.text.Paragraph; | |
import com.lowagie.text.pdf.PdfPCell; | |
import com.lowagie.text.pdf.PdfPRow; | |
import com.lowagie.text.pdf.PdfPTable; | |
public class ItextUtils { | |
public static final Font FONT_LARGE_BOLD = new Font(Font.HELVETICA, 14, Font.BOLD); | |
public static final Font FONT_STANDARD_BOLD = new Font(Font.HELVETICA, 9, Font.BOLD); | |
public static final Font FONT_STANDARD_BOLD_UNDERLINE = new Font(Font.HELVETICA, 9, Font.BOLD | Font.UNDERLINE); | |
public static final Font FONT_STANDARD_BOLD_RED = new Font(Font.HELVETICA, 9, Font.BOLD, Color.RED); | |
public static final Font FONT_STANDARD = new Font(Font.HELVETICA, 9); | |
public static final Font FONT_STANDARD_RED = new Font(Font.HELVETICA, 9, Font.NORMAL, Color.RED); | |
public static final Font FONT_STANDARD_ITALIC = new Font(Font.HELVETICA, 9, Font.ITALIC); | |
public static final Color COLOR_HEADER_SECONDARY_CELL = new Color(239,239,239); | |
public static final Color COLOR_TABLE_ALT_ROW = new Color(247,247,247); | |
public static final Color COLOR_TABLE_ROW = new Color(255,255,255); | |
public static String formatNum(BigDecimal bigDecimal) { | |
return NumberFormat.getNumberInstance().format(bigDecimal).toString(); | |
}; | |
public static String calcAve(BigDecimal compNum, BigDecimal totalNum ) { | |
return compNum.divide(totalNum, 1, BigDecimal.ROUND_HALF_UP).toString(); | |
} | |
public static String calcPercent(BigDecimal compNum, BigDecimal totalNum ) { | |
BigDecimal percentage = compNum.divide(totalNum, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)); | |
percentage = percentage.setScale(1, RoundingMode.HALF_UP); | |
return percentage.toString(); | |
} | |
public static String computePercentageChangeText(List<BigDecimal> compareList, List<String> gameDatesShort) { | |
StringBuffer displayString = new StringBuffer(); | |
BigDecimal scale = new BigDecimal(100000000); | |
BigDecimal curYearNumber = compareList.get(0).multiply(scale); | |
int compareListSize = compareList.size(); | |
//Init at 1 because assume first year is the year to compare all others to. | |
for (int i=1; i < compareListSize; i++) { | |
BigDecimal compYearNumber = compareList.get(i).multiply(scale); | |
int year1vsComp = curYearNumber.compareTo(compYearNumber); | |
BigDecimal diff = curYearNumber.subtract(compYearNumber).abs(); | |
int percentage = diff.divide(compYearNumber, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).intValue(); | |
if (year1vsComp == 0 || percentage == 0) { //comp year number is the same as cur year, so no change. | |
displayString.append("No Change over " + gameDatesShort.get(i)); | |
} else if (year1vsComp == -1) { // comp year number is bigger than cur year, so percent down. | |
displayString.append("Down " + percentage + "% over " + gameDatesShort.get(i)); | |
} else if (year1vsComp == 1) { //comp year number is less than cur year, so percent up. | |
displayString.append("Up " + percentage + "% over " + gameDatesShort.get(i)); | |
} | |
if (i+1 < compareListSize) displayString.append(", "); | |
} | |
return displayString.toString(); | |
} | |
public static PdfPTable generateTableSingColPerYear(List<String> gameDatesShort, String headerText, Color headerColor) throws DocumentException { | |
int numCols = gameDatesShort.size(); | |
PdfPTable table = new PdfPTable(numCols); | |
table.setWidthPercentage(100); | |
PdfPCell header = createPdfPCell(headerText, FONT_STANDARD_BOLD, headerColor); | |
header.setColspan(numCols); | |
table.addCell(header); | |
for ( String gameDateShort : gameDatesShort) { | |
table.addCell(createPdfPCell(gameDateShort, FONT_STANDARD_BOLD)); | |
} | |
table.setHeaderRows(2); | |
return table; | |
} | |
public static PdfPTable generateTableSingColPerYrAndDescCol(List<String> gameDatesShort, String headerText, Color headerColor) throws DocumentException { | |
int numCols = gameDatesShort.size() + 1; | |
PdfPTable table = new PdfPTable(numCols); | |
table.setWidthPercentage(100); | |
float[] columnWidths = new float[numCols]; | |
columnWidths[0] = 1.5f; | |
for (int i=1; i < numCols; i++) { | |
columnWidths[i] = 1f; | |
} | |
table.setWidths(columnWidths); | |
PdfPCell header = createPdfPCell(headerText, FONT_STANDARD_BOLD, headerColor); | |
header.setColspan(numCols); | |
table.addCell(header); | |
table.addCell(createPdfPCell("")); | |
for ( String gameDateShort : gameDatesShort) { | |
table.addCell(createPdfPCell(gameDateShort, FONT_STANDARD_BOLD)); | |
} | |
table.setHeaderRows(2); | |
return table; | |
} | |
public static PdfPTable generateTableSingColPerYrAndDescCol(List<String> gameDatesShort, String headerText, Color headerColor, float[] columnWidths ) throws DocumentException { | |
int numCols = gameDatesShort.size() + 1; | |
PdfPTable table = new PdfPTable(numCols); | |
table.setWidthPercentage(100); | |
table.setWidths(columnWidths); | |
PdfPCell header = createPdfPCell(headerText, FONT_STANDARD_BOLD, headerColor); | |
header.setColspan(numCols); | |
table.addCell(header); | |
table.addCell(createPdfPCell("")); | |
for ( String gameDateShort : gameDatesShort) { | |
table.addCell(createPdfPCell(gameDateShort, FONT_STANDARD_BOLD)); | |
} | |
table.setHeaderRows(2); | |
return table; | |
} | |
public static PdfPTable generateTableDoubColPerYearAndDescCol(List<String> gameDatesShort, String headerText, Color headerColor) throws DocumentException { | |
int numCols = gameDatesShort.size() * 2 + 1; | |
PdfPTable table = new PdfPTable(numCols); | |
table.setWidthPercentage(100); | |
float[] columnWidths = new float[numCols]; | |
columnWidths[0] = 3f; | |
for (int i=1; i < numCols; i++) { | |
columnWidths[i] = 1f; | |
} | |
table.setWidths(columnWidths); | |
PdfPCell header = createPdfPCell(headerText, FONT_STANDARD_BOLD, headerColor); | |
header.setColspan(numCols); | |
table.addCell(header); | |
table.addCell(createPdfPCell("")); | |
for ( String gameDateShort : gameDatesShort) { | |
PdfPCell cell = createPdfPCell(gameDateShort, FONT_STANDARD_BOLD); | |
cell.setColspan(2); | |
table.addCell(cell); | |
} | |
table.setHeaderRows(2); | |
return table; | |
} | |
public static PdfPTable generateTableDoubColPerYearAndDescCol(List<String> gameDatesShort, String headerText, Color headerColor, float firstColWidth) throws DocumentException { | |
int numCols = gameDatesShort.size() * 2 + 1; | |
PdfPTable table = new PdfPTable(numCols); | |
table.setWidthPercentage(100); | |
float[] columnWidths = new float[numCols]; | |
columnWidths[0] = firstColWidth; | |
for (int i=1; i < numCols; i++) { | |
columnWidths[i] = 1f; | |
} | |
table.setWidths(columnWidths); | |
PdfPCell header = createPdfPCell(headerText, FONT_STANDARD_BOLD, headerColor); | |
header.setColspan(numCols); | |
table.addCell(header); | |
table.addCell(createPdfPCell("")); | |
for ( String gameDateShort : gameDatesShort) { | |
PdfPCell cell = createPdfPCell(gameDateShort, FONT_STANDARD_BOLD); | |
cell.setColspan(2); | |
table.addCell(cell); | |
} | |
table.setHeaderRows(2); | |
return table; | |
} | |
public static PdfPTable generateTableTripColPerYearAndDescCol(List<String> gameDatesShort, String headerText, Color headerColor) throws DocumentException { | |
int numCols = gameDatesShort.size() * 3 + 1; | |
PdfPTable table = new PdfPTable(numCols); | |
table.setWidthPercentage(100); | |
float[] columnWidths = new float[numCols]; | |
columnWidths[0] = 1f; | |
for( int i=1; i<gameDatesShort.size()+1; i++ ) { | |
columnWidths[i++] = 1.5f; | |
columnWidths[i++] = 1f; | |
columnWidths[i++] = 1f; | |
} | |
table.setWidths(columnWidths); | |
PdfPCell header = createPdfPCell(headerText, FONT_STANDARD_BOLD, headerColor); | |
header.setColspan(numCols); | |
table.addCell(header); | |
table.addCell(createPdfPCell("")); | |
for ( String gameDateShort : gameDatesShort) { | |
PdfPCell cell = createPdfPCell(gameDateShort, FONT_STANDARD_BOLD); | |
cell.setColspan(3); | |
table.addCell(cell); | |
} | |
table.addCell(createPdfPCell("", COLOR_HEADER_SECONDARY_CELL)); | |
for (int j=0; j<gameDatesShort.size(); j++) { | |
table.addCell(createPdfPCell("Total", COLOR_HEADER_SECONDARY_CELL)); | |
table.addCell(createPdfPCell("First 5\nMin.", COLOR_HEADER_SECONDARY_CELL)); | |
table.addCell(createPdfPCell("Last 5\nMin.", COLOR_HEADER_SECONDARY_CELL)); | |
} | |
table.setHeaderRows(3); | |
return table; | |
} | |
@SuppressWarnings("unchecked") | |
public static PdfPTable generateTableNineColPP(String headerText, Color headerColor) throws DocumentException { | |
int numCols = 9; | |
PdfPTable table = new PdfPTable(numCols); | |
table.setWidthPercentage(100); | |
float[] columnWidths = new float[numCols]; | |
columnWidths[0] = 1.5f; | |
columnWidths[1] = 1f; | |
columnWidths[2] = 1f; | |
columnWidths[3] = 1f; | |
columnWidths[4] = 0.5f; | |
columnWidths[5] = 1.5f; | |
columnWidths[6] = 1f; | |
columnWidths[7] = 1f; | |
columnWidths[8] = 1f; | |
table.setWidths(columnWidths); | |
PdfPCell header = createPdfPCell(headerText, FONT_STANDARD_BOLD, headerColor); | |
header.setColspan(9); | |
table.addCell(header); | |
PdfPCell[] secondaryHeaderCells = new PdfPCell[9]; | |
secondaryHeaderCells[0] = createPdfPCell("", FONT_STANDARD_BOLD, COLOR_TABLE_ALT_ROW); | |
secondaryHeaderCells[1] = createPdfPCell("PP Adv.", FONT_STANDARD_BOLD, COLOR_TABLE_ALT_ROW); | |
secondaryHeaderCells[2] = createPdfPCell("GP", FONT_STANDARD_BOLD, COLOR_TABLE_ALT_ROW); | |
secondaryHeaderCells[3] = createPdfPCell("G", FONT_STANDARD_BOLD, COLOR_TABLE_ALT_ROW); | |
secondaryHeaderCells[4] = createPdfPCell("", FONT_STANDARD_BOLD, COLOR_TABLE_ALT_ROW); | |
secondaryHeaderCells[5] = createPdfPCell("", FONT_STANDARD_BOLD, COLOR_TABLE_ALT_ROW); | |
secondaryHeaderCells[6] = createPdfPCell("PP Adv.", FONT_STANDARD_BOLD, COLOR_TABLE_ALT_ROW); | |
secondaryHeaderCells[7] = createPdfPCell("GP", FONT_STANDARD_BOLD, COLOR_TABLE_ALT_ROW); | |
secondaryHeaderCells[8] = createPdfPCell("G", FONT_STANDARD_BOLD, COLOR_TABLE_ALT_ROW); | |
table.getRows().add( new PdfPRow( secondaryHeaderCells ) ); | |
table.setHeaderRows(2); | |
return table; | |
} | |
public static PdfPCell createPdfPCell(String text) { | |
PdfPCell cell = new PdfPCell(new Paragraph(text, FONT_STANDARD)); | |
cell.setBorderWidth(1f); | |
cell.setBorderColor(Color.GRAY); | |
cell.setPadding(2f); | |
cell.setHorizontalAlignment(Element.ALIGN_CENTER); | |
return cell; | |
} | |
public static PdfPCell createPdfPCell(String text, Font font) { | |
PdfPCell cell = new PdfPCell(new Paragraph(text, font)); | |
cell.setBorderWidth(1f); | |
cell.setBorderColor(Color.GRAY); | |
cell.setPadding(2f); | |
cell.setHorizontalAlignment(Element.ALIGN_CENTER); | |
return cell; | |
} | |
public static PdfPCell createPdfPCell(String text, Color color) { | |
PdfPCell cell = new PdfPCell(new Paragraph(text, FONT_STANDARD)); | |
cell.setBackgroundColor(color); | |
cell.setBorderWidth(1f); | |
cell.setBorderColor(Color.GRAY); | |
cell.setPadding(2.0f); | |
cell.setHorizontalAlignment(Element.ALIGN_CENTER); | |
return cell; | |
} | |
public static PdfPCell createPdfPCell(String text, Font font, int align) { | |
PdfPCell cell = new PdfPCell(new Paragraph(text, font)); | |
cell.setBorderWidth(1f); | |
cell.setBorderColor(Color.GRAY); | |
cell.setPadding(2f); | |
cell.setHorizontalAlignment(align); | |
return cell; | |
} | |
public static PdfPCell createPdfPCell(String text, Font font, Color color) { | |
PdfPCell cell = new PdfPCell(new Paragraph(text, font)); | |
cell.setBackgroundColor(color); | |
cell.setBorderWidth(1f); | |
cell.setBorderColor(Color.GRAY); | |
cell.setPadding(2.0f); | |
cell.setHorizontalAlignment(Element.ALIGN_CENTER); | |
return cell; | |
} | |
public static PdfPCell createPdfPCell(String text, Font font, int align, Color color) { | |
PdfPCell cell = new PdfPCell(new Paragraph(text, font)); | |
cell.setBackgroundColor(color); | |
cell.setBorderWidth(1f); | |
cell.setBorderColor(Color.GRAY); | |
cell.setPadding(2.0f); | |
cell.setHorizontalAlignment(align); | |
return cell; | |
} | |
public static PdfPCell createPdfPCell(PdfPTable table) { | |
PdfPCell cell = new PdfPCell(table); | |
cell.setPadding(0.0f); | |
cell.setBorderWidth(0.0f); | |
return cell; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment