Aspose Demos > Java Components > Aspose.Slides for Java > Java > Create from Template
Aspose.Slides Demos
Important: To produce PowerPoint presentations and PDF documents, the machine to run Aspose.Slides and Aspose.PDF doesn't need to have Microsoft PowerPoint or Adobe Acrobat installed. However, to view the contents of PowerPoint presentations and PDF documents produced by demos, the machine to view them does need a Microsoft PowerPoint or Adobe Acrobat Reader.

Northwind Demo
Generates a PPT or PDF file by utilizing user input and the Northwind database

Presenter Name:
Reporting Location:
Reporting Year:
Reporting Month:
Company Website:
Company Name:
Company Address:
Company City: State: Zip:
Company Picnic Date:
Important: To produce PowerPoint presentations and PDF documents, the machine to run Aspose.Slides and Aspose.PDF doesn't need to have Microsoft PowerPoint or Adobe Acrobat installed. However, to view the contents of PowerPoint presentations and PDF documents produced by demos, the machine to view them does need a Microsoft PowerPoint or Adobe Acrobat Reader.

Click the button below to see how demo creates presentation based ontemplate presentation file .

JAVA

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

package com.aspose.slides.demos.template.jsf;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.util.Date;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aspose.slides.Placeholders;
import com.aspose.slides.Presentation;
import com.aspose.slides.Shapes;
import com.aspose.slides.Slides;
import com.aspose.slides.TextFrame;
import com.aspose.slides.TextHolder;

public class CreateFromTemplate extends BaseDemo{

