Building table of contents
Here's how to set up a table of contents:
1. Start by Setting Up Templates: Head over to the "Manage Templates" page and create two templates:
- TOC template: The template for table of contents (TOC)
- Body Template: Another for the main content of the PDF
2. Design Your TOC Template: Now, get creative with your TOC template. For the JSON data of TOC, jump to step 4 to see a sample of the anchors in JSON format.
3. Design the Body Template: While creating this template, use the Anchor component for each chapter. Why? Well, these anchors help point to specific pages in the final PDF and come in handy when the API sends back responses in the next step. Please use the properties of Anchor component: Text, Level and Description along with expressions for dynamic data.
4. Generate a Body PDF: Once you've got your body template sorted, generate a PDF(PDF Generation API). The response will return you with the anchors and their corresponding pages. Just to give you an idea, here's what the returned JSON might look like:
{
"status": "success",
"file": "https://craftmypdf.com/output.pdf",
"transaction_ref": "a0430897-2c94-40e1-a09b-57403d811ceb",
"anchors": [
{
"page": 1,
"text": "",
"level": "0",
"description": "Welcome to CraftMyPDF!"
},
{
"page": 2,
"text": "page 2",
"level": "0",
"description": ""
}
]
}
5. Generate a TOC PDF Using the Anchor Data: With the anchor JSON data in hand, go ahead and generate a fresh table of contents PDF.
6. Bring It All Together: Last step! Use the merge PDFs API(Merge PDF URLs API) to merge the main content PDF with the table of contents PDF.
And there you have it! A neat PDF with its very own table of contents.