Free Support Forum - aspose.com

Embed PDF in a Slide

Hi ,
I am new to aspose .I am trying to embed the PDF file in a slide a provide a PDF icon for the object in JAVA.

Is it possible to embed the PDF object in the slide? If yes ,please help me on how to embed the PDF file and provide PDF icon for it.for the ebmeded object.

Hi,

I also have the same query.

I have referred the following thread:

http://www.aspose.com/community/forums/thread/90842/addoleobjectframe-pdf-evaluation.aspx

I am using the following code:

//============
File pdfFile = new File(“c:\deskcal.pdf”);
FileInputStream pdfIS = new FileInputStream(pdfFile);
byte[] bytes = new byte[1024];
ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
int read = 0;
if (pdfIS != null) {
while ((read = pdfIS.read(bytes)) > 0) {
pdfOut.write(bytes, 0, read);
}
}
OleObjectFrame obj = slide.getShapes().addOleObjectFrame(1000,1000,1000,1000, “Adobe Acrobat 7.0 Document”,pdfOut.toByteArray());

File file = new File(“c:\modified.ppt”);
FileOutputStream fileOutputStream = new FileOutputStream(file);
pres.write(fileOutputStream);
fileOutputStream.close();
//===============

This code works fine in embedding excel sheets and word docs, but doesn’t work with pdf. When I open the ppt in MS Powerpoint 2003 and double click the object, i get the following error:

"The server application, source file, or item can’t be found, or returned an unknown error. You may need to reinstall the server application."

Please help, need a solution asap.

Thanks in advance.

Dear suvaraj,

It should be possible, but I did not have any success and need further investigation. However, if you have already embedded pdf document, you can change the inner pdf document with new one.

For example, say, you already have a emebedded pdf document on slide, with alternative text set as "mypdfdoc", then you will access it like this.

C#

Presentation srcPres=new Presentation("yourSource.ppt");

Slide sld=srcPres.GetSlideByPosition(1);

OleObjectFrame pdfDoc=sld.Shapes.FindShape("mypdfdoc") as OleObjectFrame;

//Read PDF document

FileStream fin = new FileStream("yournew.pdf", FileMode.Open, FileAccess.Read);

byte[] yourNewPdfDoc = new byte[fin.Length];

fin.Read(pdfDoc, 0, pdfDoc.Length);

fin.Close();

//Set it with your new doc

pdfDoc.ObjectData=yourNewPdfDoc ;

//write it on disk

srcPres.Write("yourTarget.ppt");

HI msfaiz,

Thanks for your suggestion .But we need to do it Java.I also tried doing it as what you have explaind .But I am getting a error message when i cilck on the embedded PDF availble in the PPT.

The error message which i am getting is “The server
application, source file, or item can’t be found, or returned an
unknown error. You may need to reinstall the server application.“

The code is similar to the code provided by the other Aspose user “ser”.

After adding to the addOleObjectFrame ,I even tried geeting the information from the OLE object using getObjectData() and stream it to a PDF file .I was able to open the PDF file sucessfully from the file system which was created using getObjectData().

Is there any think to do with OLE header as explained in the below link

http://www.aspose.com/community/forums/post/62360/adding-an-embedded-pdf-file-ole-container-header-question.aspx

THis is the code which i have tried.
try

{
Presentation pres = new Presentation(new FileInputStream(new File(“C:\demo1.ppt”)));

Slide slide = pres.getSlideByPosition(2);

File file=new File(“C:\a1.pdf”);


int length=(int)file.length();

FileInputStream fstro = new FileInputStream(file);


byte[] b = new byte[length];

fstro.read(b, 0, length);


OleObjectFrame oof = slide.getShapes().addOleObjectFrame(0,0,(int)pres.getSlideSize().getX(),

(int)pres.getSlideSize().getY(),”“Adobe Acrobat 7.0 Document””, b);

pres.write(new FileOutputStream(new File(“C:\modified.ppt”)));

byte[] b1 = new byte[oof.getObjectData().length];
b1=oof.getObjectData();
fstrw.write(b1);

}

catch(Exception ex)

{
ex.printStackTrace();
System.out.println(ex.toString());

}


Please help us.

Hello suvaraj,

addOleObjectFrame method can insert OLE2 object only so it should work for Word, Excel and other similar applications.

Unfortunately, PDF stored inside presentation as OLE1 Native object and addOleObjectFrame doesn’t work in this case.

Hi alcrus,
Thanks for the update.

Is there any way to embed the PDF file other than using the addOleObjectFrame method.

