|
| 1 | +// This example demonstrates how to create a tagged PDF with a table |
| 2 | +// using the UniPDF library. The table will be properly tagged in the document |
| 3 | +// structure tree for accessibility compliance. |
| 4 | +// |
| 5 | +// The program creates a PDF document containing: |
| 6 | +// - A table with three columns and multiple rows |
| 7 | +// - Proper tagging structure for accessibility compliance |
| 8 | +// - Custom cell alignment and borders |
| 9 | +// - Structure tree root with K dictionaries for the table and its cells |
| 10 | +// |
| 11 | +// This is useful for creating accessible PDF documents that can be properly |
| 12 | +// interpreted by screen readers and other assistive technologies. |
| 13 | + |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "os" |
| 19 | + |
| 20 | + "github.com/unidoc/unipdf/v4/common/license" |
| 21 | + "github.com/unidoc/unipdf/v4/core" |
| 22 | + "github.com/unidoc/unipdf/v4/creator" |
| 23 | + "github.com/unidoc/unipdf/v4/model" |
| 24 | +) |
| 25 | + |
| 26 | +func init() { |
| 27 | + // Make sure to load your metered License API key prior to using the library. |
| 28 | + // If you need a key, you can sign up and create a free one at https://cloud.unidoc.io |
| 29 | + err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)) |
| 30 | + if err != nil { |
| 31 | + panic(err) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func main() { |
| 36 | + c := creator.New() |
| 37 | + c.SetPageMargins(50, 50, 50, 50) |
| 38 | + |
| 39 | + structTreeRoot := model.NewStructTreeRoot() |
| 40 | + |
| 41 | + // Construct base K dictionary. |
| 42 | + docK := model.NewKDictionary() |
| 43 | + docK.S = core.MakeName(string(model.StructureTypeDocument)) |
| 44 | + docK.ID = core.MakeString(docK.GenerateRandomID()) |
| 45 | + |
| 46 | + // Add K dictionary to the struct tree root. |
| 47 | + structTreeRoot.AddKDict(docK) |
| 48 | + |
| 49 | + drawTable(c, docK) |
| 50 | + |
| 51 | + // Set the struct tree root. |
| 52 | + c.SetStructTreeRoot(structTreeRoot) |
| 53 | + |
| 54 | + err := c.WriteToFile("pdf_tag_table.pdf") |
| 55 | + if err != nil { |
| 56 | + fmt.Printf("Error writing to file: %v\n", err) |
| 57 | + return |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func drawTable(c *creator.Creator, rootKObj *model.KDict) error { |
| 62 | + // Create table. |
| 63 | + table := c.NewTable(3) |
| 64 | + table.SetMargins(0, 0, 10, 0) |
| 65 | + |
| 66 | + drawCell := func(text string, align creator.CellHorizontalAlignment) { |
| 67 | + p := c.NewStyledParagraph() |
| 68 | + p.SetText(text) |
| 69 | + |
| 70 | + cell := table.NewCell() |
| 71 | + cell.SetBorder(creator.CellBorderSideAll, creator.CellBorderStyleSingle, 1) |
| 72 | + cell.SetHorizontalAlignment(align) |
| 73 | + cell.SetContent(p) |
| 74 | + } |
| 75 | + |
| 76 | + // Draw table header. |
| 77 | + drawCell("Align left", creator.CellHorizontalAlignmentLeft) |
| 78 | + drawCell("Align center", creator.CellHorizontalAlignmentCenter) |
| 79 | + drawCell("Align right", creator.CellHorizontalAlignmentRight) |
| 80 | + |
| 81 | + // Draw table content. |
| 82 | + for i := 0; i < 5; i++ { |
| 83 | + num := i + 1 |
| 84 | + |
| 85 | + drawCell(fmt.Sprintf("Product #%d", num), creator.CellHorizontalAlignmentLeft) |
| 86 | + drawCell(fmt.Sprintf("Description #%d", num), creator.CellHorizontalAlignmentCenter) |
| 87 | + drawCell(fmt.Sprintf("$%d", num*10), creator.CellHorizontalAlignmentRight) |
| 88 | + } |
| 89 | + |
| 90 | + // Tag the table with the root K dictionary. |
| 91 | + // This associates the table with the document structure tree. |
| 92 | + // The table will be a child of the document structure tree. |
| 93 | + table.AddTag(rootKObj) |
| 94 | + |
| 95 | + return c.Draw(table) |
| 96 | +} |
0 commit comments