您的位置 首页 java

想在Java中把PPT转化为PDF吗?教你用Aspose.Slides轻松搞定

pdf 已成为最广泛和最常用的数字文档格式。由于PDF格式具有固定的布局,因此大多数文档在共享之前都已转换为PDF。

在将各种文档转换为PDF格式的过程中,PPT到PDF的转换是一种流行的用例,且非常的方便省时,特别是当必须将大量PowerPoint演示文稿转换为PDF时。如果您希望在Java中通过编程的方式将PPT或PPTX转换为PDF,那么你可以认真阅读本文的教程,将演示如何使用 Aspose.Slides for Java API提供的各种选项。

如果您还未使用过Java版Aspose.Slides,可点击文末“了解更多”下载最新版体验。

想在Java中把PPT转化为PDF吗?教你用Aspose.Slides轻松搞定


在本文中,我们将介绍使用Aspose.Slides for Java的以下转换方案:

  • 使用Java将PowerPoint PPT / PPTX转换为PDF
  • 使用自定义选项将PPT / PPTX转换为PDF
  • 将PPT / PPTX转换为PDF,包括隐藏的幻灯片
  • 将PPT / PPTX转换为受密码保护的PDF
  • 将PPT / PPTX的特定幻灯片转换为PDF
  • 将PPT / PPTX转换为具有访问权限的PDF

①将Java中的PPT / PPTX转换为PDF

以下是使用Aspose.Slides for Java提供的默认选项将PowerPoint演示文稿转换为PDF的简单步骤。

  • 使用Presentation对象加载PowerPoint PPT / PPTX 。
  • 调用save()方法传递输出的PDF文件名和输出格式。

以下代码示例显示如何使用默认选项将Java中的PPTX转换为PDF。

 // Instantiate a Presentation object that represents a presentation file
Presentation pres = new Presentation("presentation.pptx");

// Save the presentation to PDF with default options
pres.save("output.pdf", SaveFormat.Pdf);  

②使用自定义选项将PPT / PPTX转换为PDF

Aspose.Slides for Java提供了PdfOptions类,可自定义PowerPoint到PDF的转换。PdfOptions类使您可以指定JPEG质量,定义图元文件的行为,设置文本压缩级别,PDF遵从级别以及其他选项。以下是使用自定义选项将PPT / PPTX转换为PDF的步骤。

  • 使用Presentation对象加载PowerPoint PPT / PPTX 。
  • 创建PdfOptions类的对象。
  • 设置/指定PdfOptions类公开的选项。
  • 调用save()方法。

下面的代码示例演示如何使用自定义选项将PowerPoint PPTX转换为Java中的PDF。

 // Instantiate a Presentation object that represents a presentation file
Presentation pres = new Presentation("presentation.pptx");

// Instantiate the PdfOptions class
PdfOptions opts = new PdfOptions();
               
// Set JPEG Quality
opts.setJpegQuality((byte) 90);

// Define behavior for Metafiles
opts.setSaveMetafilesAsPng(true);

// Set Text Compression level
opts.setTextCompression(PdfTextCompression.Flate);

// Define the PDF standard
opts.setCompliance(PdfCompliance.Pdf15);
              
INotesCommentsLayoutingOptions options = opts.getNotesCommentsLayouting();
options.setNotesPosition(NotesPositions.BottomFull);

// Save the presentation to PDF with specified options
pres.save("output.pdf", SaveFormat.Pdf, opts);  

③将PPT / PPTX转换为PDF(包括隐藏的幻灯片)

PowerPoint演示文稿包含隐藏的幻灯片时,可能会出现这种情况。在默认的PowerPoint到PDF转换中,Aspose.Slides for Java将忽略隐藏的幻灯片。但是,如果要在转换后的PDF中包含隐藏的幻灯片,则可以使用PdfOptions.setShowHiddenSlides(true)选项。

下面的代码示例演示如何将PowerPoint PPTX转换为PDF,包括Java中的隐藏幻灯片。

 Presentation pres = new Presentation("presentation.pptx");
try {
// Instantiate the PdfOptions class
PdfOptions pdfOptions = new PdfOptions();

// Specify that the generated document should include hidden slides
pdfOptions.setShowHiddenSlides(true);

// Save the presentation to PDF with specified options
pres.save("output.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
if (pres != null)
pres.dispose();
}  

④将PPT / PPTX的特定幻灯片转换为PDF

Aspose.Slides for Java还允许选择要包含在生成的PDF文档中的幻灯片。可以创建一个数组以指定要包含在PPTX到PDF转换中的幻灯片编号,并将其传递给save()方法。

下面的代码示例演示如何在Java中将PowerPoint PPTX的特定幻灯片转换为PDF。

 // Instantiate a Presentation object that represents a presentation file
Presentation pres = new Presentation("presentation.pptx");
// Setting array of slides positions
int[] slides = new int[] { 2, 3, 5 };

// Save the presentation to PDF
pres.save("output.pdf", slides, SaveFormat.Pdf);  

⑤将PPT / PPTX转换为受密码保护的PDF

将PowerPoint演示文稿转换为受密码保护的PDF,以保护文档。可以使用 PdfOptions.setPassword(“ password”)设置密码 ,并将PdfOptions对象传递给save()方法。

下面的代码示例演示如何将PowerPoint PPTX转换为Java中受密码保护的PDF。

 // Instantiate a Presentation object that represents a presentation file
Presentation pres = new Presentation("demo.pptx");

// Instantiate the PdfOptions class
PdfOptions opts = new PdfOptions();

// Setting PDF password
opts.setPassword("password");

// Save the presentation to password protected PDF
pres.save("output.pdf", SaveFormat.Pdf, opts);  

⑥使用访问权限将PPT / PPTX转换为PDF

PDF格式允许您指定不同的访问权限,例如打印权限,添加或修改文本注释或表单字段的权限等。根据此功能,Aspose.Slides for Java提供了为从PowerPoint演示文稿转换而来的PDF文档设置权限的功能。该PdfAccessPermissions类包含你可以在PowerPoint演示文稿应用到PDF转换不同的权限类型的标志集合。

下面的Java代码示例演示如何将具有访问权限的PowerPoint演示文稿转换为PDF。

 // Create PDF options
PdfOptions pdfOptions = new PdfOptions();
// Set password
pdfOptions.setPassword("my_password");
// Set access permissions
pdfOptions.setAccessPermissions(PdfAccessPermissions.PrintDocument| PdfAccessPermissions.HighQualityPrint);

// Load PowerPoint presentation
Presentation presentation = new Presentation("Presentation.pptx");
try {
   presentation.save("output.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
   if (presentation != null) presentation.dispose();
}  

还想要更多吗?如果您有任何需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询。

文章来源:智云一二三科技

文章标题:想在Java中把PPT转化为PDF吗?教你用Aspose.Slides轻松搞定

文章地址:https://www.zhihuclub.com/199776.shtml

关于作者: 智云科技

热门文章

网站地图