Skip to content

Instantly share code, notes, and snippets.

@benheymink
Created April 10, 2024 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benheymink/2b06426c3d9374474775c080d40ec00a to your computer and use it in GitHub Desktop.
Save benheymink/2b06426c3d9374474775c080d40ec00a to your computer and use it in GitHub Desktop.
docx4j pptx loading
PresentationMLPackage presentationMLPackage =
(PresentationMLPackage) OpcPackage.load(new java.io.File("src/resources/testPresentation.pptx"));
System.out.printf("Slide count: %d", presentationMLPackage.getMainPresentationPart().getSlideCount());
MainPresentationPart mdp = presentationMLPackage.getMainPresentationPart();
String notesFromSlide = mdp.getSlide(0).getNotesSlidePart().getContents().toString();
System.out.printf("Slide 1 notes: %s", notesFromSlide);
@plutext
Copy link

plutext commented Apr 10, 2024

getNotesSlidePart() won't create the Notes part; it will return null if there isn't a notes part in the deck.

So you have to check for null.

There is method createNotesSlidePart(NotesMasterPart nmp)

@benheymink
Copy link
Author

@plutext Sorry, should have been clearer - in this example some notes/content already exist, I'm simply trying to read them (and evenutally update them). I naively assumed the getContents call would return the string contents, but I think i'm missing a step around wrangling the XML that method returns

@plutext
Copy link

plutext commented Apr 10, 2024

Instead of getContents, try getXML()

@benheymink
Copy link
Author

Yes, that works (returns the XML). I'd then need to parse that XML correctly, I was looking for a lovely convenience feature to handle all of that for me :-) - i.e to get just the text contents of the notes, or to update the text of the slide notes. I'll keep digging...

(Thanks for the help btw!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment