Skip to content

Commit

Permalink
Update examples for v1.17.0 (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnsth committed Jan 15, 2022
1 parent af036b6 commit 52c0110
Show file tree
Hide file tree
Showing 15 changed files with 317 additions and 13 deletions.
1 change: 1 addition & 0 deletions document/convert_to_pdf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var filenames = []string{
"fld_simple",
"headers_footers",
"image_square",
"merge_fields",
"table",
"text_only_portrait",
"text_only_landscape",
Expand Down
Binary file added document/convert_to_pdf/merge_fields.docx
Binary file not shown.
Binary file added document/convert_to_pdf/output/merge_fields.pdf
Binary file not shown.
59 changes: 59 additions & 0 deletions document/convert_to_pdf_options/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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)
}
}

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

// Set convert options for ProcessFields to true.
co := &convert.Options{
ProcessFields: true,
}

// Convert to PDF and process the fields in document.
c := convert.ConvertToPdfWithOptions(doc, co)

err = c.WriteToFile("output/merge_fields.pdf")
if err != nil {
log.Fatalf("error converting document: %s", err)
}
}
Binary file added document/convert_to_pdf_options/merge_fields.docx
Binary file not shown.
Binary file not shown.
Binary file added document/form-activex/activex_filled.docm
Binary file not shown.
81 changes: 81 additions & 0 deletions document/form-activex/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* This example showcases getting values from ActiveX forms and changing them.
*/

package main

import (
"fmt"
"log"
"os"

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

func init() {
// This example requires both for unioffice and unipdf.
if err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)); 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() {
doc, err := document.Open("activex_filled.docm")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
defer doc.Close()
for i, p := range doc.Paragraphs() {
for _, r := range p.Runs() {
ctrl := r.Control()
if ctrl != nil {
if ctrl.Choice != nil {
if checkBox := ctrl.Choice.CheckBox; checkBox != nil {
fmt.Println("found checkbox:", checkBox.GetValue(), checkBox.GetCaption())
checkBox.SetValue(true)
checkBox.SetCaption(fmt.Sprintf("CheckBox caption %d", i))
} else if textBox := ctrl.Choice.TextBox; textBox != nil {
fmt.Println("found textbox:", textBox.GetValue(), textBox.GetCaption())
textBox.SetValue(fmt.Sprintf("New textbox value %d", i))
textBox.SetCaption(fmt.Sprintf("TextBox caption %d", i))
} else if comboBox := ctrl.Choice.ComboBox; comboBox != nil {
fmt.Println("found combo box:", comboBox.GetValue())
comboBox.SetValue(fmt.Sprintf("New combobox value %d", i))
} else if optionButton := ctrl.Choice.OptionButton; optionButton != nil {
fmt.Println("found option button:", optionButton.GetValue(), optionButton.GetCaption())
optionButton.SetValue(!optionButton.GetValue())
optionButton.SetCaption(fmt.Sprintf("Option button %d", i))
} else if toggleButton := ctrl.Choice.ToggleButton; toggleButton != nil {
fmt.Println("found toggle button:", toggleButton.GetValue(), toggleButton.GetCaption())
toggleButton.SetValue(true)
toggleButton.SetCaption(fmt.Sprintf("Toggle button %d", i))
} else if label := ctrl.Choice.Label; label != nil {
fmt.Println("found label:", label.GetCaption())
label.SetCaption(fmt.Sprintf("New label %d", i))
label.SetForeColor(uint32(0x02ff0000))
label.SetBackColor(uint32(0x020044ff))
} else if spinButton := ctrl.Choice.SpinButton; spinButton != nil {
fmt.Println("found spin button:", spinButton.GetMin(), spinButton.GetMax(), spinButton.GetPosition(), spinButton.GetWidth(), spinButton.GetHeight())
spinButton.SetPosition(1 - spinButton.GetPosition())
spinButton.SetForeColor(uint32(0x020044ff))
spinButton.SetBackColor(uint32(0x02ff0000))
} else if commandButton := ctrl.Choice.CommandButton; commandButton != nil {
fmt.Println("found command button:", commandButton.GetCaption())
commandButton.SetCaption(fmt.Sprintf("Command button %d", i))
commandButton.SetForeColor(uint32(0x02ffffff))
commandButton.SetBackColor(uint32(0x0200ff00))
} else if scrollBar := ctrl.Choice.ScrollBar; scrollBar != nil {
fmt.Println("found scroll bar:", scrollBar.GetMin(), scrollBar.GetMax(), scrollBar.GetPosition(), scrollBar.GetWidth(), scrollBar.GetHeight())
scrollBar.SetMax(100)
scrollBar.SetPosition(20)
}
}
}
}
}
doc.SaveToFile("new_activex_filled.docm")
}
63 changes: 63 additions & 0 deletions document/node-find-and-replace/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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 text "Cell 1" and regexp "What is.*"
// 3. Create a new document output named: node-find-and-replace.docx.

