main.go hosted by Oembed Proxy for GitHub
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
// Copyright 2017 FoxyUtils ehf. All rights reserved.
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/unidoc/unioffice/common"
	"github.com/unidoc/unioffice/common/license"
	"github.com/unidoc/unioffice/schema/soo/dml"
	"github.com/unidoc/unioffice/schema/soo/pml"

	"github.com/unidoc/unioffice/presentation"
)

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)
	}
}

func main() {
	startTime := time.Now()

	// Start building pptx
	ppt, err := presentation.OpenTemplate("template.potx")
	if err != nil {
		fmt.Println("presentation.OpenTemplate err ", err)
		os.Exit(1)
	}
	defer ppt.Close()

	// Clear out example slides
	for _, s := range ppt.Slides() {
		if err = ppt.RemoveSlide(s); err != nil {
			fmt.Println("ppt.RemoveSlide err ", err)
			os.Exit(1)
		}
	}

	// Add new slide from template
	layout, err := ppt.GetLayoutByName("Picture with Caption")
	if err != nil {
		fmt.Println("ppt.GetLayoutByName err ", err)
		os.Exit(1)
	}

	// Add local image to pptx
	image, err := common.ImageFromFile("gophercolor.png")
	if err != nil {
		fmt.Println("common.ImageFromFile err ", err)
		os.Exit(1)
	}

	iRef, err := ppt.AddImage(image)
	if err != nil {
		fmt.Println("ppt.AddImage err ", err)
		os.Exit(1)
	}

	slide, err := ppt.AddDefaultSlideWithLayout(layout)
	if err != nil {
		fmt.Println("ppt.AddDefaultSlideWithLayout err ", err)
		os.Exit(1)
	}

	// Inject content into placeholders
	title, _ := slide.GetPlaceholder(pml.ST_PlaceholderTypeTitle)
	title.SetText("New title")

	body, _ := slide.GetPlaceholder(pml.ST_PlaceholderTypeBody)
	body.SetText("New body text")

	imageRelID := slide.AddImageToRels(iRef)

	pic, err := slide.GetPlaceholder(pml.ST_PlaceholderTypePic)
	if err != nil {
		fmt.Println("ppt.AddImage err ", err)
		os.Exit(1)
	}

	spPr := dml.NewCT_ShapeProperties()
	spPr.BlipFill = dml.NewCT_BlipFillProperties()
	spPr.BlipFill.Blip = dml.NewCT_Blip()
	spPr.BlipFill.Blip.EmbedAttr = &imageRelID
	spPr.BlipFill.Stretch = dml.NewCT_StretchInfoProperties() // stretch to parent block with default values

	pic.X().SpPr = spPr

	if err := ppt.Validate(); err != nil {
		fmt.Println("ppt.Validate err ", err)
	}

	if err := ppt.SaveToFile("mod.pptx"); err != nil {
		fmt.Println("ppt.SaveToFile err ", err)
	}

	duration := time.Now().Sub(startTime).Seconds()
	fmt.Println("success! took ", duration, " seconds")
}