How to Create a Paragraph Style in Word using UniOffice

Plain documents are boring. A good style is the secret sauce to transform mundane text into something that pops. With UniOffice SDK, creating paragraph styles in Word documents is as easy as pie.

This blog will guide you through the process, adding a touch of wit to keep things interesting. Whether you’re making reports, resumes, or creative documents, custom styles will give your content the flair it deserves.

Why Paragraph Styles Matter

Think of paragraph styles as your document’s wardrobe. A well-dressed paragraph isn’t just about aesthetics—it’s about making your message clear and professional. Here’s why you should care:

  • Consistency: No more manual formatting for every paragraph.

  • Readability: Proper spacing, alignment, and indentation make content easier to digest.

  • Professional Appeal: Styled documents look polished and intentional.

Ready to give your Word documents a makeover? Let’s dive in.

Setting Up UniOffice

Before we jump into the code, you need to prepare your development environment.

Step 1: Grab Your API Key

First, visit UniCloud and sign up for an API key. This key is your ticket to using UniOffice SDK.

Step 2: Clone the Examples

Run the following commands to get started with the example repository:

git clone https://github.com/unidoc/unioffice-examples.git
cd unioffice-examples/document/paragraph-style/

Step 3: Configure Your Environment

Follow the UniOffice setup guide to get everything up and running.

Writing Paragraph Styles with UniOffice

Let’s break the process into bite-sized steps.

The Code

Below is the Go code for creating paragraph styles.

package main

import (
    "os"

    "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() {
    err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
    if err != nil {
        panic(err)
    }
}

func main() {
    doc := document.New()
    defer doc.Close()

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

    // Create a 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.")

    // Create a custom paragraph style
    style := doc.Styles
    customStyle := style.AddStyle("CustomStyle1", wml.ST_StyleTypeParagraph, false)
    customStyle.SetName("Custom Style 1")
    customStyle.ParagraphProperties().SetSpacing(measurement.Inch*1, measurement.Inch*1)
    customStyle.ParagraphProperties().SetAlignment(wml.ST_JcBoth)
    customStyle.ParagraphProperties().SetFirstLineIndent(measurement.Inch * 2)
    customStyle.ParagraphProperties().SetLineSpacing(12*measurement.Point, wml.ST_LineSpacingRuleAuto)

    // Apply custom style to a paragraph
    para = doc.AddParagraph()
    para.SetStyle("CustomStyle1")
    run = para.AddRun()
    run.AddText("Lorem ipsum dolor sit amet, consectetur adipiscing elit...")

    doc.SaveToFile("paragraph-style.docx")
}

Step-by-Step Breakdown

  1. Initialize the SDK

  2. The init function sets up your API key. Without this, nothing works.

  3. Create Built-In Styles

  4. Use para.SetStyle("Title") or para.SetStyle("Heading1") to apply Word’s default styles.

  5. Define a Custom Style

    • Use doc.Styles.AddStyle() to create your style.

    • Customize properties like alignment, spacing, and indentation.

  6. Apply the Custom Style

  7. Add a paragraph and assign your custom style with para.SetStyle("CustomStyle1").

Running the Code

Save the code in a file (e.g., main.go) and run it in your terminal:

go run main.go

This creates a Word document (paragraph-style.docx) with:

  • A title styled paragraph.

  • A Heading1 paragraph.

  • A custom-styled paragraph.

Custom Paragraph Styles: A Closer Look

Let’s explore what makes custom styles so powerful.

Alignment and Indentation

Proper alignment keeps your document looking tidy. Indentation adds structure. Here’s how we set both:

customStyle.ParagraphProperties().SetAlignment(wml.ST_JcBoth)
customStyle.ParagraphProperties().SetFirstLineIndent(measurement.Inch * 2)

Spacing

Good spacing gives your content room to breathe. You can set before and after spacing like this:

customStyle.ParagraphProperties().SetSpacing(measurement.Inch*1, measurement.Inch*1)

Line Spacing

Whether you prefer single, double, or custom spacing, it’s all possible. Here’s an example:

customStyle.ParagraphProperties().SetLineSpacing(12*measurement.Point, wml.ST_LineSpacingRuleAuto)

Real-World Applications

Business Proposals

Real-World Applications

Use custom styles for section headers, body text, and callouts to make your proposals shine.

Academic Papers

Ensure your document meets formatting guidelines with consistent paragraph styles.

Resumes

Stand out with tailored headings and elegant spacing that makes your qualifications pop

FAQs

Can I Edit Existing Styles?

Yes! You can modify built-in styles using doc.Styles.

What’s the Default Style for New Paragraphs?

The default style is Normal, but you can change it when creating a custom style.

How Many Custom Styles Can I Add?

As many as you need. Just ensure each has a unique name.

Learn More

Want to explore more Word manipulation tricks? Check out:

Wrapping It Up

Custom paragraph styles are a game-changer. They make your Word documents look professional, save time, and ensure consistency.

With UniOffice SDK, you’re not just formatting—you’re creating. So grab your API key, fire up your terminal, and start styling!