package main

import (
"log"
"os"
"regexp"

"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()

// Find node element by text.
nodeList := nodes.FindNodeByText("Cell 1")
for _, node := range nodeList {
log.Println("Find by text found: ", node.Text())
}

// Replace text.
nodes.ReplaceText("Cell 1", "Cell Replacement")

// Find node element by regexp.
nodeList = nodes.FindNodeByRegexp(regexp.MustCompile(`What is.*`))
for _, node := range nodeList {
log.Println("Find by regex found: ", node.Text())
}

// Replace text using regexp.
nodes.ReplaceTextByRegexp(regexp.MustCompile(`What is.*`), "Why I am replaced?")

// Save new doucment.
err = doc.SaveToFile("output/node-find-and-replace.docx")
if err != nil {
log.Fatalf("error while saving file: %v\n", err.Error())
}
}
Binary file not shown.
Binary file added document/node-find-and-replace/sample.docx
Binary file not shown.
94 changes: 94 additions & 0 deletions document/paragraph-borders/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Paragraph borders
* Create paragraph that contains border.
* For border line break paragraph, as MS Word function automatic border line -
* its similar with the paragraph that having border bottom value.
*
* The reference regarding this can be seen here https://www.avantixlearning.ca/microsoft-word/how-to-insert-and-remove-lines-in-microsoft-word
*/

// Copyright 2021 FoxyUtils ehf. All rights reserved.

package main

import (
"os"

"github.com/unidoc/unioffice/color"
"github.com/unidoc/unioffice/common/license"
"github.com/unidoc/unioffice/document"
"github.com/unidoc/unioffice/measurement"
"github.com/unidoc/unioffice/schema/soo/wml"
)

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 := document.New()
defer doc.Close()

// Create paragraph and apply Title style to paragraph.
para := doc.AddParagraph()
run := para.AddRun()
para.SetStyle("Title")
run.AddText("What is Lorem Ipsum?")

// Create paragraph and apply Heading1 style to paragraph.
para = doc.AddParagraph()
para.SetStyle("Heading1")
run = para.AddRun()
run.AddText("Lorem Ipsum is simply dummy text of the printing and typesetting industry.")

// Add border bottom to paragraph.
border := para.Borders()
border.SetBottom(wml.ST_BorderDotted, color.Auto, 1*measurement.Point)

para = doc.AddParagraph()
run = para.AddRun()
run.AddText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")

// Add border bottom to paragraph.
border = para.Borders()
border.SetBottom(wml.ST_BorderSingle, color.Auto, 1*measurement.Point)

para = doc.AddParagraph()
para.SetStyle("Heading1")
run = para.AddRun()
run.AddText("Where can I get some?")

para = doc.AddParagraph()
run = para.AddRun()
run.AddText("There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.")

// Add border All (left, right, top, bottom) to paragraph.
border = para.Borders()
border.SetAll(wml.ST_BorderDotDotDash, color.Auto, 1*measurement.Point)

para = doc.AddParagraph()
para.SetStyle("Heading1")
run = para.AddRun()
run.AddText("Why do we use it?")

para = doc.AddParagraph()
run = para.AddRun()
run.AddText("It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).")

// Add border left, right, top, bottom to paragraph.
border = para.Borders()
border.SetLeft(wml.ST_BorderDotDotDash, color.Auto, 1*measurement.Point)
border.SetRight(wml.ST_BorderDotted, color.Auto, 1*measurement.Point)
border.SetTop(wml.ST_BorderThick, color.Auto, 1*measurement.Point)
border.SetBottom(wml.ST_BorderTriple, color.Auto, 1*measurement.Point)

err := doc.SaveToFile("paragraph-borders.docx")
if err != nil {
panic(err)
}
}
Binary file added document/paragraph-borders/paragraph-borders.docx
Binary file not shown.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/unidoc/unioffice-examples
go 1.13

