-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocument.go
More file actions
1958 lines (1832 loc) · 48.4 KB
/
document.go
File metadata and controls
1958 lines (1832 loc) · 48.4 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
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
package asposepdf
/*
#cgo CFLAGS: -g -Wall
#include <stdlib.h>
#include "extern_c.h"
*/
import "C"
import (
"encoding/json"
"errors"
"fmt"
"runtime"
"strings"
"unsafe"
)
// Document represents a PDF-document.
type Document struct {
pdf unsafe.Pointer
}
// New creates a new PDF-document.
//
// Example:
//
// pdf_new, err := New()
// if err != nil {
// fmt.Errorf("New(): %v", err)
// } else {
// // working with new PDF-document
// }
func New() (*Document, error) {
runtime.LockOSThread()
var err *C.char
doc := C.PDFDocument_New(&err)
err_str := C.GoString(err)
C.c_free_string(err)
if doc != nil {
return &Document{doc}, nil
} else {
return &Document{nil}, errors.New(err_str)
}
}
// Open opens a PDF-document with filename.
//
// Example:
//
// pdf, err := Open("example.pdf")
// if err != nil {
// fmt.Errorf("Open(): %v", err)
// } else {
// // working with open PDF-document
// }
func Open(filename string) (*Document, error) {
runtime.LockOSThread()
var err *C.char
_filename := C.CString(filename)
defer C.free(unsafe.Pointer(_filename))
doc := C.PDFDocument_Open(_filename, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if doc != nil {
return &Document{doc}, nil
} else {
return &Document{nil}, errors.New(err_str)
}
}
// OpenWithPassword opens a password-protected PDF-document.
// Example:
//
// pdf, err := OpenWithPassword("example.pdf", "password")
// if err != nil {
// fmt.Errorf("OpenWithPassword(): %v", err)
// } else {
// // working with open PDF-document
// }
func OpenWithPassword(filename string, password string) (*Document, error) {
runtime.LockOSThread()
var err *C.char
_filename := C.CString(filename)
defer C.free(unsafe.Pointer(_filename))
_password := C.CString(password)
defer C.free(unsafe.Pointer(_password))
doc := C.PDFDocument_Open_With_Password(_filename, _password, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if doc != nil {
return &Document{doc}, nil
} else {
return &Document{nil}, errors.New(err_str)
}
}
// MergeDocuments creates a new PDF-document by merging the provided documents.
//
// Example:
//
// pdf_merged, err := MergeDocuments([]*Document{pdf1, pdf2})
// if err != nil {
// fmt.Errorf("MergeDocuments(): %v", err)
// } else {
// // working with new merged PDF-document
// }
func MergeDocuments(documents []*Document) (*Document, error) {
if len(documents) == 0 {
return nil, errors.New("MergeDocuments(): no documents to merge")
}
// Create a new empty PDF document
merged, err := New()
if err != nil {
return nil, fmt.Errorf("MergeDocuments(): failed to create new document: %w", err)
}
// Append each input document to the merged document
for i, document := range documents {
if document == nil || document.pdf == nil {
return nil, fmt.Errorf("MergeDocuments(): document at index %d is nil or invalid", i)
}
if err := merged.Append(document); err != nil {
return nil, fmt.Errorf("MergeDocuments(): failed to append document at index %d: %w", i, err)
}
}
return merged, nil
}
// splitDocument is a helper used by Split and SplitDocument.
// Splits the source document into multiple documents based on the page range string.
//
// Each part of the pagerange string (separated by `;`) defines the page range for a new PDF-document.
func splitDocument(document *Document, pagerange string) ([]*Document, error) {
if document == nil || document.pdf == nil {
return nil, errors.New("splitDocument: source document is nil or invalid")
}
if pagerange == "" {
return nil, errors.New("splitDocument: empty page range string")
}
parts := strings.Split(pagerange, ";")
result := make([]*Document, 0, len(parts))
for i, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
return nil, fmt.Errorf("splitDocument: empty page range at index %d", i)
}
newdoc, err := New()
if err != nil {
return nil, fmt.Errorf("splitDocument: failed to create new document for range %q: %w", part, err)
}
if err := newdoc.AppendPages(document, part); err != nil {
newdoc.Close()
return nil, fmt.Errorf("splitDocument: failed to append pages %q: %w", part, err)
}
result = append(result, newdoc)
}
return result, nil
}
// SplitDocument creates multiple new PDF-documents by extracting pages from the source PDF-document.
//
// Each part of the pagerange string (separated by `;`) defines the page range for a new PDF-document.
//
// Example:
//
// pdfs, err := asposepdf.SplitDocument(pdf_source, "1-2;3;4-")
// // pdfs[0] will contain pages 1-2, pdfs[1] page 3, pdfs[2] pages 4 to end.
func SplitDocument(document *Document, pagerange string) ([]*Document, error) {
return splitDocument(document, pagerange)
}
// splitAtPage is an internal helper used by SplitAtPage and SplitAt.
// Splits the document at the specified page into two new PDF-documents:
// [1..=page] and [page+1..end].
func splitAtPage(document *Document, page int) (*Document, *Document, error) {
if document == nil || document.pdf == nil {
return nil, nil, errors.New("splitAtPage: source document is nil or invalid")
}
pageCount, err := document.PageCount()
if err != nil {
return nil, nil, fmt.Errorf("splitAtPage: failed to get page count: %w", err)
}
if page < 1 || page >= int(pageCount) {
return nil, nil, fmt.Errorf("splitAtPage: page %d is out of valid range (1-%d exclusive)", page, pageCount)
}
// Create first document for pages 1 to page
left, err := New()
if err != nil {
return nil, nil, fmt.Errorf("splitAtPage: failed to create first document: %w", err)
}
if err := left.AppendPages(document, fmt.Sprintf("1-%d", page)); err != nil {
left.Close()
return nil, nil, fmt.Errorf("splitAtPage: failed to append left pages: %w", err)
}
// Create second document for pages page+1 to end
right, err := New()
if err != nil {
right.Close()
return nil, nil, fmt.Errorf("splitAtPage: failed to create second document: %w", err)
}
if err := right.AppendPages(document, fmt.Sprintf("%d-", page+1)); err != nil {
left.Close()
right.Close()
return nil, nil, fmt.Errorf("splitAtPage: failed to append right pages: %w", err)
}
return left, right, nil
}
// SplitAtPage splits the PDF-document into two new PDF-documents.
// The first document includes pages 1 to 'page' (inclusive).
// The second document includes pages from 'page+1' to the end.
//
// Example:
//
// left, right, err := SplitAtPage(source, 3)
// // 'left' contains pages 1-3, 'right' contains pages 4 to end
func SplitAtPage(document *Document, page int) (*Document, *Document, error) {
return splitAtPage(document, page)
}
// Close releases allocated resources for PDF-document.
//
// Example:
//
// defer pdf.Close()
func (document *Document) Close() error {
defer runtime.UnlockOSThread()
var err *C.char
C.PDFDocument_Release(document.pdf, &err)
document.pdf = nil
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// About returns metadata information about the Aspose.PDF for Go via C++.
//
// The metadata is returned as a ProductInfo struct, deserialized from a JSON string.
// It includes product name, version, release date, licensing status, and related details.
//
// See also: product_info.go
//
// Example:
//
// info, err := pdf.About()
func (document *Document) About() (*ProductInfo, error) {
var err *C.char
jsonStr := C.PDFDocument_About(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return nil, errors.New(err_str)
}
defer C.c_free_string(jsonStr)
goJSON := C.GoString(jsonStr)
var info ProductInfo
if e := json.Unmarshal([]byte(goJSON), &info); e != nil {
return nil, e
}
return &info, nil
}
// Save saves previously opened PDF-document.
//
// Example:
//
// err := pdf.Save()
func (document *Document) Save() error {
var err *C.char
C.PDFDocument_Save(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// SaveAs saves previously opened PDF-document with new filename.
//
// Example:
//
// err := pdf.SaveAs("new_filename.pdf")
func (document *Document) SaveAs(filename string) error {
var err *C.char
_filename := C.CString(filename)
defer C.free(unsafe.Pointer(_filename))
C.PDFDocument_Save_As(document.pdf, _filename, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// SetLicense licenses with filename.
//
// Example:
//
// err := pdf.SetLicense("Aspose.PDF.Go.lic")
func (document *Document) SetLicense(filename string) error {
var err *C.char
_filename := C.CString(filename)
defer C.free(unsafe.Pointer(_filename))
C.PDFDocument_set_License(document.pdf, _filename, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// Split creates multiple new PDF-documents by extracting pages from the current PDF-document.
//
// Each part of the pagerange string (separated by `;`) defines the page range for a new PDF-document.
//
// Example:
//
// pdfs, err := pdf.Split("1-2;3;4-")
// // pdfs[0] will contain pages 1-2, pdfs[1] page 3, pdfs[2] pages 4 to end.
func (document *Document) Split(pagerange string) ([]*Document, error) {
return splitDocument(document, pagerange)
}
// SplitAt splits the current PDF-document into two new PDF-documents.
// The first document includes pages 1 to 'page' (inclusive).
// The second document includes pages from 'page+1' to the end.
//
// Example:
//
// left, right, err := SplitAtPage(source, 3)
// // 'left' contains pages 1-3, 'right' contains pages 4 to end
func (document *Document) SplitAt(page int) (*Document, *Document, error) {
return splitAtPage(document, page)
}
// ExtractText returns PDF-document contents as plain text.
//
// Example:
//
// txt, err := pdf.ExtractText()
func (document *Document) ExtractText() (string, error) {
var err *C.char
txt := C.PDFDocument_ExtractText(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return "", errors.New(err_str)
} else {
return C.GoString(txt), nil
}
}
// Optimize optimizes PDF-document content.
//
// Example:
//
// err := pdf.Optimize()
func (document *Document) Optimize() error {
var err *C.char
C.PDFDocument_Optimize(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// OptimizeResource optimizes resources of PDF-document.
//
// Example:
//
// err := pdf.OptimizeResource()
func (document *Document) OptimizeResource() error {
var err *C.char
C.PDFDocument_OptimizeResource(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// OptimizeFileSize optimizes size of PDF-document with image compression quality.
//
// Example:
//
// err := pdf.OptimizeFileSize(50)
func (document *Document) OptimizeFileSize(imageQuality int32) error {
var err *C.char
C.PDFDocument_OptimizeFileSize(document.pdf, C.int(imageQuality), &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// Repair repaires PDF-document.
//
// Example:
//
// err := pdf.Repair()
func (document *Document) Repair() error {
var err *C.char
C.PDFDocument_Repair(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// Grayscale converts PDF-document to black and white.
//
// Example:
//
// err := pdf.Grayscale()
func (document *Document) Grayscale() error {
var err *C.char
C.PDFDocument_Grayscale(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// Flatten flattens PDF-document.
//
// Example:
//
// err := pdf.Flatten()
func (document *Document) Flatten() error {
var err *C.char
C.PDFDocument_Flatten(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// EmbedFonts embeds fonts a PDF-document.
//
// Example:
//
// err := pdf.EmbedFonts()
func (document *Document) EmbedFonts() error {
var err *C.char
C.PDFDocument_EmbedFonts(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// UnembedFonts unembeds fonts a PDF-document.
//
// Example:
//
// err := pdf.UnembedFonts()
func (document *Document) UnembedFonts() error {
var err *C.char
C.PDFDocument_UnembedFonts(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// RemoveAnnotations removes annotations from PDF-document.
//
// Example:
//
// err := pdf.RemoveAnnotations()
func (document *Document) RemoveAnnotations() error {
var err *C.char
C.PDFDocument_RemoveAnnotations(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// RemoveAttachments removes attachments from PDF-document.
//
// Example:
//
// err := pdf.RemoveAttachments()
func (document *Document) RemoveAttachments() error {
var err *C.char
C.PDFDocument_RemoveAttachments(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// RemoveBlankPages removes blank pages from PDF-document.
//
// Example:
//
// err := pdf.RemoveBlankPages()
func (document *Document) RemoveBlankPages() error {
var err *C.char
C.PDFDocument_RemoveBlankPages(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// RemoveBookmarks removes bookmarks from PDF-document.
//
// Example:
//
// err := pdf.RemoveBookmarks()
func (document *Document) RemoveBookmarks() error {
var err *C.char
C.PDFDocument_RemoveBookmarks(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// RemoveHiddenText removes hidden text from PDF-document.
//
// Example:
//
// err := pdf.RemoveHiddenText()
func (document *Document) RemoveHiddenText() error {
var err *C.char
C.PDFDocument_RemoveHiddenText(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// RemoveImages removes images from PDF-document.
//
// Example:
//
// err := pdf.RemoveImages()
func (document *Document) RemoveImages() error {
var err *C.char
C.PDFDocument_RemoveImages(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// RemoveJavaScripts removes java scripts from PDF-document.
//
// Example:
//
// err := pdf.RemoveJavaScripts()
func (document *Document) RemoveJavaScripts() error {
var err *C.char
C.PDFDocument_RemoveJavaScripts(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// RemoveTables removes tables from PDF-document.
//
// Example:
//
// err := pdf.RemoveTables()
func (document *Document) RemoveTables() error {
var err *C.char
C.PDFDocument_RemoveTables(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// RemoveWatermarks removes watermarks from PDF-document.
//
// Example:
//
// err := pdf.RemoveWatermarks()
func (document *Document) RemoveWatermarks() error {
var err *C.char
C.PDFDocument_RemoveWatermarks(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// RemoveTextHeaders removes text headers from PDF-document.
//
// Example:
//
// err := pdf.RemoveTextHeaders()
func (document *Document) RemoveTextHeaders() error {
var err *C.char
C.PDFDocument_RemoveTextHeaders(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// RemoveTextFooters removes text footers from PDF-document.
//
// Example:
//
// err := pdf.RemoveTextFooters()
func (document *Document) RemoveTextFooters() error {
var err *C.char
C.PDFDocument_RemoveTextFooters(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// SetBackground sets PDF-document background color.
//
// Example:
//
// err := pdf.SetBackground(200, 100, 101)
func (document *Document) SetBackground(r, g, b int32) error {
var err *C.char
C.PDFDocument_set_Background(document.pdf, C.int(r), C.int(g), C.int(b), &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// Rotate rotates PDF-document.
//
// Example:
//
// err := pdf.Rotate(asposepdf.RotationOn180)
func (document *Document) Rotate(rotation int32) error {
var err *C.char
C.PDFDocument_Rotate(document.pdf, C.int(rotation), &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// Crop crops pages of a PDF-document.
//
// Example:
//
// err := pdf.Crop(10)
func (document *Document) Crop(margin float64) error {
var err *C.char
C.PDFDocument_Crop(document.pdf, C.double(margin), &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// WordCount returns word count in PDF-document.
//
// Example:
//
// word_count, err := pdf.WordCount()
func (document *Document) WordCount() (int32, error) {
var err *C.char
cnt_int := C.PDFDocument_get_WordCount(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return -1, errors.New(err_str)
} else {
return int32(cnt_int), nil
}
}
// CharacterCount returns character count in PDF-document.
//
// Example:
//
// character_count, err := pdf.CharacterCount()
func (document *Document) CharacterCount() (int32, error) {
var err *C.char
cnt_int := C.PDFDocument_get_CharacterCount(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return -1, errors.New(err_str)
} else {
return int32(cnt_int), nil
}
}
// ReplaceText replaces text in PDF-document.
//
// Example:
//
// err := pdf.ReplaceText("old text", "new text")
func (document *Document) ReplaceText(findText, replaceText string) error {
var err *C.char
_findText := C.CString(findText)
defer C.free(unsafe.Pointer(_findText))
_replaceText := C.CString(replaceText)
defer C.free(unsafe.Pointer(_replaceText))
C.PDFDocument_ReplaceText(document.pdf, _findText, _replaceText, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// ReplaceFont replaces font in a PDF-document.
//
// Example:
//
// err := pdf.ReplaceFont("Times", "Helvetica-Bold")
func (document *Document) ReplaceFont(findFontName, replaceFontName string) error {
var err *C.char
_findFontName := C.CString(findFontName)
defer C.free(unsafe.Pointer(_findFontName))
_replaceFontName := C.CString(replaceFontName)
defer C.free(unsafe.Pointer(_replaceFontName))
C.PDFDocument_ReplaceFont(document.pdf, _findFontName, _replaceFontName, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// AddPageNum adds page number to a PDF-document.
//
// Example:
//
// err := pdf.AddPageNum()
func (document *Document) AddPageNum() error {
var err *C.char
C.PDFDocument_AddPageNum(document.pdf, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// AddTextHeader adds text in Header of a PDF-document.
//
// Example:
//
// err := pdf.AddTextHeader("Aspose")
func (document *Document) AddTextHeader(header string) error {
var err *C.char
_header := C.CString(header)
defer C.free(unsafe.Pointer(_header))
C.PDFDocument_AddTextHeader(document.pdf, _header, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// AddTextFooter adds text in Footer of a PDF-document.
//
// Example:
//
// err := pdf.AddTextFooter("Footer")
func (document *Document) AddTextFooter(footer string) error {
var err *C.char
_footer := C.CString(footer)
defer C.free(unsafe.Pointer(_footer))
C.PDFDocument_AddTextFooter(document.pdf, _footer, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// AddWatermark adds watermark to PDF-document.
//
// Example:
//
// err := pdf.AddWatermark("Watermark", "Arial", 16, "#010101", 100, 100, 45, true, 0.5)
func (document *Document) AddWatermark(text string, fontName string, fontSize float64, foregroundColor string, xPosition int32, yPosition int32, rotation int32, isBackground bool, opacity float64) error {
var err *C.char
_text := C.CString(text)
defer C.free(unsafe.Pointer(_text))
_fontName := C.CString(fontName)
defer C.free(unsafe.Pointer(_fontName))
_foregroundColor := C.CString(foregroundColor)
defer C.free(unsafe.Pointer(_foregroundColor))
_isBackground := 0
if isBackground {
_isBackground = 1
}
C.PDFDocument_AddWatermark(document.pdf, _text, _fontName, C.double(fontSize), _foregroundColor, C.int(xPosition), C.int(yPosition), C.int(rotation), C.int(_isBackground), C.double(opacity), &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// SaveDocX saves previously opened PDF-document as DocX-document with filename.
//
// Example:
//
// err := pdf.SaveDocX("filename.docx")
func (document *Document) SaveDocX(filename string) error {
var err *C.char
_filename := C.CString(filename)
defer C.free(unsafe.Pointer(_filename))
C.PDFDocument_Save_DocX(document.pdf, _filename, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// SaveDocXEnhanced saves previously opened PDF-document as Enhanced Recognition Mode DocX-document with filename.
//
// Example:
//
// err := pdf.SaveDocXEnhanced("filename.docx")
func (document *Document) SaveDocXEnhanced(filename string) error {
var err *C.char
_filename := C.CString(filename)
defer C.free(unsafe.Pointer(_filename))
C.PDFDocument_Save_DocXEnhanced(document.pdf, _filename, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// SaveDoc saves previously opened PDF-document as Doc-document with filename.
//
// Example:
//
// err := pdf.SaveDoc("filename.doc")
func (document *Document) SaveDoc(filename string) error {
var err *C.char
_filename := C.CString(filename)
defer C.free(unsafe.Pointer(_filename))
C.PDFDocument_Save_Doc(document.pdf, _filename, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// SaveXlsX saves previously opened PDF-document as XlsX-document with filename.
//
// Example:
//
// err := pdf.SaveXlsX("filename.xlsx")
func (document *Document) SaveXlsX(filename string) error {
var err *C.char
_filename := C.CString(filename)
defer C.free(unsafe.Pointer(_filename))
C.PDFDocument_Save_XlsX(document.pdf, _filename, &err)
err_str := C.GoString(err)
C.c_free_string(err)
if err_str != ERR_OK {
return errors.New(err_str)
} else {
return nil
}
}
// SavePptX saves previously opened PDF-document as PptX-document with filename.
//
// Example:
//
// err := pdf.SavePptX("filename.pptx")
func (document *Document) SavePptX(filename string) error {
var err *C.char
_filename := C.CString(filename)
defer C.free(unsafe.Pointer(_filename))
C.PDFDocument_Save_PptX(document.pdf, _filename, &err)
err_str := C.GoString(err)
C.c_free_string(err)