-
Notifications
You must be signed in to change notification settings - Fork 45
[US-1037] Header Footer Example Code #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8ef5d3f
added example code taken from documentation
kellemNegasi b0bfd53
updated example name
kellemNegasi dd08548
changed example name
kellemNegasi 11d5849
skipped slide-size.pptx file
kellemNegasi 8d622d7
revert skip for slide-size and fix error logging
kellemNegasi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,4 +3,5 @@ chart.pptx | |
| lists.pptx | ||
| picture.pptx | ||
| several_slides.pptx | ||
| table.pptx | ||
| table.pptx | ||
| slide-size.pptx | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
|
|
||
| "github.com/unidoc/unioffice/v2/common" | ||
| "github.com/unidoc/unioffice/v2/common/license" | ||
| "github.com/unidoc/unioffice/v2/document" | ||
| "github.com/unidoc/unioffice/v2/measurement" | ||
| "github.com/unidoc/unioffice/v2/schema/soo/ofc/sharedTypes" | ||
| "github.com/unidoc/unioffice/v2/schema/soo/wml" | ||
| ) | ||
|
|
||
| func init() { | ||
| // Make sure to load your metered License API key prior to using the library. | ||
| // If you need a key, you can sign up and create a free one at https://cloud.unidoc.io | ||
| err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| } | ||
|
|
||
| var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." | ||
|
|
||
| func main() { | ||
| doc := document.New() | ||
| defer doc.Close() | ||
|
|
||
| // Adding content | ||
| for i := 0; i < 25; i++ { | ||
| doc.AddParagraph().AddRun().AddText(text) | ||
| } | ||
|
|
||
| // Getting the image | ||
| img, err := common.ImageFromFile("gophercolor.png") | ||
| if err != nil { | ||
| log.Fatalf("unable to create image: %s", err) | ||
| } | ||
|
|
||
| // Checking if there is a header | ||
| hdr, ok := doc.BodySection().GetHeader(wml.ST_HdrFtrDefault) | ||
| if !ok { | ||
| // If not, creating it | ||
| hdr = doc.AddHeader() | ||
| doc.BodySection().SetHeader(hdr, wml.ST_HdrFtrDefault) | ||
| } | ||
|
|
||
| // Main header with an image | ||
| hdrPara := hdr.AddParagraph() | ||
| hdrRun := hdrPara.AddRun() | ||
| hdrRun.AddText("Main Document Title") | ||
| hdrRun.AddBreak() | ||
|
|
||
| iref, err := hdr.AddImage(img) | ||
| if err != nil { | ||
| log.Fatalf("unable to add image to header: %s", err) | ||
| } | ||
| imgInl, _ := hdrPara.AddRun().AddDrawingInline(iref) | ||
| imgInl.SetSize(0.5*measurement.Inch, 0.5*measurement.Inch) | ||
|
|
||
| // Even header | ||
| evenHdr := doc.AddHeader() | ||
| evenHdr.AddParagraph().AddRun().AddText("Even Header") | ||
| doc.BodySection().SetHeader(evenHdr, wml.ST_HdrFtrEven) | ||
|
|
||
| // Odd header | ||
| oddHdr := doc.AddHeader() | ||
| oddHdr.AddParagraph().AddRun().AddText("Odd Header") | ||
| doc.BodySection().SetHeader(oddHdr, wml.ST_HdrFtrDefault) | ||
|
|
||
| // Set EvenAndOddHeaders flag | ||
| boolTrue := true | ||
| doc.Settings.X().EvenAndOddHeaders = &wml.CT_OnOff{ | ||
| ValAttr: &sharedTypes.ST_OnOff{Bool: &boolTrue}, | ||
| } | ||
|
|
||
| // Add Footer | ||
| ftr := doc.AddFooter() | ||
| ftrPara := ftr.AddParagraph() | ||
| ftrPara.Properties().AddTabStop(6*measurement.Inch, wml.ST_TabJcRight, wml.ST_TabTlcNone) | ||
| ftrRun := ftrPara.AddRun() | ||
| ftrRun.AddText("Some subtitle goes here") | ||
| ftrRun.AddTab() | ||
| ftrRun.AddText("Pg ") | ||
| ftrRun.AddField(document.FieldCurrentPage) | ||
| ftrRun.AddText(" of ") | ||
| ftrRun.AddField(document.FieldNumberOfPages) | ||
| doc.BodySection().SetFooter(ftr, wml.ST_HdrFtrDefault) | ||
| doc.BodySection().SetFooter(ftr, wml.ST_HdrFtrEven) | ||
|
|
||
| // Save the file | ||
| if err := doc.SaveToFile("combined-header-footer.docx"); err != nil { | ||
| fmt.Println(err) | ||
anovik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.