Hi,
I am not able to use Aspose slides with MS powerpoint,it well works fine with Open Office.Attached is the sample working code.Can any body provide me the complete code where i can render an image which resides in my server to Aspose Slides using java servlets.
Please do help me atleast in Modifying the attached code.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpSession;
import com.alphablox.blox.repository.Log;
import com.alphablox.blox.ChartBlox;
import com.alphablox.blox.PresentBlox;
import com.alphablox.blox.uimodel.PresentBloxModel;
import com.alphablox.blox.uimodel.core.Static;
import com.alphablox.server.Server;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.awt.Color;
import com.aspose.slides.*;
import java.net.URL;
public class PptServlet extends HttpServlet {
public void destroy () {
super.destroy ();
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
Presentation pres;
FileInputStream source = null;
FileOutputStream destination = null;
PrintStream dataOut = null;
DataInputStream dataIn;
try
{
// Log.sendMessage(Log.MESSAGE_LEVEL_DEBUG, "inside try", " ");
//determine title of image
PresentBlox pb = null;
if (request.getParameter("batch") != null)
{
String batch = (String) request.getParameter("batch");
if (batch.equalsIgnoreCase("TRUE"))
{
pb = (PresentBlox) session.getAttribute("batchReportBlox");
}
else
{
pb = (PresentBlox) session.getAttribute("mainReportBlox");
}
}
else
{
pb = (PresentBlox) session.getAttribute("mainReportBlox");
}
ChartBlox chart = pb.getChartBlox();
String oldTitle = chart.getTitle();
PresentBloxModel model = pb.getPresentBloxModel();
Static titleBlox = (Static) model.searchForComponent("customTitle");
if (titleBlox != null)
{
chart.setTitle(titleBlox.getTitle() + "\\n" + oldTitle);
}
//create temp file
String timeStamp = new java.util.Date().getTime() + "";
String filePath = Server.getServer().getRepositoryRoot() + "temp" + File.separatorChar + session.getId() + File.separatorChar;
String fileName = filePath + timeStamp + "temp";
try
{
chart.writeChartToFile(fileName, 800, 600, com.alphablox.blox.form.FormBlox.RENDER_DHTML);
fileName += ".png";
}
catch (Exception e)
{
String pathFrom = Server.getServer().getRepositoryRoot() + File.separatorChar + "temp.png";
fileName += ".png";
String pathTo = fileName;
copy(pathFrom, pathTo);
}
ServletContext context = request.getSession().getServletContext();
String realContextPath = context.getRealPath(request.getContextPath());
int pos = realContextPath.lastIndexOf("\\");
realContextPath = realContextPath.substring(0, pos);
String pptPath = realContextPath + "\\" + "demo.ppt";
String destPath = realContextPath + "\\" + "demo_new.ppt";
Log.sendMessage(Log.MESSAGE_LEVEL_DEBUG, "pptServlet.java", "pptPath: " + pptPath);
source = new FileInputStream(pptPath);
destination = new FileOutputStream(destPath);
pres = new Presentation(source);
Slide slide = pres.getSlideByPosition(1);
slide.setFollowMasterBackground(false);
slide.getBackground().getFillFormat().setType(FillType.PICTURE);
InputStream iStream = new BufferedInputStream(new FileInputStream(fileName));
Picture pic = new com.aspose.slides.Picture(pres, iStream);
int picId = pres.getPictures().add(pic);
slide.getBackground().getFillFormat().setPictureId(picId);
pres.write(destination);
source.close();
destination.close();
String servletPath = request.getRequestURL().toString();
pos = servletPath.lastIndexOf("/");
String pptDemoPath = servletPath.substring(0, pos) + "/" + "demo_new.ppt";
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/vnd.ms-powerpoint");
response.addHeader("Content-Disposition", "attachment; filename=demo_new.ppt");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
URL url = new URL(pptDemoPath);
// Use Buffered Stream for reading/writing.
bis = new BufferedInputStream(url.openStream());
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
// Simple read/write loop.
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bis.close();
bos.flush();
bos.close();
response.flushBuffer();
}
catch (Exception e)
{
Log.sendMessage(Log.MESSAGE_LEVEL_DEBUG, "pptServlet.java", "Exception: " + e.getMessage().toString());
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
private void copy(String src, String dst) throws IOException
{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
out.close();
}
public void init (ServletConfig config) throws ServletException
{
System.out.println("hi init");
super.init(config);
System.out.println("hi init");
}
}
Thanks®ards,
Rajesh