Skip to content

Commit c90ef4e

Browse files
author
Open Lowcode SAS
committed
Addition of example file, and correction of a few layout bugs
1 parent 71e3343 commit c90ef4e

File tree

3 files changed

+207
-12
lines changed

3 files changed

+207
-12
lines changed
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
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+
}

src/org/openlowcode/tools/pdf/PDFMultiPageTable.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public void setCellContent(String cellcontent, int columnindex) {
201201
}
202202

203203
private float getHeadersHeight(float leftinmm, float rightinmm) throws IOException {
204-
204+
if (this.columnwidthinmm==null) initColumnWidthInMm(leftinmm,rightinmm);
205205
float maxheight = 0;
206206
for (int i = 0; i < this.columnnumber; i++) {
207207
float columnwidth = this.columnwidthinmm[i];
@@ -216,20 +216,21 @@ private float getHeadersHeight(float leftinmm, float rightinmm) throws IOExcepti
216216

217217
}
218218

219-
private float[] getColumnWidthInMm(float leftinmm, float rightinmm) {
220-
columnwidthinmm = new float[columnnumber];
219+
private void initColumnWidthInMm(float leftinmm, float rightinmm) {
220+
this.columnwidthinmm = new float[columnnumber];
221221
float totalrelativewidth = 0;
222222
for (int i = 0; i < columnrelativewidth.length; i++)
223223
totalrelativewidth += columnrelativewidth[i];
224224
for (int i = 0; i < columnnumber; i++) {
225225
this.columnwidthinmm[i] = (rightinmm - leftinmm - this.leftmargin - this.rightmargin)
226226
* columnrelativewidth[i] / totalrelativewidth;
227227
}
228-
return columnwidthinmm;
228+
229229
}
230230

231231
protected float getLineHeight(float leftinmm, float rightinmm, int lineindex) throws IOException {
232232
float maxheight = 0;
233+
233234
for (int i = 0; i < this.columnnumber; i++) {
234235
float columnwidth = this.columnwidthinmm[i];
235236
String text = this.cellscontent.get(lineindex)[i].text;
@@ -256,7 +257,7 @@ protected float getLineHeight(float leftinmm, float rightinmm, int lineindex) th
256257
*/
257258
private float printHeader(PDFPage currentpage, float mmfromtopforsection, float leftinmm, float rightinmm)
258259
throws IOException {
259-
float[] columnwidthinmm = getColumnWidthInMm(leftinmm, rightinmm);
260+
260261
float headerheight = getHeadersHeight(leftinmm, rightinmm);
261262
float currentleft = leftinmm;
262263
for (int i = 0; i < columnwidthinmm.length; i++) {
@@ -283,7 +284,7 @@ private float printHeader(PDFPage currentpage, float mmfromtopforsection, float
283284
*/
284285
protected float printOneRow(PDFPage currentpage, int rowindex, float mmfromtopforsection, float leftinmm,
285286
float rightinmm) throws IOException {
286-
float[] columnwidthinmm = getColumnWidthInMm(leftinmm, rightinmm);
287+
287288
float rowheight = this.getLineHeight(leftinmm, rightinmm, rowindex);
288289
float currentleft = leftinmm;
289290
for (int i = 0; i < columnwidthinmm.length; i++) {

src/org/openlowcode/tools/pdf/PDFPage.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ public boolean isTopAtZero() {
323323
}
324324

325325
/**
326+
* Generates an A4 format page with dimensions indicated in mm
326327
* @param portrait
327328
* @param leftrightmargin
328329
* @param topbottommargin
@@ -376,14 +377,14 @@ public PDFPage(float customwidth, float customheight, float leftrightmargin, flo
376377
}
377378

378379
/**
379-
*
380+
* generates an A4 page with margin size indicated in mm, with topatzero = true
380381
* @param portrait true if portrait, false is landscape
381382
* @param leftrightmargin margin at left and right in mm
382383
* @param topbottommargin margin at top and bottom in mm
383384
* @throws IOException
384385
*/
385386
public PDFPage(boolean portrait, float leftrightmargin, float topbottommargin) throws IOException {
386-
this(portrait, topbottommargin, leftrightmargin, false);
387+
this(portrait, topbottommargin, leftrightmargin, true);
387388
}
388389

389390
/**
@@ -711,7 +712,7 @@ public void drawFreeFontTextAt(float left, float top, Color color, boolean bold,
711712
}
712713

713714
contentStream.beginText();
714-
contentStream.newLineAtOffset(left * MM_TO_POINT, newtop * MM_TO_POINT);
715+
contentStream.newLineAtOffset(left * MM_TO_POINT, newtop * MM_TO_POINT-fontinpoint);
715716

716717
PDFont font = PDType1Font.HELVETICA;
717718
if (!serif) {
@@ -1014,12 +1015,12 @@ public void drawCenteredTextAt(boolean title, float top, int paragraphnumber, St
10141015

10151016
}
10161017
float fontSize = LINE_HEIGHT_NORMAL_TEXT;
1017-
float verticaloffset = (LINE_HEIGHT_NORMAL_TEXT) * (paragraphnumber)
1018+
float verticaloffset = (LINE_HEIGHT_NORMAL_TEXT) * (paragraphnumber+1)
10181019
+ (PARAGRAPH_SPACING_NORMAL_TEXT * paragraphnumber);
10191020
if (title) {
10201021
font = titlefont;
10211022
fontSize = LINE_HEIGHT_TITLE_TEXT;
1022-
verticaloffset = (LINE_HEIGHT_TITLE_TEXT) * (paragraphnumber)
1023+
verticaloffset = (LINE_HEIGHT_TITLE_TEXT) * (paragraphnumber+1)
10231024
+ (PARAGRAPH_SPACING_TITLE_TEXT * paragraphnumber);
10241025

10251026
}
@@ -1055,7 +1056,7 @@ public float getPageBottom() {
10551056
float pagebottom = topbottommargin;
10561057
if (this.topatzero)
10571058
pagebottom = height - pagebottom;
1058-
return topbottommargin;
1059+
return pagebottom;
10591060
}
10601061

10611062
/**

0 commit comments

Comments
 (0)