我在使用asponse将PPT转成PDF时,提示我空指针异常
public static void main(String[] args) throws IOException {
String outPath = "D:/project/123.pdf";
String inPath = "D:/project/22.pptx";
if (!isPptLicense()) {
return;
}
FileOutputStream outputStream = null;
//读取ppt文件
FileInputStream fileInput = new FileInputStream(inPath);
System.out.println(fileInput.toString());
Presentation pres = new Presentation(fileInput);
//指定输出路径
outputStream = new FileOutputStream(new File(outPath));
//输出
pres.save(outputStream, com.aspose.slides.SaveFormat.Pdf);
outputStream.close();
}
private static boolean isPptLicense() {
boolean result = false;
try {
String license =
"<License>\n" +
" <Data>\n" +
" <Products>\n" +
" <Product>Aspose.Total for Java</Product>\n" +
" <Product>Aspose.Words for Java</Product>\n" +
" </Products>\n" +
" <EditionType>Enterprise</EditionType>\n" +
" <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
" <LicenseExpiry>20991231</LicenseExpiry>\n" +
" <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
" </Data>\n" +
" <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
"</License>";
InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
我最开始以为是我路径的问题,因此我尝试把路径改为D:\project\123.pdf,但是还是不行。同样的方法我去处理word和Excel转PDF都没问题,唯独PPT失败,我使用的slides版本是15.9.0
[](链接:https://pan.baidu.com/s/1tP0SC4bxl39msg8zVqvMqg
提取码:x8tp)
这是我使用的jar包
这是官方的示例,直接另存即可,不用Stream
package com.aspose.slides.examples.presentations.conversion;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.slides.examples.RunExamples;
public class ConvertToPDF
{
public static void main(String[] args)
{
//ExStart:ConvertToPDF
// The path to the documents directory.
String dataDir = RunExamples.getDataDir_Conversion();
// Instantiate a Presentation object that represents a presentation file
Presentation presentation = new Presentation(dataDir + "ConvertToPDF.pptx");
try
{
// Save the presentation to PDF with default options
presentation.save(dataDir + "output_out.pdf", SaveFormat.Pdf);
}
finally
{
if (presentation != null) presentation.dispose();
}
//ExEnd:ConvertToPDF
}
}
是不是路径有问题啊?检查一下
public bool PptToPdf(string PathFile, string OutPathFile)
{
try
{
//PPT
Presentation ppt = new Presentation(PathFile);
ppt.Save(OutPathFile, Aspose.Slides.Export.SaveFormat.Pdf);
return true;
}
catch (Exception ex)
{
return false;
}
}
检查方法是否适用于当前版本的 Aspose.Cells 和 Aspose.Slides for Java 的 ppt、pptx、xls 和 xlsx 文件,有的可能不适用于ppt文件。
(下面是一个在stackoverflow寻找到的一个相关解答,希望能帮助到你)
https://stackoverflow.com/questions/15762792/aspose-xlsx-and-pptx-files-conversion-exception