-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdocument.go
151 lines (121 loc) · 3.8 KB
/
document.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
package poppler
// #cgo pkg-config: poppler-glib
// #include <poppler.h>
// #include <stdlib.h>
// #include <glib.h>
// #include <unistd.h>
import "C"
import (
"errors"
"unsafe"
"path/filepath"
)
type Document struct {
doc poppDoc
openedPages []*Page
}
type DocumentInfo struct {
PdfVersion, Title, Author, Subject, KeyWords, Creator, Producer, Metadata string
CreationDate, ModificationDate, Pages int
IsLinearized bool
}
func (d *Document) Info() DocumentInfo {
return DocumentInfo{
PdfVersion: toString(C.poppler_document_get_pdf_version_string(d.doc)),
Title: toString(C.poppler_document_get_title(d.doc)),
Author: toString(C.poppler_document_get_author(d.doc)),
Subject: toString(C.poppler_document_get_subject(d.doc)),
KeyWords: toString(C.poppler_document_get_keywords(d.doc)),
Creator: toString(C.poppler_document_get_creator(d.doc)),
Producer: toString(C.poppler_document_get_producer(d.doc)),
Metadata: toString(C.poppler_document_get_metadata(d.doc)),
CreationDate: int(C.poppler_document_get_creation_date(d.doc)),
ModificationDate: int(C.poppler_document_get_modification_date(d.doc)),
Pages: int(C.poppler_document_get_n_pages(d.doc)),
IsLinearized: toBool(C.poppler_document_is_linearized(d.doc)),
}
}
func (d *Document) GetNPages() int {
return int(C.poppler_document_get_n_pages(d.doc))
}
func (d *Document) GetPage(i int) (page *Page) {
p := C.poppler_document_get_page(d.doc, C.int(i))
page = &Page{
p: p,
openedAnnots: nil,
}
d.openedPages = append(d.openedPages, page)
return page
}
func (d *Document) HasAttachments() bool {
return toBool(C.poppler_document_has_attachments(d.doc))
}
func (d *Document) GetNAttachments() int {
return int(C.poppler_document_get_n_attachments(d.doc))
}
func (d *Document) Close() {
for i := 0; i < len(d.openedPages); i++ {
d.openedPages[i].Close()
}
d.openedPages = []*Page{}
C.g_object_unref(C.gpointer(d.doc))
}
func (d *Document) NewAnnot(t AnnotType, r Rectangle, q []Quad) (Annot, error) {
am := C.poppler_annot_mapping_new();
annot := Annot {
am: am,
}
pRect := rectangleToPopplerRectangle(r)
pQuad := quadsToGArray(q)
defer C.g_array_free(pQuad, 1)
switch (t){
case AnnotHighlight:
am.annot = C.poppler_annot_text_markup_new_highlight(d.doc, &pRect, pQuad)
case AnnotUnderline:
am.annot = C.poppler_annot_text_markup_new_underline(d.doc, &pRect, pQuad)
case AnnotSquiggly:
am.annot = C.poppler_annot_text_markup_new_squiggly(d.doc, &pRect, pQuad)
case AnnotStrikeOut:
am.annot = C.poppler_annot_text_markup_new_strikeout(d.doc, &pRect, pQuad)
default:
C.poppler_annot_mapping_free(am)
return annot, errors.New("invalid type for new annotation")
}
if am.annot == nil {
C.poppler_annot_mapping_free(am)
return annot, errors.New("failed to create annotation")
}
/* Can't get real annot mapping area as done in
* poppler_page_get_annot_mapping() since page is
* needed for page->page->getCropBox() and
* page->page->getRotate()
*
* as a placeholder we just use the annot rect
*/
annot.am.area = pRect
return annot, nil
}
func (d *Document) Save(filename string) (saved bool, err error) {
filename, err = filepath.Abs(filename)
if err != nil {
return false, err
}
var e *C.GError
cFilename := (*C.gchar)(C.CString(filename))
defer C.free(unsafe.Pointer(cFilename))
cUri := C.g_filename_to_uri(cFilename, nil, nil)
cBool := C.poppler_document_save (d.doc, cUri, &e);
if e != nil {
err = errors.New(C.GoString((*C.char)(e.message)))
return false, err
}
if cBool == C.TRUE {
return true, nil
}
return false, nil
}
/*
func (d *Document) GetAttachments() []Attachment {
return
}
*/