I'm building a custom print template closely following the directions in Chuck Ainslie's articles. One thing I'd like to do is generate a table of contents on the fly with the actual page numbers.
Is there any way too find what part of the document a layoutrect instance contains? Basically, I want to sc开发者_JAVA百科an the original document for specific tags (say <h1> tags), then figure out which layoutrect contains those tags. From there I can figure out which devicerect is the parent and that tells me the page number.
During the layout, when the onLayoutComplete handler is called, there doesn't seem to be any way to get the source of what was actually laid out.
I managed to generate a dynamic table of contents (dynamic in the sense that the page numbers are collected at print time and are not in the static html page). It isn't pretty though.
I couldn't find any way to determine what part of my document was being flowed into the specific device and layout rects. Instead, I break my document up into individual html files, one for each section. To print, I create an html file that looks roughly like this:
<html>
<body>
<h1>Table of Contents</h1>
<table>
<tr><th>Report</th><th>Page</th></tr>
<tr><td><a href="0.html">Foo</a></td><td>0</td></tr>
<tr><td><a href="1.html">Bar</a></td><td>0</td></tr>
<tr><td><a href="2.html">Etc</a></td><td>0</td></tr>
...
</table>
</body>
</html>
Rather than send that document to the printer, my print template pulls out all the section URLs from the document object. It then sends each of these to the printer and for each, it tracks the page number the section is printed on. When printing is complete, it updates the original document and replaces the '0' placeholders with the actual page numbers. Then the table of contents is printed.
It's not very elegant and now I have to add the rest of the UI around my print template code.
精彩评论