Skip to content

Commit 71e3343

Browse files
author
Open Lowcode SAS
committed
First Open-source commit. A little bit nervous.
1 parent 589eec4 commit 71e3343

17 files changed

+3526
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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.tools.pdf;
12+
13+
import java.io.IOException;
14+
import java.util.logging.Logger;
15+
16+
import org.openlowcode.tools.pdf.PDFPage.BoxTextContent;
17+
import org.openlowcode.tools.pdf.PDFPageBand.PartialPrintFeedback;
18+
19+
/**
20+
* A paragraph with a bullet point. 3 levels are managed
21+
*
22+
* @author <a href="https://openlowcode.com/">Open Lowcode SAS</a>
23+
*
24+
*/
25+
public class BulletText implements PDFPageBandSection {
26+
private String text;
27+
private static Logger logger = Logger.getLogger(SectionText.class.getName());
28+
private String remainingtext;
29+
private int texttype;
30+
private boolean firstprint;
31+
32+
/**
33+
* @param text
34+
* @param level an integer from 1 to 4 for the level of bullet (in theory, more
35+
* than 4 is possible, but as each bullet takes around 10% of text
36+
* zone width, it is likely not to be readbl
37+
*/
38+
public BulletText(String text, int level) {
39+
if (level < 1)
40+
throw new RuntimeException("level should be between 1 (included) and 3 (included)");
41+
if (level > 3)
42+
throw new RuntimeException("level should be between 1 (included) and 3 (included)");
43+
44+
this.text = text;
45+
this.remainingtext = text;
46+
texttype = PDFPage.TEXTTYPE_BULLET_L1;
47+
if (level == 2)
48+
texttype = PDFPage.TEXTTYPE_BULLET_L2;
49+
if (level == 3)
50+
texttype = PDFPage.TEXTTYPE_BULLET_L3;
51+
this.firstprint = true;
52+
}
53+
54+
@Override
55+
public void print(PDFPageBand pageband, PDFPage currentpage, float mmfromtopforsection, float leftinmm,
56+
float rightinmm) throws IOException {
57+
PDFPage.calculateBoxAndMaybeWriteText(leftinmm, mmfromtopforsection, rightinmm, text, true, currentpage,
58+
texttype);
59+
60+
}
61+
62+
@Override
63+
public float getSectionHeight(float leftinmm, float rightinmm) throws IOException {
64+
return PDFPage.calculateBoxAndMaybeWriteText(leftinmm, 0, rightinmm, text, false, null, texttype).getHeight();
65+
}
66+
67+
@Override
68+
public boolean breakableSection() {
69+
return true;
70+
}
71+
72+
@Override
73+
public PartialPrintFeedback printPartial(PDFPageBand pageband, float spaceleft, PDFPage currentpage,
74+
float mmfromtopforsection, float leftinmm, float rightinmm) throws IOException {
75+
float heightforremaining = PDFPage
76+
.calculateBoxAndMaybeWriteText(leftinmm, 0, rightinmm, remainingtext, false, null, texttype)
77+
.getHeight();
78+
BoxTextContent feedback = PDFPage.writeAsMuchTextAsPossible(leftinmm, mmfromtopforsection, rightinmm, spaceleft,
79+
remainingtext, currentpage, texttype, !firstprint);
80+
this.remainingtext = feedback.getTextleftout();
81+
logger.fine("drop remaining textn space left " + spaceleft + "-----------------------------------------------");
82+
logger.fine("remaining text length = " + (this.remainingtext != null ? this.remainingtext.length() : "NULL"));
83+
if (feedback.isCutinsideparagraph())
84+
firstprint = false;
85+
// note: do not care about new height as at end of page, that is why we give
86+
// back zero although it is dirty
87+
if (this.remainingtext.length() > 0)
88+
return new PartialPrintFeedback(0, false);
89+
return new PartialPrintFeedback(mmfromtopforsection + heightforremaining, true);
90+
}
91+
92+
@Override
93+
public String dropContentSample() {
94+
return "Section header sample: "
95+
+ (this.text != null ? (this.text.length() > 100 ? this.text.substring(0, 100) + "..." : this.text)
96+
: "NULLSTRING");
97+
}
98+
99+
@Override
100+
public void setParentDocument(PDFDocument document) {
101+
// Do nothing
102+
103+
}
104+
105+
@Override
106+
public void initialize() {
107+
// do nothing
108+
109+
}
110+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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.tools.pdf;
12+
13+
import java.io.IOException;
14+
import java.util.ArrayList;
15+
16+
import org.openlowcode.tools.pdf.PDFPageBand.PartialPrintFeedback;
17+
18+
/**
19+
* This component prints a table of content made of all the SectionHeaders of
20+
* all the PDFPageBandSections of the document
21+
*
22+
* @author <a href="https://openlowcode.com/">Open Lowcode SAS</a>
23+
*
24+
*/
25+
public class DocumentTableOfContent implements PDFPageBandSection {
26+
27+
private ArrayList<PagedLabel> content;
28+
29+
private PDFDocument parentdocument;
30+
31+
private int currentindex;
32+
33+
public DocumentTableOfContent() {
34+
currentindex = 0;
35+
content = new ArrayList<PagedLabel>();
36+
}
37+
38+
@Override
39+
public void print(PDFPageBand pageband, PDFPage currentpage, float mmfromtopforsection, float leftinmm,
40+
float rightinmm) throws IOException {
41+
for (int i = 0; i < content.size(); i++) {
42+
PagedLabel label = content.get(i);
43+
currentpage.drawSimpleTextAt(false, leftinmm, mmfromtopforsection, i, 0, label.getLabel());
44+
currentpage.drawCalculatedText(rightinmm, mmfromtopforsection, i, 0, false,
45+
() -> "" + label.getPageNumber());
46+
}
47+
48+
}
49+
50+
@Override
51+
public float getSectionHeight(float leftinmm, float rightinmm) throws IOException {
52+
return new PDFPage.BoxTextContent(content.size(), 1, PDFPage.TEXTTYPE_PLAIN).getHeight();
53+
}
54+
55+
@Override
56+
public boolean breakableSection() {
57+
return true;
58+
}
59+
60+
@Override
61+
public PartialPrintFeedback printPartial(PDFPageBand pageband, float spaceleft, PDFPage currentpage,
62+
float mmfromtopforsection, float leftinmm, float rightinmm) throws IOException {
63+
int numberoflines = PDFPage.getNumberOfLinesAvailableForParagraph(spaceleft);
64+
int linestoprint = content.size() - currentindex;
65+
boolean full = true;
66+
if (numberoflines < linestoprint) {
67+
linestoprint = numberoflines;
68+
full = false;
69+
}
70+
for (int i = 0; i < linestoprint; i++) {
71+
PagedLabel label = content.get(i + currentindex);
72+
currentpage.drawSimpleTextAt(false, leftinmm + 15 + label.getOriginalOffset() / 3, mmfromtopforsection, i,
73+
0, label.getLabel());
74+
currentpage.drawCalculatedText(leftinmm + 15, mmfromtopforsection, i, 0, false,
75+
() -> "" + label.getPageNumber());
76+
}
77+
currentindex = currentindex + linestoprint;
78+
return new PartialPrintFeedback(
79+
mmfromtopforsection + new PDFPage.BoxTextContent(linestoprint, 1, PDFPage.TEXTTYPE_PLAIN).getHeight(),
80+
full);
81+
}
82+
83+
@Override
84+
public String dropContentSample() {
85+
// TODO Auto-generated method stub
86+
return null;
87+
}
88+
89+
@Override
90+
public void setParentDocument(PDFDocument document) {
91+
this.parentdocument = document;
92+
93+
}
94+
95+
@Override
96+
public void initialize() {
97+
for (int i = 0; i < parentdocument.getPartsNumber(); i++) {
98+
PDFPart thispart = parentdocument.getPart(i);
99+
if (thispart instanceof PDFPageBand) {
100+
PDFPageBand thisband = (PDFPageBand) thispart;
101+
for (int j = 0; j < thisband.getSectionNumber(); j++) {
102+
PDFPageBandSection section = thisband.getSectionAt(j);
103+
if (section instanceof SectionHeader)
104+
this.content.add((SectionHeader) (section));
105+
}
106+
}
107+
}
108+
109+
}
110+
111+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.tools.pdf;
12+
13+
import java.io.IOException;
14+
import java.io.OutputStream;
15+
import java.util.ArrayList;
16+
17+
import org.apache.pdfbox.pdmodel.PDDocument;
18+
19+
/**
20+
* A document to be printed in PDF. This document is made of PDF Parts
21+
*
22+
* @author <a href="https://openlowcode.com/">Open Lowcode SAS</a>
23+
*
24+
*/
25+
public class PDFDocument {
26+
private ArrayList<PDFPart> documentparts;
27+
private int documentpagenumber = -1;
28+
29+
/**
30+
* @return the number of parts in the PDF Document
31+
*/
32+
int getPartsNumber() {
33+
return documentparts.size();
34+
}
35+
36+
PDFPart getPart(int index) {
37+
return documentparts.get(index);
38+
}
39+
40+
/**
41+
* Adds a PDFPart to be printed in order (first parts added are printed first in
42+
* the document)
43+
*
44+
* @param newpart
45+
*/
46+
public void addPDFPart(PDFPart newpart) {
47+
documentparts.add(newpart);
48+
newpart.setParentPDFDocument(this);
49+
}
50+
51+
/**
52+
* This method returns the total number of pages of the document. It should only
53+
* be called during final printing. You should use it in the
54+
* PDFPage.drawCalculatedText
55+
*
56+
* @return
57+
*
58+
*/
59+
public int getDocumentPageNumber() {
60+
if (this.documentpagenumber == -1)
61+
throw new RuntimeException(
62+
"This method should only be called in final print. You may want to use the method PDFPage.drawCalculatedText.");
63+
return this.documentpagenumber;
64+
}
65+
66+
public void PrintAndSave(OutputStream outputstream) throws IOException {
67+
PDDocument document = new PDDocument();
68+
69+
for (int i = 0; i < documentparts.size(); i++) {
70+
documentparts.get(i).initialize();
71+
}
72+
73+
int pagesbefore = 0;
74+
for (int i = 0; i < documentparts.size(); i++) {
75+
PDFPart thispart = documentparts.get(i);
76+
thispart.layoutPages(pagesbefore);
77+
pagesbefore += thispart.getPageNumber();
78+
}
79+
this.documentpagenumber = pagesbefore;
80+
for (int i = 0; i < documentparts.size(); i++) {
81+
documentparts.get(i).print(document);
82+
pagesbefore += documentparts.get(i).getPageNumber();
83+
}
84+
document.save(outputstream);
85+
document.close();
86+
}
87+
88+
public PDFDocument() {
89+
documentparts = new ArrayList<PDFPart>();
90+
}
91+
}

0 commit comments

Comments
 (0)