Skip to content

Commit d391885

Browse files
author
Open Lowcode SAS
committed
Close #62
1 parent 29b188f commit d391885

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public void setParentDocument(PDFDocument document) {
9494

9595
@Override
9696
public void initialize() {
97+
if (parentdocument==null) throw new RuntimeException("Table of content has not been added correctly to Page Band.");
9798
for (int i = 0; i < parentdocument.getPartsNumber(); i++) {
9899
PDFPart thispart = parentdocument.getPart(i);
99100
if (thispart instanceof PDFPageBand) {
@@ -104,6 +105,14 @@ public void initialize() {
104105
this.content.add((SectionHeader) (section));
105106
}
106107
}
108+
if (thispart instanceof PDFPage) {
109+
PDFPage thispage = (PDFPage) thispart;
110+
PagedLabel[] labelsforpage = thispage.getTOCLabelsForPage();
111+
if (labelsforpage!=null) for (int j=0;j<labelsforpage.length;j++) {
112+
this.content.add(labelsforpage[j]);
113+
114+
}
115+
}
107116
}
108117

109118
}

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ private interface PageExecutable {
113113
private PDPage page;
114114
private PDPageContentStream contentStream;
115115
private PDDocument document;
116-
116+
private ArrayList<PagedLabel> toclabelsforpage;
117+
118+
117119
/**
118120
* This method should only be called during the final layout. Please use the
119121
* method PDFPage.drawCalculatedText
@@ -377,6 +379,7 @@ public PDFPage(boolean portrait, float leftrightmargin, float topbottommargin, b
377379

378380
this.topatzero = topatzero;
379381
widgetstoprint = new ArrayList<PageExecutable>();
382+
toclabelsforpage = new ArrayList<PagedLabel>();
380383
}
381384

382385
public void addHeader(PDFPageBandHeaders header) throws IOException {
@@ -408,6 +411,7 @@ public PDFPage(
408411
this.topatzero = topatzero;
409412

410413
widgetstoprint = new ArrayList<PageExecutable>();
414+
toclabelsforpage = new ArrayList<PagedLabel>();
411415
}
412416

413417
/**
@@ -2103,6 +2107,10 @@ public static String remove(String text) {
21032107
return b.toString();
21042108
}
21052109

2110+
/**
2111+
* @param contentStream
2112+
* @param text
2113+
*/
21062114
public static void securedShowText(PDPageContentStream contentStream, String text) {
21072115
try {
21082116
contentStream.showText(remove(text));
@@ -2112,6 +2120,23 @@ public static void securedShowText(PDPageContentStream contentStream, String tex
21122120
}
21132121
}
21142122

2123+
/**
2124+
* @return all the labels to be inserted in the table of content for this page
2125+
*/
2126+
public PagedLabel[] getTOCLabelsForPage() {
2127+
return toclabelsforpage.toArray(new PagedLabel[0]);
2128+
}
2129+
2130+
/**
2131+
* Adds a label for this page to be added in table of content
2132+
*
2133+
* @param label text of the label
2134+
* @param offset offset in mm
2135+
*/
2136+
public void setTOCLabelForPage(String label,float offset) {
2137+
toclabelsforpage.add(new PagedLabelInPage(label,offset));
2138+
}
2139+
21152140
@Override
21162141
protected int getPageNumber() {
21172142
// only a page is used by a PDFPage
@@ -2129,5 +2154,33 @@ protected void layoutPages(int pagesbefore) {
21292154
this.pageindex = pagesbefore + 1;
21302155

21312156
}
2157+
2158+
private class PagedLabelInPage implements PagedLabel {
2159+
2160+
private String label;
2161+
private float offset;
2162+
2163+
public PagedLabelInPage(String label,float offset) {
2164+
this.label = label;
2165+
this.offset = offset;
2166+
}
2167+
2168+
@Override
2169+
public String getLabel() {
2170+
return label;
2171+
}
2172+
2173+
@Override
2174+
public int getPageNumber() {
2175+
return getPageIndex();
2176+
}
2177+
2178+
@Override
2179+
public float getOriginalOffset() {
2180+
return offset;
2181+
}
2182+
2183+
}
2184+
21322185

21332186
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ protected void layoutPages(int pagesbefore) throws IOException {
172172
* @param sectiontoprint
173173
*/
174174
public void printNewSection(PDFPageBandSection sectiontoprint) throws IOException {
175+
if (sectiontoprint==null) throw new RuntimeException("Adding null section in page band ");
175176
this.sections.add(sectiontoprint);
176-
sectiontoprint.setParentDocument(this.getParent());
177177
}
178178

179179
/**
@@ -234,6 +234,8 @@ public PartialPrintFeedback(float newcursor, boolean finished) {
234234

235235
@Override
236236
protected void initialize() {
237+
for (int i = 0; i < this.sections.size(); i++)
238+
this.sections.get(i).setParentDocument(this.getParent());
237239
for (int i = 0; i < this.sections.size(); i++)
238240
this.sections.get(i).initialize();
239241
}

0 commit comments

Comments
 (0)