require (
github.com/go-ole/go-ole v1.2.5
github.com/unidoc/unioffice v1.15.0
github.com/unidoc/unipdf/v3 v3.28.0
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
github.com/go-ole/go-ole v1.2.6
github.com/unidoc/unioffice v1.17.0
github.com/unidoc/unipdf/v3 v3.31.0
golang.org/x/sys v0.0.0-20211109065445-02f5c0300f6e // indirect
)
24 changes: 15 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ole/go-ole v1.2.5 h1:t4MGB5xEDZvXI+0rMjjsfBsD7yAgp/s9ZDkL1JndXwY=
github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand All @@ -20,8 +21,11 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/montanaflynn/stats v0.6.6/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/richardlehane/msoleps v1.0.1 h1:RfrALnSNXzmXLbGct/P2b4xkFz4e8Gmj/0Vj9M9xC1o=
github.com/richardlehane/msoleps v1.0.1/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTKbjLycmwiWUfWg=
github.com/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q=
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -30,14 +34,16 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/unidoc/pkcs7 v0.0.0-20200411230602-d883fd70d1df h1:1RV3lxQ6L6xGFNhngpP9iMjJPSwvH3p17JNbK9u5274=
github.com/trimmer-io/go-xmp v1.0.0/go.mod h1:Aaptr9sp1lLv7UnCAdQ+gSHZyY2miYaKmcNVj7HRBwA=
github.com/unidoc/pkcs7 v0.0.0-20200411230602-d883fd70d1df/go.mod h1:UEzOZUEpJfDpywVJMUT8QiugqEZC29pDq7kdIZhWCr8=
github.com/unidoc/pkcs7 v0.1.0 h1:9bQfbWMYsIfUP8PyhTcBudOsvbLpNH0MBv4U0P/jDTE=
github.com/unidoc/pkcs7 v0.1.0/go.mod h1:UEzOZUEpJfDpywVJMUT8QiugqEZC29pDq7kdIZhWCr8=
github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a h1:RLtvUhe4DsUDl66m7MJ8OqBjq8jpWBXPK6/RKtqeTkc=
github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a/go.mod h1:j+qMWZVpZFTvDey3zxUkSgPJZEX33tDgU/QIA0IzCUw=
github.com/unidoc/unioffice v1.15.0 h1:pMEL9pXap4Ew+KJj6iP1pyE9VH33PcYYLRlZ9KQC3FI=
github.com/unidoc/unioffice v1.15.0/go.mod h1:GNSDPZRcSSMRSiZWVJNWH22nFv705ppUQLa1pfbgAAk=
github.com/unidoc/unipdf/v3 v3.28.0 h1:flSCHcrfPHbxaSMltbUqk2Kd+2CsYWOiItj8XqRQc4o=
github.com/unidoc/unipdf/v3 v3.28.0/go.mod h1:WdRz3hVhi/M0jFGXhsT5/9FDyRfga6KgI2ZQqjiOXaM=
github.com/unidoc/unioffice v1.17.0 h1:B65bz1LEbK6N49BOjfrDiG5tbreZLvpWprd8mfCc3WE=
github.com/unidoc/unioffice v1.17.0/go.mod h1:q+c+7USR3+8hBhK7+XgGq6OO3yUHUv2EXTP8DfF4gcY=
github.com/unidoc/unipdf/v3 v3.31.0 h1:cDjCgV2eUuuCatFauzC+kt39VoAvYzOy+QfgZafVOyY=
github.com/unidoc/unipdf/v3 v3.31.0/go.mod h1:Lf6mZ1+s/7mzFI1+gXFRae3qZgGg3LEAeZJhbUx3+80=
github.com/unidoc/unitype v0.2.1 h1:x0jMn7pB/tNrjEVjy3Ukpxo++HOBQaTCXcTYFA6BH3w=
github.com/unidoc/unitype v0.2.1/go.mod h1:mafyug7zYmDOusqa7G0dJV45qp4b6TDAN+pHN7ZUIBU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand All @@ -51,8 +57,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf h1:2ucpDCmfkl8Bd/FsLtiD653Wf96cW37s+iGx93zsu4k=
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211109065445-02f5c0300f6e h1:i6Vklmyu+fZMFYpum+sR4ZWABGW7MyIxfJZXYvcnbns=
golang.org/x/sys v0.0.0-20211109065445-02f5c0300f6e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
Expand Down

0 comments on commit 52c0110

Please sign in to comment.