在 Android 上将 PPT 和 PPTX 转换为 PDF(包含高级功能)
概述
在 Android 上将 PowerPoint 演示文稿(PPT、PPTX、ODP 等)转换为 PDF 格式具有多种优势,包括在不同设备之间的兼容性以及保留演示文稿的版面和格式。本指南演示如何将演示文稿转换为 PDF 文档,使用各种选项控制图像质量,包含隐藏幻灯片,对 PDF 文件进行密码保护,检测字体替换,选择特定幻灯片进行转换,并对输出文档应用合规标准。
PowerPoint 转 PDF 转换
- PPT
- PPTX
- ODP
要将演示文稿转换为 PDF,请将文件名作为参数传递给 Presentation 类,然后使用 save 方法将演示文稿保存为 PDF。 Presentation 类公开了通常用于将演示文稿转换为 PDF 的 save 方法。
Note
Aspose.Slides for Android via Java inserts its API information and version number into output documents. For example, when converting a presentation to PDF, Aspose.Slides populates the Application field with “Aspose.Slides” and the PDF Producer field with a value in “Aspose.Slides v XX.XX” form. 注意您无法指示 Aspose.Slides 更改或删除输出文档中的此信息。Aspose.Slides 允许您转换:
- 整个演示文稿为 PDF
- 演示文稿中的特定幻灯片为 PDF
Aspose.Slides 将演示文稿导出为 PDF,确保生成的 PDF 与原始演示文稿高度匹配。转换过程中元素和属性被准确呈现,包括:
- 图像
- 文本框和形状
- 文本格式
- 段落格式
- 超链接
- 页眉和页脚
- 项目符号
- 表格
将 PowerPoint 转换为 PDF
标准的 PowerPoint 到 PDF 转换过程使用默认选项。在这种情况下,Aspose.Slides 会尝试使用最佳设置和最高质量级别将提供的演示文稿转换为 PDF。
以下示例加载演示文稿并使用默认导出设置将所有可见幻灯片保存为 PDF。
import com.aspose.slides.*;
Presentation presentation = new Presentation("PowerPoint.ppt");
try {
presentation.save("PPT-to-PDF.pdf", SaveFormat.Pdf);
} finally {
presentation.dispose();
}
Note
Aspose 提供了一个免费在线的 PowerPoint 转 PDF 转换器 来演示演示文稿到 PDF 的转换过程。您可以使用此转换器运行测试,以实时实现此处描述的步骤。使用选项将 PowerPoint 转换为 PDF
Aspose.Slides 提供自定义选项——位于 PdfOptions 类下的属性——允许您自定义生成的 PDF、使用密码锁定 PDF,或指定转换过程的执行方式。
使用自定义选项将 PowerPoint 转换为 PDF
使用自定义转换选项,您可以定义光栅图像的首选质量设置,指定元文件的处理方式,设置文本的压缩级别,配置图像的 DPI 等。
以下示例将演示文稿导出为 PDF 1.5,JPEG 质量设置为 90,图像分辨率设置为 300 DPI,元文件保存为 PNG,并使用 Flate 文本压缩。
import com.aspose.slides.*;
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setJpegQuality((byte)90);
pdfOptions.setSufficientResolution(300);
pdfOptions.setSaveMetafilesAsPng(true);
pdfOptions.setTextCompression(PdfTextCompression.Flate);
pdfOptions.setCompliance(PdfCompliance.Pdf15);
Presentation presentation = new Presentation("PowerPoint.pptx");
try {
presentation.save("PowerPoint-to-PDF.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
presentation.dispose();
}
自动选择图像压缩
当演示文稿包含大量照片或截图时,减小导出 PDF 的大小可以更方便地通过电子邮件发送或下载。调用 PdfOptions.setBestImagesCompressionRatio 并传入 true,可在 PDF 导出期间自动为每个图像选择最合适的压缩算法。
以下示例加载现有演示文稿,启用自动图像压缩选择,并将其保存为 PDF 文档。所有其他 PDF 导出设置保持默认值。
import com.aspose.slides.PdfOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setBestImagesCompressionRatio(true);
Presentation presentation = new Presentation("image-heavy.pptx");
try {
presentation.save("image-heavy.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
presentation.dispose();
}
此选项为每个图像选择压缩算法。降低传递给 setJpegQuality 的值会降低 JPEG 图像质量,而降低传递给 setSufficientResolution 的值会使图像降采样到更低的分辨率,从而可能降低细节。启用自动压缩选择本身并不会降低这两个设置。
选择图像压缩算法需要额外的处理时间和内存。大小的缩减取决于演示文稿的图像和导出设置;并不能保证每个演示文稿都有固定的缩减量。
将嵌入的 OLE 文件保留为 PDF 附件
如果演示文稿包含嵌入的 Excel 工作簿,您可能希望 PDF 接收者既能访问工作簿的数据,又能查看幻灯片。调用 setIncludeOleData 并传入 true,即可在生成的 PDF 中将嵌入的 OLE 文件保留为附件。
默认值为 false:OLE 对象的预览图像或图标会在 PDF 页面上呈现,但其嵌入的文件不会作为附件包含。将该选项设为 true 则会额外包含文件数据。预览仍然是视觉呈现;附件允许接收者单独打开或保存嵌入的文件。OLE 对象不会在 PDF 页面上变成交互式的 Excel 工作表。
以下示例加载已包含嵌入式 Excel 工作簿的演示文稿,并将其导出为带有工作簿附件的 PDF。
import com.aspose.slides.*;
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setIncludeOleData(true);
Presentation presentation = new Presentation("presentation.pptx");
try {
presentation.save("presentation.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
presentation.dispose();
}
- 在支持文件附件的查看器(如 Adobe Acrobat Reader)中打开导出的 PDF。
- 打开查看器的 Attachments(附件)面板并定位嵌入的工作簿。
- 保存附件并在 Excel 中打开以检查其数据,或在查看器允许的情况下直接打开。PDF 页面上的预览与附件是分开的。
Note
PDF/A 标准对附件施加了限制:PDF/A-1 禁止嵌入文件,PDF/A-2 仅允许 PDF/A 附件,PDF/A-3 允许其他文件类型,包括 Excel 工作簿。这些是标准的要求,而非 Aspose.Slides 的特定限制。此示例使用默认的 PDF 合规性设置,并未演示 PDF/A 导出。将 PowerPoint 转换为包含隐藏幻灯片的 PDF
如果演示文稿包含隐藏幻灯片,您可以使用来自 PdfOptions 类的 setShowHiddenSlides 方法,将隐藏幻灯片作为页面包含在生成的 PDF 中。
以下示例将演示文稿导出为 PDF,包括所有隐藏幻灯片。
import com.aspose.slides.*;
Presentation presentation = new Presentation("PowerPoint.pptx");
try {
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setShowHiddenSlides(true);
presentation.save("PowerPoint-to-PDF.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
presentation.dispose();
}
将 PowerPoint 转换为受密码保护的 PDF
以下示例将演示文稿导出为需要密码 password 才能打开的 PDF。访问权限允许打印,包括高质量打印。
import com.aspose.slides.*;
Presentation presentation = new Presentation("PowerPoint.pptx");
try {
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setPassword("password");
pdfOptions.setAccessPermissions(PdfAccessPermissions.PrintDocument | PdfAccessPermissions.HighQualityPrint);
presentation.save("PPTX-to-PDF.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
presentation.dispose();
}
检测字体替换
Aspose.Slides 在 PdfOptions 类下提供 setWarningCallback 方法,使您能够在演示文稿到 PDF 的转换过程中检测字体替换。
以下示例将演示文稿导出为 PDF 并在控制台打印字体替换警告。仅在导出期间使用不可用字体进行替换时才会打印警告。
import com.aspose.slides.*;
class FontSubstitutionHandler implements IWarningCallback {
public int warning(IWarningInfo warning) {
if (warning.getWarningType() == WarningType.DataLoss && warning.getDescription().startsWith("Font will be substituted")) {
System.out.println("Font substitution warning: " + warning.getDescription());
}
return ReturnAction.Continue;
}
}
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setWarningCallback(new FontSubstitutionHandler());
Presentation presentation = new Presentation("sample.pptx");
try {
presentation.save("output.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
presentation.dispose();
}
Note
有关字体替换的更多信息,请参阅 Font Substitution 文章。处理没有专用粗体字形的字体
即使字体没有专用的粗体字形,演示文稿仍可以对文本应用粗体格式。文本仍然可以通过合成粗体(人为加粗常规字形)来显得粗体。当该文本在 PDF 中看起来过于粗重或与预期外观不符时,请尝试调用 PdfOptions.setRasterizeUnsupportedFontStyles 并传入 true。此选项在 PDF 导出期间将受影响的文本渲染为位图,可能会改善某些字体的显示效果。其默认值为 false。
示例演示文稿包含两个文本框:一个包含常规文本,另一个对同一没有专用粗体字形的字体应用了粗体格式。以下示例加载该演示文稿,启用不支持的字体样式光栅化,并将其导出为 PDF:
import com.aspose.slides.PdfOptions;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setRasterizeUnsupportedFontStyles(true);
Presentation presentation = new Presentation("unsupported-bold.pptx");
try {
presentation.save("rasterized.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
presentation.dispose();
}
以下预览显示了禁用和启用选项的输出。在本例中,禁用选项时粗体文本的笔画更粗。启用选项后,其笔画更细;常规文本保持不变。在为您的演示文稿选择设置之前,请比较这些结果。
禁用选项 (false,默认) |
启用选项 (true) |
|---|---|
![]() |
![]() |
在本例中,启用该选项后仅将粗体文本转换为位图:该文本无法被选中、复制或在没有 OCR 的情况下搜索,且在 800% 放大时边缘显得更柔和。常规文本仍然可搜索。禁用该选项时,两段文字均保持为文本。
此选项在字体没有专用粗体字形时,将粗体格式的文本光栅化。Font substitution 则是在原始字体不可用时选择另一个字体。
将 PowerPoint 中选定的幻灯片转换为 PDF
以下示例将演示文稿的第 1 张和第 3 张幻灯片导出为 PDF。此数组中的幻灯片编号从 1 开始,且输入演示文稿必须至少包含三张幻灯片。
import com.aspose.slides.*;
Presentation presentation = new Presentation("PowerPoint.pptx");
try {
int[] slides = { 1, 3 };
presentation.save("PPTX-to-PDF.pdf", slides, SaveFormat.Pdf);
} finally {
presentation.dispose();
}
使用自定义幻灯片尺寸将 PowerPoint 转换为 PDF
以下示例将演示文稿的第一张幻灯片复制到一个新演示文稿中,幻灯片尺寸为 612 × 792 点(8.5 × 11 英寸)。它会缩放幻灯片内容以适应尺寸并将此单张幻灯片导出为 PDF。
import com.aspose.slides.*;
float slideWidth = 612;
float slideHeight = 792;
Presentation presentation = new Presentation("SelectedSlides.pptx");
Presentation resizedPresentation = new Presentation();
try {
resizedPresentation.getSlideSize().setSize(slideWidth, slideHeight, SlideSizeScaleType.EnsureFit);
ISlide slide = presentation.getSlides().get_Item(0);
resizedPresentation.getSlides().insertClone(0, slide);
// 删除新建演示文稿时创建的空幻灯片。
resizedPresentation.getSlides().removeAt(1);
resizedPresentation.save("PDF_with_custom_slide_size.pdf", SaveFormat.Pdf);
} finally {
resizedPresentation.dispose();
presentation.dispose();
}
在备注幻灯片视图中将 PowerPoint 转换为 PDF
以下示例将演示文稿导出为 PDF,将每张幻灯片的演讲者备注放在幻灯片下方。请使用包含演讲者备注的演示文稿来查看效果。
import com.aspose.slides.*;
Presentation presentation = new Presentation("SelectedSlides.pptx");
try {
NotesCommentsLayoutingOptions notesOptions = new NotesCommentsLayoutingOptions();
notesOptions.setNotesPosition(NotesPositions.BottomFull);
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setSlidesLayoutOptions(notesOptions);
presentation.save("PDF_with_notes.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
presentation.dispose();
}
PDF 的可访问性和合规性标准
Aspose.Slides 允许您使用符合 Web Content Accessibility Guidelines (WCAG) 的转换过程。您可以使用以下任意合规标准将 PowerPoint 文档导出为 PDF:PDF/A1a、PDF/A1b 和 PDF/UA。
以下代码演示了基于不同合规标准生成多个 PDF 的 PowerPoint 到 PDF 的转换过程:
import com.aspose.slides.*;
Presentation presentation = new Presentation("pres.pptx");
try {
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setCompliance(PdfCompliance.PdfA1a);
presentation.save("pres-a1a-compliance.pdf", SaveFormat.Pdf, pdfOptions);
pdfOptions.setCompliance(PdfCompliance.PdfA1b);
presentation.save("pres-a1b-compliance.pdf", SaveFormat.Pdf, pdfOptions);
pdfOptions.setCompliance(PdfCompliance.PdfUa);
presentation.save("pres-ua-compliance.pdf", SaveFormat.Pdf, pdfOptions);
} finally {
presentation.dispose();
}
Note
Aspose.Slides 支持 PDF 转换操作,允许您将 PDF 文件转换为常见的文件格式。您可以执行 PDF to HTML、PDF to image、PDF to JPG 和 PDF to PNG 转换。对专用格式的其他 PDF 转换操作——PDF to SVG、PDF to TIFF 和 PDF to XML——也受到支持。注意: 导出为 PDF/UA 时,Aspose.Slides 将诸如 SmartArt、图表和公式等复杂图形视为单个图形。单个路径元素不会作为独立内容保留,可能会被标记为伪影;仅为整个图形提供替代文本。
常见问题
我可以批量将多个 PowerPoint 文件转换为 PDF 吗?
是的,Aspose.Slides 支持将多个 PPT 或 PPTX 文件批量转换为 PDF。您可以遍历文件并以编程方式执行转换过程。
是否可以对转换后的 PDF 设置密码保护?
可以。使用 PdfOptions 类在转换过程中设置密码并定义访问权限。
如何在 PDF 中包含隐藏幻灯片?
在 PdfOptions 类中调用 setShowHiddenSlides 并传入 true,即可在生成的 PDF 中包含隐藏幻灯片。
Aspose.Slides 能否在 PDF 中保持高图像质量?
可以,您可以通过在 PdfOptions 类中使用诸如 setJpegQuality 和 setSufficientResolution 等方法来控制图像质量,从而确保 PDF 中的图像保持高质量。
Aspose.Slides 是否支持 PDF/A 合规标准?
是的,Aspose.Slides 允许您导出符合 various standards 的 PDF,包括 PDF/A1a、PDF/A1b 和 PDF/UA,确保您的文档符合可访问性和归档要求。

