How to Add a Border in Word to a Paragraph using UniOffice
Adding borders to paragraphs in Word documents can enhance the readability and aesthetics of your content. Whether it’s highlighting key sections, distinguishing headings, or just giving your document a polished look, paragraph borders can be very useful.
This guide will walk you through the process of adding different types of borders to paragraphs using the UniOffice library in Go. We’ll cover how to create borders for each side of the paragraph, use various border styles, and adjust the border thickness.
Clone the Project Repository
The code examples we’ll use are part of a sample repository provided by UniDoc. To get started, clone the repository by running:
If using SSH
git clone [email protected]:unidoc/unioffice-examples.git
If using HTTPS
git clone https://github.com/unidoc/unioffice-examples.git
Once cloned, navigate to the document/paragraph-borders
folder
cd unioffice-examples/document/paragraph-borders/
How It Works
The following Go code demonstrates how to add borders to paragraphs using UniOffice. This example covers various ways to customize borders, such as setting different styles and thicknesses. The code also shows how to apply these settings programmatically.
Code Explanation
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() {
// Load your API key before using the library.
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}
func main() {
doc := document.New()
defer doc.Close()
// Create paragraph with Title style.
para := doc.AddParagraph()
run := para.AddRun()
para.SetStyle("Title")
run.AddText("What is Lorem Ipsum?")
// Create paragraph with Heading1 style.
para = doc.AddParagraph()
para.SetStyle("Heading1")
run = para.AddRun()
run.AddText("Lorem Ipsum is simply dummy text of the printing and typesetting industry.")
// Add a bottom border.
border := para.Borders()
border.SetBottom(wml.ST_BorderDotted, color.Auto, 1*measurement.Point)
para = doc.AddParagraph()
run = para.AddRun()
run.AddText("Lorem Ipsum has been the industry's standard dummy text since the 1500s.")
// Add a single line bottom border.
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 Lorem Ipsum available...")
// Add borders to all sides.
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("The use of Lorem Ipsum ensures that readers focus on layout...")
// Set individual borders for left, right, top, and bottom.
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)
}
}
Step-by-Step Breakdown
1. Import Necessary Packages
The first step involves importing required packages:
os
for reading environment variables.github.com/unidoc/unioffice
packages for document handling, border settings, and license configuration.
2. Initialize the License Key
The init()
function configures the metered license key, which is required for using UniOffice’s advanced features. Make sure to replace the placeholder with your actual API key.
3. Create a New Document
The document.New()
function initializes a new Word document. Always remember to close the document with defer doc.Close()
to ensure that resources are released properly.
4. Adding a Paragraph with a Title Style
To create a paragraph, use doc.AddParagraph()
, and apply the “Title” style using para.SetStyle("Title")
. Adding text is as simple as calling run.AddText()
.
5. Adding Borders to Paragraphs
Borders can be added to paragraphs using the Borders()
function, followed by specific methods to customize border properties:
SetBottom()
adds a border to the bottom. You can set the style (like dotted or single), color, and thickness.SetAll()
applies the same border settings to all sides.
6. Using Multiple Border Types
The code demonstrates various border types and configurations:
wml.ST_BorderDotted
for dotted lines.wml.ST_BorderSingle
for a single solid line.wml.ST_BorderDotDotDash
for a dot-dash pattern.
7. Setting Borders on All Sides Individually
For more customized borders, individual settings for each side can be applied:
SetLeft()
,SetRight()
,SetTop()
, andSetBottom()
allow you to use different border styles and thicknesses for each side.
8. Save the Document
Finally, the document is saved using doc.SaveToFile("paragraph-borders.docx")
. Make sure to check for any errors during the save process.
Running the Code
After setting up the code, execute it using the command below. Make sure to have the proper directory and dependencies in place:
go run main.go
If everything is configured correctly, a Word document named paragraph-borders.docx
will be created with multiple paragraphs, each featuring different border styles.
Understanding Border Styles and Their Uses
Borders can be used in various scenarios to make documents more engaging:
Bottom Borders for Titles
Using a border below the title helps to distinguish it from the body content. The dotted style (wml.ST_BorderDotted
) works well for documents that need a subtle touch.
Different Styles for Emphasis
For example, using wml.ST_BorderTriple
as a bottom border adds a strong visual divider between sections. This is helpful for breaking up lengthy documents.
Combining Multiple Borders
When you want to highlight a section, setting borders on all sides with different styles can make that content stand out. Experimenting with combinations like a thick top border (wml.ST_BorderThick
) and a dotted side border (wml.ST_BorderDotted
) can create unique visual effects.
Tips for Customizing Borders
1. Choosing the Right Style
UniOffice provides several border styles, including:
Single
,Double
,Dotted
,Dashed
,DotDash
,DotDotDash
,Triple
, andThick
. Pick styles based on the tone and purpose of your document.
2. Adjusting Border Thickness
The measurement.Point
unit lets you fine-tune the border thickness. A thicker border may suit headings, while thinner borders are better for separating paragraphs.
3. Experimenting with Colors
Using colors other than black can make borders visually appealing. UniOffice’s color
package allows you to set colors using standard color names, hexadecimal values, or RGB.
4. Handling Multi-Line Paragraphs
Borders automatically adjust to accommodate paragraph height. For multi-line content, using a bottom border provides a clean separation from subsequent paragraphs.
Why Use UniOffice for Adding Borders?
UniOffice is a powerful library that simplifies the process of working with Word documents in Go. It is especially useful for:
Creating dynamic documents with custom styles.
Integrating document processing into existing systems.
The programmatic approach not only saves time but also ensures consistency across documents.
Troubleshooting Common Issues
License Key Errors
If you encounter issues related to license keys, make sure:
Your environment variable
UNIDOC_LICENSE_API_KEY
is set correctly.You have an active UniCloud account and a valid key.
Border Not Appearing
Ensure that:
The paragraph exists and has content.
You are applying the correct border type and color.
The document is saved successfully without errors.
Run-Time Errors
Common issues include missing libraries or improper paths. Double-check your Go setup and make sure dependencies are installed.
Final Thoughts
Adding borders to Word paragraphs using UniOffice is straightforward once you understand the basics. By following the steps outlined in this guide, you can easily customize your document layout programmatically. The ability to set border styles, colors, and thickness programmatically offers flexibility that goes beyond what you can achieve manually in Word.