How to Add a Watermark in MS Word Documents
With Unidoc's UniOffice or UniSuite you can add watermarks to your word documents as required. You can add watermarks as an image or as text.
Functions
- watermark:=doc.AddWatermarkPicture(imagefilename)
- watermark:=doc.AddWatermarkText("TEST")
To use a picture as a watermark in your Word document, you can try the below example.
// Copyright 2017 FoxyUtils ehf. All rights reserved. package main import ( "log" "os" "github.com/unidoc/unioffice/common" "github.com/unidoc/unioffice/common/license" "github.com/unidoc/unioffice/document" "github.com/unidoc/unioffice/measurement" ) 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) } } 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") }You can also add text as a watermark in a Word document. To add a text as a watermark, follow the below example.
// Copyright 2017 FoxyUtils ehf. All rights reserved. package main import ( "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) } } 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") }
You can also visit the UniOffice example repository on Github to see what else can be done using UniOffice.