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