|
| 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 | +} |
0 commit comments