1 package org.simantics.document.linking.report.pdf;
4 import java.util.ArrayList;
7 import org.simantics.document.linking.report.DocumentElement;
8 import org.simantics.document.linking.report.TableOfContents;
9 import org.simantics.document.linking.report.Document.TextSize;
10 import org.simantics.document.linking.report.TableColumn.Alignment;
11 import org.simantics.utils.datastructures.Pair;
14 public class PDFTocElement implements TableOfContents {
27 public PDFTocElement(PDFDocument writer) throws Exception{
29 stream = new PDFPageStream();
30 itemFont = writer.getFont(TextSize.SMALL);
31 titleFont = writer.getFont(TextSize.LARGE);
32 stream.setDefaultFont(itemFont);
35 tocTable= new PDFTable(writer, stream);
36 linesPerPage = tocTable.getAvailableLines();
37 //linesPerPage = stream.getAvailableLines();
38 PDFPage page = stream.getCurrentPage();
39 page.setFont(titleFont);
40 page.writeLine("Table of Contents");
41 page.setFont(itemFont);
43 linesOnFirstPage = tocTable.getAvailableLines();
44 //linesOnFirstPage = page.availableLines;
46 private List<Pair<String, PDFElement>> toc = new ArrayList<Pair<String,PDFElement>>();
48 public void addTocElement(String label, DocumentElement element) {
49 toc.add(new Pair<String, PDFElement>(label, (PDFElement)element));
52 public void create(PDFPageStream destStream, PDFPage after) throws Exception {
53 int tocPages = getEstimatedPages();
55 List<PDFPage> pages = destStream.getPages();
56 int tocIndex = pages.indexOf(after)+1;
58 tocTable.addColumn("Document", 0.9);
59 tocTable.addColumn("Page", 0.1).setAlignment(Alignment.RIGHT);
60 tocTable.setHeaderVisible(false);
61 tocTable.setLinesVisible(false);
62 for (Pair<String, PDFElement> item : toc) {
63 int pageIndex = pages.indexOf(item.second.getPage());
65 pageIndex += tocPages + 1;
67 tocTable.writeRow(item.first, Integer.toString(pageIndex));
68 // stream.getCurrentPage().writeLine( + " " + pageIndex);
71 pages.addAll(tocIndex, stream.getPages());
77 public int getEstimatedPages() {
79 int items = toc.size();
80 items -= linesOnFirstPage;