Skip to content

Commit

Permalink
Updated examples for unioffice v1.16.0 (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnsth authored Nov 10, 2021
1 parent d54af34 commit af036b6
Show file tree
Hide file tree
Showing 50 changed files with 420 additions and 1 deletion.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,36 @@ The folder hierarchy is as follows:
- `document/` folder contains examples for creating and processing Word DOCX document files.
- `spreadsheet/` folder contains examples for creating and processing Excel XLSX spreadsheet files.
- `presentation/` folder contains examples for creating and processing Powerpoint PPTX presentation files.
- `license/` folder contains examples for using metered api key license and offline license files.

## License codes
UniOffice requires license codes to operate, there are two options:
- Metered License API keys: Free ones can be obtained at https://cloud.unidoc.io
- Offline Perpetual codes: Can be purchased at https://unidoc.io/pricing

Most of the examples demonstrate loading the Metered License API keys through an environment
variable `UNIDOC_LICENSE_API_KEY`.

Examples for Offline Perpetual License Key loading can be found in the license subdirectory.

# UniOffice License Loading.

The examples here illustrate how to work with UniOffice license codes and keys.
There are two types of licenses.

## Offline License
Offline licenses are cryptography based and contain full signed information that is verified based on signatures without making any outbound connections,
hence the name "offline". This kind of license is suitable for users deploying OEM products to their customers or where there are strict restrictions
on outbound connections due to firewalls and/or compliance requirements.

## Metered License (API keys)
The metered license is the most convenient way to get started with UniDoc products and the Free tier enables a powerful way to get started for free.
Anyone can get a free metered API key by signing up on http://cloud.unidoc.io/

## Examples

- [license/metered/main.go](unioffice_license_loading_metered.go) Demonstrates how to load the Metered API license key and how to print out relevant information.
- [license/offline/main.go](unioffice_license_loading_offline.go) Demonstrates how to print out information about the license after loading an offline license key.

### Build all examples

Expand All @@ -18,4 +48,3 @@ Simply run the build script which builds all the binaries to subfolder `bin/`
```bash
$ ./build_examples.sh
```

Binary file added document/convert_to_pdf/chart.docx
Binary file not shown.
Binary file added document/convert_to_pdf/fld_simple.docx
Binary file not shown.
Binary file added document/convert_to_pdf/headers_footers.docx
Binary file not shown.
Binary file added document/convert_to_pdf/image_square.docx
Binary file not shown.
67 changes: 67 additions & 0 deletions document/convert_to_pdf/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* This example showcases PDF generation from docx document with UniOffice package.
*/

package main

import (
"fmt"
"log"
"os"

unipdflicense "github.com/unidoc/unipdf/v3/common/license"

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

func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := unipdflicense.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
fmt.Printf("ERROR: Failed to set metered key: %v\n", err)
fmt.Printf("Make sure to get a valid key from https://cloud.unidoc.io\n")
fmt.Printf("If you don't have one - Grab one in the Free Tier at https://cloud.unidoc.io\n")
panic(err)
}

// This example requires both for unioffice and unipdf.
err = license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
fmt.Printf("ERROR: Failed to set metered key: %v\n", err)
fmt.Printf("Make sure to get a valid key from https://cloud.unidoc.io\n")
fmt.Printf("If you don't have one - Grab one in the Free Tier at https://cloud.unidoc.io\n")
panic(err)
}
}

var filenames = []string{
"chart",
"fld_simple",
"headers_footers",
"image_square",
"table",
"text_only_portrait",
"text_only_landscape",
"textbox_anchor",
"textbox_inline",
}

func main() {
for _, filename := range filenames {
outputPath := fmt.Sprintf("output/%s.pdf", filename)
doc, err := document.Open(filename + ".docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
defer doc.Close()
c := convert.ConvertToPdf(doc)

err = c.WriteToFile(outputPath)
if err != nil {
log.Fatalf("error converting document: %s", err)
}
}
}
Binary file added document/convert_to_pdf/output/chart.pdf
Binary file not shown.
Binary file added document/convert_to_pdf/output/fld_simple.pdf
Binary file not shown.
Binary file not shown.
Binary file added document/convert_to_pdf/output/image_square.pdf
Binary file not shown.
Binary file added document/convert_to_pdf/output/table.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added document/convert_to_pdf/output/textbox_anchor.pdf
Binary file not shown.
Binary file added document/convert_to_pdf/output/textbox_inline.pdf
Binary file not shown.
Binary file added document/convert_to_pdf/table.docx
Binary file not shown.
Binary file added document/convert_to_pdf/text_only_landscape.docx
Binary file not shown.
Binary file added document/convert_to_pdf/text_only_portrait.docx
Binary file not shown.
Binary file added document/convert_to_pdf/textbox_anchor.docx
Binary file not shown.
Binary file added document/convert_to_pdf/textbox_inline.docx
Binary file not shown.
Binary file added document/convert_to_pdf_fonts/fonts.docx
Binary file not shown.
Binary file added document/convert_to_pdf_fonts/fonts.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
67 changes: 67 additions & 0 deletions document/convert_to_pdf_fonts/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* This example showcases PDF generation from docx document with UniOffice package.
*/

package main

import (
"fmt"
"log"
"os"

unipdflicense "github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/model"

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

func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := unipdflicense.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
fmt.Printf("ERROR: Failed to set metered key: %v\n", err)
fmt.Printf("Make sure to get a valid key from https://cloud.unidoc.io\n")
fmt.Printf("If you don't have one - Grab one in the Free Tier at https://cloud.unidoc.io\n")
panic(err)
}

// This example requires both for unioffice and unipdf.
err = license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
fmt.Printf("ERROR: Failed to set metered key: %v\n", err)
fmt.Printf("Make sure to get a valid key from https://cloud.unidoc.io\n")
fmt.Printf("If you don't have one - Grab one in the Free Tier at https://cloud.unidoc.io\n")
panic(err)
}
}