We even found that
addOleObjectFrame method in .Net supports the embeding the PDF.But how can we do it in java.


Are you sure, .Net version supports it? Actually, it should work in both versions.
But if it works then try latest Java 1.8.3 released yesterday.
It has the same OLE functionality like in .Net now.

Hi alcrus,
We tried with the new patch released by aspose. We are getting the below error message while
creating the object frame.Now we are getting error even for Excel embedding.

The java version we are using it 1.6


OleObjectFrame oof = slide.getShapes().addOleObjectFrame(200,200,200,

200,“Excel.Sheet.8”, b);

ERROR MESSAGE

Exception in thread “main” java.lang.NoClassDefFoundError: javax/media/jai/RenderedOp
at com.aspose.slides.OleObjectFrame.setObjectData(SourceFile:365)
at com.aspose.slides.Shapes.addOleObjectFrame(SourceFile:376)
at ppt.main(ppt.java:51)

You don’t have JAI installed.

Hi alcrus,

After adding the jai_codec and jai_code jar files we are able to excute the code.But now after even updating the new version of the aspose JAR file we are getting the same error message.

The Error message is

"The server application, source file, or item can't be found, or returned an unknown error. You may need to reinstall the server application."

Hi suvaraj,
Your problem seems to because of JAI unless there is some incompactibility issue between JAI and the new release.

Hi Faiz,
I tried manually embedding pdf and then modifiying it thru java code:
//=================
File file = new File(“c:\original.ppt”);
FileInputStream origFIS = new FileInputStream(file);
Presentation pres = new Presentation(origFIS);

Slide origSlide = pres.getSlideByPosition(1);
File pdfFile = new File(“c:\deskcal.pdf”);

FileInputStream pdfIS = new FileInputStream(pdfFile);

byte[] bytes = new byte[1024];

ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();


Shapes shapes = origSlide.getShapes();
Shape shape = null;
for (int i = 0; i < shapes.size(); i++) {
shape = shapes.get(i);
if (shape instanceof OleObjectFrame) {
int read = 0;
if (pdfIS != null) {
while ((read = pdfIS.read(bytes)) > 0) {
pdfOut.write(bytes, 0, read);
}
}
OleObjectFrame obj = (OleObjectFrame) shape;
obj.setObjectData(bytes);
}
}
FileOutputStream fileOutputStream = new FileOutputStream(file);

pres.write(fileOutputStream);

fileOutputStream.close();

//================

After running the code, when i open the ppt and double click the object, i still get the same error.
Is there something i am missing?

JAI contains 5 jars: clibwrapper_jiio.jar jai_codec.jar jai_core.jar jai_imageio.jar mlibwrapper_jai.jar

I have tested latest 1.8.3 version with Word and Excel documents again. Everything works fine.
About Pdf I already wrote that I think it shouldn’t work in both Java and .Net.
Write access to OLE1 objects is not implemented.

Hi alcrus,

Hoo…We are upset to hear that Aspose.slides doesn’t support embedding PDF in PPT.

Even we are able to embed all the microsoft products in the PPT.But our req. is very specific to embeding the PDF to PPT.

But in the release you have provided on
04-21-2008.It is mentioned that
Aspose.Slides Ver. Java 1.8.3.0 will support for Ole1 native objects.And OleObjectFrame class can recognize and read
data from almost any type of Ole2 and Ole1 ole objects like in .Net version.

We also tried with the Aspose.Slides Ver. Java 1.8.3.0.But we are not able to embed the PDF.

Could Aspose team update us on how to do it using Aspose.Slides
Ver. Java 1.8.3.0 or when it is possible to make a new release
which will support the embedding of PDF in PPT??.



Yes, 1.8.3 can read any Ole1 and Ole2 objects but can write Ole2 only.
We tried to implement embedding pdf objects some time ago but without success.
Currently we should reinvestigate such possibility.
In theory, it’s possible to implement this feature.

Hi alcrus,
Thanks very much for the update.We will be waiting for the update from Aspose team on embeding the PDF file.
Thanks a lot for your support…

Any updates on this please to embed pdf in ppt. I am using Apose.slides 17.6

@rishi1989,

I have observed your comments. I have shared code snippet with you. This will help you to achieve your requirements. Please share feedback with us if there is still an issue.

Presentation pres = new Presentation(pptxFileName);
IShapeCollection shapes = pres.getSlides().get_Item(0).getShapes();
shapes.addOleObjectFrame(0, 0, 100, 100, “AcroExch.Document.DC”,
File.readAllBytes(oleObjectFileName));
pres.save(pptxOutFileName, SaveFormat.Pptx);