forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.go
1175 lines (1057 loc) · 34.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
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 2017 FoxyUtils ehf. All rights reserved.
//
// Use of this software package and source code is governed by the terms of the
// UniDoc End User License Agreement (EULA) that is available at:
// https://unidoc.io/eula/
// A trial license code for evaluation can be obtained at https://unidoc.io.
package document
import (
"archive/zip"
"errors"
"flag"
"fmt"
"image"
"image/jpeg"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/unidoc/unioffice"
"github.com/unidoc/unioffice/color"
"github.com/unidoc/unioffice/common"
"github.com/unidoc/unioffice/common/license"
"github.com/unidoc/unioffice/measurement"
"github.com/unidoc/unioffice/zippkg"
"github.com/unidoc/unioffice/schema/soo/dml"
st "github.com/unidoc/unioffice/schema/soo/ofc/sharedTypes"
"github.com/unidoc/unioffice/schema/soo/pkg/relationships"
"github.com/unidoc/unioffice/schema/soo/wml"
)
// Document is a text document that can be written out in the OOXML .docx
// format. It can be opened from a file on disk and modified, or created from
// scratch.
type Document struct {
common.DocBase
x *wml.Document
Settings Settings // document settings
Numbering Numbering // numbering styles within the doucment
Styles Styles // styles that are use and can be used within the document
headers []*wml.Hdr
hdrRels []common.Relationships
footers []*wml.Ftr
ftrRels []common.Relationships
docRels common.Relationships
themes []*dml.Theme
webSettings *wml.WebSettings
fontTable *wml.Fonts
endNotes *wml.Endnotes
footNotes *wml.Footnotes
}
// New constructs an empty document that content can be added to.
func New() *Document {
d := &Document{x: wml.NewDocument()}
d.ContentTypes = common.NewContentTypes()
d.x.Body = wml.NewCT_Body()
d.x.ConformanceAttr = st.ST_ConformanceClassTransitional
d.docRels = common.NewRelationships()
d.AppProperties = common.NewAppProperties()
d.CoreProperties = common.NewCoreProperties()
d.ContentTypes.AddOverride("/word/document.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml")
d.Settings = NewSettings()
d.docRels.AddRelationship("settings.xml", unioffice.SettingsType)
d.ContentTypes.AddOverride("/word/settings.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml")
d.Rels = common.NewRelationships()
d.Rels.AddRelationship(unioffice.RelativeFilename(unioffice.DocTypeDocument, "", unioffice.CorePropertiesType, 0), unioffice.CorePropertiesType)
d.Rels.AddRelationship("docProps/app.xml", unioffice.ExtendedPropertiesType)
d.Rels.AddRelationship("word/document.xml", unioffice.OfficeDocumentType)
d.Numbering = NewNumbering()
d.Numbering.InitializeDefault()
d.ContentTypes.AddOverride("/word/numbering.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml")
d.docRels.AddRelationship("numbering.xml", unioffice.NumberingType)
d.Styles = NewStyles()
d.Styles.InitializeDefault()
d.ContentTypes.AddOverride("/word/styles.xml", "application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml")
d.docRels.AddRelationship("styles.xml", unioffice.StylesType)
d.x.Body = wml.NewCT_Body()
return d
}
// GetOrCreateCustomProperties returns the custom properties of the document (and if they not exist yet, creating them first)
func (d *Document) GetOrCreateCustomProperties() common.CustomProperties {
if d.CustomProperties.X() == nil {
d.createCustomProperties()
}
return d.CustomProperties
}
func (d *Document) createCustomProperties() {
d.CustomProperties = common.NewCustomProperties()
d.addCustomRelationships()
}
func (d *Document) addCustomRelationships() {
d.ContentTypes.AddOverride("/docProps/custom.xml", "application/vnd.openxmlformats-officedocument.custom-properties+xml")
d.Rels.AddRelationship("docProps/custom.xml", unioffice.CustomPropertiesType)
}
// X returns the inner wrapped XML type.
func (d *Document) X() *wml.Document {
return d.x
}
// AddHeader creates a header associated with the document, but doesn't add it
// to the document for display.
func (d *Document) AddHeader() Header {
hdr := wml.NewHdr()
d.headers = append(d.headers, hdr)
path := fmt.Sprintf("header%d.xml", len(d.headers))
d.docRels.AddRelationship(path, unioffice.HeaderType)
d.ContentTypes.AddOverride("/word/"+path, "application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml")
d.hdrRels = append(d.hdrRels, common.NewRelationships())
return Header{d, hdr}
}
// Headers returns the headers defined in the document.
func (d *Document) Headers() []Header {
ret := []Header{}
for _, h := range d.headers {
ret = append(ret, Header{d, h})
}
return ret
}
// Footers returns the footers defined in the document.
func (d *Document) Footers() []Footer {
ret := []Footer{}
for _, f := range d.footers {
ret = append(ret, Footer{d, f})
}
return ret
}
// AddFooter creates a Footer associated with the document, but doesn't add it
// to the document for display.
func (d *Document) AddFooter() Footer {
ftr := wml.NewFtr()
d.footers = append(d.footers, ftr)
path := fmt.Sprintf("footer%d.xml", len(d.footers))
d.docRels.AddRelationship(path, unioffice.FooterType)
d.ContentTypes.AddOverride("/word/"+path, "application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml")
d.ftrRels = append(d.ftrRels, common.NewRelationships())
return Footer{d, ftr}
}
// BodySection returns the default body section used for all preceding
// paragraphs until the previous Section. If there is no previous sections, the
// body section applies to the entire document.
func (d *Document) BodySection() Section {
if d.x.Body.SectPr == nil {
d.x.Body.SectPr = wml.NewCT_SectPr()
}
return Section{d, d.x.Body.SectPr}
}
// Save writes the document to an io.Writer in the Zip package format.
func (d *Document) Save(w io.Writer) error {
if err := d.x.Validate(); err != nil {
unioffice.Log("validation error in document: %s", err)
}
dt := unioffice.DocTypeDocument
if !license.GetLicenseKey().IsLicensed() && flag.Lookup("test.v") == nil {
fmt.Println("Unlicensed version of UniOffice")
fmt.Println("- Get a license on https://unidoc.io")
hdr := d.AddHeader()
para := hdr.AddParagraph()
para.Properties().AddTabStop(2.5*measurement.Inch, wml.ST_TabJcCenter, wml.ST_TabTlcNone)
run := para.AddRun()
run.AddTab()
run.AddText("Unlicensed version of UniOffice - Get a license on https://unidoc.io")
run.Properties().SetBold(true)
run.Properties().SetSize(14)
run.Properties().SetColor(color.Red)
d.BodySection().SetHeader(hdr, wml.ST_HdrFtrDefault)
}
z := zip.NewWriter(w)
defer z.Close()
if err := zippkg.MarshalXML(z, unioffice.BaseRelsFilename, d.Rels.X()); err != nil {
return err
}
if err := zippkg.MarshalXMLByType(z, dt, unioffice.ExtendedPropertiesType, d.AppProperties.X()); err != nil {
return err
}
if err := zippkg.MarshalXMLByType(z, dt, unioffice.CorePropertiesType, d.CoreProperties.X()); err != nil {
return err
}
if d.CustomProperties.X() != nil {
if err := zippkg.MarshalXMLByType(z, dt, unioffice.CustomPropertiesType, d.CustomProperties.X()); err != nil {
return err
}
}
if d.Thumbnail != nil {
tn, err := z.Create("docProps/thumbnail.jpeg")
if err != nil {
return err
}
if err := jpeg.Encode(tn, d.Thumbnail, nil); err != nil {
return err
}
}
if err := zippkg.MarshalXMLByType(z, dt, unioffice.SettingsType, d.Settings.X()); err != nil {
return err
}
documentFn := unioffice.AbsoluteFilename(dt, unioffice.OfficeDocumentType, 0)
if err := zippkg.MarshalXML(z, documentFn, d.x); err != nil {
return err
}
if err := zippkg.MarshalXML(z, zippkg.RelationsPathFor(documentFn), d.docRels.X()); err != nil {
return err
}
if d.Numbering.X() != nil {
if err := zippkg.MarshalXMLByType(z, dt, unioffice.NumberingType, d.Numbering.X()); err != nil {
return err
}
}
if err := zippkg.MarshalXMLByType(z, dt, unioffice.StylesType, d.Styles.X()); err != nil {
return err
}
if d.webSettings != nil {
if err := zippkg.MarshalXMLByType(z, dt, unioffice.WebSettingsType, d.webSettings); err != nil {
return err
}
}
if d.fontTable != nil {
if err := zippkg.MarshalXMLByType(z, dt, unioffice.FontTableType, d.fontTable); err != nil {
return err
}
}
if d.endNotes != nil {
if err := zippkg.MarshalXMLByType(z, dt, unioffice.EndNotesType, d.endNotes); err != nil {
return err
}
}
if d.footNotes != nil {
if err := zippkg.MarshalXMLByType(z, dt, unioffice.FootNotesType, d.footNotes); err != nil {
return err
}
}
for i, thm := range d.themes {
if err := zippkg.MarshalXMLByTypeIndex(z, dt, unioffice.ThemeType, i+1, thm); err != nil {
return err
}
}
for i, hdr := range d.headers {
fn := unioffice.AbsoluteFilename(dt, unioffice.HeaderType, i+1)
if err := zippkg.MarshalXML(z, fn, hdr); err != nil {
return err
}
if !d.hdrRels[i].IsEmpty() {
zippkg.MarshalXML(z, zippkg.RelationsPathFor(fn), d.hdrRels[i].X())
}
}
for i, ftr := range d.footers {
fn := unioffice.AbsoluteFilename(dt, unioffice.FooterType, i+1)
if err := zippkg.MarshalXMLByTypeIndex(z, dt, unioffice.FooterType, i+1, ftr); err != nil {
return err
}
if !d.ftrRels[i].IsEmpty() {
zippkg.MarshalXML(z, zippkg.RelationsPathFor(fn), d.ftrRels[i].X())
}
}
for i, img := range d.Images {
if err := common.AddImageToZip(z, img, i+1, unioffice.DocTypeDocument); err != nil {
return err
}
}
if err := zippkg.MarshalXML(z, unioffice.ContentTypesFilename, d.ContentTypes.X()); err != nil {
return err
}
if err := d.WriteExtraFiles(z); err != nil {
return err
}
return z.Close()
}
// AddTable adds a new table to the document body.
func (d *Document) AddTable() Table {
elts := wml.NewEG_BlockLevelElts()
d.x.Body.EG_BlockLevelElts = append(d.x.Body.EG_BlockLevelElts, elts)
c := wml.NewEG_ContentBlockContent()
elts.EG_ContentBlockContent = append(elts.EG_ContentBlockContent, c)
tbl := wml.NewCT_Tbl()
c.Tbl = append(c.Tbl, tbl)
return Table{d, tbl}
}
func (d *Document) InsertTableAfter(relativeTo Paragraph) Table {
return d.insertTable(relativeTo, false)
}
func (d *Document) InsertTableBefore(relativeTo Paragraph) Table {
return d.insertTable(relativeTo, true)
}
func (d *Document) insertTable(relativeTo Paragraph, before bool) Table {
body := d.x.Body
if body == nil {
return d.AddTable()
}
relX := relativeTo.X()
for i, ble := range body.EG_BlockLevelElts {
for _, c := range ble.EG_ContentBlockContent {
for j, p := range c.P {
// found the paragraph
if p == relX {
tbl := wml.NewCT_Tbl()
elts := wml.NewEG_BlockLevelElts()
cbc := wml.NewEG_ContentBlockContent()
elts.EG_ContentBlockContent = append(elts.EG_ContentBlockContent, cbc)
cbc.Tbl = append(cbc.Tbl, tbl)
body.EG_BlockLevelElts = append(body.EG_BlockLevelElts, nil)
if before {
copy(body.EG_BlockLevelElts[i+1:], body.EG_BlockLevelElts[i:])
body.EG_BlockLevelElts[i] = elts
if j != 0 {
elts := wml.NewEG_BlockLevelElts()
cbc := wml.NewEG_ContentBlockContent()
elts.EG_ContentBlockContent = append(elts.EG_ContentBlockContent, cbc)
cbc.P = c.P[:j]
body.EG_BlockLevelElts = append(body.EG_BlockLevelElts, nil)
copy(body.EG_BlockLevelElts[i+1:], body.EG_BlockLevelElts[i:])
body.EG_BlockLevelElts[i] = elts
}
c.P = c.P[j:]
} else {
copy(body.EG_BlockLevelElts[i+2:], body.EG_BlockLevelElts[i+1:])
body.EG_BlockLevelElts[i+1] = elts
if j != len(c.P)-1 {
elts := wml.NewEG_BlockLevelElts()
cbc := wml.NewEG_ContentBlockContent()
elts.EG_ContentBlockContent = append(elts.EG_ContentBlockContent, cbc)
cbc.P = c.P[j+1:]
body.EG_BlockLevelElts = append(body.EG_BlockLevelElts, nil)
copy(body.EG_BlockLevelElts[i+3:], body.EG_BlockLevelElts[i+2:])
body.EG_BlockLevelElts[i+2] = elts
}
c.P = c.P[:j+1]
}
return Table{d, tbl}
}
}
for _, tbl := range c.Tbl {
for _, crc := range tbl.EG_ContentRowContent {
for _, tr := range crc.Tr {
for _, ccc := range tr.EG_ContentCellContent {
for _, tc := range ccc.Tc {
for i, ble := range tc.EG_BlockLevelElts {
for _, cbcOuter := range ble.EG_ContentBlockContent {
for j, p := range cbcOuter.P {
if p == relX {
elts := wml.NewEG_BlockLevelElts()
cbcInner := wml.NewEG_ContentBlockContent()
elts.EG_ContentBlockContent = append(elts.EG_ContentBlockContent, cbcInner)
tbl := wml.NewCT_Tbl()
cbcInner.Tbl = append(cbcInner.Tbl, tbl)
tc.EG_BlockLevelElts = append(tc.EG_BlockLevelElts, nil)
if before {
copy(tc.EG_BlockLevelElts[i+1:], tc.EG_BlockLevelElts[i:])
tc.EG_BlockLevelElts[i] = elts
if j != 0 {
elts := wml.NewEG_BlockLevelElts()
cbc := wml.NewEG_ContentBlockContent()
elts.EG_ContentBlockContent = append(elts.EG_ContentBlockContent, cbc)
cbc.P = cbcOuter.P[:j]
tc.EG_BlockLevelElts = append(tc.EG_BlockLevelElts, nil)
copy(tc.EG_BlockLevelElts[i+1:], tc.EG_BlockLevelElts[i:])
tc.EG_BlockLevelElts[i] = elts
}
cbcOuter.P = cbcOuter.P[j:]
} else {
copy(tc.EG_BlockLevelElts[i+2:], tc.EG_BlockLevelElts[i+1:])
tc.EG_BlockLevelElts[i+1] = elts
if j != len(c.P)-1 {
elts := wml.NewEG_BlockLevelElts()
cbc := wml.NewEG_ContentBlockContent()
elts.EG_ContentBlockContent = append(elts.EG_ContentBlockContent, cbc)
cbc.P = cbcOuter.P[j+1:]
tc.EG_BlockLevelElts = append(tc.EG_BlockLevelElts, nil)
copy(tc.EG_BlockLevelElts[i+3:], tc.EG_BlockLevelElts[i+2:])
tc.EG_BlockLevelElts[i+2] = elts
}
cbcOuter.P = cbcOuter.P[:j+1]
}
return Table{d, tbl}
}
}
}
}
}
}
}
}
}
}
}
return d.AddTable()
}
func (d *Document) tables(bc *wml.EG_ContentBlockContent) []Table {
ret := []Table{}
for _, t := range bc.Tbl {
ret = append(ret, Table{d, t})
for _, crc := range t.EG_ContentRowContent {
for _, tr := range crc.Tr {
for _, ccc := range tr.EG_ContentCellContent {
for _, tc := range ccc.Tc {
for _, ble := range tc.EG_BlockLevelElts {
for _, cbc := range ble.EG_ContentBlockContent {
for _, tbl := range d.tables(cbc) {
ret = append(ret, tbl)
}
}
}
}
}
}
}
}
return ret
}
// Tables returns the tables defined in the document.
func (d *Document) Tables() []Table {
ret := []Table{}
if d.x.Body == nil {
return nil
}
for _, ble := range d.x.Body.EG_BlockLevelElts {
for _, c := range ble.EG_ContentBlockContent {
for _, t := range d.tables(c) {
ret = append(ret, t)
}
}
}
return ret
}
// AddParagraph adds a new paragraph to the document body.
func (d *Document) AddParagraph() Paragraph {
elts := wml.NewEG_BlockLevelElts()
d.x.Body.EG_BlockLevelElts = append(d.x.Body.EG_BlockLevelElts, elts)
c := wml.NewEG_ContentBlockContent()
elts.EG_ContentBlockContent = append(elts.EG_ContentBlockContent, c)
p := wml.NewCT_P()
c.P = append(c.P, p)
return Paragraph{d, p}
}
// RemoveParagraph removes a paragraph from a document.
func (d *Document) RemoveParagraph(p Paragraph) {
if d.x.Body == nil {
return
}
for _, ble := range d.x.Body.EG_BlockLevelElts {
for _, c := range ble.EG_ContentBlockContent {
for i, pa := range c.P {
// do we need to remove this paragraph
if pa == p.x {
copy(c.P[i:], c.P[i+1:])
c.P = c.P[0 : len(c.P)-1]
return
}
}
if c.Sdt != nil && c.Sdt.SdtContent != nil && c.Sdt.SdtContent.P != nil {
for i, pa := range c.Sdt.SdtContent.P {
if pa == p.x {
copy(c.P[i:], c.P[i+1:])
c.P = c.P[0 : len(c.P)-1]
return
}
}
}
}
}
}
// StructuredDocumentTags returns the structured document tags in the document
// which are commonly used in document templates.
func (d *Document) StructuredDocumentTags() []StructuredDocumentTag {
ret := []StructuredDocumentTag{}
for _, ble := range d.x.Body.EG_BlockLevelElts {
for _, c := range ble.EG_ContentBlockContent {
if c.Sdt != nil {
ret = append(ret, StructuredDocumentTag{d, c.Sdt})
}
}
}
return ret
}
// Paragraphs returns all of the paragraphs in the document body including tables.
func (d *Document) Paragraphs() []Paragraph {
ret := []Paragraph{}
if d.x.Body == nil {
return nil
}
for _, ble := range d.x.Body.EG_BlockLevelElts {
for _, c := range ble.EG_ContentBlockContent {
for _, p := range c.P {
ret = append(ret, Paragraph{d, p})
}
}
}
for _, t := range d.Tables() {
for _, r := range t.Rows() {
for _, c := range r.Cells() {
ret = append(ret, c.Paragraphs()...)
}
}
}
return ret
}
// HasFootnotes returns a bool based on the presence or abscence of footnotes within
// the document.
func (d *Document) HasFootnotes() bool {
return d.footNotes != nil
}
// Footnotes returns the footnotes defined in the document.
func (d *Document) Footnotes() []Footnote {
ret := []Footnote{}
for _, f := range d.footNotes.CT_Footnotes.Footnote {
ret = append(ret, Footnote{d, f})
}
return ret
}
// Footnote returns the footnote based on the ID; this can be used nicely with
// the run.IsFootnote() functionality.
func (d *Document) Footnote(id int64) Footnote {
for _, f := range d.Footnotes() {
if f.id() == id {
return f
}
}
return Footnote{}
}
// HasEndnotes returns a bool based on the presence or abscence of endnotes within
// the document.
func (d *Document) HasEndnotes() bool {
return d.endNotes != nil
}
// Endnotes returns the endnotes defined in the document.
func (d *Document) Endnotes() []Endnote {
ret := []Endnote{}
for _, f := range d.endNotes.CT_Endnotes.Endnote {
ret = append(ret, Endnote{d, f})
}
return ret
}
// Endnote returns the endnote based on the ID; this can be used nicely with
// the run.IsEndnote() functionality.
func (d *Document) Endnote(id int64) Endnote {
for _, f := range d.Endnotes() {
if f.id() == id {
return f
}
}
return Endnote{}
}
// SaveToFile writes the document out to a file.
func (d *Document) SaveToFile(path string) error {
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
return d.Save(f)
}
// Open opens and reads a document from a file (.docx).
func Open(filename string) (*Document, error) {
f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error opening %s: %s", filename, err)
}
defer f.Close()
fi, err := os.Stat(filename)
if err != nil {
return nil, fmt.Errorf("error opening %s: %s", filename, err)
}
_ = fi
return Read(f, fi.Size())
}
// OpenTemplate opens a document, removing all content so it can be used as a
// template. Since Word removes unused styles from a document upon save, to
// create a template in Word add a paragraph with every style of interest. When
// opened with OpenTemplate the document's styles will be available but the
// content will be gone.
func OpenTemplate(filename string) (*Document, error) {
d, err := Open(filename)
if err != nil {
return nil, err
}
d.x.Body = wml.NewCT_Body()
return d, nil
}
// Read reads a document from an io.Reader.
func Read(r io.ReaderAt, size int64) (*Document, error) {
doc := New()
// numbering is not required
doc.Numbering.x = nil
td, err := ioutil.TempDir("", "gooxml-docx")
if err != nil {
return nil, err
}
doc.TmpPath = td
zr, err := zip.NewReader(r, size)
if err != nil {
return nil, fmt.Errorf("parsing zip: %s", err)
}
files := []*zip.File{}
files = append(files, zr.File...)
addCustom := false
for _, f := range files {
if f.FileHeader.Name == "docProps/custom.xml" {
addCustom = true
break
}
}
if addCustom {
doc.createCustomProperties()
}
ca := doc.x.ConformanceAttr
decMap := zippkg.DecodeMap{}
decMap.SetOnNewRelationshipFunc(doc.onNewRelationship)
// we should discover all contents by starting with these two files
decMap.AddTarget(unioffice.ContentTypesFilename, doc.ContentTypes.X(), "", 0)
decMap.AddTarget(unioffice.BaseRelsFilename, doc.Rels.X(), "", 0)
if err := decMap.Decode(files); err != nil {
return nil, err
}
doc.x.ConformanceAttr = ca
for _, f := range files {
if f == nil {
continue
}
if err := doc.AddExtraFileFromZip(f); err != nil {
return nil, err
}
}
if addCustom {
customPropertiesExist := false
for _, rel := range doc.Rels.X().Relationship {
if rel.TargetAttr == "docProps/custom.xml" {
customPropertiesExist = true
break
}
}
if !customPropertiesExist {
doc.addCustomRelationships()
}
}
return doc, nil
}
// Validate validates the structure and in cases where it't possible, the ranges
// of elements within a document. A validation error dones't mean that the
// document won't work in MS Word or LibreOffice, but it's worth checking into.
func (d *Document) Validate() error {
if d == nil || d.x == nil {
return errors.New("document not initialized correctly, nil base")
}
for _, v := range []func() error{d.validateTableCells, d.validateBookmarks} {
if err := v(); err != nil {
return err
}
}
if err := d.x.Validate(); err != nil {
return err
}
return nil
}
func (d *Document) validateBookmarks() error {
bmnames := make(map[string]struct{})
for _, bm := range d.Bookmarks() {
if _, ok := bmnames[bm.Name()]; ok {
return fmt.Errorf("duplicate bookmark %s found", bm.Name())
}
bmnames[bm.Name()] = struct{}{}
}
return nil
}
func (d *Document) validateTableCells() error {
for _, elt := range d.x.Body.EG_BlockLevelElts {
for _, c := range elt.EG_ContentBlockContent {
for _, t := range c.Tbl {
for _, rc := range t.EG_ContentRowContent {
for _, row := range rc.Tr {
hasCell := false
for _, ecc := range row.EG_ContentCellContent {
cellHasPara := false
for _, cell := range ecc.Tc {
hasCell = true
for _, cellElt := range cell.EG_BlockLevelElts {
for _, cellCont := range cellElt.EG_ContentBlockContent {
if len(cellCont.P) > 0 {
cellHasPara = true
break
}
}
}
}
if !cellHasPara {
return errors.New("table cell must contain a paragraph")
}
}
// OSX Word requires this and won't open the file otherwise
if !hasCell {
return errors.New("table row must contain a cell")
}
}
}
}
}
}
return nil
}
// AddImage adds an image to the document package, returning a reference that
// can be used to add the image to a run and place it in the document contents.
func (d *Document) AddImage(i common.Image) (common.ImageRef, error) {
r := common.MakeImageRef(i, &d.DocBase, d.docRels)
if i.Data == nil && i.Path == "" {
return r, errors.New("image must have data or a path")
}
if i.Format == "" {
return r, errors.New("image must have a valid format")
}
if i.Size.X == 0 || i.Size.Y == 0 {
return r, errors.New("image must have a valid size")
}
d.Images = append(d.Images, r)
fn := fmt.Sprintf("media/image%d.%s", len(d.Images), i.Format)
rel := d.docRels.AddRelationship(fn, unioffice.ImageType)
d.ContentTypes.EnsureDefault("png", "image/png")
d.ContentTypes.EnsureDefault("jpeg", "image/jpeg")
d.ContentTypes.EnsureDefault("jpg", "image/jpeg")
d.ContentTypes.EnsureDefault("wmf", "image/x-wmf")
d.ContentTypes.EnsureDefault(i.Format, "image/"+i.Format)
r.SetRelID(rel.X().IdAttr)
return r, nil
}
// GetImageByRelID returns an ImageRef with the associated relation ID in the
// document.
func (d *Document) GetImageByRelID(relID string) (common.ImageRef, bool) {
for _, img := range d.Images {
if img.RelID() == relID {
return img, true
}
}
return common.ImageRef{}, false
}
// FormFields extracts all of the fields from a document. They can then be
// manipulated via the methods on the field and the document saved.
func (d *Document) FormFields() []FormField {
ret := []FormField{}
for _, p := range d.Paragraphs() {
runs := p.Runs()
for i, r := range runs {
for _, ic := range r.x.EG_RunInnerContent {
// skip non form fields
if ic.FldChar == nil || ic.FldChar.FfData == nil {
continue
}
// found a begin form field
if ic.FldChar.FldCharTypeAttr == wml.ST_FldCharTypeBegin {
// ensure it has a name
if len(ic.FldChar.FfData.Name) == 0 || ic.FldChar.FfData.Name[0].ValAttr == nil {
continue
}
field := FormField{x: ic.FldChar.FfData}
// for text input boxes, we need a pointer to where to set
// the text as well
if ic.FldChar.FfData.TextInput != nil {
// ensure we always have at lest two IC's
for j := i + 1; j < len(runs)-1; j++ {
if len(runs[j].x.EG_RunInnerContent) == 0 {
continue
}
ic := runs[j].x.EG_RunInnerContent[0]
// look for the 'separate' field
if ic.FldChar != nil && ic.FldChar.FldCharTypeAttr == wml.ST_FldCharTypeSeparate {
if len(runs[j+1].x.EG_RunInnerContent) == 0 {
continue
}
// the value should be the text in the next inner content that is not a field char
if runs[j+1].x.EG_RunInnerContent[0].FldChar == nil {
field.textIC = runs[j+1].x.EG_RunInnerContent[0]
break
}
}
}
}
ret = append(ret, field)
}
}
}
}
return ret
}
func (d *Document) onNewRelationship(decMap *zippkg.DecodeMap, target, typ string, files []*zip.File, rel *relationships.Relationship, src zippkg.Target) error {
dt := unioffice.DocTypeDocument
switch typ {
case unioffice.OfficeDocumentType, unioffice.OfficeDocumentTypeStrict:
d.x = wml.NewDocument()
decMap.AddTarget(target, d.x, typ, 0)
// look for the document relationships file as well
decMap.AddTarget(zippkg.RelationsPathFor(target), d.docRels.X(), typ, 0)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, 0)
case unioffice.CorePropertiesType:
decMap.AddTarget(target, d.CoreProperties.X(), typ, 0)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, 0)
case unioffice.CustomPropertiesType:
decMap.AddTarget(target, d.CustomProperties.X(), typ, 0)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, 0)
case unioffice.ExtendedPropertiesType, unioffice.ExtendedPropertiesTypeStrict:
decMap.AddTarget(target, d.AppProperties.X(), typ, 0)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, 0)
case unioffice.ThumbnailType, unioffice.ThumbnailTypeStrict:
// read our thumbnail
for i, f := range files {
if f == nil {
continue
}
if f.Name == target {
rc, err := f.Open()
if err != nil {
return fmt.Errorf("error reading thumbnail: %s", err)
}
d.Thumbnail, _, err = image.Decode(rc)
rc.Close()
if err != nil {
return fmt.Errorf("error decoding thumbnail: %s", err)
}
files[i] = nil
}
}
case unioffice.SettingsType, unioffice.SettingsTypeStrict:
decMap.AddTarget(target, d.Settings.X(), typ, 0)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, 0)
case unioffice.NumberingType, unioffice.NumberingTypeStrict:
d.Numbering = NewNumbering()
decMap.AddTarget(target, d.Numbering.X(), typ, 0)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, 0)
case unioffice.StylesType, unioffice.StylesTypeStrict:
d.Styles.Clear()
decMap.AddTarget(target, d.Styles.X(), typ, 0)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, 0)
case unioffice.HeaderType, unioffice.HeaderTypeStrict:
hdr := wml.NewHdr()
decMap.AddTarget(target, hdr, typ, uint32(len(d.headers)))
d.headers = append(d.headers, hdr)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, len(d.headers))
// look for header rels
hdrRel := common.NewRelationships()
decMap.AddTarget(zippkg.RelationsPathFor(target), hdrRel.X(), typ, 0)
d.hdrRels = append(d.hdrRels, hdrRel)
case unioffice.FooterType, unioffice.FooterTypeStrict:
ftr := wml.NewFtr()
decMap.AddTarget(target, ftr, typ, uint32(len(d.footers)))
d.footers = append(d.footers, ftr)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, len(d.footers))
// look for footer rels
ftrRel := common.NewRelationships()
decMap.AddTarget(zippkg.RelationsPathFor(target), ftrRel.X(), typ, 0)
d.ftrRels = append(d.ftrRels, ftrRel)
case unioffice.ThemeType, unioffice.ThemeTypeStrict:
thm := dml.NewTheme()
decMap.AddTarget(target, thm, typ, uint32(len(d.themes)))
d.themes = append(d.themes, thm)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, len(d.themes))
case unioffice.WebSettingsType, unioffice.WebSettingsTypeStrict:
d.webSettings = wml.NewWebSettings()
decMap.AddTarget(target, d.webSettings, typ, 0)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, 0)
case unioffice.FontTableType, unioffice.FontTableTypeStrict:
d.fontTable = wml.NewFonts()
decMap.AddTarget(target, d.fontTable, typ, 0)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, 0)
case unioffice.EndNotesType, unioffice.EndNotesTypeStrict:
d.endNotes = wml.NewEndnotes()
decMap.AddTarget(target, d.endNotes, typ, 0)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, 0)
case unioffice.FootNotesType, unioffice.FootNotesTypeStrict:
d.footNotes = wml.NewFootnotes()
decMap.AddTarget(target, d.footNotes, typ, 0)
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, 0)
case unioffice.ImageType, unioffice.ImageTypeStrict:
var iref common.ImageRef
for i, f := range files {
if f == nil {
continue
}
if f.Name == target {
path, err := zippkg.ExtractToDiskTmp(f, d.TmpPath)
if err != nil {
return err
}
img, err := common.ImageFromFile(path)
if err != nil {
return err
}
iref = common.MakeImageRef(img, &d.DocBase, d.docRels)
d.Images = append(d.Images, iref)
files[i] = nil
}
}
ext := "." + strings.ToLower(iref.Format())
rel.TargetAttr = unioffice.RelativeFilename(dt, src.Typ, typ, len(d.Images))
// ensure we don't change image formats
if newExt := filepath.Ext(rel.TargetAttr); newExt != ext {
rel.TargetAttr = rel.TargetAttr[0:len(rel.TargetAttr)-len(newExt)] + ext
}
default:
unioffice.Log("unsupported relationship type: %s tgt: %s", typ, target)
}
return nil
}
// InsertParagraphAfter adds a new empty paragraph after the relativeTo
// paragraph.