-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
221 lines (190 loc) · 6.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Extract highlighted text.
*
* Run as: go run list_highlights.go input.pdf [input2.pdf, ...]
*/
package main
import (
"encoding/csv"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/unidoc/unipdf/v3/annotator"
"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/core"
"github.com/unidoc/unipdf/v3/extractor"
"github.com/unidoc/unipdf/v3/model"
)
// Cmd-line init
func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}
// UniDOC Playground init
// func init() {
// os.Args = []string{"playground", "one_page.pdf", "two_pages.pdf"}
// }
var (
// Need to make the highlight regions slightly smaller in height, so as not
// overlap with text below the highlight
scaleW, scaleH float64
// For visualizing the highlight rects that UniPDF sees compared to what you see in the PDF
visualize bool
)
func main() {
flag.Float64Var(&scaleW, "scale-w", 1.0, "scale the highlight rect's width")
flag.Float64Var(&scaleH, "scale-h", 1.0, "scale the highlight rect's height")
flag.BoolVar(&visualize, "vis", false, "create viz_*.pdf files with highlight rects drawn for debugging")
flag.Parse()
fnames := flag.Args()
if len(fnames) < 2 {
fmt.Println("Usage: go run list_highlights.go input.pdf [input2.pdf, ...]")
os.Exit(1)
}
// Create CSV writer
csvW := csv.NewWriter(os.Stdout)
csvW.Write([]string{"Filename", "Page_num", "Highlighted_text"})
// Iterate input PDFs
for _, inputPath := range fnames {
pdfReader, f, err := model.NewPdfReaderFromFile(inputPath, nil)
if err != nil {
fmt.Printf("error: could not create PdfReader for %q: %q\n", inputPath, err)
continue
}
defer f.Close()
numPages, err := pdfReader.GetNumPages()
if err != nil {
fmt.Printf("error: could not get number of pages of %q: %q\n", inputPath, err)
continue
}
// Map a page number to slice of Annotator rects (for visualizing/debugging)
pageVizRectsMap := make(map[int][]annotator.RectangleAnnotationDef)
// Iterate pages
for i := 1; i <= numPages; i++ {
page, err := pdfReader.GetPage(i)
if err != nil {
fmt.Printf("error: could not get page %d of %q: %q\n", i, inputPath, err)
continue
}
ex, err := extractor.New(page)
if err != nil {
fmt.Printf("error: could not create extractor for page %d of %q: %q\n", i, inputPath, err)
continue
}
pageText, _, _, err := ex.ExtractPageText()
if err != nil {
fmt.Printf("error: could not extract text on page %d of %q: %q\n", i, inputPath, err)
continue
}
annotations, err := page.GetAnnotations()
if err != nil {
fmt.Printf("error: could not get annotations on page %d of %q: %q\n", i, inputPath, err)
continue
}
vizRects := make([]annotator.RectangleAnnotationDef, 0)
for _, annotation := range annotations {
highlight, isHL := annotation.GetContext().(*model.PdfAnnotationHighlight)
if !isHL {
// skip non-highlight annotation
continue
}
objArr, isObjArr := highlight.QuadPoints.(*core.PdfObjectArray)
if !isObjArr {
// skip non-ObjectArray
continue
}
quadPts, err := objArr.ToFloat64Array()
if err != nil {
fmt.Printf("error: could not convert PdfObjectArrary %q to Float64Array: %q\n", objArr, err)
continue
}
if len(quadPts)%8 != 0 {
fmt.Printf("error: needs QuadPoints to be a multiple of 8, its length is %d\n", len(quadPts))
continue
}
// Iterate sets of QuadPoints to get individual highlight rects, and extract text for the rect
var allText []string
var llx, lly, urx, ury float64
var w, h, cx, cy float64
for i := 0; i < len(quadPts); i += 8 {
pts := quadPts[i : i+8]
// Original diagonal corners of rect, and width and height
llx, lly, urx, ury = pts[4], pts[5], pts[2], pts[3]
w, h = (urx - llx), (ury - lly)
// Get center
cx, cy = llx+(w/2), lly+(h/2)
// Scale sides
w, h = w*scaleW, h*scaleH
// Recompute diagonal corners
llx, lly, urx, ury = cx-(w/2), cy-(h/2), cx+(w/2), cy+(h/2)
rect := model.PdfRectangle{Llx: llx, Lly: lly, Urx: urx, Ury: ury}
pageText.ApplyArea(rect)
text := pageText.Text()
text = strings.TrimSpace(text)
if len(text) > 0 {
allText = append(allText, text)
}
if visualize {
rectDef := annotator.RectangleAnnotationDef{}
rectDef.X = llx
rectDef.Y = lly
rectDef.Width = w
rectDef.Height = h
vizRects = append(vizRects, rectDef)
}
}
if len(allText) > 0 {
text := strings.Join(allText, " ")
csvW.Write([]string{inputPath, fmt.Sprintf("%d", i), text})
}
}
if len(vizRects) > 0 {
pageVizRectsMap[i] = vizRects
}
}
if visualize {
opt := &model.ReaderToWriterOpts{
// Callback is executed for every page, with the page as pageNum, during the Reader-to-Writer conversion
PageProcessCallback: func(pageNum int, page *model.PdfPage) error {
// See if this page has any viz/debug annotation rects
rectDefs, ok := pageVizRectsMap[pageNum]
if !ok {
return nil
}
// Add rects to page
for _, rectDef := range rectDefs {
rectDef.Opacity = 1
rectDef.BorderEnabled = true
rectDef.BorderWidth = 1.5
rectDef.BorderColor = model.NewPdfColorDeviceRGB(1, 0, 0) // Red border
rectAnnotation, err := annotator.CreateRectangleAnnotation(rectDef)
if err != nil {
return err
}
page.AddAnnotation(rectAnnotation)
}
return nil
},
}
pdfWriter, err := pdfReader.ToWriter(opt)
if err != nil {
fmt.Printf("error: could not create Writer: %q\n", err)
os.Exit(1)
}
dir, file := filepath.Split(inputPath)
outputPath := filepath.Join(dir, "viz_"+file)
err = pdfWriter.WriteToFile(outputPath)
if err != nil {
fmt.Printf("error: could not write VIZ PDF to %q: %q\n", outputPath, err)
return
}
}
}
csvW.Flush()
}