From 35bbc3d74180b42d51a0937022ef3da8bdbd42a8 Mon Sep 17 00:00:00 2001 From: haduart Date: Sun, 5 Apr 2015 12:06:24 +0200 Subject: [PATCH 1/2] Adding maven structure, so now it could be done using maven: mvn clean compile exec:exec --- pom.xml | 55 ++++ .../java}/pdftablesample/Column.java | 56 ++-- .../java}/pdftablesample/PDFSample.java | 0 .../pdftablesample/PDFTableGenerator.java | 262 ++++++++--------- src/{ => main/java}/pdftablesample/Table.java | 274 +++++++++--------- .../java}/pdftablesample/TableBuilder.java | 140 ++++----- 6 files changed, 421 insertions(+), 366 deletions(-) create mode 100644 pom.xml rename src/{ => main/java}/pdftablesample/Column.java (94%) rename src/{ => main/java}/pdftablesample/PDFSample.java (100%) rename src/{ => main/java}/pdftablesample/PDFTableGenerator.java (97%) rename src/{ => main/java}/pdftablesample/Table.java (95%) rename src/{ => main/java}/pdftablesample/TableBuilder.java (95%) diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ce93cf2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + pdftablesample + paginated-pdfbox-table + + 1.0.0-SNAPSHOT + Paginated PDFBox Table Sample + jar + + + + 1.8.9 + + + + + org.apache.pdfbox + pdfbox + ${pdfbox.version} + + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + + exec + + + + + java + ${project.build.directory}/exec-working-directory + + + -classpath + + + pdftablesample.PDFSample + + + + + + + diff --git a/src/pdftablesample/Column.java b/src/main/java/pdftablesample/Column.java similarity index 94% rename from src/pdftablesample/Column.java rename to src/main/java/pdftablesample/Column.java index 51256f8..8598722 100755 --- a/src/pdftablesample/Column.java +++ b/src/main/java/pdftablesample/Column.java @@ -1,28 +1,28 @@ -package pdftablesample; - -public class Column { - - private String name; - private float width; - - public Column(String name, float width) { - this.name = name; - this.width = width; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public float getWidth() { - return width; - } - - public void setWidth(float width) { - this.width = width; - } -} +package pdftablesample; + +public class Column { + + private String name; + private float width; + + public Column(String name, float width) { + this.name = name; + this.width = width; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public float getWidth() { + return width; + } + + public void setWidth(float width) { + this.width = width; + } +} diff --git a/src/pdftablesample/PDFSample.java b/src/main/java/pdftablesample/PDFSample.java similarity index 100% rename from src/pdftablesample/PDFSample.java rename to src/main/java/pdftablesample/PDFSample.java diff --git a/src/pdftablesample/PDFTableGenerator.java b/src/main/java/pdftablesample/PDFTableGenerator.java similarity index 97% rename from src/pdftablesample/PDFTableGenerator.java rename to src/main/java/pdftablesample/PDFTableGenerator.java index 23e6175..7293443 100755 --- a/src/pdftablesample/PDFTableGenerator.java +++ b/src/main/java/pdftablesample/PDFTableGenerator.java @@ -1,131 +1,131 @@ -package pdftablesample; - -import java.io.IOException; -import java.util.Arrays; - -import org.apache.pdfbox.exceptions.COSVisitorException; -import org.apache.pdfbox.pdmodel.PDDocument; -import org.apache.pdfbox.pdmodel.PDPage; -import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; - -public class PDFTableGenerator { - - // Generates document from Table object - public void generatePDF(Table table) throws IOException, COSVisitorException { - PDDocument doc = null; - try { - doc = new PDDocument(); - drawTable(doc, table); - doc.save("sample.pdf"); - } finally { - if (doc != null) { - doc.close(); - } - } - } - - // Configures basic setup for the table and draws it page by page - public void drawTable(PDDocument doc, Table table) throws IOException { - // Calculate pagination - Integer rowsPerPage = new Double(Math.floor(table.getHeight() / table.getRowHeight())).intValue() - 1; // subtract - Integer numberOfPages = new Double(Math.ceil(table.getNumberOfRows().floatValue() / rowsPerPage)).intValue(); - - // Generate each page, get the content and draw it - for (int pageCount = 0; pageCount < numberOfPages; pageCount++) { - PDPage page = generatePage(doc, table); - PDPageContentStream contentStream = generateContentStream(doc, page, table); - String[][] currentPageContent = getContentForCurrentPage(table, rowsPerPage, pageCount); - drawCurrentPage(table, currentPageContent, contentStream); - } - } - - // Draws current page table grid and border lines and content - private void drawCurrentPage(Table table, String[][] currentPageContent, PDPageContentStream contentStream) - throws IOException { - float tableTopY = table.isLandscape() ? table.getPageSize().getWidth() - table.getMargin() : table.getPageSize().getHeight() - table.getMargin(); - - // Draws grid and borders - drawTableGrid(table, currentPageContent, contentStream, tableTopY); - - // Position cursor to start drawing content - float nextTextX = table.getMargin() + table.getCellMargin(); - // Calculate center alignment for text in cell considering font height - float nextTextY = tableTopY - (table.getRowHeight() / 2) - - ((table.getTextFont().getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * table.getFontSize()) / 4); - - // Write column headers - writeContentLine(table.getColumnsNamesAsArray(), contentStream, nextTextX, nextTextY, table); - nextTextY -= table.getRowHeight(); - nextTextX = table.getMargin() + table.getCellMargin(); - - // Write content - for (int i = 0; i < currentPageContent.length; i++) { - writeContentLine(currentPageContent[i], contentStream, nextTextX, nextTextY, table); - nextTextY -= table.getRowHeight(); - nextTextX = table.getMargin() + table.getCellMargin(); - } - - contentStream.close(); - } - - // Writes the content for one line - private void writeContentLine(String[] lineContent, PDPageContentStream contentStream, float nextTextX, float nextTextY, - Table table) throws IOException { - for (int i = 0; i < table.getNumberOfColumns(); i++) { - String text = lineContent[i]; - contentStream.beginText(); - contentStream.moveTextPositionByAmount(nextTextX, nextTextY); - contentStream.drawString(text != null ? text : ""); - contentStream.endText(); - nextTextX += table.getColumns().get(i).getWidth(); - } - } - - private void drawTableGrid(Table table, String[][] currentPageContent, PDPageContentStream contentStream, float tableTopY) - throws IOException { - // Draw row lines - float nextY = tableTopY; - for (int i = 0; i <= currentPageContent.length + 1; i++) { - contentStream.drawLine(table.getMargin(), nextY, table.getMargin() + table.getWidth(), nextY); - nextY -= table.getRowHeight(); - } - - // Draw column lines - final float tableYLength = table.getRowHeight() + (table.getRowHeight() * currentPageContent.length); - final float tableBottomY = tableTopY - tableYLength; - float nextX = table.getMargin(); - for (int i = 0; i < table.getNumberOfColumns(); i++) { - contentStream.drawLine(nextX, tableTopY, nextX, tableBottomY); - nextX += table.getColumns().get(i).getWidth(); - } - contentStream.drawLine(nextX, tableTopY, nextX, tableBottomY); - } - - private String[][] getContentForCurrentPage(Table table, Integer rowsPerPage, int pageCount) { - int startRange = pageCount * rowsPerPage; - int endRange = (pageCount * rowsPerPage) + rowsPerPage; - if (endRange > table.getNumberOfRows()) { - endRange = table.getNumberOfRows(); - } - return Arrays.copyOfRange(table.getContent(), startRange, endRange); - } - - private PDPage generatePage(PDDocument doc, Table table) { - PDPage page = new PDPage(); - page.setMediaBox(table.getPageSize()); - page.setRotation(table.isLandscape() ? 90 : 0); - doc.addPage(page); - return page; - } - - private PDPageContentStream generateContentStream(PDDocument doc, PDPage page, Table table) throws IOException { - PDPageContentStream contentStream = new PDPageContentStream(doc, page, false, false); - // User transformation matrix to change the reference when drawing. - // This is necessary for the landscape position to draw correctly - if (table.isLandscape()) { - contentStream.concatenate2CTM(0, 1, -1, 0, table.getPageSize().getWidth(), 0); - } - contentStream.setFont(table.getTextFont(), table.getFontSize()); - return contentStream; - } -} +package pdftablesample; + +import java.io.IOException; +import java.util.Arrays; + +import org.apache.pdfbox.exceptions.COSVisitorException; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; + +public class PDFTableGenerator { + + // Generates document from Table object + public void generatePDF(Table table) throws IOException, COSVisitorException { + PDDocument doc = null; + try { + doc = new PDDocument(); + drawTable(doc, table); + doc.save("sample.pdf"); + } finally { + if (doc != null) { + doc.close(); + } + } + } + + // Configures basic setup for the table and draws it page by page + public void drawTable(PDDocument doc, Table table) throws IOException { + // Calculate pagination + Integer rowsPerPage = new Double(Math.floor(table.getHeight() / table.getRowHeight())).intValue() - 1; // subtract + Integer numberOfPages = new Double(Math.ceil(table.getNumberOfRows().floatValue() / rowsPerPage)).intValue(); + + // Generate each page, get the content and draw it + for (int pageCount = 0; pageCount < numberOfPages; pageCount++) { + PDPage page = generatePage(doc, table); + PDPageContentStream contentStream = generateContentStream(doc, page, table); + String[][] currentPageContent = getContentForCurrentPage(table, rowsPerPage, pageCount); + drawCurrentPage(table, currentPageContent, contentStream); + } + } + + // Draws current page table grid and border lines and content + private void drawCurrentPage(Table table, String[][] currentPageContent, PDPageContentStream contentStream) + throws IOException { + float tableTopY = table.isLandscape() ? table.getPageSize().getWidth() - table.getMargin() : table.getPageSize().getHeight() - table.getMargin(); + + // Draws grid and borders + drawTableGrid(table, currentPageContent, contentStream, tableTopY); + + // Position cursor to start drawing content + float nextTextX = table.getMargin() + table.getCellMargin(); + // Calculate center alignment for text in cell considering font height + float nextTextY = tableTopY - (table.getRowHeight() / 2) + - ((table.getTextFont().getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * table.getFontSize()) / 4); + + // Write column headers + writeContentLine(table.getColumnsNamesAsArray(), contentStream, nextTextX, nextTextY, table); + nextTextY -= table.getRowHeight(); + nextTextX = table.getMargin() + table.getCellMargin(); + + // Write content + for (int i = 0; i < currentPageContent.length; i++) { + writeContentLine(currentPageContent[i], contentStream, nextTextX, nextTextY, table); + nextTextY -= table.getRowHeight(); + nextTextX = table.getMargin() + table.getCellMargin(); + } + + contentStream.close(); + } + + // Writes the content for one line + private void writeContentLine(String[] lineContent, PDPageContentStream contentStream, float nextTextX, float nextTextY, + Table table) throws IOException { + for (int i = 0; i < table.getNumberOfColumns(); i++) { + String text = lineContent[i]; + contentStream.beginText(); + contentStream.moveTextPositionByAmount(nextTextX, nextTextY); + contentStream.drawString(text != null ? text : ""); + contentStream.endText(); + nextTextX += table.getColumns().get(i).getWidth(); + } + } + + private void drawTableGrid(Table table, String[][] currentPageContent, PDPageContentStream contentStream, float tableTopY) + throws IOException { + // Draw row lines + float nextY = tableTopY; + for (int i = 0; i <= currentPageContent.length + 1; i++) { + contentStream.drawLine(table.getMargin(), nextY, table.getMargin() + table.getWidth(), nextY); + nextY -= table.getRowHeight(); + } + + // Draw column lines + final float tableYLength = table.getRowHeight() + (table.getRowHeight() * currentPageContent.length); + final float tableBottomY = tableTopY - tableYLength; + float nextX = table.getMargin(); + for (int i = 0; i < table.getNumberOfColumns(); i++) { + contentStream.drawLine(nextX, tableTopY, nextX, tableBottomY); + nextX += table.getColumns().get(i).getWidth(); + } + contentStream.drawLine(nextX, tableTopY, nextX, tableBottomY); + } + + private String[][] getContentForCurrentPage(Table table, Integer rowsPerPage, int pageCount) { + int startRange = pageCount * rowsPerPage; + int endRange = (pageCount * rowsPerPage) + rowsPerPage; + if (endRange > table.getNumberOfRows()) { + endRange = table.getNumberOfRows(); + } + return Arrays.copyOfRange(table.getContent(), startRange, endRange); + } + + private PDPage generatePage(PDDocument doc, Table table) { + PDPage page = new PDPage(); + page.setMediaBox(table.getPageSize()); + page.setRotation(table.isLandscape() ? 90 : 0); + doc.addPage(page); + return page; + } + + private PDPageContentStream generateContentStream(PDDocument doc, PDPage page, Table table) throws IOException { + PDPageContentStream contentStream = new PDPageContentStream(doc, page, false, false); + // User transformation matrix to change the reference when drawing. + // This is necessary for the landscape position to draw correctly + if (table.isLandscape()) { + contentStream.concatenate2CTM(0, 1, -1, 0, table.getPageSize().getWidth(), 0); + } + contentStream.setFont(table.getTextFont(), table.getFontSize()); + return contentStream; + } +} diff --git a/src/pdftablesample/Table.java b/src/main/java/pdftablesample/Table.java similarity index 95% rename from src/pdftablesample/Table.java rename to src/main/java/pdftablesample/Table.java index fffef20..854d1b1 100755 --- a/src/pdftablesample/Table.java +++ b/src/main/java/pdftablesample/Table.java @@ -1,137 +1,137 @@ -package pdftablesample; - -import java.util.List; - -import org.apache.pdfbox.pdmodel.common.PDRectangle; -import org.apache.pdfbox.pdmodel.font.PDFont; - -public class Table { - - // Table attributes - private float margin; - private float height; - private PDRectangle pageSize; - private boolean isLandscape; - private float rowHeight; - - // font attributes - private PDFont textFont; - private float fontSize; - - // Content attributes - private Integer numberOfRows; - private List columns; - private String[][] content; - private float cellMargin; - - public Table() { - } - - public Integer getNumberOfColumns() { - return this.getColumns().size(); - } - - public float getWidth() { - float tableWidth = 0f; - for (Column column : columns) { - tableWidth += column.getWidth(); - } - return tableWidth; - } - - public float getMargin() { - return margin; - } - - public void setMargin(float margin) { - this.margin = margin; - } - - public PDRectangle getPageSize() { - return pageSize; - } - - public void setPageSize(PDRectangle pageSize) { - this.pageSize = pageSize; - } - - public PDFont getTextFont() { - return textFont; - } - - public void setTextFont(PDFont textFont) { - this.textFont = textFont; - } - - public float getFontSize() { - return fontSize; - } - - public void setFontSize(float fontSize) { - this.fontSize = fontSize; - } - - public String[] getColumnsNamesAsArray() { - String[] columnNames = new String[getNumberOfColumns()]; - for (int i = 0; i < getNumberOfColumns() - 1; i++) { - columnNames[i] = columns.get(i).getName(); - } - return columnNames; - } - - public List getColumns() { - return columns; - } - - public void setColumns(List columns) { - this.columns = columns; - } - - public Integer getNumberOfRows() { - return numberOfRows; - } - - public void setNumberOfRows(Integer numberOfRows) { - this.numberOfRows = numberOfRows; - } - - public float getHeight() { - return height; - } - - public void setHeight(float height) { - this.height = height; - } - - public float getRowHeight() { - return rowHeight; - } - - public void setRowHeight(float rowHeight) { - this.rowHeight = rowHeight; - } - - public String[][] getContent() { - return content; - } - - public void setContent(String[][] content) { - this.content = content; - } - - public float getCellMargin() { - return cellMargin; - } - - public void setCellMargin(float cellMargin) { - this.cellMargin = cellMargin; - } - - public boolean isLandscape() { - return isLandscape; - } - - public void setLandscape(boolean isLandscape) { - this.isLandscape = isLandscape; - } -} +package pdftablesample; + +import java.util.List; + +import org.apache.pdfbox.pdmodel.common.PDRectangle; +import org.apache.pdfbox.pdmodel.font.PDFont; + +public class Table { + + // Table attributes + private float margin; + private float height; + private PDRectangle pageSize; + private boolean isLandscape; + private float rowHeight; + + // font attributes + private PDFont textFont; + private float fontSize; + + // Content attributes + private Integer numberOfRows; + private List columns; + private String[][] content; + private float cellMargin; + + public Table() { + } + + public Integer getNumberOfColumns() { + return this.getColumns().size(); + } + + public float getWidth() { + float tableWidth = 0f; + for (Column column : columns) { + tableWidth += column.getWidth(); + } + return tableWidth; + } + + public float getMargin() { + return margin; + } + + public void setMargin(float margin) { + this.margin = margin; + } + + public PDRectangle getPageSize() { + return pageSize; + } + + public void setPageSize(PDRectangle pageSize) { + this.pageSize = pageSize; + } + + public PDFont getTextFont() { + return textFont; + } + + public void setTextFont(PDFont textFont) { + this.textFont = textFont; + } + + public float getFontSize() { + return fontSize; + } + + public void setFontSize(float fontSize) { + this.fontSize = fontSize; + } + + public String[] getColumnsNamesAsArray() { + String[] columnNames = new String[getNumberOfColumns()]; + for (int i = 0; i < getNumberOfColumns() - 1; i++) { + columnNames[i] = columns.get(i).getName(); + } + return columnNames; + } + + public List getColumns() { + return columns; + } + + public void setColumns(List columns) { + this.columns = columns; + } + + public Integer getNumberOfRows() { + return numberOfRows; + } + + public void setNumberOfRows(Integer numberOfRows) { + this.numberOfRows = numberOfRows; + } + + public float getHeight() { + return height; + } + + public void setHeight(float height) { + this.height = height; + } + + public float getRowHeight() { + return rowHeight; + } + + public void setRowHeight(float rowHeight) { + this.rowHeight = rowHeight; + } + + public String[][] getContent() { + return content; + } + + public void setContent(String[][] content) { + this.content = content; + } + + public float getCellMargin() { + return cellMargin; + } + + public void setCellMargin(float cellMargin) { + this.cellMargin = cellMargin; + } + + public boolean isLandscape() { + return isLandscape; + } + + public void setLandscape(boolean isLandscape) { + this.isLandscape = isLandscape; + } +} diff --git a/src/pdftablesample/TableBuilder.java b/src/main/java/pdftablesample/TableBuilder.java similarity index 95% rename from src/pdftablesample/TableBuilder.java rename to src/main/java/pdftablesample/TableBuilder.java index 4d55545..47be4c3 100755 --- a/src/pdftablesample/TableBuilder.java +++ b/src/main/java/pdftablesample/TableBuilder.java @@ -1,70 +1,70 @@ -package pdftablesample; - -import java.util.List; - -import org.apache.pdfbox.pdmodel.common.PDRectangle; -import org.apache.pdfbox.pdmodel.font.PDFont; - -public class TableBuilder { - - private Table table = new Table(); - - public TableBuilder setHeight(float height) { - table.setHeight(height); - return this; - } - - public TableBuilder setNumberOfRows(Integer numberOfRows) { - table.setNumberOfRows(numberOfRows); - return this; - } - - public TableBuilder setRowHeight(float rowHeight) { - table.setRowHeight(rowHeight); - return this; - } - - public TableBuilder setContent(String[][] content) { - table.setContent(content); - return this; - } - - public TableBuilder setColumns(List columns) { - table.setColumns(columns); - return this; - } - - public TableBuilder setCellMargin(float cellMargin) { - table.setCellMargin(cellMargin); - return this; - } - - public TableBuilder setMargin(float margin) { - table.setMargin(margin); - return this; - } - - public TableBuilder setPageSize(PDRectangle pageSize) { - table.setPageSize(pageSize); - return this; - } - - public TableBuilder setLandscape(boolean landscape) { - table.setLandscape(landscape); - return this; - } - - public TableBuilder setTextFont(PDFont textFont) { - table.setTextFont(textFont); - return this; - } - - public TableBuilder setFontSize(float fontSize) { - table.setFontSize(fontSize); - return this; - } - - public Table build() { - return table; - } -} +package pdftablesample; + +import java.util.List; + +import org.apache.pdfbox.pdmodel.common.PDRectangle; +import org.apache.pdfbox.pdmodel.font.PDFont; + +public class TableBuilder { + + private Table table = new Table(); + + public TableBuilder setHeight(float height) { + table.setHeight(height); + return this; + } + + public TableBuilder setNumberOfRows(Integer numberOfRows) { + table.setNumberOfRows(numberOfRows); + return this; + } + + public TableBuilder setRowHeight(float rowHeight) { + table.setRowHeight(rowHeight); + return this; + } + + public TableBuilder setContent(String[][] content) { + table.setContent(content); + return this; + } + + public TableBuilder setColumns(List columns) { + table.setColumns(columns); + return this; + } + + public TableBuilder setCellMargin(float cellMargin) { + table.setCellMargin(cellMargin); + return this; + } + + public TableBuilder setMargin(float margin) { + table.setMargin(margin); + return this; + } + + public TableBuilder setPageSize(PDRectangle pageSize) { + table.setPageSize(pageSize); + return this; + } + + public TableBuilder setLandscape(boolean landscape) { + table.setLandscape(landscape); + return this; + } + + public TableBuilder setTextFont(PDFont textFont) { + table.setTextFont(textFont); + return this; + } + + public TableBuilder setFontSize(float fontSize) { + table.setFontSize(fontSize); + return this; + } + + public Table build() { + return table; + } +} From 424194ab9b9d0f6cfea78ab7d9f7ccd419b18046 Mon Sep 17 00:00:00 2001 From: haduart Date: Sun, 5 Apr 2015 12:09:35 +0200 Subject: [PATCH 2/2] Adding maven structure, so now it could be done using maven: mvn clean compile exec:exec --- src/main/java/pdftablesample/PDFTableGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/pdftablesample/PDFTableGenerator.java b/src/main/java/pdftablesample/PDFTableGenerator.java index 7293443..ca43767 100755 --- a/src/main/java/pdftablesample/PDFTableGenerator.java +++ b/src/main/java/pdftablesample/PDFTableGenerator.java @@ -70,7 +70,7 @@ private void drawCurrentPage(Table table, String[][] currentPageContent, PDPageC // Writes the content for one line private void writeContentLine(String[] lineContent, PDPageContentStream contentStream, float nextTextX, float nextTextY, - Table table) throws IOException { + Table table) throws IOException { for (int i = 0; i < table.getNumberOfColumns(); i++) { String text = lineContent[i]; contentStream.beginText();