Skip to content

Commit

Permalink
Merge pull request #44 from dhorions/csv_handling
Browse files Browse the repository at this point in the history
DataTable handling
  • Loading branch information
dhorions committed Apr 5, 2016
2 parents f032471 + d7d9603 commit 12367a8
Show file tree
Hide file tree
Showing 6 changed files with 787 additions and 46 deletions.
105 changes: 73 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,97 @@
boxable
[Boxable](http://dhorions.github.io/boxable/) - A java library to build tables in PDF documents.
=======
http://dhorions.github.io/boxable/

Create tables in pdf documents using [PDFBox](http://pdfbox.apache.org)

Here are a few examples of the type of tables boxable can created :
Boxable is a library that can be used to easily create tables in pdf documents. It uses the [PDFBox](https://pdfbox.apache.org/) PDF library under the hood.

* [Table with text and images](https://s3.amazonaws.com/misc.quodlibet.be/Boxable/BoxableSample1.pdf)
![alt text](https://s3.amazonaws.com/misc.quodlibet.be/Boxable/sample1_preview.png)
* [Vertical and Horizontal text](https://s3.amazonaws.com/misc.quodlibet.be/Boxable/BoxableSample3.pdf)
![alt text](https://s3.amazonaws.com/misc.quodlibet.be/Boxable/sample3_preview.png)
# Features

- Build tables in pdf documents
- Convert csv data into tables in pdf documents
- Convert Lists into tables in pdf documents

Example code can be found [here](https://github.com/dhorions/boxable/blob/master/src/test/java/be/quodlibet/boxable/TableTest.java)

The project is Java 7 compliant.

Maven :
The project can be used as a Maven dependency :
# Maven
```xml
<dependency>
<groupId>com.github.dhorions</groupId>
<artifactId>boxable</artifactId>
<version>1.3</version>
<version>1.4</version>
</dependency>
```
For other build systems, check the [Maven Central Repository](http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22boxable%22).


The project can also be used as a Maven dependency by using jitpack.io.
* add the repository :
```xml
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
# Tutorial

A tutorial is being created and will be accessible at http://dhorions.github.io/boxable/.
If you want to help, please let us know [here](https://github.com/dhorions/boxable/issues/41).

# Usage examples

## Create a pdf from a csv file

```java
String data = readData("https://s3.amazonaws.com/misc.quodlibet.be/Boxable/Eurostat_Immigration_Applications.csv");
BaseTable pdfTable = new BaseTable(yStart, yStartNewPage, bottomMargin, tableWidth, margin, doc, page, true,true);
DataTable t = new DataTable(pdfTable, page);
t.addCsvToTable(data, DataTable.HASHEADER, ';');
pdfTable.draw();
```
* add the dependency
```xml
<dependencies>
<dependency>
<groupId>com.github.dhorions</groupId>
<artifactId>boxable</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
Output : [CSVExamplePortrait.pdf](https://s3.amazonaws.com/misc.quodlibet.be/Boxable/CSVexamplePortrait.pdf)

## Create a pdf from a List

```java
List<List> data = new ArrayList();
data.add(new ArrayList<>(
Arrays.asList("Column One", "Column Two", "Column Three", "Column Four", "Column Five")));
for (int i = 1; i <= 100; i++) {
data.add(new ArrayList<>(
Arrays.asList("Row " + i + " Col One", "Row " + i + " Col Two", "Row " + i + " Col Three", "Row " + i + " Col Four", "Row " + i + " Col Five")));
BaseTable dataTable = new BaseTable(yStart, yStartNewPage, bottomMargin, tableWidth, margin, doc, page, true, true);
DataTable t = new DataTable(dataTable, page);
t.addListToTable(data, DataTable.HASHEADER);
dataTable.draw();
}
```
Output : [ListExampleLandscape.pdf](https://s3.amazonaws.com/misc.quodlibet.be/Boxable/ListExampleLandscape.pdf)

##Build tables in pdf documents

```java
BaseTable table = new BaseTable(yStart, yStartNewPage, bottomMargin, tableWidth, margin, doc, page, true,
drawContent);
//Create Header row
Row<PDPage> headerRow = table.createRow(15f);
Cell<PDPage> cell = headerRow.createCell(100, "Awesome Facts About Belgium");
cell.setFont(PDType1Font.HELVETICA_BOLD);
cell.setFillColor(Color.BLACK);
table.addHeaderRow(headerRow);
List<String[]> facts = getFacts();
for (String[] fact : facts) {
Row<PDPage> row = table.createRow(10f);
cell = row.createCell((100 / 3.0f) * 2, fact[0] );
for (int i = 1; i < fact.length; i++) {
cell = row.createCell((100 / 9f), fact[i]);
}
}
table.draw();
```

Special Thanks to [dgautier](https://github.com/dgautier),[ZeerDonker](https://github.com/ZeerDonker),[Frulenzo](https://github.com/Frulenzo),[dobluth](https://github.com/dobluth) and [schmitzhermes](https://github.com/schmitzhermes) for their valuable contributions.



Special Thanks to these awesome contributers :
- [dgautier](https://github.com/dgautier)
- [ZeerDonker](https://github.com/ZeerDonker)
- [Frulenzo](https://github.com/Frulenzo)
- [dobluth](https://github.com/dobluth)
- [schmitzhermes](https://github.com/schmitzhermes)

=======

Copyright [2016] [Quodlibet.be]
Copyright [2016](Quodlibet.be)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
19 changes: 14 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.dhorions</groupId>
<artifactId>boxable</artifactId>
<version>1.3</version>
<version>1.4</version>
<packaging>jar</packaging>

<name>Boxable, a high-level API to creates table on top of Apache Pdfbox</name>
Expand All @@ -22,11 +22,9 @@
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.0-RC3</version>
<version>2.0.0</version>
<scope>compile</scope>
</dependency>


<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -39,14 +37,25 @@
<version>18.0</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement>
<snapshotRepository>
Expand Down
78 changes: 75 additions & 3 deletions src/main/java/be/quodlibet/boxable/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public class Cell<T extends PDPage> {

private boolean textRotated = false;

private final HorizontalAlignment align;
private final VerticalAlignment valign;
private HorizontalAlignment align;
private VerticalAlignment valign;

float horizontalFreeSpace = 0;
float verticalFreeSpace = 0;
Expand Down Expand Up @@ -654,5 +654,77 @@ public boolean isColspanCell() {

public void setColspanCell(boolean isColspanCell) {
this.isColspanCell = isColspanCell;
}
}

public void setAlign(HorizontalAlignment align)
{
this.align = align;
}

public void setValign(VerticalAlignment valign)
{
this.valign = valign;
}

/**
* <p>
* Copies the style of an existing cell to this cell
* </p>
*
* @param sourceCell
*/
public void copyCellStyle(Cell sourceCell)
{
Boolean leftBorder = this.leftBorderStyle == null;
setBorderStyle(sourceCell.getTopBorder());
if (leftBorder) {
this.leftBorderStyle = null;//if left border wasn't set, don't set it now
}
this.font = sourceCell.getFont();//otherwise paragraph gets invalidated
this.fontBold = sourceCell.getFontBold();
setFillColor(sourceCell.getFillColor());
setTextColor(sourceCell.getTextColor());
setAlign(sourceCell.getAlign());
setValign(sourceCell.getValign());
}
/**
* <p>
* Compares the style of a cell with another cell
* </p>
*
* @param sourceCell
* @return
*/
public Boolean hasSameStyle(Cell sourceCell)
{
if (!sourceCell.getTopBorder().equals(getTopBorder())) {
return false;
}
if (!sourceCell.getFont().equals(getFont())) {
return false;
}
if (!sourceCell.getFontBold().equals(getFontBold())) {
return false;
}
if (!sourceCell.getFillColor().equals(getFillColor())) {
return false;
}
if (!sourceCell.getTextColor().equals(getTextColor())) {
return false;
}
if (!sourceCell.getAlign().equals(getAlign())) {
return false;
}
if (!sourceCell.getValign().equals(getValign())) {
return false;
}
return true;
}


public void setWidth(float width)
{
this.width = width;
}

}
Loading

0 comments on commit 12367a8

Please sign in to comment.