2017-08-28 20:56:18 -05:00
// Copyright 2017 Baliance. All rights reserved.
package main
import (
2018-01-24 18:59:09 -06:00
"log"
"baliance.com/gooxml/common"
2017-08-28 20:56:18 -05:00
"baliance.com/gooxml/document"
"baliance.com/gooxml/measurement"
2017-09-23 08:28:52 -05:00
"baliance.com/gooxml/schema/soo/wml"
2017-08-28 20:56:18 -05:00
)
func main ( ) {
doc := document . New ( )
2018-01-24 18:59:09 -06:00
img , err := common . ImageFromFile ( "gophercolor.png" )
if err != nil {
log . Fatalf ( "unable to create image: %s" , err )
}
2017-08-28 20:56:18 -05:00
hdr := doc . AddHeader ( )
2018-01-24 18:59:09 -06:00
// We need to add a reference of the image to the header instead of to the
// document
iref , err := hdr . AddImage ( img )
if err != nil {
log . Fatalf ( "unable to to add image to document: %s" , err )
}
2017-08-28 20:56:18 -05:00
para := hdr . AddParagraph ( )
2017-08-31 19:28:39 -05:00
para . Properties ( ) . AddTabStop ( 2.5 * measurement . Inch , wml . ST_TabJcCenter , wml . ST_TabTlcNone )
2017-08-28 20:56:18 -05:00
run := para . AddRun ( )
run . AddTab ( )
run . AddText ( "My Document Title" )
2018-01-24 18:59:09 -06:00
imgInl , _ := para . AddRun ( ) . AddDrawingInline ( iref )
imgInl . SetSize ( 1 * measurement . Inch , 1 * measurement . Inch )
2017-08-28 20:56:18 -05:00
// Headers and footers are not immediately associated with a document as a
// document can have multiple headers and footers for different sections.
doc . BodySection ( ) . SetHeader ( hdr , wml . ST_HdrFtrDefault )
ftr := doc . AddFooter ( )
para = ftr . AddParagraph ( )
2017-08-31 19:28:39 -05:00
para . Properties ( ) . AddTabStop ( 6 * measurement . Inch , wml . ST_TabJcRight , wml . ST_TabTlcNone )
2017-08-28 20:56:18 -05:00
run = para . AddRun ( )
run . AddText ( "Some subtitle goes here" )
run . AddTab ( )
run . AddText ( "Pg " )
run . AddField ( document . FieldCurrentPage )
run . AddText ( " of " )
run . AddField ( document . FieldNumberOfPages )
doc . BodySection ( ) . SetFooter ( ftr , wml . ST_HdrFtrDefault )
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 `
for i := 0 ; i < 5 ; i ++ {
para = doc . AddParagraph ( )
run = para . AddRun ( )
run . AddText ( lorem )
}
doc . SaveToFile ( "header-footer.docx" )
}