Image Effects

In this guide, you’ll learn how to apply image effects to pictures added to DOCX files using UniOffice.

Before you begin

You should get your API key from your UniCloud account.

If this is your first time using UniOffice SDK, follow this guide to set up a local development environment.

Clone the project repository

In your terminal, clone the examples repository. It contains the Go code we will be using for this guide.

git clone https://github.com/unidoc/unioffice-examples

To get the example navigate to the path document/image folder in the unioffice-examples directory.

cd unioffice-examples/document/image/

How it works

As always the necessary libraries are imported in the import section in lines 4-13. Then the metered API key is set in the init function defined in lines 15-22.

The main function is then defined in lines 24-71. In line 20, a new document object is created using document.New(). Following that, a for loop is defined in lines 26-66 to apply the image effects one by one.

First the image is loaded from file using common.ImageFromFile("gophercolor.png") in line 27. Then doc.AddImage(img) is used to add the image to the document package and get an image reference object. This reference object is used to add the image to a paragraph run, placing it into the document content. This is done in line 38 using:

anchored, err := doc.AddParagraph().AddRun().AddDrawingAnchored(imgref)

Then the image effects are applied in the switch block in (lines 45-67). The switch statement applies different effects based on the current iteration index. Iterating from 0 to 6, the loop adds the image to a new page each time and applies a specific effect. Thus, seven different effects are applied across seven pages of the .docx document.

The supported effects are:

  • Soft Edges: This effect softens the edges of the image. It is applied using SetSoftEdgeImageEffect(radius measurement.Distance) method. The extent of this effect is controlled by the radius parameter.

  • Glow: This adds a glowing effect around the edges of the image. It is set using the SetGlowImageEffect(radius measurement.Distance, c color.Color) method. The glow area is determined by the radius, and the glow color by the c parameter.

  • Inner Shadow: This effect adds an inner shadow to the image with specific width, blur, direction, and color settings. This is applied using SetInnerShadowImageEffect(radius measurement.Distance, offset measurement.Distance, c color.Color, degrees float64).

    • The radius controls how spread out and fuzzy the shadow appears.
    • The offset defines how far the shadow is positioned from the edges.
    • The color parameter specifies the shadow’s color.
    • The degrees parameter sets the shadow direction as follows.
      • 0° places the shadow on the right,
      • 90° on the bottom.
      • 180° on the top.
  • Outer Shadow: Similar to the Inner Shadow effect, but the shadow lies outside the edges of the image. This is applied using SetOuterShadowImageEffect(radius measurement.Distance, offset measurement.Distance, c color.Color, degrees float64). The parameters function the same way as in the inner shadow effect.

  • Reflection: This effect creates a vertical reflection of the image. This effect is applied using SetReflectionImageEffect(radius measurement.Distance, opacity float64, size float64). The radius controls the blur of the reflection, while The opacity controls the transparency of the reflection. And The size parameter sets the proportion of the reflected image relative to the original..

  • Bevel: Adds 3D depth to the image. This effect is set using the SetBevelImageEffect(). By default it applies a top bevel with width and height of 6 points.

  • 3D Rotation: Rotates the image in three-dimensional space and is applied using Set3DRotationImageEffect() method. By default it rotates the image 315° on the X axis, 35° on the Y-axis and 0° on Z-axis.

Finally the document is saved to file in line 70.

Run the code

Run this command to create a new document with the image that has the mentioned effects applied

go run main.go

Sample output

Here are a sample outputs where the effects in case 2 and 3 in the code are applied.

Output 1: Glow Effect with glow area i.e radius = 8 and color = Blue.

Image result 1

Output 2: Inner Shadow Effect with radius = 8, offset=10, color = Red and degrees=150°.

Image result 2

Got any Questions?

We're here to help you.