func main() {

// register all fonts from the folder
err := convert.RegisterFontsFromDirectory("fonts/PTSans")
if err != nil {
log.Fatalf("Error registering fonts from the folder: %s\n", err)
}

// register fonts in more precise way
zcoolRegular, err := model.NewCompositePdfFontFromTTFFile("fonts/ZCOOL/ZCOOLXiaoWei-Regular.ttf")
if err != nil {
log.Fatalf("error opening font: %s\n", err)
}
convert.RegisterFont("SimHei", convert.FontStyle_Regular, zcoolRegular) // we can use one font instead of other

doc, err := document.Open("fonts.docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
defer doc.Close()
c := convert.ConvertToPdf(doc)

err = c.WriteToFile("fonts.pdf")
if err != nil {
log.Fatalf("error converting document: %s", err)
}
}
86 changes: 86 additions & 0 deletions document/node-combine/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2017 FoxyUtils ehf. All rights reserved.
//
// This example shows how Nodes can be used to work
// generically with document contents to find and copy
// contents across files. In this example we
// 1. Load an input sample document sample1.docx and sample2.docx
// 2. Identify table element at sample1.docx
// 3. Identify paragraph element at sample2.docx
// 4. Create a new document that contains table from sample1.docx and some of paragraph from sample2.docx
// 5. Save the output to file with names: node-combine.docx.

package main

import (
"log"
"os"

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

func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := license.SetMeteredKey(os.Getenv("UNIDOC_LICENSE_API_KEY"))
if err != nil {
panic(err)
}
}

func main() {
doc1, err := document.Open("sample1.docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
defer doc1.Close()

doc2, err := document.Open("sample2.docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
defer doc2.Close()

// Get document element as nodes.
nodes1 := doc1.Nodes()

nodes2 := doc2.Nodes()

// Find nodes by condition,
// FindNodeByCondition take 2 argument, function and wholeElements: true or false.
nodeList1 := nodes1.FindNodeByCondition(func(node *document.Node) bool {
switch node.X().(type) {
case *document.Table:
return true
}
return false
}, false)

// Create new document of node tables.
newDoc := document.New()
defer newDoc.Close()

for _, node := range nodeList1 {
newDoc.AppendNode(node)
}

// Find nodes by condition.
// FindNodeByCondition take 2 argument, function and wholeElements: true or false.
nodeList2 := nodes2.FindNodeByCondition(func(node *document.Node) bool {
switch node.X().(type) {
case *document.Paragraph:
return true
}
return false
}, false)

for _, node := range nodeList2 {
newDoc.AppendNode(node)
}

// Save new doucment.
err = newDoc.SaveToFile("output/node-combine.docx")
if err != nil {
log.Fatalf("error while saving file: %v\n", err.Error())
}
}
Binary file added document/node-combine/output/node-combine.docx
Binary file not shown.
Binary file added document/node-combine/sample1.docx
Binary file not shown.
Binary file added document/node-combine/sample2.docx
Binary file not shown.
83 changes: 83 additions & 0 deletions document/node-extraction/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2017 FoxyUtils ehf. All rights reserved.
//
// This example shows how Nodes can be used to work
// generically with document contents to find and copy
// contents across files. In this example we
// 1. Load an input sample document sample.docx
// 2. Identify paragraphs with style "heading 1" and use that as a section divider
// 3. Create a new document for each section, and output each section to separate file with names: node-document-i.docx where i is the section index.
// In addition we illustrate how to perform some simple replacements that are included in the output files.

package main

import (
"fmt"
"log"
"os"

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

func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := license.SetMeteredKey(os.Getenv("UNIDOC_LICENSE_API_KEY"))
if err != nil {
panic(err)
}
}

func main() {
doc, err := document.Open("sample.docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
defer doc.Close()

// Get document element as nodes.
nodes := doc.Nodes()

// Replace text inside nodes.
nodes.ReplaceText("Where can I get some?", "The Title is Replaced")
nodes.ReplaceText("Why do we use it?", "I Am The New Title of Document")

// Find nodes by style name.
nodesByStyle := nodes.FindNodeByStyleName("heading 1")

// Create new document.
for i, nodeParent := range nodesByStyle {
// Create new document.
newDoc := document.New()
defer newDoc.Close()
fmt.Println("New document will be created")
fmt.Println("Heading:", nodeParent.Text())

nextNodeIndex := i + 1
minIndex := -1
for ni, node := range nodes.X() {
if nodeParent.X() == node.X() {
minIndex = ni
}

// If there's next node, break the loop and go to next parentNode.
if len(nodesByStyle) > nextNodeIndex {
if nodesByStyle[nextNodeIndex].X() == node.X() {
minIndex = ni
break
}
}

// Insert node to new document.
if ni >= minIndex && minIndex > -1 {
newDoc.AppendNode(node)
}
}

// Save new doucment.
err := newDoc.SaveToFile(fmt.Sprintf("output/node-document-%d.docx", i))
if err != nil {
log.Fatalf("error while saving file: %v\n", err.Error())
}
}
}
Binary file not shown.
Binary file not shown.
Binary file added document/node-extraction/sample.docx
Binary file not shown.
Loading

0 comments on commit af036b6

Please sign in to comment.