Back to Blog
Developer Blog
How to Create a Table of Contents Based on Bookmark Information in PDFs
Foxit PDF SDK for Linux
Foxit PDF SDK for Mac
Foxit PDF SDK for Windows
This article contains sample code that enables you to create a table of contents based on bookmark Information in PDFs. We use Foxit PDF SDK to do it and you can get a free 30 day trial here.
Platform: Windows, Mac, Linux
Programming Language: C++, Java, C#, Objective-C, C
License Key requirement: Standard
C++
void AddTOCToPDF(PDFDoc doc) {
//Set the table of contents configuration.
Int32Array intarray;
int depth = doc.GetBookmarkLevelDepth();
if (depth > 0) {
for (int i = 1; i <= depth; i++) {
intarray.Add(i);
}
}
WString title = L"";
TableOfContentsConfig toc_config = TableOfContentsConfig(title, intarray, true, false);
//Add the table of contents
doc.AddTableOfContents(toc_config);
}
Standard Java:
static void AddTOCToPDF(PDFDoc doc) throws PDFException {
Int32Array intarray = new Int32Array();
int depth = doc.getBookmarkLevelDepth();
if (depth > 0) {
for (int i = 1; i <= depth; i++) {
intarray.add(i);
}
}
String title = "";
TableOfContentsConfig toc_config = new TableOfContentsConfig(title, intarray, true, false);
//Add the table of contents
doc.addTableOfContents(toc_config);
}
C#:
static void AddTOCToPDF(PDFDoc doc)
{
//Set the table of contents configuration.
using (var intarray = new Int32Array())
{
int depth = doc.GetBookmarkLevelDepth();
if (depth > 0)
{
for (int i = 1; i <= depth; i++)
{
intarray.Add(i);
}
}
string title = "";
using (var toc_config = new TableOfContentsConfig(title, intarray, true, false))
{
//Add the table of contents
doc.AddTableOfContents(toc_config);
}
}
}
Objective-C:
void addTOCToPDF(FSPDFDoc* doc) {
@autoreleasepool {
//Set the table of contents configuration.
FSInt32Array* intarray = [[FSInt32Array alloc] init];
int depth = [doc getBookmarkLevelDepth];
if (depth > 0) {
for (int i = 1; i <= depth; i++) {
[intarray add:i];
}
}
NSString* title = @"";
FSTableOfContentsConfig* toc_config = [[FSTableOfContentsConfig alloc] initWithTitle:title bookmark_level_array:intarray is_show_serial_number:true include_toc_pages:false];
//Add the table of contents
[doc addTableOfContentsWithTableOfContentsConfig:toc_config];
}
}
C:
void AddTOCToPDF(FS_PDFDOC_HANDLE &doc) {
//Set the table of contents configuration.
int depth = 0;
FSDK_PDFDoc_GetBookmarkLevelDepth(doc, depth);
int* intarray = (int*)malloc(depth * sizeof(int));
if (depth > 0) {
for (int i = 1; i <= depth; i++) {
intarray[i - 1] = i;
}
}
FS_WSTR title;
title.str = L"";
title.len = 0;
FSTableOfContentsConfig toc_config;
toc_config.bookmark_level_array = intarray;
toc_config.bookmark_level_array_length = depth;
toc_config.title = title;
toc_config.is_show_serial_number = true;
toc_config.include_toc_pages = false;
//Add the table of contents
FSDK_PDFDoc_AddTableOfContents0(doc, toc_config);
free(intarray);
}