Setting pptx Theme in Java

Setting pptx Theme in Java

I am trying to merge some pptx documents programmatically using java. I figured out how to do this in essence using Apache POI but the documents I am trying to merge do not work.

I am trying to merge some pptx documents programmatically using java. I figured out how to do this in essence using Apache POI but the documents I am trying to merge do not work.

After significant searching and trial and error I figured out that the reason for this is that the pptx documents do not have theme information (i.e., if I click into powerpoint and check the slide master view it's blank). If I goto the themes in the Design Ribbon and select 'office theme' or another theme then save. the files will merge charmingly. Otherwise, I run into the following error:

Exception in thread "main" java.lang.IllegalArgumentException: Failed to fetch default style for otherStyle and level=0
    at org.apache.poi.xslf.usermodel.XSLFTextParagraph.getDefaultMasterStyle(XSLFTextParagraph.java:1005)
    at org.apache.poi.xslf.usermodel.XSLFTextParagraph.fetchParagraphProperty(XSLFTextParagraph.java:1029)
    at org.apache.poi.xslf.usermodel.XSLFTextParagraph.isBullet(XSLFTextParagraph.java:654)
    at org.apache.poi.xslf.usermodel.XSLFTextParagraph.copy(XSLFTextParagraph.java:1044)
    at org.apache.poi.xslf.usermodel.XSLFTextShape.copy(XSLFTextShape.java:631)
    at org.apache.poi.xslf.usermodel.XSLFSheet.appendContent(XSLFSheet.java:358)
    at com.apsiva.main.Snippet.main(Snippet.java:28)

The following is the code I ran:

package com.apsiva.main;

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;

import org.apache.poi.xslf.usermodel.SlideLayout; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFSlide; import org.apache.poi.xslf.usermodel.XSLFSlideLayout;

public class Snippet { /** Merge the pptx files in the array <decks> to the desired destination * chosen in <outputPath> */ public static void main(String[] args) { try { FileInputStream empty = new FileInputStream("C:/Users/Alex/workspace/OutputWorker/tmp/base2.pptx"); XMLSlideShow pptx;

            pptx = new XMLSlideShow(empty);
            XSLFSlideLayout defaultLayout = pptx.getSlideMasters()[0].getLayout(SlideLayout.TITLE_AND_CONTENT);

            FileInputStream is = new FileInputStream("C:/Users/Alex/workspace/OutputWorker/tmp/noWork.pptx");

// FileInputStream is = new FileInputStream("C:/Users/Alex/workspace/OutputWorker/tmp/works2.pptx"); XMLSlideShow src = new XMLSlideShow(is); is.close(); for (XSLFSlide srcSlide: src.getSlides()){ pptx.createSlide(defaultLayout).appendContent(srcSlide); } FileOutputStream out = new FileOutputStream("C:/POI-TEST-OUTPUT.pptx"); pptx.write(out); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

I want to get these files to merge and I believe the solution is to programmatically assign the theme to the files. How can it be done?

Thank you for your consideration!

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Java Fundamentals: Learn Java for absolute beginners |Simpliv

Java Fundamentals: Learn Java for absolute beginners

Java Essentials : Learn Core Java From Basic to Advance

Learn Java Programming Using Practical Assignments. Start Building Back-end Web Applications Robust Test Automation Frameworks By End Of The Course. Learn More!

Fundamentos de Java: Aprende Java desde cero, sin misterios | Simpliv

Fundamentos de Java: Aprende Java desde cero, sin misterios