Repository navigation
Expand file tree
/
Copy pathpdf_info_obj.go
More file actions
101 lines (89 loc) · 2.54 KB
/
Copy pathpdf_info_obj.go
File metadata and controls
101 lines (89 loc) · 2.54 KB
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
package gopdf
import (
"encoding/hex"
"fmt"
"io"
"time"
)
// PdfInfo Document Information Dictionary
type PdfInfo struct {
Title string // The document’s title
Author string // The name of the person who created the document
Subject string // The subject of the document
Creator string // If the document was converted to PDF from another format, the name of the application which created the original document
Producer string // If the document was converted to PDF from another format, the name of the application that converted the original document to PDF
CreationDate time.Time // The date and time the document was created, in human-readable form
}
type infoObj struct {
getRoot func() *GoPdf
}
func (i *infoObj) init(funcGetRoot func() *GoPdf) {
i.getRoot = funcGetRoot
}
func (i *infoObj) getType() string {
return "Info"
}
func (i *infoObj) write(w io.Writer, objID int) error {
var zerotime time.Time
root := i.getRoot()
info := root.info
protection := root.protection()
io.WriteString(w, "<<\n")
if info != nil {
if info.Author != "" {
if err := writeInfoText(w, "Author", info.Author, objID, protection); err != nil {
return err
}
}
if info.Title != "" {
if err := writeInfoText(w, "Title", info.Title, objID, protection); err != nil {
return err
}
}
if info.Subject != "" {
if err := writeInfoText(w, "Subject", info.Subject, objID, protection); err != nil {
return err
}
}
if info.Creator != "" {
if err := writeInfoText(w, "Creator", info.Creator, objID, protection); err != nil {
return err
}
}
if info.Producer != "" {
if err := writeInfoText(w, "Producer", info.Producer, objID, protection); err != nil {
return err
}
}
if !zerotime.Equal(info.CreationDate) {
dateStr := "D:" + infodate(info.CreationDate)
if protection != nil {
enc, err := rc4Cip(protection.objectkey(objID), []byte(dateStr))
if err != nil {
return err
}
fmt.Fprintf(w, "/CreationDate <%X>\n", enc)
} else {
fmt.Fprintf(w, "/CreationDate(%s)\n", dateStr)
}
}
}
io.WriteString(w, ">>\n")
return nil
}
func writeInfoText(w io.Writer, key, val string, objID int, protection *PDFProtection) error {
hexStr := "FEFF" + encodeUtf8(val)
if protection != nil {
raw, err := hex.DecodeString(hexStr)
if err != nil {
return err
}
enc, err := rc4Cip(protection.objectkey(objID), raw)
if err != nil {
return err
}
hexStr = fmt.Sprintf("%X", enc)
}
_, err := fmt.Fprintf(w, "/%s <%s>\n", key, hexStr)
return err
}