forked from unidoc/unioffice-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and update examples for UniOffice v1.7.0
- Loading branch information
Showing
7 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |