[Unit] Adding a table of contents

Demos, code samples. Only questions related to the existing topics are allowed here.
jonjon
Posts: 470
Joined: Sat Aug 27, 2005 4:19 pm

Post by jonjon »

OK Sergey, I'll wait. Thanks.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

In DOC/RTF file, PAGEREF is a reference to a bookmark. When generating TOC, MS Word adds a hidden auto-maintained bookmark before each heading.
I can add checkpoints before each heading when generating TOC. However, they will not be hidden or auto-maintained, they will be like any other checkpoints. If it's ok, I'll make an example.
jonjon
Posts: 470
Joined: Sat Aug 27, 2005 4:19 pm

Post by jonjon »

Sergey,

Thank you for taking the time to review my question. I already have added checkpoints (bookmarks) before each heading. However the difficult part for me is to update the TOC generation so that the page number displayed is a reference to those bookmarks and not a static page number, when exported to RTF. An example would be very welcome.

John.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

This unit contains code for generation TOC for RTF:
rvtocrtf.zip
(3.72 KiB) Downloaded 5240 times
It contains the same functions as the original version, with the same parameters.
Differences:
- new functions add checkpoints before each heading in the document;
- new functions insert RTF fields "PAGEREF" in place of page numbers.

There are 2 main ways for insertion RTF codes using TRichView:
1) using text with a style having rvteoRTFCode in Options;
2) using some control and processing OnSaveComponentToFile.

This example uses the first method. Its disadvantage: in TRichView, you can see all these codes directly in text.

The functions still require formatted document, because they calculate page numbers that will be displayed in MS Word when you load this document initially. You still need to select TOC paragraphs in Word and press F9 to update page numbers.
The code for calculating initial values of page numbers could be removed (they can be displayed initially, as, for example, '?'), it would simplify the code.

Update:
2018-Apr-18: for compatibility with TRichView 17.3; the margins problem in the RVReportHelper version is fixed.

Old version:
http://www.trichview.com/support/files/rvtocrtf.zip - for TRichView 17.2 and older
jonjon
Posts: 470
Joined: Sat Aug 27, 2005 4:19 pm

Post by jonjon »

This is exactly what I needed, thank you very much Sergey!
SculptureEK
Posts: 4
Joined: Wed Nov 21, 2012 3:24 pm

Small problem in TOC generation

Post by SculptureEK »

The number of pages of the Table of Contents is not correctly calculated IF
- the margins of the RichView are greater than the default margins AND
- there is a TOC line which is just a bit too large to fit in 1 line AND
- because of this line we need a 2nd page for the TOC
Obviously, the RichView used to build the TOC thinks that the TOC fits in 1 page while it needs 2 pages in fact.

It works with this change in function AddTableOfContents (TRVReportHelper overload):

// building TOC in RVReportHelper2, calculating TOCPageCount
RVReportHelper2.RichView.Style := RVReportHelper.RichView.Style;
ADD-> RVReportHelper2.RichView.LeftMargin := RVReportHelper.RichView.LeftMargin;
ADD-> RVReportHelper2.RichView.RightMargin := RVReportHelper.RichView.RightMargin;
RVReportHelper2.RichView.AddNL(Title, TitleStyleNo, TitleParaNo);

Kind regards,

Hans
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

You are right, margins must by copied from RVReportHelper to RVReportHelper2, all 4 margins

Update 2018-Apr-18: this fix is included in the unit.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: [Unit] Adding a table of contents

Post by Sergey Tkachenko »

I updated the demos for compatibility with TRichView 17.3

Here is a brief overview of available "table of context demo projects":

1. Adding TOC in TRichView, pages are calculated according to TRVPrint settings. Alternatively, adding TOC in TRVReportHelper, using the specified PageWidth and PageHeight: http://www.trichview.com/forums/viewtop ... 787#p15121
How to use: http://www.trichview.com/forums/viewtop ... 122#p15122

2. Adding TOC containing RTF codes in place of page numbers. If you save this document in RTF, open in MS Word and update fields, references to pages will be added in these places: http://www.trichview.com/forums/viewtop ... 373#p20373 (before updating fields, MS Word will display page numbers calculated by TRichView).
This unit is used exactly like the previous unit.

3. A special item type that does not build a TOC itself, but, if exported to RTF or DocX, inserts a TOC code. When MS Word updates this field, it builds a TOC itself. This item was initially introduced here: http://www.trichview.com/forums/viewtop ... f=3&t=7034 , but now it is included in TRichView unit as <TRichView Dir>\TRichView\Source\RVTOCItem.pas.
Like any other non-text object, it is inserted using AddItem or InsertItem methods.
Since this item does not build TOC itself but simply generates RTF/DocX code, it is universal, you can insert it in TRichView, TRVReportHelper or in a ScaleRichView editor.

4. ScaleRichView version of TOC is in <TRichView Dir>\SRVControls\Demos\DocViewer\
Initially, it was almost an exact copy of #1, it just calculated page numbers in TSRichViewEdit. But in one of recent updates, we added new features:
- TOC items may be hyperlinks
- TOC may be built without page numbers
(for example, hypertext TOC is useful for HTML export)
Post Reply