Skip to content

Commit

Permalink
Add and update examples for UniOffice v1.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnsth committed Nov 9, 2020
1 parent ac57fe6 commit 5c42755
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 5 deletions.
54 changes: 54 additions & 0 deletions document/doc-existing-header/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2020 FoxyUtils ehf. All rights reserved.

package main

import (
"fmt"
"github.com/unidoc/unioffice/common/license"
"github.com/unidoc/unioffice/document"
"github.com/unidoc/unioffice/schema/soo/wml"
)

const licenseKey = `
-----BEGIN UNIDOC LICENSE KEY-----
Free trial license keys are available at: https://unidoc.io/
-----END UNIDOC LICENSE KEY-----
`

func init() {
err := license.SetLicenseKey(licenseKey, `Company Name`)
if err != nil {
panic(err)
}
}

// setHeader sets the header by creating new or using existing header
func setOrCreateHeader(doc *document.Document, text string) {
// Check if header with the given type exists already
hdr, ok := doc.BodySection().GetHeader(wml.ST_HdrFtrDefault)
if !ok {
hdr = doc.AddHeader()
doc.BodySection().SetHeader(hdr, wml.ST_HdrFtrDefault)
}

// Add Text to header
para := hdr.AddParagraph()
run := para.AddRun()
run.AddBreak()
run.AddText(text)
}

func main() {
doc := document.New()

// This will create a new header with text "Header 1"
setOrCreateHeader(doc, "Header 1")

// This will add a new text to the existing header
setOrCreateHeader(doc, "Header 2")

if err := doc.SaveToFile("doc-existing-header.docx"); err != nil {
fmt.Println(err)
return
}
}
61 changes: 61 additions & 0 deletions document/even-odd-header/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2020 FoxyUtils ehf. All rights reserved.

// This example shows how to create a document and add headers
// by separating them into odd pages and even pages

package main

import (
"fmt"
"github.com/unidoc/unioffice/common/license"
"github.com/unidoc/unioffice/document"
"github.com/unidoc/unioffice/schema/soo/ofc/sharedTypes"
"github.com/unidoc/unioffice/schema/soo/wml"
)

const licenseKey = `
-----BEGIN UNIDOC LICENSE KEY-----
Free trial license keys are available at: https://unidoc.io/
-----END UNIDOC LICENSE KEY-----
`

func init() {
err := license.SetLicenseKey(licenseKey, `Company Name`)
if err != nil {
panic(err)
}
}

var lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin lobortis, lectus dictum feugiat tempus, sem neque finibus enim, sed eleifend sem nunc ac diam. Vestibulum tempus sagittis elementum`

func main() {
// Create a document
doc := document.New()
defer doc.Close()

// First add some content
for i := 0; i < 100; i++ {
doc.AddParagraph().AddRun().AddText(lorem)
}

// Construct even header
evenHdr := doc.AddHeader()
evenHdr.AddParagraph().AddRun().AddText("Even Header")
doc.BodySection().SetHeader(evenHdr, wml.ST_HdrFtrEven)

// Construct odd header
oddHdr := doc.AddHeader()
oddHdr.AddParagraph().AddRun().AddText("Odd Header")
doc.BodySection().SetHeader(oddHdr, wml.ST_HdrFtrDefault)

// Set the EvenAndOddHeaders flag
boolTrue := true
doc.Settings.X().EvenAndOddHeaders = &wml.CT_OnOff{
ValAttr: &sharedTypes.ST_OnOff{Bool: &boolTrue},
}

// Save the file
if err := doc.SaveToFile("even-odd-header.docx"); err != nil {
fmt.Println(err)
}
}
Binary file added document/merge-documents/document0.docx
Binary file not shown.
Binary file added document/merge-documents/document1.docx
Binary file not shown.
44 changes: 44 additions & 0 deletions document/merge-documents/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2017 FoxyUtils ehf. All rights reserved.
// This example demonstrates merging two documents into one.

package main

import (
"log"

"github.com/unidoc/unioffice/common/license"
"github.com/unidoc/unioffice/document"
)

const licenseKey = `
-----BEGIN UNIDOC LICENSE KEY-----
Free trial license keys are available at: https://unidoc.io/
-----END UNIDOC LICENSE KEY-----
`

func init() {
err := license.SetLicenseKey(licenseKey, `Company Name`)
if err != nil {
panic(err)
}
}

func main() {
doc0, err := document.Open("document0.docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
defer doc0.Close()
doc1, err := document.Open("document1.docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
defer doc1.Close()
doc0.AddParagraph().AddRun().AddPageBreak()

err = doc0.Append(doc1)
if err != nil {
log.Fatalf("error appending document: %s", err)
}
doc0.SaveToFile("merged.docx")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.12

require (
github.com/go-ole/go-ole v1.2.4
github.com/unidoc/unioffice v1.6.0
github.com/unidoc/unioffice v1.7.0
)
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/unidoc/unioffice v1.4.0 h1:yl+TbZJu2GTVYAYvu51wppj0R+fPC67xzVcy91qgrzI=
github.com/unidoc/unioffice v1.4.0/go.mod h1:7wl8btOkZW1TfqfpDWoujRXkUpowwisGRYDo7COHBiI=
github.com/unidoc/unioffice v1.5.1 h1:mfLUYRYUahIVkS2/6xp6YitrWS+9dWmxm/TmEJ3jnRk=
github.com/unidoc/unioffice v1.5.1/go.mod h1:7wl8btOkZW1TfqfpDWoujRXkUpowwisGRYDo7COHBiI=
github.com/unidoc/unioffice v1.7.0 h1:EdNrVxdYL3HncKzpASzYOK4vl37rjH2CAWf5tAONLS0=
github.com/unidoc/unioffice v1.7.0/go.mod h1:7wl8btOkZW1TfqfpDWoujRXkUpowwisGRYDo7COHBiI=

0 comments on commit 5c42755

Please sign in to comment.