java pdf生成_想在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,可点击文末“了解更多”下载最新版体验。

34d83451e1e02953e74472b866dcc1bc.png

在本文中,我们将介绍使用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 filePresentation pres = new Presentation("presentation.pptx");// Save the presentation to PDF with default optionspres.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 filePresentation pres = new Presentation("presentation.pptx");// Instantiate the PdfOptions classPdfOptions opts = new PdfOptions();               // Set JPEG Qualityopts.setJpegQuality((byte) 90);// Define behavior for Metafilesopts.setSaveMetafilesAsPng(true);// Set Text Compression levelopts.setTextCompression(PdfTextCompression.Flate);// Define the PDF standardopts.setCompliance(PdfCompliance.Pdf15);              INotesCommentsLayoutingOptions options = opts.getNotesCommentsLayouting();options.setNotesPosition(NotesPositions.BottomFull);// Save the presentation to PDF with specified optionspres.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 classPdfOptions pdfOptions = new PdfOptions();// Specify that the generated document should include hidden slidespdfOptions.setShowHiddenSlides(true);// Save the presentation to PDF with specified optionspres.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 filePresentation pres = new Presentation("presentation.pptx");// Setting array of slides positionsint[] slides = new int[] { 2, 3, 5 };// Save the presentation to PDFpres.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 filePresentation pres = new Presentation("demo.pptx");// Instantiate the PdfOptions classPdfOptions opts = new PdfOptions();// Setting PDF passwordopts.setPassword("password");// Save the presentation to password protected PDFpres.save("output.pdf", SaveFormat.Pdf, opts);

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

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

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

// Create PDF optionsPdfOptions pdfOptions = new PdfOptions();// Set passwordpdfOptions.setPassword("my_password");// Set access permissionspdfOptions.setAccessPermissions(PdfAccessPermissions.PrintDocument| PdfAccessPermissions.HighQualityPrint);// Load PowerPoint presentationPresentation presentation = new Presentation("Presentation.pptx");try {   presentation.save("output.pdf", SaveFormat.Pdf, pdfOptions);} finally {   if (presentation != null) presentation.dispose();}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值