diff options
author | Kurt <kurt.w.jung@gmail.com> | 2018-01-08 15:24:06 -0500 |
---|---|---|
committer | Kurt <kurt.w.jung@gmail.com> | 2018-01-08 15:24:06 -0500 |
commit | 2b7dc4c2c0c37ed765386c79b37b829e49c1c912 (patch) | |
tree | 2ea2bb70aeeade8c8bdf2c2fa9d16b611b02d396 /fpdf_test.go | |
parent | c8debdafb5eeac8330420d4b1699cc1094237805 (diff) | |
parent | 80c5811b6b15479b473e814cad0aae7de9920bb3 (diff) |
Merge branch 'dsfish-df-table-of-contents'
Diffstat (limited to 'fpdf_test.go')
-rw-r--r-- | fpdf_test.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/fpdf_test.go b/fpdf_test.go index ff41fd2..c7d843a 100644 --- a/fpdf_test.go +++ b/fpdf_test.go @@ -2060,3 +2060,34 @@ func ExampleFpdf_AddSpotColor() { // Output: // Successfully generated pdf/Fpdf_AddSpotColor.pdf } + +// This example demonstrates how to use `RegisterAlias` to create a table of +// contents. +func ExampleFpdf_TableOfContents() { + pdf := gofpdf.New("P", "mm", "A4", "") + pdf.SetFont("Arial", "", 12) + pdf.AddPage() + + // Write the table of contents. We use aliases instead of the page number + // because we don't know which page the section will begin on. + numSections := 3 + for i := 1; i <= numSections; i++ { + pdf.Cell(0, 10, fmt.Sprintf("Section %d begins on page {%d}", i, i)) + pdf.Ln(10) + } + + // Write the sections. Before we start writing, we use `RegisterAlias` to + // ensure that the alias written in the table of contents will be replaced + // by the current page number. + for i := 1; i <= numSections; i++ { + pdf.AddPage() + pdf.RegisterAlias(fmt.Sprintf("{%d}", i), fmt.Sprintf("%d", pdf.PageNo())) + pdf.Write(10, fmt.Sprintf("Section %d", i)) + } + + fileStr := example.Filename("Fpdf_TableOfContents") + err := pdf.OutputFileAndClose(fileStr) + example.Summary(err, fileStr) + // Output: + // Successfully generated pdf/Fpdf_TableOfContents.pdf +} |