[Unit] Adding a table of contents

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

Post by jonjon » Tue Jun 07, 2011 6:31 pm

OK Sergey, I'll wait. Thanks.

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

Post by Sergey Tkachenko » Sun Oct 30, 2011 12:11 pm

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: 293
Joined: Sat Aug 27, 2005 4:19 pm

Post by jonjon » Sun Oct 30, 2011 12:27 pm

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: 14212
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko » Sun Oct 30, 2011 4:52 pm

This unit contains code for generation TOC for RTF:
rvtocrtf.zip
(3.72 KiB) Downloaded 89 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: 293
Joined: Sat Aug 27, 2005 4:19 pm

Post by jonjon » Mon Oct 31, 2011 1:28 pm

This is exactly what I needed, thank you very much Sergey!

SculptureEK
Posts: 3
Joined: Wed Nov 21, 2012 3:24 pm

Small problem in TOC generation

Post by SculptureEK » Mon Oct 27, 2014 5:41 pm

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: 14212
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko » Tue Oct 28, 2014 1:24 pm

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: 14212
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: [Unit] Adding a table of contents

Post by Sergey Tkachenko » Wed Apr 18, 2018 12:22 pm

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)

KellyKen
Posts: 1
Joined: Tue Dec 04, 2018 12:38 am

Unit Adding a table of contents

Post by KellyKen » Fri Dec 07, 2018 1:07 pm

The main website indicates that there is a way to import the table of contents of any PDF and automatically create bookmarks for you. Ive been unable to find a topic addressing how this is done. Could you please direct me to the thread where this function is addressed? Thanks

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

Re: [Unit] Adding a table of contents

Post by Sergey Tkachenko » Sat Dec 08, 2018 2:00 pm

Sorry, where did you find information about TOC in PDF?

Post Reply