Copy and Reorder Slides
CopySlide takes a slide from any open presentation and appends it to this one.
MoveSlide then puts it where it belongs, because a copy always lands last.
Between them they cover assembling a deck from parts: a standard title slide
from one file, content from another, a fixed closing slide from a third.
| Call | Effect |
|---|---|
pptTo.CopySlide(s) | Deep-copies slide s and appends it as the last slide. |
pptTo.MoveSlide(from, to) | Moves the slide at index from to index to. Both zero-based. |
Copying between presentations
pptFrom, err := presentation.Open("source.pptx")
if err != nil {
return err
}
defer pptFrom.Close()
pptTo, err := presentation.Open("extract.pptx")
if err != nil {
return err
}
defer pptTo.Close()
if _, err := pptTo.CopySlide(pptFrom.Slides()[0]); err != nil {
return err
}
if err := pptTo.MoveSlide(len(pptTo.Slides())-1, 1); err != nil {
return err
}
pptTo.SaveToFile("output.pptx")Both files stay open until the copy is done, since CopySlide reads from the
source presentation rather than from a detached slide value. The slide XML is
marshalled and unmarshalled, so the copy is independent of the original and
editing one does not affect the other.
If the source slide’s layout is not already in the destination, CopySlide
brings it across along with the layout’s images, registers it on the destination
master, and points the new slide at it. That is why a copied slide keeps its
design instead of falling back to whatever the destination’s first layout looks
like.
Passing a slide from the same presentation duplicates it. The layout lookup finds the layout already present, so only the slide part is copied.
Moving slides
MoveSlide indexes the slide list, zero-based, and returns an error when either
argument is outside it. Moving a slide to its own position is a no-op rather
than an error. The example moves the freshly copied slide to index 1, making it
the second slide, not the first.
Positions are evaluated against the list as it is when you call, so moving several slides means recomputing indices after each call rather than planning them all up front.
Limitations
Only the layout’s relationships are carried onto the copied slide. The source
slide’s own relationships are not, so a picture, chart or hyperlink placed
directly on the slide being copied loses the part it points at. Slides whose
content comes entirely from the layout and from text copy cleanly. This is read
from CopySlide in the library source rather than observed, so verify against
your own decks before relying on it.
Speaker notes attached to the source slide are not copied either.
Slides() returns fresh Slide values each call, and CopySlide wants one of
those values, not an index. Calling len(pptTo.Slides())-1 immediately after a
copy is the reliable way to name the slide that was just added.
Run the example
The example copies the first slide of source.pptx into extract.pptx, moves
it into second position and writes output.pptx.
git clone https://github.com/unidoc/unioffice-examples.git
cd unioffice-examples/presentation/copy-reorder-slide
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.