|
| 1 | +/******************************************************************************** |
| 2 | + * Copyright (c) 2019 [Open Lowcode SAS](https://openlowcode.com/) |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0 . |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + ********************************************************************************/ |
| 10 | + |
| 11 | +package org.openlowcode.samples.pdf; |
| 12 | + |
| 13 | +import java.awt.Color; |
| 14 | +import java.io.File; |
| 15 | +import java.io.FileOutputStream; |
| 16 | +import java.io.IOException; |
| 17 | + |
| 18 | +import org.openlowcode.tools.pdf.BulletText; |
| 19 | +import org.openlowcode.tools.pdf.DocumentTableOfContent; |
| 20 | +import org.openlowcode.tools.pdf.PDFDocument; |
| 21 | +import org.openlowcode.tools.pdf.PDFMultiPageTable; |
| 22 | +import org.openlowcode.tools.pdf.PDFPage; |
| 23 | +import org.openlowcode.tools.pdf.PDFPageBand; |
| 24 | +import org.openlowcode.tools.pdf.PDFPageBandHeaders; |
| 25 | +import org.openlowcode.tools.pdf.ParagraphHeader; |
| 26 | +import org.openlowcode.tools.pdf.SectionHeader; |
| 27 | +import org.openlowcode.tools.pdf.SectionText; |
| 28 | + |
| 29 | +/** |
| 30 | + * Provides an example of using the Open Lowcode PDF framework to build a |
| 31 | + * multi-page PDF document. This example aims at using all features of the |
| 32 | + * framework except images (as this is more complex to run). This generates a |
| 33 | + * file called Openlowcodepdf-sample.pdf in the root execution folder of the |
| 34 | + * application |
| 35 | + * |
| 36 | + * @author <a href="https://openlowcode.com/">Open Lowcode SAS</a> |
| 37 | + * |
| 38 | + */ |
| 39 | +public class SampleOpenLowcodePDFDocument { |
| 40 | + |
| 41 | + public static void main(String[] args) { |
| 42 | + try { |
| 43 | + // first step: declare the document |
| 44 | + PDFDocument document = new PDFDocument(); |
| 45 | + // second step: add content |
| 46 | + feedcontent(document); |
| 47 | + // third step: output to file |
| 48 | + File outputfile = new File("Openlowcodepdf-sample.pdf"); |
| 49 | + FileOutputStream outputstream = new FileOutputStream(outputfile); |
| 50 | + |
| 51 | + document.PrintAndSave(outputstream); |
| 52 | + outputstream.close(); |
| 53 | + System.out.println("PDF generated at " + outputfile.getAbsolutePath()); |
| 54 | + } catch (Exception e) { |
| 55 | + System.err.println("Exception encountered " + e.getClass().getName() + " " + e.getMessage()); |
| 56 | + e.printStackTrace(System.err); |
| 57 | + System.exit(1); |
| 58 | + } |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + private static void feedcontent(PDFDocument document) throws IOException { |
| 63 | + // declare headers |
| 64 | + PageHeaders header = new PageHeaders(); |
| 65 | + |
| 66 | + // declare a coverpage |
| 67 | + PDFPage coverpage = new PDFPage(true, 15, 10); |
| 68 | + coverpage.addHeader(header); |
| 69 | + coverpage.drawCenteredTextAt(true, coverpage.getPagetop() + 100, 0, "Open Lowcode PDF"); |
| 70 | + coverpage.drawCenteredTextAt(false, coverpage.getPagetop() + 130, 0, "automatically generated document"); |
| 71 | + document.addPDFPart(coverpage); |
| 72 | + |
| 73 | + // create document body |
| 74 | + PDFPageBand documentbody = new PDFPageBand(header, true, 15, 10); |
| 75 | + document.addPDFPart(documentbody); |
| 76 | + |
| 77 | + // title for table of content |
| 78 | + documentbody.printNewSection(new SectionHeader(15, "A - Table of Content")); |
| 79 | + documentbody.printNewSection(new DocumentTableOfContent()); |
| 80 | + // paragraph on installation |
| 81 | + documentbody.printNewSection(new SectionHeader(15, "1 - Install Open Lowcode PDF on your project")); |
| 82 | + documentbody.printNewSection(new ParagraphHeader("Prerequisites")); |
| 83 | + documentbody.printNewSection(new SectionText("Before you can use Open Lowcode PDF, you" |
| 84 | + + " need make sure the following prerequisites are available:")); |
| 85 | + documentbody.printNewSection( |
| 86 | + new BulletText("Install Apache PDF Box jar (please refer to Apache site for details)", 1)); |
| 87 | + documentbody.printNewSection(new BulletText( |
| 88 | + "Open Lowcode has been tested on PDFBox 4.0.7, although we expect no problem if you run it on a different version", |
| 89 | + 2)); |
| 90 | + documentbody.printNewSection(new BulletText( |
| 91 | + "Download and install the JAR from Open Lowcode PDF and make sure it is available in your classpath", |
| 92 | + 1)); |
| 93 | + documentbody.printNewSection(new BulletText( |
| 94 | + "Ensure you have prepared your favorite soft drink. We recommend also eating slightly-toasted bread with fresh basil, tomatoes, olive oil and garlic while performing programming on Open Lowcode PDF", |
| 95 | + 1)); |
| 96 | + documentbody.printNewSection(new BulletText("Ensure you have started playing your favourite music", 1)); |
| 97 | + documentbody.printNewSection(new BulletText( |
| 98 | + "If someone is watching TV in your house, please ensure that you have your noise-canceling headset switched-on", |
| 99 | + 1)); |
| 100 | + documentbody |
| 101 | + .printNewSection(new BulletText("Ensure you have a development environment with java 8 or higher", 1)); |
| 102 | + documentbody.printNewSection(new BulletText( |
| 103 | + "Open Lowcode PDF was compiled with java 8 Zulu JVM, although it should work with further versions of java.", |
| 104 | + 2)); |
| 105 | + |
| 106 | + documentbody.printNewSection(new ParagraphHeader("Write your first document")); |
| 107 | + documentbody.printNewSection(new SectionText( |
| 108 | + "We recommend you start from the SampleOpenLowcodePDFDocument file that provides you an overview of the capabilities of the toolset. Javadoc is also available")); |
| 109 | + documentbody.printNewSection(new SectionHeader(15, "2 - Open Lowcode design principles")); |
| 110 | + documentbody.printNewSection(new SectionText( |
| 111 | + "Open Lowcode PDF was designed to help you generate as quickly as possible standard corporate documents.")); |
| 112 | + |
| 113 | + PDFMultiPageTable detailedtable = new PDFMultiPageTable(new float[] { 25, 55, 20 }); |
| 114 | + |
| 115 | + detailedtable.setHeader("Value", 0); |
| 116 | + detailedtable.setHeader("Rationale", 1); |
| 117 | + detailedtable.setHeader("Since", 2); |
| 118 | + |
| 119 | + detailedtable.addOneLineContent(new String[] { "Metric System", |
| 120 | + "Open Lowcode PDF has been designed with the metric system", "the beginning" }); |
| 121 | + |
| 122 | + detailedtable.addOneLineContent(new String[] { "Coordinates", |
| 123 | + "Open Lowcode PDF is using the most common type of coordinates system in programming, meaning (0,0) is at the top-left.", |
| 124 | + "the beginning" }); |
| 125 | + |
| 126 | + detailedtable.addOneLineContent(new String[] { "Paper Size", |
| 127 | + "Standard paper size is ISO A4. Other ISO sizes are available. ", "the beginning" }); |
| 128 | + detailedtable.addOneLineContent(new String[] { "Letters", |
| 129 | + "Open Low code PDF is using default Helvetica-like font on your system. Font sizes are based on classical layout of corporate documents." |
| 130 | + + "Objective of the framework is to give good enough default font choices so that you do not have to worry about it.", |
| 131 | + "the beginning" }); |
| 132 | + documentbody.printNewSection(detailedtable); |
| 133 | + documentbody.printNewSection(new SectionHeader(15, "3 - Legal stuff")); |
| 134 | + documentbody.printNewSection(new SectionText( |
| 135 | + "This is not legal advice, however, you can use Open Lowcode PDF even in a proprietary software, as long as you are just using the library, not modifying it.")); |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + private static class PageHeaders implements PDFPageBandHeaders { |
| 140 | + |
| 141 | + private float topheaderspace = 18; // in mm |
| 142 | + private float bottomheaderspace = 10; // in mm |
| 143 | + |
| 144 | + @Override |
| 145 | + public void printHeaders(PDFPage currentpage, float leftprintableinmm, float topprintableinmm, |
| 146 | + float rightprintableinmm, float bottomprintableinmm) throws IOException { |
| 147 | + currentpage.drawLine(false, currentpage.getPageLeft(), currentpage.getPagetop() + topheaderspace-5, |
| 148 | + currentpage.getPageRight(), currentpage.getPagetop() + topheaderspace-5, Color.BLACK); |
| 149 | + currentpage.drawFreeFontTextAt(currentpage.getPageRight() - 50, currentpage.getPagetop(), Color.RED, true, |
| 150 | + false, false, 13, "EXAMPLE ONLY"); |
| 151 | + |
| 152 | + currentpage.drawBox(true, currentpage.getPageRight() - 53, currentpage.getPagetop() - 1, |
| 153 | + currentpage.getPageRight() - 10, currentpage.getPagetop() + 6, Color.RED); |
| 154 | + currentpage.drawLine(false, currentpage.getPageLeft(), currentpage.getPageBottom() - bottomheaderspace, |
| 155 | + currentpage.getPageRight(), currentpage.getPageBottom() - bottomheaderspace, Color.BLACK); |
| 156 | + |
| 157 | + currentpage.drawCenteredTextAt(false, currentpage.getPagetop(), 0, "Open Lowcode PDF"); |
| 158 | + currentpage.drawCenteredTextAt(false, currentpage.getPagetop(), 1, "Sample file"); |
| 159 | + currentpage.drawSimpleTextAt(true, currentpage.getPageLeft(), |
| 160 | + currentpage.getPageBottom() - bottomheaderspace, 0, 0, |
| 161 | + "This document is just an example of the Open Lowcode PDF framework. It may contain inaccurate or obsolete information"); |
| 162 | + currentpage.drawCalculatedText(currentpage.getPageRight() - 20, |
| 163 | + currentpage.getPageBottom() - bottomheaderspace, 0, 0, true, |
| 164 | + // important to use calculated text when printing section with page number |
| 165 | + () -> ("page " + currentpage.getPageIndex() + "/" |
| 166 | + + currentpage.getParent().getDocumentPageNumber())); |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public float getTopHeaderSpace() { |
| 172 | + |
| 173 | + return topheaderspace; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public float getBottomHeaderSpace() { |
| 178 | + return bottomheaderspace; |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public float getLeftHeaderSpace() { |
| 183 | + return 0; |
| 184 | + } |
| 185 | + |
| 186 | + @Override |
| 187 | + public float getRightHeaderSpace() { |
| 188 | + return 0; |
| 189 | + } |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | +} |
0 commit comments