-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated examples for unioffice v1.16.0 (#18)
- Loading branch information
Showing
50 changed files
with
420 additions
and
1 deletion.
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
Binary file not shown.
Binary file not shown.
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,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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,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) | ||
} | ||
} |
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,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 not shown.
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,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 not shown.
Oops, something went wrong.