diff options
author | Kevin Seidel <kevin@zahlz.com> | 2018-01-31 08:45:10 +0100 |
---|---|---|
committer | Kevin Seidel <kevin@zahlz.com> | 2018-01-31 08:45:10 +0100 |
commit | f27fb86f5efd6e011db7bff1ddbdae87c31a8e48 (patch) | |
tree | 6174fe31cac6a056fba94a8c9f49acb649042e80 /fpdf_test.go | |
parent | 93d1c15c05dd7d06f99dad920f11545a8a8654d0 (diff) | |
parent | d65c2335e8ec3db6c87c9e23aa2ff329da57e182 (diff) |
Merge branch 'master' of https://github.com/jung-kurt/gofpdf
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..c7cd582 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_RegisterAlias() { + 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_RegisterAlias") + err := pdf.OutputFileAndClose(fileStr) + example.Summary(err, fileStr) + // Output: + // Successfully generated pdf/Fpdf_RegisterAlias.pdf +} |