Spacing and Indentation
Everything on this page is a paragraph property, set directly on the
Paragraph and applying to the whole block. Spacing controls the gap above and
below the paragraph; indentation controls how far its edges sit from the page
margins. Neither has anything to do with the gap between lines inside the
paragraph, which is line spacing.
The setters
| Call | Effect |
|---|---|
SetBeforeSpacing(d) | Gap above the paragraph. |
SetAfterSpacing(d) | Gap below the paragraph. |
SetLeftIndent(d) | Left edge moves in by d. |
SetRightIndent(d) | Right edge moves in by d. |
SetFirstLineIndent(d) | First line only moves in by d. |
SetHangingIndent(d) | Every line except the first moves in by d. |
SetAlignment(wml.ST_Jc...) | Left, right, center or justified. |
IgnoreSpaceBetweenParagraphOfSameStyle() | Drops before and after spacing between consecutive paragraphs sharing a style. |
SetOutlineLvl(n) | Outline level for the navigation pane and table of contents. |
Every distance is a measurement.Distance, and the constants in the
measurement package are all expressed in points, so measurement.Inch*3,
measurement.Point*216 and measurement.Twips*4320 are interchangeable ways of
writing the same length. The value is truncated to twips on the way into the
file, so a distance built from measurement.Millimeter or
measurement.Centimeter can land a twip short of the round number you were
aiming at.
For alignment, wml.ST_JcLeft and wml.ST_JcRight are absolute, while
wml.ST_JcStart and wml.ST_JcEnd follow the reading direction. Use the second
pair if the document has any right-to-left content.
Doing it
para := doc.AddParagraph()
para.AddRun().AddText(lorem)
para.SetAlignment(wml.ST_JcCenter)
para.SetBeforeSpacing(measurement.Inch * 2)
para.SetAfterSpacing(measurement.Inch * 2)
para.SetLeftIndent(measurement.Inch * 3)Between two paragraphs, the first one’s after spacing and the second one’s
before spacing both apply, which is why a run of paragraphs that each set an
inch on both sides ends up looking much sparser than expected.
IgnoreSpaceBetweenParagraphOfSameStyle sets w:contextualSpacing to suppress
that, which is how list items avoid drifting apart. It applies only between
neighbors that use the same style, and it takes no argument, so there is no way
to switch it back off through this method.
SetFirstLineIndent and SetHangingIndent write different attributes of the
same w:ind element and are the two directions of the same idea. Setting both
on one paragraph leaves Word to reconcile them; pick one.
Limitations
Passing measurement.Zero to any of the four indent setters deletes the
attribute instead of writing zero. A paragraph cannot use SetLeftIndent(0) to
cancel an indent inherited from its style. Set the style’s indent, or apply a
different style.
SetOutlineLvl subtracts one from the argument before writing it, so
SetOutlineLvl(3) produces w:outlineLvl of 2, which is the level Word calls
Level 3. The example passes 3 and gets the third level. The style-level
equivalent, ParagraphStyleProperties.SetOutlineLevel, writes the value
unchanged, so the two are off by one from each other.
SetBeforeSpacing and SetAfterSpacing write an unsigned attribute. Negative
distances are not clamped, they wrap, and the result is a document Word may
refuse to open.
SetBeforeLineSpacing and SetAfterLineSpacing exist and sound like they take
a number of lines, but they convert their argument through
measurement.Twips in the same way the point-based setters do, while
w:beforeLines and w:afterLines are read by Word in hundredths of a line.
The values will not mean what the names suggest. Use SetBeforeSpacing and
SetAfterSpacing.
Run the example
The example writes eleven paragraphs of the same lorem ipsum, each one changing a single setting, so the differences are what you read. It also ends with four paragraphs stepping line spacing from single through triple, showing the 12 point baseline the auto rule is measured against.
Note that it never calls doc.Close(). That is fine here, because Close only
removes temporary files created by document.Open, and this example builds the
document with document.New.
git clone https://github.com/unidoc/unioffice-examples.git
cd unioffice-examples/document/paragraph_spacing_and_indentation
go run main.goIf this is your first time using UniOffice, follow the getting started guide to create an API key and set up your development environment.