Skip to content

Commit 175ca93

Browse files
committed
Merge pull request #1 from dhorions/multi-line-rows
Multi-Line table rows and bookmarks
2 parents f66f048 + 5a46d88 commit 175ca93

File tree

5 files changed

+266
-32
lines changed

5 files changed

+266
-32
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
Quodlibet.be
3+
*/
4+
package be.quodlibet.boxable.examples;
5+
6+
import be.quodlibet.boxable.pdfCell;
7+
import be.quodlibet.boxable.pdfRow;
8+
import be.quodlibet.boxable.pdfTable;
9+
import java.awt.Color;
10+
import java.io.IOException;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
import org.apache.pdfbox.exceptions.COSVisitorException;
14+
import org.apache.pdfbox.pdmodel.PDDocument;
15+
import org.apache.pdfbox.pdmodel.PDPage;
16+
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
17+
import org.apache.pdfbox.pdmodel.font.PDType1Font;
18+
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
19+
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
20+
21+
22+
public class example2
23+
{
24+
public static void main(String[] args)
25+
{
26+
try
27+
{
28+
//Set margins
29+
float Margin = 10;
30+
List<String[]> facts = getFacts();
31+
//A list of bookmarks of all the tables
32+
List<PDOutlineItem> bookmarks = new ArrayList();
33+
//Initialize Document
34+
PDDocument doc = new PDDocument();
35+
PDPage page = addNewPage(doc);
36+
PDPageContentStream pageContentStream = new PDPageContentStream(doc, page);
37+
//Initialize table
38+
float tableWidth = page.findMediaBox().getWidth()-(2*Margin);
39+
float top = page.findMediaBox().getHeight()-(2*Margin);
40+
pdfTable table = new pdfTable( (top - (1* 20f)),Margin, page, pageContentStream);
41+
//Create Header row
42+
pdfRow headerrow = new pdfRow(15f);
43+
pdfCell cell = new pdfCell(tableWidth,"Awesome Facts About Belgium");
44+
cell.setFont(PDType1Font.HELVETICA_BOLD);
45+
cell.setFillColor(Color.BLACK);cell.setTextColor(Color.WHITE);
46+
headerrow.addCell(cell);
47+
table.drawRow(headerrow);
48+
//Create 2 column row
49+
pdfRow row = new pdfRow(15f);
50+
cell = new pdfCell((tableWidth/4) * 3 ,"Source:");
51+
cell.setFont(PDType1Font.HELVETICA);
52+
row.addCell(cell);
53+
cell = new pdfCell((tableWidth/4),"http://www.factsofbelgium.com/");
54+
cell.setFont(PDType1Font.HELVETICA_OBLIQUE);
55+
row.addCell(cell);
56+
table.drawRow(row);
57+
//Create Fact header row
58+
pdfRow factHeaderrow = new pdfRow(15f);
59+
cell = new pdfCell((tableWidth/3) * 2 ,"Fact");
60+
cell.setFont(PDType1Font.HELVETICA);cell.setFontSize(6);
61+
cell.setFillColor(Color.LIGHT_GRAY);
62+
factHeaderrow.addCell(cell);
63+
cell = new pdfCell((tableWidth/3),"Tags");
64+
cell.setFillColor(Color.LIGHT_GRAY);
65+
cell.setFont(PDType1Font.HELVETICA_OBLIQUE);cell.setFontSize(6);
66+
factHeaderrow.addCell(cell);
67+
table.drawRow(factHeaderrow);
68+
69+
//Add multiple rows with random facts about Belgium
70+
int bookmarkid = 0;
71+
for(String[] fact : facts)
72+
{
73+
74+
row = new pdfRow(10f);
75+
cell = new pdfCell((tableWidth/3)*2 ,fact[0]+ " " + fact[0]+ " " + fact[0]);
76+
cell.setFont(PDType1Font.HELVETICA);cell.setFontSize(6);
77+
row.addCell(cell);
78+
//Create a bookmark for each record
79+
PDOutlineItem outlineItem = new PDOutlineItem();
80+
outlineItem.setTitle((++bookmarkid ) + ") " + fact[0]);
81+
row.setBookmark( outlineItem);
82+
83+
for(int i = 1; i< fact.length; i++)
84+
{
85+
cell = new pdfCell(((tableWidth/9)) ,fact[i]);
86+
cell.setFont(PDType1Font.HELVETICA_OBLIQUE);cell.setFontSize(6);
87+
//Set colors
88+
if(fact[i].contains("beer"))cell.setFillColor(Color.yellow);
89+
if(fact[i].contains("champion"))cell.setTextColor(Color.GREEN);
90+
row.addCell(cell);
91+
92+
}
93+
table.drawRow(row);
94+
//Start a new page if needed
95+
if(table.isEndOfPage() )
96+
{
97+
pageContentStream.close();
98+
//Start new table on new page
99+
page = addNewPage(doc);
100+
pageContentStream = new PDPageContentStream(doc, page);
101+
//Get all bookmarks of previous table
102+
bookmarks.addAll(table.getBookmarks());
103+
104+
table = new pdfTable( (top - (1* 20f)),Margin, page, pageContentStream);
105+
106+
//redraw all headers on each page
107+
table.drawRow(headerrow);
108+
table.drawRow(factHeaderrow);
109+
}
110+
111+
}
112+
table.endTable(tableWidth);
113+
//Get all bookmarks of previous table
114+
bookmarks.addAll(table.getBookmarks());
115+
//Close Stream and save pdf
116+
pageContentStream.close();
117+
//Create document outline
118+
PDDocumentOutline outline = new PDDocumentOutline();
119+
for(PDOutlineItem bm : bookmarks)
120+
{
121+
outline.appendChild(bm);
122+
}
123+
doc.getDocumentCatalog().setDocumentOutline(outline);
124+
//Save the document
125+
doc.save("Boxable_example2.pdf");
126+
127+
}
128+
catch (IOException | COSVisitorException io)
129+
{
130+
System.out.println(io.getMessage());
131+
}
132+
}
133+
private static PDPage addNewPage(PDDocument doc)
134+
{
135+
PDPage page = new PDPage();
136+
doc.addPage(page);
137+
return page;
138+
}
139+
private static List<String[]> getFacts()
140+
{
141+
List<String[]> facts = new ArrayList();
142+
facts.add(new String[]{"Oil Painting was invented by the Belgian van Eyck brothers","art","inventions","science"});
143+
facts.add(new String[]{"The Belgian Adolphe Sax invented the Saxophone","inventions","music",""});
144+
facts.add(new String[]{"11 sites in Belgium are on the UNESCO World Heritage List","art","history",""});
145+
facts.add(new String[]{"Belgium was the second country in the world to legalize same-sex marriage","politics","",""});
146+
facts.add(new String[]{"In the seventies, schools served light beer during lunch","beer","health",""});
147+
facts.add(new String[]{"Belgium has the sixth fastest domestic internet connection in the world","science","technology",""});
148+
facts.add(new String[]{"Belgium hosts the World's Largest Sand Sculpture Festival","art","festivals","world championship"});
149+
facts.add(new String[]{"Belgium has compulsary education between the ages of 6 and 18","education","",""});
150+
facts.add(new String[]{"Belgium also has more comic makers per square kilometer than any other country in the world","art","social","world championship"});
151+
facts.add(new String[]{"Belgium has one of the lowest proportion of McDonald's restaurants per inhabitant in the developed world","food","health",""});
152+
facts.add(new String[]{"Belgium has approximately 178 beer breweries","beer","food",""});
153+
facts.add(new String[]{"Gotye was born in Bruges, Belgium","music","celebrities",""});
154+
facts.add(new String[]{"The Belgian Coast Tram is the longest tram line in the world","technology","world championship",""});
155+
facts.add(new String[]{"Stefan Everts is the only motocross racer with 10 World Championship titles.","celebrities","sports","world champions"});
156+
facts.add(new String[]{"Tintin was conceived by Belgian artist Hergé","art","celebrities","inventions"});
157+
facts.add(new String[]{"Brussels Airport is the world's biggest selling point of chocolate","food","world champions",""});
158+
facts.add(new String[]{"Tomorrowland is the biggest electronic dance music festival in the world","festivals","music","world champion"});
159+
facts.add(new String[]{"French Fries are actually from Belgium","food","inventions",""});
160+
facts.add(new String[]{"Herman Van Rompy is the first full-time president of the European Council","politics","",""});
161+
facts.add(new String[]{"Belgians are the fourth most money saving people in the world","economy","social",""});
162+
facts.add(new String[]{"The Belgian highway system is the only man-made structure visible from the moon at night","technology","world champions",""});
163+
facts.add(new String[]{"Andreas Vesalius, the founder of modern human anatomy, is from Belgium","celebrities","education","history"});
164+
facts.add(new String[]{"Napoleon was defeated in Waterloo, Belgium","celebrities","history","politicians"});
165+
facts.add(new String[]{"The first natural color picture in National Geographic was of a flower garden in Gent, Belgium in 1914","art","history","science"});
166+
facts.add(new String[]{"Rock Werchter is the Best Festival in the World","festivals","music","world champions"});
167+
168+
//Make the table a bit bigger
169+
facts.addAll(facts);facts.addAll(facts);facts.addAll(facts);
170+
171+
172+
return facts;
173+
}
174+
}

