Skip to content

Commit 567273d

Browse files
committed
improves code
1 parent 734d910 commit 567273d

File tree

6 files changed

+47
-19
lines changed

6 files changed

+47
-19
lines changed

accessibility/pdf_tag_annots.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ func createPdfWithTextAnnotations(outputPath string) error {
5454
// Create a new page with standard letter size.
5555
page := model.NewPdfPage()
5656
mediaBox := core.MakeArrayFromFloats([]float64{0, 0, 612, 792})
57-
page.MediaBox, _ = model.NewPdfRectangle(*mediaBox)
57+
mediaRect, err := model.NewPdfRectangle(*mediaBox)
58+
if err != nil {
59+
return fmt.Errorf("failed to create MediaBox rectangle: %w", err)
60+
}
61+
page.MediaBox = mediaRect
5862

5963
// Add first text annotation.
6064
textAnnotation1 := model.NewPdfAnnotationText()

accessibility/pdf_tag_form.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package main
1616

1717
import (
18-
"fmt"
1918
"log"
2019
"os"
2120

@@ -67,7 +66,7 @@ func main() {
6766
form := model.NewPdfAcroForm()
6867
fields := core.MakeArray()
6968

70-
// Create text fields and it's label
69+
// Create text fields and its label
7170
for idx, fdef := range textFieldsDef {
7271
opt := annotator.TextFieldOptions{}
7372
textf, err := annotator.NewTextField(page, fdef.Name, fdef.Rect, opt)
@@ -97,7 +96,7 @@ func main() {
9796

9897
k, err := p.GenerateKDict()
9998
if err != nil {
100-
fmt.Errorf("Error: %v", err)
99+
log.Fatalf("Error: %v", err)
101100
}
102101

103102
k.Alt = core.MakeString(fdef.Tooltip) // Set alternative text for the label.
@@ -137,7 +136,7 @@ func main() {
137136
}
138137
}
139138

140-
// Add Submit button that will submit all fields value.
139+
// Add Submit button that will submit all field values.
141140
func addSubmitButton(page *model.PdfPage, form *model.PdfAcroForm) error {
142141
optSubmit := annotator.FormSubmitActionOptions{
143142
Url: "https://unidoc.io",
@@ -163,7 +162,7 @@ func addSubmitButton(page *model.PdfPage, form *model.PdfAcroForm) error {
163162
return nil
164163
}
165164

166-
// Add Reset button that would reset the specified fields to it's default value.
165+
// Add Reset button that would reset the specified fields to its default value.
167166
func addResetButton(page *model.PdfPage, form *model.PdfAcroForm, fields *core.PdfObjectArray) error {
168167
optReset := annotator.FormResetActionOptions{
169168
Rectangle: draw.Rectangle{

accessibility/pdf_tag_grid.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,24 @@ func main() {
3838
// Add K dictionary to the struct tree root.
3939
structTreeRoot.AddKDict(docK)
4040

41-
drawGrid(c, docK)
41+
err := drawGrid(c, docK)
42+
if err != nil {
43+
fmt.Printf("Error drawing grid: %v\n", err)
44+
return
45+
}
4246

4347
// Set the struct tree root.
4448
c.SetStructTreeRoot(structTreeRoot)
4549

46-
err := c.WriteToFile("pdf_tag_grid.pdf")
50+
err = c.WriteToFile("pdf_tag_grid.pdf")
4751
if err != nil {
4852
fmt.Printf("Error writing to file: %v\n", err)
4953
return
5054
}
5155
}
5256

5357
func drawGrid(c *creator.Creator, rootKObj *model.KDict) error {
54-
// Create table.
58+
// Create grid.
5559
grid := c.NewGrid(3)
5660
grid.SetMargins(0, 0, 10, 0)
5761

@@ -75,12 +79,12 @@ func drawGrid(c *creator.Creator, rootKObj *model.KDict) error {
7579
currentRow = grid.NewRow()
7680
currentRow.SetSection(creator.GridRowSectionHeader) // Set the first row as a header row.
7781

78-
// Draw table header.
82+
// Draw grid header.
7983
drawCell("Align left", creator.CellHorizontalAlignmentLeft)
8084
drawCell("Align center", creator.CellHorizontalAlignmentCenter)
8185
drawCell("Align right", creator.CellHorizontalAlignmentRight)
8286

83-
// Draw table content.
87+
// Draw grid content.
8488
for i := 0; i < 5; i++ {
8589
num := i + 1
8690

@@ -91,7 +95,7 @@ func drawGrid(c *creator.Creator, rootKObj *model.KDict) error {
9195
drawCell(fmt.Sprintf("$%d", num*10), creator.CellHorizontalAlignmentRight)
9296
}
9397

94-
// Draw tabke footer.
98+
// Draw grid footer.
9599
currentRow = grid.NewRow()
96100
currentRow.SetSection(creator.GridRowSectionFooter) // Set the last row as a footer row.
97101

accessibility/pdf_tag_link_annot.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package main
1010

1111
import (
1212
"fmt"
13+
"log"
1314
"os"
1415

1516
"github.com/unidoc/unipdf/v4/common/license"
@@ -55,7 +56,10 @@ func main() {
5556
heading.SetFontSize(20)
5657
chunk := heading.Append("Accessible Links in PDF Documents")
5758
chunk.Style.FontSize = 20
58-
c.Draw(heading)
59+
60+
if err := c.Draw(heading); err != nil {
61+
log.Fatalf("failed to draw heading: %v", err)
62+
}
5963

6064
// Example 1: Link with descriptive text (no Alt needed)
6165
p1 := c.NewStyledParagraph()
@@ -76,7 +80,10 @@ func main() {
7680
AltText: altText,
7781
MCID: 1,
7882
})
79-
c.Draw(p1)
83+
84+
if err := c.Draw(p1); err != nil {
85+
log.Fatalf("failed to draw paragraph: %v", err)
86+
}
8087

8188
docK.AddKChild(annot)
8289

@@ -97,7 +104,10 @@ func main() {
97104
AltText: altText,
98105
MCID: 2,
99106
})
100-
c.Draw(p2)
107+
108+
if err := c.Draw(p2); err != nil {
109+
log.Fatalf("failed to draw paragraph: %v", err)
110+
}
101111

102112
docK.AddKChild(annot)
103113

@@ -117,7 +127,10 @@ func main() {
117127
AltText: altText,
118128
MCID: 3,
119129
})
120-
c.Draw(p3)
130+
131+
if err := c.Draw(p3); err != nil {
132+
log.Fatalf("failed to draw paragraph: %v", err)
133+
}
121134

122135
docK.AddKChild(annot)
123136

@@ -136,7 +149,10 @@ func main() {
136149
AltText: altText,
137150
MCID: 4,
138151
})
139-
c.Draw(p4)
152+
153+
if err := c.Draw(p4); err != nil {
154+
log.Fatalf("failed to draw paragraph: %v", err)
155+
}
140156

141157
docK.AddKChild(annot)
142158

accessibility/pdf_tag_list.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ func main() {
8080
}
8181
}
8282

83-
c.Draw(list)
83+
if err := c.Draw(list); err != nil {
84+
log.Fatalf("failed to draw list: %v", err)
85+
}
8486

8587
// Set the struct tree root.
8688
c.SetStructTreeRoot(structTreeRoot)

accessibility/pdf_tag_table.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ func main() {
4646
// Add K dictionary to the struct tree root.
4747
structTreeRoot.AddKDict(docK)
4848

49-
drawTable(c, docK)
49+
if err := drawTable(c, docK); err != nil {
50+
fmt.Printf("Error drawing table: %v\n", err)
51+
return
52+
}
5053

5154
// Set the struct tree root.
5255
c.SetStructTreeRoot(structTreeRoot)

0 commit comments

Comments
 (0)