mirror of
https://github.com/unidoc/unioffice.git
synced 2025-04-25 13:48:53 +08:00
document: add more fine grained control of line spacing
This commit is contained in:
parent
4293d9cb58
commit
9471100d92
BIN
_examples/document/line-spacing/line-spacing.docx
Normal file
BIN
_examples/document/line-spacing/line-spacing.docx
Normal file
Binary file not shown.
35
_examples/document/line-spacing/main.go
Normal file
35
_examples/document/line-spacing/main.go
Normal file
@ -0,0 +1,35 @@
|
||||
// Copyright 2017 Baliance. All rights reserved.
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"baliance.com/gooxml/document"
|
||||
"baliance.com/gooxml/measurement"
|
||||
"baliance.com/gooxml/schema/soo/wml"
|
||||
)
|
||||
|
||||
func main() {
|
||||
doc := document.New()
|
||||
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.`
|
||||
|
||||
// single spaced
|
||||
para := doc.AddParagraph()
|
||||
run := para.AddRun()
|
||||
run.AddText(lorem)
|
||||
run.AddText(lorem)
|
||||
run.AddBreak()
|
||||
|
||||
// double spaced is twice the text height (24 points in this case as the text height is 12 points)
|
||||
para = doc.AddParagraph()
|
||||
para.Properties().Spacing().SetLineSpacing(24*measurement.Point, wml.ST_LineSpacingRuleAuto)
|
||||
run = para.AddRun()
|
||||
run.AddText(lorem)
|
||||
run.AddText(lorem)
|
||||
run.AddBreak()
|
||||
|
||||
if err := doc.Validate(); err != nil {
|
||||
log.Fatalf("error during validation: %s", err)
|
||||
}
|
||||
doc.SaveToFile("line-spacing.docx")
|
||||
}
|
@ -28,14 +28,25 @@ func (p ParagraphProperties) X() *wml.CT_PPr {
|
||||
}
|
||||
|
||||
// SetSpacing sets the spacing that comes before and after the paragraph.
|
||||
// Deprecated: See Spacing() instead which allows finer control.
|
||||
func (p ParagraphProperties) SetSpacing(before, after measurement.Distance) {
|
||||
p.x.Spacing = wml.NewCT_Spacing()
|
||||
if p.x.Spacing == nil {
|
||||
p.x.Spacing = wml.NewCT_Spacing()
|
||||
}
|
||||
p.x.Spacing.BeforeAttr = &sharedTypes.ST_TwipsMeasure{}
|
||||
p.x.Spacing.BeforeAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(before / measurement.Twips))
|
||||
p.x.Spacing.AfterAttr = &sharedTypes.ST_TwipsMeasure{}
|
||||
p.x.Spacing.AfterAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(after / measurement.Twips))
|
||||
}
|
||||
|
||||
// Spacing returns the paragraph spacing settings.
|
||||
func (p ParagraphProperties) Spacing() ParagraphSpacing {
|
||||
if p.x.Spacing == nil {
|
||||
p.x.Spacing = wml.NewCT_Spacing()
|
||||
}
|
||||
return ParagraphSpacing{p.x.Spacing}
|
||||
}
|
||||
|
||||
// SetAlignment controls the paragraph alignment
|
||||
func (p ParagraphProperties) SetAlignment(align wml.ST_Jc) {
|
||||
if align == wml.ST_JcUnset {
|
||||
|
64
document/paragraphspacing.go
Normal file
64
document/paragraphspacing.go
Normal file
@ -0,0 +1,64 @@
|
||||
// Copyright 2018 Baliance. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by the terms of the Affero GNU General
|
||||
// Public License version 3.0 as published by the Free Software Foundation and
|
||||
// appearing in the file LICENSE included in the packaging of this file. A
|
||||
// commercial license can be purchased by contacting sales@baliance.com.
|
||||
|
||||
package document
|
||||
|
||||
import (
|
||||
"baliance.com/gooxml"
|
||||
"baliance.com/gooxml/measurement"
|
||||
"baliance.com/gooxml/schema/soo/ofc/sharedTypes"
|
||||
"baliance.com/gooxml/schema/soo/wml"
|
||||
)
|
||||
|
||||
// ParagraphSpacing controls the spacing for a paragraph and its lines.
|
||||
type ParagraphSpacing struct {
|
||||
x *wml.CT_Spacing
|
||||
}
|
||||
|
||||
// SetBefore sets the spacing that comes before the paragraph.
|
||||
func (p ParagraphSpacing) SetBefore(before measurement.Distance) {
|
||||
p.x.BeforeAttr = &sharedTypes.ST_TwipsMeasure{}
|
||||
p.x.BeforeAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(before / measurement.Twips))
|
||||
}
|
||||
|
||||
// SetAfter sets the spacing that comes after the paragraph.
|
||||
func (p ParagraphSpacing) SetAfter(after measurement.Distance) {
|
||||
p.x.AfterAttr = &sharedTypes.ST_TwipsMeasure{}
|
||||
p.x.AfterAttr.ST_UnsignedDecimalNumber = gooxml.Uint64(uint64(after / measurement.Twips))
|
||||
}
|
||||
|
||||
// SetLineSpacing sets the spacing between lines in a paragraph.
|
||||
func (p ParagraphSpacing) SetLineSpacing(d measurement.Distance, rule wml.ST_LineSpacingRule) {
|
||||
if rule == wml.ST_LineSpacingRuleUnset {
|
||||
p.x.LineRuleAttr = wml.ST_LineSpacingRuleUnset
|
||||
p.x.LineAttr = nil
|
||||
} else {
|
||||
p.x.LineRuleAttr = rule
|
||||
p.x.LineAttr = &wml.ST_SignedTwipsMeasure{}
|
||||
p.x.LineAttr.Int64 = gooxml.Int64(int64(d / measurement.Twips))
|
||||
}
|
||||
}
|
||||
|
||||
// SetBeforeAuto controls if spacing before a paragraph is automatically determined.
|
||||
func (p ParagraphSpacing) SetBeforeAuto(b bool) {
|
||||
if b {
|
||||
p.x.BeforeAutospacingAttr = &sharedTypes.ST_OnOff{}
|
||||
p.x.BeforeAutospacingAttr.Bool = gooxml.Bool(true)
|
||||
} else {
|
||||
p.x.BeforeAutospacingAttr = nil
|
||||
}
|
||||
}
|
||||
|
||||
// SetAfterAuto controls if spacing after a paragraph is automatically determined.
|
||||
func (p ParagraphSpacing) SetAfterAuto(b bool) {
|
||||
if b {
|
||||
p.x.AfterAutospacingAttr = &sharedTypes.ST_OnOff{}
|
||||
p.x.AfterAutospacingAttr.Bool = gooxml.Bool(true)
|
||||
} else {
|
||||
p.x.AfterAutospacingAttr = nil
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user