forked from pdfcpu/pdfcpu
/
annotate.go
1127 lines (990 loc) · 28.1 KB
/
annotate.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2021 The pdfcpu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pdfcpu
import (
"fmt"
"sort"
"strconv"
"strings"
"time"
"github.com/88250/pdfcpu/pkg/log"
"github.com/pkg/errors"
)
// AnnotationFlags represents the PDF annotation flags.
type AnnotationFlags int
const ( // See table 165
AnnInvisible AnnotationFlags = 1 << iota
AnnHidden
AnnPrint
AnnNoZoom
AnnNoRotate
AnnNoView
AnnReadOnly
AnnLocked
AnnToggleNoView
AnnLockedContents
)
// AnnotationType represents the various PDF annotation types.
type AnnotationType int
const (
AnnText AnnotationType = iota
AnnLink
AnnFreeText
AnnLine
AnnSquare
AnnCircle
AnnPolygon
AnnPolyLine
AnnHighLight
AnnUnderline
AnnSquiggly
AnnStrikeOut
AnnStamp
AnnCaret
AnnInk
AnnPopup
AnnFileAttachment
AnnSound
AnnMovie
AnnWidget
AnnScreen
AnnPrinterMark
AnnTrapNet
AnnWatermark
Ann3D
AnnRedact
)
var annotTypes = map[string]AnnotationType{
"Text": AnnText,
"Link": AnnLink,
"FreeText": AnnFreeText,
"Line": AnnLine,
"Square": AnnSquare,
"Circle": AnnCircle,
"Polygon": AnnPolygon,
"PolyLine": AnnPolyLine,
"HighLight": AnnHighLight,
"Underline": AnnUnderline,
"Squiggly": AnnSquiggly,
"StrikeOut": AnnStrikeOut,
"Stamp": AnnStamp,
"Caret": AnnCaret,
"Ink": AnnInk,
"Popup": AnnPopup,
"FileAttachment": AnnFileAttachment,
"Sound": AnnSound,
"Movie": AnnMovie,
"Widget": AnnWidget,
"Screen": AnnScreen,
"PrinterMark": AnnPrinterMark,
"TrapNet": AnnTrapNet,
"Watermark": AnnWatermark,
"3D": Ann3D,
"Redact": AnnRedact,
}
// AnnotTypeStrings manages string representations for annotation types.
var AnnotTypeStrings = map[AnnotationType]string{
AnnText: "Text",
AnnLink: "Link",
AnnFreeText: "FreeText",
AnnLine: "Line",
AnnSquare: "Square",
AnnCircle: "Circle",
AnnPolygon: "Polygon",
AnnPolyLine: "PolyLine",
AnnHighLight: "HighLight",
AnnUnderline: "Underline",
AnnSquiggly: "Squiggly",
AnnStrikeOut: "StrikeOut",
AnnStamp: "Stamp",
AnnCaret: "Caret",
AnnInk: "Ink",
AnnPopup: "Popup",
AnnFileAttachment: "FileAttachment",
AnnSound: "Sound",
AnnMovie: "Movie",
AnnWidget: "Widget",
AnnScreen: "Screen",
AnnPrinterMark: "PrinterMark",
AnnTrapNet: "TrapNet",
AnnWatermark: "Watermark",
Ann3D: "3D",
AnnRedact: "Redact",
}
// AnnotationRenderer is the interface for PDF annotations.
type AnnotationRenderer interface {
RenderDict(pageIndRef IndirectRef) Dict
Type() AnnotationType
RectString() string
ID() string
ContentString() string
}
// Annotation represents a PDF annnotation.
type Annotation struct {
SubType AnnotationType // The type of annotation that this dictionary describes.
Rect Rectangle // The annotation rectangle, defining the location of the annotation on the page in default user space units.
Contents string // Text that shall be displayed for the annotation.
P *IndirectRef // An indirect reference to the page object with which this annotation is associated.
NM string // (Since V1.4) The annotation name, a text string uniquely identifying it among all the annotations on its page.
ModDate string // The date and time when the annotation was most recently modified.
F AnnotationFlags // A set of flags specifying various characteristics of the annotation.
C *SimpleColor // The background color of the annotation’s icon when closed.
}
// NewAnnotation returns a new annotation.
func NewAnnotation(
typ AnnotationType,
rect Rectangle,
contents string,
pageIndRef *IndirectRef,
nm string,
f AnnotationFlags,
backgrCol *SimpleColor) Annotation {
return Annotation{
SubType: typ,
Rect: rect,
Contents: contents,
P: pageIndRef,
NM: nm,
F: f,
C: backgrCol}
}
// NewAnnotationForRawType returns a new annotation of a specific type.
func NewAnnotationForRawType(
typ string,
rect Rectangle,
contents string,
pageIndRef *IndirectRef,
nm string,
f AnnotationFlags,
backgrCol *SimpleColor) Annotation {
return NewAnnotation(annotTypes[typ], rect, contents, pageIndRef, nm, f, backgrCol)
}
// ID returns the annotation id.
func (ann Annotation) ID() string {
return ann.NM
}
// ContentString returns a string representation of ann's contents.
func (ann Annotation) ContentString() string {
return ann.Contents
}
// RectString returns ann's positioning rectangle.
func (ann Annotation) RectString() string {
return ann.Rect.ShortString()
}
// Type returns ann's type.
func (ann Annotation) Type() AnnotationType {
return ann.SubType
}
// TypeString returns a string representation of ann's type.
func (ann Annotation) TypeString() string {
return AnnotTypeStrings[ann.SubType]
}
// RenderDict is a stub for behavior that renders ann's PDF dict.
func (ann Annotation) RenderDict(pageIndRef IndirectRef) Dict {
return nil
}
// PopupAnnotation represents PDF Popup annotations.
type PopupAnnotation struct {
Annotation
ParentIndRef *IndirectRef // The parent annotation with which this pop-up annotation shall be associated.
Open bool // A flag specifying whether the annotation shall initially be displayed open.
}
// NewPopupAnnotation returns a new popup annotation.
func NewPopupAnnotation(
rect Rectangle,
pageIndRef *IndirectRef,
contents, id string,
f AnnotationFlags,
backgrCol *SimpleColor,
parentIndRef *IndirectRef) PopupAnnotation {
ann := NewAnnotation(AnnPopup, rect, contents, pageIndRef, id, f, backgrCol)
return PopupAnnotation{
Annotation: ann,
ParentIndRef: parentIndRef}
}
// ContentString returns a string representation of ann's content.
func (ann PopupAnnotation) ContentString() string {
s := "\"" + ann.Contents + "\""
if ann.ParentIndRef != nil {
s = "-> #" + ann.ParentIndRef.ObjectNumber.String()
}
return s
}
// MarkupAnnotation represents a PDF markup annotation.
type MarkupAnnotation struct {
Annotation
T string // The text label that shall be displayed in the title bar of the annotation’s pop-up window when open and active. This entry shall identify the user who added the annotation.
PopupIndRef *IndirectRef // An indirect reference to a pop-up annotation for entering or editing the text associated with this annotation.
CA *float64 // (Default: 1.0) The constant opacity value that shall be used in painting the annotation.
RC string // A rich text string that shall be displayed in the pop-up window when the annotation is opened.
CreationDate string // The date and time when the annotation was created.
Subj string // Text representing a short description of the subject being addressed by the annotation.
}
// NewMarkupAnnotation returns a new markup annotation.
func NewMarkupAnnotation(
subType AnnotationType,
rect Rectangle,
pageIndRef *IndirectRef,
contents, id, title string,
f AnnotationFlags,
backgrCol *SimpleColor,
popupIndRef *IndirectRef,
ca *float64,
rc, subject string) MarkupAnnotation {
ann := NewAnnotation(subType, rect, contents, pageIndRef, id, f, backgrCol)
return MarkupAnnotation{
Annotation: ann,
T: title,
PopupIndRef: popupIndRef,
CreationDate: DateString(time.Now()),
CA: ca,
RC: rc,
Subj: subject}
}
// TextAnnotation represents a PDF text annotation aka "Sticky Note".
type TextAnnotation struct {
MarkupAnnotation
Open bool // A flag specifying whether the annotation shall initially be displayed open.
Name string // The name of an icon that shall be used in displaying the annotation. Comment, Key, (Note), Help, NewParagraph, Paragraph, Insert
}
// NewTextAnnotation returns a new text annotation.
func NewTextAnnotation(
rect Rectangle,
contents, id, title string,
f AnnotationFlags,
backgrCol *SimpleColor,
ca *float64,
rc, subj string,
open bool,
name string) TextAnnotation {
ma := NewMarkupAnnotation(AnnText, rect, nil, contents, id, title, f, backgrCol, nil, ca, rc, subj)
return TextAnnotation{
MarkupAnnotation: ma,
Open: open,
Name: name,
}
}
// RenderDict renders ann into a PDF annotation dict.
func (ann TextAnnotation) RenderDict(pageIndRef IndirectRef) Dict {
subject := "Sticky Note"
if ann.Subj != "" {
subject = ann.Subj
}
d := Dict(map[string]Object{
"Type": Name("Annot"),
"Subtype": Name(ann.TypeString()),
"Rect": ann.Rect.Array(),
"P": pageIndRef,
"F": Integer(ann.F),
"CreationDate": StringLiteral(ann.CreationDate),
"Subj": StringLiteral(subject),
"Open": Boolean(ann.Open),
})
if ann.CA != nil {
d.Insert("CA", Float(*ann.CA))
}
if ann.PopupIndRef != nil {
d.Insert("Popup", *ann.PopupIndRef)
}
if ann.RC != "" {
d.InsertString("RC", ann.RC)
}
if ann.Name != "" {
d.InsertName("Name", ann.Name)
}
if ann.Contents != "" {
d.InsertString("Contents", ann.Contents)
}
if ann.NM != "" {
d.InsertString("NM", ann.NM) // check for uniqueness across annotations on this page
} else {
// new UUID
}
if ann.T != "" {
d.InsertString("T", ann.T)
}
if ann.C != nil {
d.Insert("C", NewNumberArray(float64(ann.C.R), float64(ann.C.G), float64(ann.C.B)))
}
return d
}
// LinkAnnotation represents a PDF link annotation.
type LinkAnnotation struct {
Annotation
URI string
Page int
Quad QuadPoints // Shall be ignored if any coordinate lies outside the region specified by Rect.
}
// NewLinkAnnotation returns a new link annotation.
func NewLinkAnnotation(
rect Rectangle,
quad QuadPoints,
uri, id string,
f AnnotationFlags,
backgrCol *SimpleColor) LinkAnnotation {
ann := NewAnnotation(AnnLink, rect, "", nil, id, f, backgrCol)
return LinkAnnotation{
Annotation: ann,
URI: uri,
Quad: quad,
}
}
// ContentString returns a string representation of ann's content.
func (ann LinkAnnotation) ContentString() string {
s := "(internal)"
if len(ann.URI) > 0 {
s = ann.URI
}
return s
}
// RenderDict renders ann into a PDF annotation dict.
func (ann LinkAnnotation) RenderDict(pageIndRef IndirectRef) Dict {
actionDict := Dict(map[string]Object{
"Type": Name("Action"),
"S": Name("URI"),
"URI": StringLiteral(ann.URI),
})
d := Dict(map[string]Object{
"Type": Name("Annot"),
"Subtype": Name(ann.TypeString()),
"Rect": ann.Rect.Array(),
"P": pageIndRef,
"F": Integer(ann.F),
"Border": NewIntegerArray(0, 0, 0), // no border
"H": Name("I"), // default
"A": actionDict,
})
if ann.NM != "" {
d.InsertString("NM", ann.NM)
} else {
// new UUID
}
if ann.C != nil {
d.Insert("C", NewNumberArray(float64(ann.C.R), float64(ann.C.G), float64(ann.C.B)))
}
if ann.Quad != nil {
d.Insert("QuadPoints", ann.Quad.Array())
}
return d
}
// AnnotationObjNrs returns a list of object numbers representing known annotation dict indirect references.
func (ctx *Context) AnnotationObjNrs() ([]int, error) {
// Note: Not all cached annotations are based on IndRefs!
// pdfcpu also caches direct annot dict objects (violating the PDF spec) for listing purposes.
// Such annotations may only be removecd as part of removing all annotations (for a page).
objNrs := []int{}
for _, pageAnnots := range ctx.PageAnnots {
for _, annots := range pageAnnots {
for k := range annots {
if k[0] != '?' {
i, err := strconv.Atoi(k)
if err != nil {
return nil, err
}
objNrs = append(objNrs, i)
}
}
}
}
return objNrs, nil
}
func (ctx *Context) addAnnotation(ann AnnotationRenderer, pageNr int, objNr string) error {
pgAnnots, ok := ctx.PageAnnots[pageNr]
if !ok {
pgAnnots = PgAnnots{}
ctx.PageAnnots[pageNr] = pgAnnots
}
annots, ok := pgAnnots[ann.Type()]
if !ok {
annots = AnnotMap{}
pgAnnots[ann.Type()] = annots
}
if _, ok := annots[objNr]; ok {
return errors.Errorf("addAnnotation: obj#%s already cached", objNr)
}
annots[objNr] = ann
return nil
}
func (ctx *Context) removeAnnotation(pageNr int, objNr string) error {
pgAnnots, ok := ctx.PageAnnots[pageNr]
if !ok {
return errors.Errorf("removeAnnotation: no page annotations cached for page %d", pageNr)
}
for annType, annots := range pgAnnots {
if _, ok := annots[objNr]; ok {
delete(annots, objNr)
if len(annots) == 0 {
delete(pgAnnots, annType)
if len(pgAnnots) == 0 {
delete(ctx.PageAnnots, pageNr)
}
}
return nil
}
}
return errors.Errorf("removeAnnotation: no page annotation cached for obj#%s", objNr)
}
func (ctx *Context) findAnnotByID(id string, annots Array) (int, error) {
for i, o := range annots {
d, err := ctx.DereferenceDict(o)
if err != nil {
return -1, err
}
s := d.StringEntry("NM")
if s == nil {
continue
}
if *s == id {
return i, nil
}
}
return -1, nil
}
func (ctx *Context) findAnnotByObjNr(objNr int, annots Array) (int, error) {
for i, o := range annots {
indRef, _ := o.(IndirectRef)
if indRef.ObjectNumber.Value() == objNr {
return i, nil
}
}
return -1, nil
}
func (ctx *Context) createAnnot(ar AnnotationRenderer, pageIndRef *IndirectRef) (*IndirectRef, error) {
d := ar.RenderDict(*pageIndRef)
return ctx.IndRefForNewObject(d)
}
// Annotation returns an annotation renderer.
// Validation sets up a cache of annotation renderers.
func (xRefTable *XRefTable) Annotation(d Dict) (AnnotationRenderer, error) {
subtype := d.NameEntry("Subtype")
o, _ := d.Find("Rect")
arr, err := xRefTable.DereferenceArray(o)
if err != nil {
return nil, err
}
r, err := RectForArray(arr)
if err != nil {
return nil, err
}
bb, err := d.StringEntryBytes("Contents")
if err != nil {
return nil, err
}
contents := string(bb)
var nm string
s := d.StringEntry("NM") // This is what pdfcpu refers to as the annotation id.
if s != nil {
nm = *s
}
var f AnnotationFlags
i := d.IntEntry("F")
if i != nil {
f = AnnotationFlags(*i)
}
var ann AnnotationRenderer
switch *subtype {
case "Text":
ann = NewTextAnnotation(*r, contents, nm, "", f, nil, nil, "", "", true, "")
case "Link":
var uri string
o, found := d.Find("A")
if found && o != nil {
d, err := xRefTable.DereferenceDict(o)
if err != nil {
return nil, err
}
bb, err := xRefTable.DereferenceStringEntryBytes(d, "URI")
if err != nil {
return nil, err
}
if len(bb) > 0 {
uri = string(bb)
}
}
ann = NewLinkAnnotation(*r, nil, uri, nm, f, nil)
case "Popup":
parentIndRef := d.IndirectRefEntry("Parent")
ann = NewPopupAnnotation(*r, nil, contents, nm, f, nil, parentIndRef)
// TODO handle remaining annotation types.
default:
ann = NewAnnotationForRawType(*subtype, *r, contents, nil, nm, f, nil)
}
return ann, nil
}
// AnnotMap represents annotations by object number of the corresponding annotation dict.
type AnnotMap map[string]AnnotationRenderer
// PgAnnots represents a map of page annotations by type.
type PgAnnots map[AnnotationType]AnnotMap
// ListAnnotations returns a formatted list of annotations for selected pages.
func (xRefTable *XRefTable) ListAnnotations(selectedPages IntSet) (int, []string, error) {
var (
j int
pageNrs []int
)
ss := []string{}
for k := range xRefTable.PageAnnots {
pageNrs = append(pageNrs, k)
}
sort.Ints(pageNrs)
for _, i := range pageNrs {
if selectedPages != nil {
if _, found := selectedPages[i]; !found {
continue
}
}
pageAnnots := xRefTable.PageAnnots[i]
if len(pageAnnots) == 0 {
continue
}
ss = append(ss, "")
ss = append(ss, fmt.Sprintf("Page %d:", i))
for annType, annots := range pageAnnots {
var (
maxLenRect int
maxLenContent int
)
maxLenID := 2
for _, ann := range annots {
if len(ann.RectString()) > maxLenRect {
maxLenRect = len(ann.RectString())
}
if len(ann.ID()) > maxLenID {
maxLenID = len(ann.ID())
}
if len(ann.ContentString()) > maxLenContent {
maxLenContent = len(ann.ContentString())
}
}
ss = append(ss, "")
ss = append(ss, fmt.Sprintf(" %s:", AnnotTypeStrings[annType]))
s1 := (" obj# ")
s2 := fmt.Sprintf("%%%ds", maxLenRect)
s3 := fmt.Sprintf("%%%ds", maxLenID)
s4 := fmt.Sprintf("%%%ds", maxLenContent)
s := fmt.Sprintf(s1+s2+" "+s3+" "+s4, "rect", "id", "content")
ss = append(ss, s)
ss = append(ss, " "+strings.Repeat("=", len(s)-4))
for objNr, ann := range annots {
s := "?"
if objNr[0] != '?' {
s = objNr
}
ss = append(ss, fmt.Sprintf(" %5s "+s2+" "+s3+" "+s4, s, ann.RectString(), ann.ID(), ann.ContentString()))
j++
}
}
}
return j, append([]string{fmt.Sprintf("%d annotations available", j)}, ss...), nil
}
func (ctx *Context) addAnnotationToDirectObj(
annots Array,
annotIndRef, pageDictIndRef *IndirectRef,
pageDict Dict,
pageNr int,
ar AnnotationRenderer,
incr bool) (bool, error) {
i, err := ctx.findAnnotByID(ar.ID(), annots)
if err != nil {
return false, err
}
if i >= 0 {
return false, errors.Errorf("page %d: duplicate annotation with id:%s\n", pageNr, ar.ID())
}
pageDict.Update("Annots", append(annots, *annotIndRef))
if incr {
// Mark page dict obj for incremental writing.
ctx.Write.IncrementWithObjNr(pageDictIndRef.ObjectNumber.Value())
}
ctx.EnsureVersionForWriting()
return true, nil
}
// AddAnnotation adds ar to pageDict.
func (ctx *Context) AddAnnotation(pageDictIndRef *IndirectRef, pageDict Dict, pageNr int, ar AnnotationRenderer, incr bool) (bool, error) {
// Create xreftable entry for annotation.
annotIndRef, err := ctx.createAnnot(ar, pageDictIndRef)
if err != nil {
return false, err
}
// Add annotation to xreftable page annotation cache.
err = ctx.addAnnotation(ar, pageNr, annotIndRef.ObjectNumber.String())
if err != nil {
return false, err
}
if incr {
// Mark new annotaton dict obj for incremental writing.
ctx.Write.IncrementWithObjNr(annotIndRef.ObjectNumber.Value())
}
obj, found := pageDict.Find("Annots")
if !found {
pageDict.Insert("Annots", Array{*annotIndRef})
if incr {
// Mark page dict obj for incremental writing.
ctx.Write.IncrementWithObjNr(pageDictIndRef.ObjectNumber.Value())
}
ctx.EnsureVersionForWriting()
return true, nil
}
ir, ok := obj.(IndirectRef)
if !ok {
return ctx.addAnnotationToDirectObj(obj.(Array), annotIndRef, pageDictIndRef, pageDict, pageNr, ar, incr)
}
// Annots array is an IndirectReference.
o, err := ctx.Dereference(ir)
if err != nil || o == nil {
return false, err
}
annots, _ := o.(Array)
i, err := ctx.findAnnotByID(ar.ID(), annots)
if err != nil {
return false, err
}
if i >= 0 {
return false, errors.Errorf("page %d: duplicate annotation with id:%s\n", pageNr, ar.ID())
}
entry, ok := ctx.FindTableEntryForIndRef(&ir)
if !ok {
return false, errors.Errorf("page %d: can't dereference Annots indirect reference(obj#:%d)\n", pageNr, ir.ObjectNumber)
}
entry.Object = append(annots, *annotIndRef)
if incr {
// Mark Annot array obj for incremental writing.
ctx.Write.IncrementWithObjNr(ir.ObjectNumber.Value())
}
ctx.EnsureVersionForWriting()
return true, nil
}
// AddAnnotations adds ar to selected pages.
func (ctx *Context) AddAnnotations(selectedPages IntSet, ar AnnotationRenderer, incr bool) (bool, error) {
var ok bool
if incr {
ctx.Write.Increment = true
ctx.Write.Offset = ctx.Read.FileSize
}
for k, v := range selectedPages {
if !v {
continue
}
if k > ctx.PageCount {
return false, errors.Errorf("pdfcpu: invalid page number: %d", k)
}
pageDictIndRef, err := ctx.PageDictIndRef(k)
if err != nil {
return false, err
}
d, err := ctx.DereferenceDict(*pageDictIndRef)
if err != nil {
return false, err
}
added, err := ctx.AddAnnotation(pageDictIndRef, d, k, ar, incr)
if err != nil {
return false, err
}
if added {
ok = true
}
}
return ok, nil
}
// AddAnnotationsMap adds annotations in m to corresponding pages.
func (ctx *Context) AddAnnotationsMap(m map[int][]AnnotationRenderer, incr bool) (bool, error) {
var ok bool
if incr {
ctx.Write.Increment = true
ctx.Write.Offset = ctx.Read.FileSize
}
for i, annots := range m {
if i > ctx.PageCount {
return false, errors.Errorf("pdfcpu: invalid page number: %d", i)
}
pageDictIndRef, err := ctx.PageDictIndRef(i)
if err != nil {
return false, err
}
d, err := ctx.DereferenceDict(*pageDictIndRef)
if err != nil {
return false, err
}
for _, annot := range annots {
added, err := ctx.AddAnnotation(pageDictIndRef, d, i, annot, incr)
if err != nil {
return false, err
}
if added {
ok = true
}
}
}
return ok, nil
}
func (ctx *Context) removeAllAnnotations(pageDict Dict, pageDictObjNr, pageNr int, incr bool) (bool, error) {
var err error
obj, found := pageDict.Find("Annots")
if !found {
return false, nil
}
ir, ok := obj.(IndirectRef)
if ok {
obj, err = ctx.Dereference(ir)
if err != nil || obj == nil {
return false, err
}
objNr := ir.ObjectNumber.Value()
if err := ctx.deleteObject(ir); err != nil {
return false, err
}
if incr {
// Modify Annots array obj for incremental writing.
ctx.Write.IncrementWithObjNr(objNr)
}
}
annots, _ := obj.(Array)
for _, o := range annots {
ir, _ := o.(IndirectRef)
objNr := ir.ObjectNumber.Value()
if err := ctx.deleteObject(ir); err != nil {
return false, err
}
if incr {
// Mark annotation dict obj for incremental writing.
ctx.Write.IncrementWithObjNr(objNr)
}
}
pageDict.Delete("Annots")
if incr {
// Mark page dict obj for incremental writing.
ctx.Write.IncrementWithObjNr(pageDictObjNr)
}
// Remove xref table page annotation cache.
delete(ctx.PageAnnots, pageNr)
return true, nil
}
func (ctx *Context) removeAnnotationsFromPageDictByID(ids []string, pageNr int, annots Array, incr bool) (Array, bool, error) {
var ok bool
for _, id := range ids {
i, err := ctx.findAnnotByID(id, annots)
if err != nil {
return nil, false, err
}
if i >= 0 {
ok = true
ir1, _ := annots[i].(IndirectRef)
// Remove annotation from xreftable page annotation cache.
err = ctx.removeAnnotation(pageNr, ir1.ObjectNumber.String())
if err != nil {
return nil, false, err
}
if err := ctx.deleteObject(ir1); err != nil {
return nil, false, err
}
if incr {
// Mark annotation dict obj for incremental writing.
ctx.Write.IncrementWithObjNr(ir1.ObjectNumber.Value())
}
if len(annots) == 1 {
return nil, ok, nil
}
annots = append(annots[:i], annots[i+1:]...)
}
}
return annots, ok, nil
}
func (ctx *Context) removeAnnotationsFromPageDictByObjNr(objNrSet IntSet, pageNr int, annots Array, incr bool) (Array, bool, error) {
var ok bool
for objNr, v := range objNrSet {
if !v {
continue
}
i, err := ctx.findAnnotByObjNr(objNr, annots)
if err != nil {
return nil, false, err
}
if i >= 0 {
ok = true
ir1, _ := annots[i].(IndirectRef)
// Remove annotation from xreftable page annotation cache.
err = ctx.removeAnnotation(pageNr, ir1.ObjectNumber.String())
if err != nil {
return nil, false, err
}
if err := ctx.deleteObject(ir1); err != nil {
return nil, false, err
}
if incr {
// Mark annotation dict obj for incremental writing.
ctx.Write.IncrementWithObjNr(ir1.ObjectNumber.Value())
}
delete(objNrSet, objNr)
if len(annots) == 1 {
return nil, ok, nil
}
annots = append(annots[:i], annots[i+1:]...)
}
}
return annots, ok, nil
}
func (ctx *Context) removeAnnotationsFromPageDictByIDOrObjNr(ids []string, objNrSet IntSet, pageNr int, annots Array, incr bool) (Array, bool, error) {
var (
ok bool
err error
)
if len(ids) > 0 {
annots, ok, err = ctx.removeAnnotationsFromPageDictByID(ids, pageNr, annots, incr)
if err != nil || annots == nil {
return nil, ok, err
}
}
return ctx.removeAnnotationsFromPageDictByObjNr(objNrSet, pageNr, annots, incr)
}
func (ctx *Context) removeAnnotationsFromDirectObj(
annots Array,
ids []string,
objNrSet IntSet,
pageDict Dict,
pageDictObjNr, pageNr int,
incr bool) (bool, error) {
var (
ok bool
err error
)
annots, ok, err = ctx.removeAnnotationsFromPageDictByIDOrObjNr(ids, objNrSet, pageNr, annots, incr)
if err != nil {
return false, err
}
if !ok {
return false, nil
}
if incr {
// Mark page dict obj for incremental writing.
ctx.Write.IncrementWithObjNr(pageDictObjNr)
}
if annots == nil {
pageDict.Delete("Annots")
return ok, nil
}
pageDict.Update("Annots", annots)
return true, nil
}
// RemoveAnnotationsFromPageDict removes an annotation by its object number annObjNr from pageDict.
func (ctx *Context) RemoveAnnotationsFromPageDict(ids []string, objNrSet IntSet, pageDict Dict, pageDictObjNr, pageNr int, incr bool) (bool, error) {
var (
ok bool