All Downloads are FREE. Search and download functionalities are using the official Maven repository.

pro.verron.officestamper.experimental.PowerpointStamper Maven / Gradle / Ivy

Go to download

Office-stamper is a Java template engine for docx documents, forked from org.wickedsource.docx-stamper

The newest version!
package pro.verron.officestamper.experimental;

import org.docx4j.dml.CTRegularTextRun;
import org.docx4j.dml.CTTextParagraph;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.PresentationMLPackage;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import pro.verron.officestamper.api.OfficeStamper;
import pro.verron.officestamper.api.OfficeStamperException;
import pro.verron.officestamper.core.Placeholders;

import java.io.OutputStream;
import java.util.List;

/**
 * The PowerpointStamper class implements the OfficeStamper interface
 * to provide functionality for stamping Powerpoint presentations with
 * context and writing the result to an OutputStream.
 */
public class PowerpointStamper
        implements OfficeStamper {

    @Override
    public void stamp(
            PresentationMLPackage template,
            Object context,
            OutputStream outputStream
    )
            throws OfficeStamperException {
        Class ctTextParagraphClass = CTTextParagraph.class;
        List ctTextParagraphs = PowerpointCollector.collect(template,
                ctTextParagraphClass);
        for (CTTextParagraph paragraph : ctTextParagraphs) {
            PowerpointParagraph paragraph1 = new PowerpointParagraph(
                    paragraph);
            String string = paragraph1.asString();
            for (var variable : Placeholders.findVariables(string)) {
                var replacement = new CTRegularTextRun();
                var evaluationContext = new StandardEvaluationContext(context);
                var parserConfiguration = new SpelParserConfiguration();
                var parser = new SpelExpressionParser(parserConfiguration);
                var expression = parser.parseExpression(variable.content());
                var value = expression.getValue(evaluationContext);

                replacement.setT((String) value);
                paragraph1.replace(variable, replacement);
            }

        }
        try {
            template.save(outputStream);
        } catch (Docx4JException e) {
            throw new OfficeStamperException(e);
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy