We’re converting the PPTX to byte array using the following code.
public static byte[] convertToByteArray(final Presentation pptx, final SaveOptions options) {
Validate.notNull(pptx, “Presentation seems to be not yet been initialized and document could not be null”);
byte[] pptBytes = null;
final ByteArrayOutputStream pptStream = new ByteArrayOutputStream();
try {
pptBytes = AsposeWrapper.invoke(new Callable<byte[]>() {
public byte[] call() throws Exception {
pptx.save(pptStream, SourceFormat.Pptx);
return pptStream.toByteArray();
}
});
} finally {
StreamUtils.closeQuietly(pptStream);
}
return pptBytes;
}
Now when trying to create the new Presentation instance using the byte array, Aspose is showing us that the file format is unsupported.
public static void testLogic(byte[] pptBytes) {
Presentation destPpt = createPresentation(pptBytes);
SaveOptions saveOptions = new PptxOptions();
destPpt = createPresentation(convertToByteArray(destPpt, saveOptions));
}
and,
public static Presentation createPresentation(byte[] inputDoc) {
Validate.notNull(inputDoc, “Could not create a Presentation object with null byte array for doc”);
// if (!isValidPresentation(inputDoc)) {
// throw new UnsupportedFileException();
// }
final InputStream inputStream = new ByteArrayInputStream(inputDoc);
try {
return new Presentation(inputStream);
} finally {
StreamUtils.closeQuietly(inputStream);
}
}
This means that the logic which we’re using for converting to byte array is errornous and just to let you guys know we’re using similar logic for Aspose.Words which actually worked. Thanks.
Method to invoke -
AsposeSlidesUtility.testLogic(p1);
com.aspose.slides.PptUnsupportedFormatException: Unknown file format.
Looking for your response on this in- correct way convert pptx to byte array. Thanks.
Hi Praneeth,
I have worked over your requirement after our conversation in live chat session where you mentioned that you wished to save presentation as stream. Please try using the following sample code on your end to serve the purpose. If there is still an issue then please provide the sample presentation that is causing issue on your end.
public static void saveByteStream()
{
Presentation pres=new Presentation(“D:\Aspose Data\test.pptx”);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// write bytes to bos …
pres.save(bos, SaveFormat.Pptx);
byte[] sink = bos.toByteArray();
InputStream myInputStream = new ByteArrayInputStream(sink);
Presentation presnew=new Presentation(myInputStream);
presnew.save(“D:\Aspose Data\Test.pdf”, SaveFormat.Pdf);
}
Many Thanks,
See we need byte[] given a Presentation instance and we’re using the code we shared above:
public static byte[] convertToByteArray(final Presentation pptx, final SaveOptions options) {
Validate.notNull(pptx, “Presentation seems to be not yet been initialized and document could not be null”);
byte[] pptBytes = null;
final ByteArrayOutputStream pptStream = new ByteArrayOutputStream();
try {
pptBytes = AsposeWrapper.invoke(new Callable<byte[]>() {
public byte[] call() throws Exception {
pptx.save(pptStream, SourceFormat.Pptx);
return pptStream.toByteArray();
}
});
} finally {
StreamUtils.closeQuietly(pptStream);
}
return pptBytes;
}
Now, we’re seeing that the byte[] we received using above code is not valid as when we’re reusing it the exception is being thrown. (com.aspose.slides.PptUnsupportedFormatException: Unknown file format.)
If you see the code:
public static void testLogic(byte[] pptBytes) {
// creating the Presentation instance
Presentation destPpt = createPresentation(pptBytes);
// converting to byte arrray
byte[] bytes = convertToByteArray(destPpt, saveOptions);
// next line will break everything
Presentation newPpt = createPresentation(bytes);
// Question is why this exception is occuring?
}
Thanks.
Hi Praneeth,
Thanks for your elaboration. An issue with ID SLIDESJAVA-34720 has been added in our issue tracking system for further investigation and resolution. This thread has been linked with the issue so that you may be automatically notified once the issue will be resolved.
We are sorry for your inconvenience.
Hey Mudassir, Just curios if you’ve reproduced the issue we talked about at your end?
Hi Praneeth,
pptx.save(pptStream, SourceFormat.Pptx)
Thanks Mudassir. This helped. Thank you.
The issues you have found earlier (filed as SLIDESJAVA-34720) have been fixed in this update.
This message was posted using Notification2Forum from Downloads module by Aspose Notifier.