Skip to content

Commit

Permalink
Merge pull request #14 from unidoc/rc-v1.13.0
Browse files Browse the repository at this point in the history
Prepare examples for unioffice v1.13.0
  • Loading branch information
gunnsth committed Jul 30, 2021
2 parents 2e25a0d + ddf1e0a commit d9729e9
Show file tree
Hide file tree
Showing 23 changed files with 176 additions and 4 deletions.
Binary file added document/doc-to-pdf-fonts/fonts.docx
Binary file not shown.
Binary file added document/doc-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/doc-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)
}
}
2 changes: 1 addition & 1 deletion document/doc-to-pdf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var filenames = []string{

func main() {
for _, filename := range filenames {
outputPath := filename + ".pdf"
outputPath := fmt.Sprintf("output/%s.pdf", filename)
doc, err := document.Open(filename + ".docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
Expand Down
Binary file added document/doc-to-pdf/output/chart.pdf
Binary file not shown.
Binary file added document/doc-to-pdf/output/headers_footers.pdf
Binary file not shown.
Binary file added document/doc-to-pdf/output/image_square.pdf
Binary file not shown.
Binary file added document/doc-to-pdf/output/table.pdf
Binary file not shown.
Binary file not shown.
Binary file added document/doc-to-pdf/output/text_only_portrait.pdf
Binary file not shown.
Binary file added document/doc-to-pdf/output/textbox_anchor.pdf
Binary file not shown.
Binary file added document/doc-to-pdf/output/textbox_inline.pdf
Binary file not shown.
Binary file added document/watermark-picture/gophercolor.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions document/watermark-picture/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2017 FoxyUtils ehf. All rights reserved.

package main

import (
"log"

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

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

para := doc.AddParagraph()
run := para.AddRun()
for i := 0; i < 16; i++ {
run.AddText(lorem)
}

// Put image watermark to document.
img1, err := common.ImageFromFile("gophercolor.png")
if err != nil {
log.Fatalf("unable to create image: %s", err)
}

// Add image to document.
img1ref, err := doc.AddImage(img1)
if err != nil {
log.Fatalf("unable to add image to document: %s", err)
}

// Set watermark to doc.
watermark := doc.AddWatermarkPicture(img1ref)
watermark.SetPictureWashout(true)

// Get image size and set watermark size.
// SetPictureSize accept parameter image width and image height.
imageSize := img1ref.Size()
watermark.SetPictureSize(int64(imageSize.X*measurement.Point), int64(imageSize.Y*measurement.Point))

doc.SaveToFile("watermark-picture.docx")
}
44 changes: 44 additions & 0 deletions document/watermark-text/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2017 FoxyUtils ehf. All rights reserved.
package main

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

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

para := doc.AddParagraph()
run := para.AddRun()
for i := 0; i < 16; i++ {
run.AddText(lorem)
}

// Set watermark to doc.
watermark := doc.AddWatermarkText("TEST")

// Change style of watermark text.
// Currently having 2 function,
// SetTextStyleBold and SetTextStyleItalic.
watermark.SetTextStyleBold(true)
watermark.SetTextStyleItalic(false)

doc.SaveToFile("watermark-text.docx")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/go-ole/go-ole v1.2.5
github.com/unidoc/unioffice v1.12.0
github.com/unidoc/unioffice v1.13.0
github.com/unidoc/unipdf/v3 v3.25.0
golang.org/x/sys v0.0.0-20210414055047-fe65e336abe0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ github.com/unidoc/pkcs7 v0.0.0-20200411230602-d883fd70d1df h1:1RV3lxQ6L6xGFNhngp
github.com/unidoc/pkcs7 v0.0.0-20200411230602-d883fd70d1df/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.12.0 h1:5AjvI1QbNfqcZZ83o2heEsCI43BH4XMrQD+IUKVUgV8=
github.com/unidoc/unioffice v1.12.0/go.mod h1:8QJAWaP6ZNjyoONjKXmzbM07/nDDgHpj5uO7xaBH6Ok=
github.com/unidoc/unioffice v1.13.0 h1:4FWEQDqd2nNjZq/CaRAHlKxmJDuLD1Pm/vDCnYkO9MM=
github.com/unidoc/unioffice v1.13.0/go.mod h1:Wt6osBNYJnvTSjijk915JAf+NedEeFuVb0AZjCif0OI=
github.com/unidoc/unipdf/v3 v3.22.0/go.mod h1:WdRz3hVhi/M0jFGXhsT5/9FDyRfga6KgI2ZQqjiOXaM=
github.com/unidoc/unipdf/v3 v3.25.0 h1:OJmfZg2BJdA10oOcb6leyi3lDjeDf5SV1OFlBrBc5+4=
github.com/unidoc/unipdf/v3 v3.25.0/go.mod h1:WdRz3hVhi/M0jFGXhsT5/9FDyRfga6KgI2ZQqjiOXaM=
Expand Down
Binary file modified presentation/text_extraction/extract.pptx
Binary file not shown.

0 comments on commit d9729e9

Please sign in to comment.