]> gerrit.simantics Code Review - simantics/platform.git/blob
c6fc5330eecddcd0612a924d468d5c90d1aab9da
[simantics/platform.git] /
1 package org.simantics.document.linking.report.pdf;
2
3 import java.awt.Font;
4 import java.util.ArrayList;
5 import java.util.List;
6
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;
12
13
14 public class PDFTocElement implements TableOfContents {
15         
16         PDFDocument writer;
17         PDFPageStream stream;
18         
19         Font itemFont;
20         Font titleFont;
21         
22         int linesPerPage;
23         int linesOnFirstPage;
24         
25         PDFTable tocTable;
26         
27         public PDFTocElement(PDFDocument writer) throws Exception{
28                 this.writer = writer;
29                 stream = new PDFPageStream();
30                 itemFont = writer.getFont(TextSize.SMALL);
31                 titleFont = writer.getFont(TextSize.LARGE);
32                 stream.setDefaultFont(itemFont);
33                 
34                 stream.nextPage();
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);
42                 page.writeLine("");
43                 linesOnFirstPage = tocTable.getAvailableLines();
44                 //linesOnFirstPage = page.availableLines;
45         }
46         private List<Pair<String, PDFElement>> toc = new ArrayList<Pair<String,PDFElement>>();
47         @Override
48         public void addTocElement(String label, DocumentElement element) {
49                 toc.add(new Pair<String, PDFElement>(label, (PDFElement)element));
50         }
51         
52         public void create(PDFPageStream destStream, PDFPage after) throws Exception {
53                 int tocPages = getEstimatedPages();
54
55                 List<PDFPage> pages = destStream.getPages();
56                 int tocIndex = pages.indexOf(after)+1;
57                 
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());
64                         if (pageIndex >= 0) {
65                                 pageIndex += tocPages + 1;
66                         }
67                         tocTable.writeRow(item.first, Integer.toString(pageIndex));
68 //                      stream.getCurrentPage().writeLine( + "  " + pageIndex);
69                 }
70                 
71                 pages.addAll(tocIndex, stream.getPages());
72                 stream = null;
73         }
74         
75         
76         
77         public int getEstimatedPages() {
78                 int pages = 1;
79                 int items = toc.size();
80                 items -= linesOnFirstPage;
81                 while (items > 0) {
82                         items-= linesPerPage;
83                         pages++;
84                 }
85                 return pages;
86         }
87
88 }