src/be/quodlibet/boxable/pdfCell.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public pdfCell(float width, String text)
2020
{
2121
this.width = width;
2222
this.text = text;
23+
2324
}
2425

2526
public Color getTextColor()
@@ -81,5 +82,11 @@ public float getFontSize()
8182
public void setFontSize(float fontSize)
8283
{
8384
this.fontSize = fontSize;
84-
}
85+
}
86+
87+
public pdfParagraph getParagraph()
88+
{
89+
return new pdfParagraph( text, font, (int)fontSize, (int)width);
90+
}
91+
8592
}

src/be/quodlibet/boxable/pdfParagraph.java

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,30 @@
1212
import org.apache.pdfbox.pdmodel.font.PDType1Font;
1313

1414
public class pdfParagraph {
15-
/** position X */
16-
private float x;
1715

18-
/** position Y */
19-
private float y;
2016

21-
/** width of this paragraph */
17+
2218
private int width = 500;
2319

24-
/** text to write */
20+
2521
private String text;
26-
27-
/** font to use */
2822
private PDFont font = PDType1Font.HELVETICA;
29-
30-
/** font size to use */
3123
private int fontSize = 10;
3224

3325
private int color = 0;
3426

35-
public pdfParagraph(float x, float y, String text) {
36-
this.x = x;
37-
this.y = y;
27+
public pdfParagraph( String text, PDFont font, int fontSize, int width)
28+
{
3829
this.text = text;
30+
this.font = font;
31+
this.fontSize = fontSize;
32+
this.width = width;
3933
}
4034

41-
/**
42-
* Break the text in lines
43-
* @return
44-
*/
45-
public List<String> getLines() throws IOException {
46-
List<String> result = new ArrayList();//Lists.newArrayList();
35+
36+
public List<String> getLines() throws IOException
37+
{
38+
List<String> result = new ArrayList();
4739

4840
String[] split = text.split("(?<=\\W)");
4941
int[] possibleWrapPoints = new int[split.length];
@@ -94,14 +86,6 @@ public int getColor() {
9486
return color;
9587
}
9688

97-
public float getX() {
98-
return x;
99-
}
100-
101-
public float getY() {
102-
return y;
103-
}
104-
10589
public int getWidth() {
10690
return width;
10791
}

src/be/quodlibet/boxable/pdfRow.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
package be.quodlibet.boxable;
66

77

8+
import java.io.IOException;
89
import java.util.ArrayList;
910
import java.util.List;
11+
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
1012

1113
public class pdfRow {
12-
14+
PDOutlineItem bookmark;
1315
List<pdfCell> cells;
1416
float height;
1517
public pdfRow(List<pdfCell> cells, float height)
@@ -26,9 +28,21 @@ public void addCell(pdfCell cell)
2628
if (cells == null) cells = new ArrayList();
2729
cells.add(cell);
2830
}
29-
public float getHeight()
31+
public float getHeight() throws IOException
32+
{
33+
//return height;
34+
float maxheight = new Float(0);
35+
for( pdfCell cell : this.cells)
36+
{
37+
float cellHeight = ( cell.getParagraph().getLines().size() * this.height);
38+
if(cellHeight > maxheight) maxheight = cellHeight;
39+
}
40+
return maxheight;
41+
}
42+
public float getLineHeight() throws IOException
3043
{
3144
return height;
45+
3246
}
3347

3448
public void setHeight(float height)
@@ -58,6 +72,16 @@ public float getWidth()
5872
return totalWidth;
5973
}
6074

75+
public PDOutlineItem getBookmark()
76+
{
77+
return bookmark;
78+
}
79+
80+
public void setBookmark(PDOutlineItem bookmark)
81+
{
82+
this.bookmark = bookmark;
83+
}
84+
6185

6286

6387
}

0 commit comments

Comments
 (0)