    public Boolean execute() {
        try {
            initLicense(new com.aspose.slides.License());

            String path = "/resources/";

            context = FacesContext.getCurrentInstance();
            request = (HttpServletRequest) context.getExternalContext().getRequest();
            response = (HttpServletResponse) context.getExternalContext().getResponse();
            Presentation pres = new Presentation(getFile(request, path + "demo.ppt"));
            Slides slides = pres.getSlides();
            for (int i=0; i<slides.size(); i++)
            {
                // Set text of standard slide placeholders
                Placeholders pholders = slides.get(i).getPlaceholders();
                for (int j=0; j<pholders.size(); j++)
                {
                    TextHolder th = (TextHolder) (pholders.get(j) instanceof TextHolder? pholders.get(j): null);
                    if (th != null)
                    {
                        if (th.getText().equals("H1"))
                            th.getParagraphs().get(0).getPortions().get(0).setText("Demo Presentation");
                        else if (th.getText().equals("SH1"))
                            th.getParagraphs().get(0).getPortions().get(0).setText("Aspose.Slides.Template");
                        else if (th.getText().equals("T1"))
                        {
                            switch (i)
                            {
                                case 1:
                                    th.getParagraphs().get(0).getPortions().get(0).setText("With Aspose.Slides you can: Open, read and save Microsoft PowerPoint presentations.");
                                    break;
                                case 2:
                                    th.getParagraphs().get(0).getPortions().get(0).setText("With Aspose.Slides you can: Change standard text placeholders as well as custom text frames.");
                                    break;
                                case 3:
                                    th.getParagraphs().get(0).getPortions().get(0).setText("With Aspose.Slides you can: Replace background pictures with any picture from presentation or new picture.");
                                    break;
                            }
                        }
                    }
                }

                // Set text for text frames
                Shapes shapes = slides.get(i).getShapes();
                for (int j=0; j<shapes.size(); j++)
                {
                    TextFrame tf = shapes.get(j).getTextFrame();
                    if (tf != null)
                    {
                        if (tf.getText().equals("CT1"))
                            tf.getParagraphs().get(0).getPortions().get(0).setText("Created on:");
                        else if (tf.getText().equals("CT2"))
                            tf.getParagraphs().get(0).getPortions().get(0).setText((new Date(System.currentTimeMillis())).toString());
                        else if (tf.getText().equals("P1"))
                        {
                            tf.getParagraphs().get(0).getPortions().get(0).setText("Slide " + (i+1));
                        }
                    }
                }
            }

//            try 
//            {
//                pres.SaveToPdf(MapPath(".") + "\\demoppt.pdf");
//            }
//            catch (Exception ex)
//            {
//                StreamWriter sr = File.CreateText(MapPath(".") + "\\demoppt.lst");
//                sr.WriteLine(ex.Message);
//                sr.WriteLine(ex.StackTrace);
//            }

            //
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            pres.write(out);
            setResponse("demo_new.ppt", out);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}

JAVA

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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

package com.aspose.slides.demos.template.jsf;

import java.io.Serializable;

import java.io.*;
import java.util.*;
import java.net.URL;

import javax.servlet.*;
import javax.servlet.http.*;

import javax.faces.context.FacesContext;

///////////////////////
import java.text.SimpleDateFormat;
import com.icesoft.faces.context.ByteArrayResource;
import com.icesoft.faces.context.Resource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Locale;

import java.lang.reflect.*;

public abstract class BaseDemo
{
    protected FacesContext context;
    protected HttpServletRequest request;
    protected HttpServletResponse response;

    // Default Constructor
    public BaseDemo() {
    }
   
    public abstract Boolean execute();

    protected String getFileName(HttpServletRequest request, String absoluteName) 
        throws java.io.FileNotFoundException
    {
        URL url = getClass().getClassLoader().getResource(absoluteName);
        if (null==url && null!=request)
        {
            String path = 
                request.getRealPath(absoluteName).replace("\\", "/");

            System.err.println("Failed to load " + absoluteName);
            System.err.println("Loading file from " + path);

            return path;
        }

        String path = new File(url.getPath()).getAbsolutePath();
        //return url.toString();
        return path;
    }

    protected InputStream getFile(HttpServletRequest request, String absoluteName) 
        throws java.io.FileNotFoundException
    {
        InputStream is = getClass().getClassLoader().getResourceAsStream(absoluteName);
        if (null==is && null!=request)
        {
            String path = 
                request.getRealPath(absoluteName).replace("\\", "/");

            //System.err.println("Failed to load " + absoluteName);
            //System.err.println("Loading file from " + path);
            is = new FileInputStream(path);

            if (null==is) {
                System.err.println("getFile NULL: " + path);
            }
        }

        return is;
    }

    public String generate() {

        context = FacesContext.getCurrentInstance();
        request = (HttpServletRequest) context.getExternalContext().getRequest();
        response = (HttpServletResponse) context.getExternalContext().getResponse();

        try
        {
            if (execute())
            {
                // we should NOT use it for ICEFaces
                //context.responseComplete();
            }
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return "stay";
    }

    //////////////////////////////////////////////////////////
    protected final String FILE_NAME = "result.ppt";

    public Resource getGeneratedfile() {
        byte[] res = getResult();
        if (null==res || res.length<1) {
            return null;
        }
        return new ByteArrayResource(res);
    }

    private String fileName = FILE_NAME;
    public String getFilename() {
        return fileName;
    }

    private String respType = "application/powerpoint";
    public String getResptype() {
        return respType;
    }
    public void setResptype(String v) {
        respType = v;
    }

    public void setFilename(String v) {

        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MMM-dd_hh_mm_ss-", Locale.US);
        String curDate = formatter.format(new Date());

        this.fileName = curDate + v;
    }

    public  byte[] getResult()
    {
        return resp;
    }

    protected void setResponse(String fName, ByteArrayOutputStream out) throws IOException {
        setFilename(fName);
        resp = out.toByteArray();
        out.close();
        // outputStream.write(this.response);
    }
    protected byte[] resp;


    /**
     *
     * Initializes license (if demo license is available).
     * you may insert your licensing code here that would use obj's
     * setLicense based on InputStream rather than passing it on
     * to 3rd-party class.
     *
     */
    public static void initLicense(Object obj) {
        try {

            Class myclass = Class.forName("com.aspose.demos.Common");
            if (null==myclass) {
                System.out.println("initLicense not found");
                return ;
            }

            //Use reflection to list methods and invoke them
            Method[] methods = myclass.getMethods();
            Object object = myclass.newInstance();

            Class partypes[] = new Class[1];
            partypes[0] = Object.class;
           
            Method meth = myclass.getMethod("initLicense", partypes);
            if (null!=meth) {

                    //System.out.println("Calling " + meth.getName());
                    Object arglist[] = new Object[1];
                    arglist[0] = obj;

                    meth.invoke(object, arglist);
            }
        } catch (ClassNotFoundException cnfe) {
            // Ignore
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

XHTML

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ice="http://www.icesoft.com/icefaces/component"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jstl/core">
<ui:composition
    template="/WEB-INF/includes/templates/page-template.xhtml">
    <ui:define name="pageTitle">
        Aspose.Slides Demos
    </ui:define>    
    <ui:define name="page-content">     
        <ui:decorate
            template="/WEB-INF/includes/templates/tabbed_container.xhtml">
            <ui:define name="example">
                <div class="componentDescriptionTxt"><b>Important:</b> To
                produce PowerPoint presentations and PDF documents, the machine to
                run Aspose.Slides and Aspose.PDF doesn't need to have Microsoft
                PowerPoint or Adobe Acrobat installed. However, to view the contents
                of PowerPoint presentations and PDF documents produced by demos, the
                machine to view them does need a Microsoft PowerPoint or Adobe
                Acrobat Reader.
                <p class="componentDescriptionTxt">Click the button below to see
                how demo creates presentation based on <a href="demo.ppt">template
                presentation file</a>.</p>
                </div>
                    <h:commandButton id="button1"
                        value="Click here to create a presentation"
                        action="#{createFromTemplate.generate}"></h:commandButton>
                <ice:outputResource 
                    id="outResource"
                    mimeType="application/powerpoint"
                    resource="#{createFromTemplate.generatedfile}"
                    fileName="#{createFromTemplate.filename}"
                    shared="false" />
            </ui:define>
        </ui:decorate>
    </ui:define>
</ui:composition>
</html>