Export PowerPoint Presentations to PDF
- 6 minutes to read
The DevExpress Presentation API allows you to export loaded or generated presentations to PDF documents.
Use one of the Presentation.exportToPdf() method overloads to save a presentation to an output stream or writable byte channel.
Export a Presentation to PDF
The Presentation API supports the following PDF export methods:
| Method | Description |
|---|---|
| exportToPdf(stream) | Exports the presentation to PDF and writes the generated document to the specified output stream. |
| exportToPdf(stream, options) | Exports the presentation to PDF, writes the generated document to the specified output stream, and applies PDF export options. |
| exportToPdf(channel) | Writes the presentation as a PDF document to the specified writable byte channel. |
| exportToPdf(channel, options) | Writes the presentation as a PDF document to a writable byte channel and applies PDF export options. |
Export to an Output Stream
The following code snippet loads a presentation from a PPTX file and exports it to a PDF document through a FileOutputStream:
import com.devexpress.docs.presentation.Presentation;
import java.io.FileOutputStream;
import java.nio.file.*;
// Load an existing presentation.
try (Presentation presentation =
new Presentation(
Files.readAllBytes(Path.of("source/presentation.pptx")))) {
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
Path pdfPath = outputDir.resolve("presentation.pdf");
// Export the presentation to PDF.
try (FileOutputStream stream = new FileOutputStream(pdfPath.toFile())) {
presentation.exportToPdf(stream);
}
}
Export to a Writable Byte Channel
The following code snippet exports a presentation to a PDF document through a WritableByteChannel:
import com.devexpress.docs.presentation.Presentation;
import java.nio.channels.*;
import java.nio.file.*;
// Load an existing presentation.
try (Presentation presentation =
new Presentation(
Files.readAllBytes(Path.of("source/presentation.pptx")))) {
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
// Create a writable byte channel for the output PDF file.
try(FileChannel channel =
FileChannel.open(outputDir.resolve("presentation.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
// Export the presentation to PDF.
presentation.exportToPdf(channel);
}
}
Configure PDF Export Options
Create a PdfExportOptions object and pass it to the Presentation.exportToPdf method to customize PDF output.
You can customize the following PDF export settings:
- Security and encryption
- Digital signatures
- Image optimization
- Attachments
- Document properties (metadata)
The following code snippet exports a PowerPoint presentation to PDF and sets the PDF document metadata, including the author, application, keywords, producer, subject, and title:
package presentation;
import com.devexpress.docs.presentation.*;
import com.devexpress.docs.presentation.exports.PdfExportOptions;
import java.io.FileOutputStream;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
try(Presentation presentation =
new Presentation(Files.readAllBytes(Path.of("Sample.pptx")))) {
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
try (FileOutputStream exportStream = new FileOutputStream(
outputDir.resolve("presentation.pdf").toFile())) {
PdfExportOptions exportOptions = new PdfExportOptions();
exportOptions.getDocumentOptions().setAuthor("Simon Peacock");
exportOptions.getDocumentOptions().setApplication("DevExpress Presentation API");
exportOptions.getDocumentOptions().setKeywords("Presentation, DevExpress, Exported, PDF");
exportOptions.getDocumentOptions().setProducer("Developer Express Inc., 26.1.4");
exportOptions.getDocumentOptions().setSubject("Test Presentation");
exportOptions.getDocumentOptions().setTitle("Presentation Title");
presentation.exportToPdf(exportStream, exportOptions);
}
}
}
}
Protect PDF Documents
PDF security supports:
- User passwords that restrict document access
- Owner passwords that control permitted operations
- Encryption algorithms
- Restrictions on printing, copying, editing, and interactive operations
Use the setEncryptionOptions(EncryptionOptions value) method to protect exported PDF documents with passwords, encryption algorithms, and operation permissions.
The following code snippet exports the presentation to a password-protected PDF document:
import com.devexpress.docs.pdf.*;
import com.devexpress.docs.office.*;
import com.devexpress.system.drawing.*;
import com.devexpress.docs.presentation.*;
import com.devexpress.docs.presentation.exports.*;
import java.nio.channels.FileChannel;
import java.nio.file.*;
// Load a presentation.
try (Presentation presentation =
new Presentation(
Files.readAllBytes(Path.of("input/presentation-blank.pptx")))) {
// Create PDF export options.
PdfExportOptions options = new PdfExportOptions() {{
setEncryptionOptions(
new EncryptionOptions("OWNER_PASSWORD", "USER_PASSWORD") {{
// Restrict available operations.
setDataExtractionPermissions(DocumentDataExtractionPermissions.NOT_ALLOWED);
setPrintPermissions(DocumentPrintPermissions.LOW_QUALITY);
setModificationPermissions(DocumentModificationPermissions.NOT_ALLOWED);
// Specify the encryption algorithm.
setAlgorithm(EncryptionAlgorithm.AES_256);
}}
);
}};
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
// Create an output stream for the exported PDF document.
try(FileChannel channel =
FileChannel.open(outputDir.resolve("presentation.pdf"),
StandardOpenOption.CREATE,
StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING)) {
// Export the presentation to PDF.
presentation.exportToPdf(channel, options);
}
}
Password Behavior
A PDF document can be protected with both a user password and an owner password.
- The user password controls whether a document can be opened.
- The owner password controls available operations.
If a user opens a document with the owner password, all operations become available regardless of permission restrictions.
Note
If the owner password is not specified, or if the owner and user passwords are identical, entering the password grants full access to the document.
Sign Exported PDF Documents
Use the getSignatureOptions() method to configure a digital signature for the exported PDF document.
The following code snippet configures signature settings:
// Create PDF export options.
PdfExportOptions options = new PdfExportOptions() {{
// Specify signature settings.
getSignatureOptions().setHashAlgorithm(HashAlgorithm.SHA_256);
getSignatureOptions().setLocation("USA");
getSignatureOptions().setContactInfo("john.doe@example.com");
// getSignatureOptions().setCertificate(/*Create X509Certificate2*/);
}};
Note
Assign a valid signing certificate before exporting the presentation.
Optimize Images in PDF Output
Use image optimization settings to reduce PDF file size or control image rendering quality.
Use the following methods to specify image optimization options:
| Method | Description |
|---|---|
| setConvertImagesToJpeg() | Sets whether to convert bitmap images to JPEG format. |
| setImageQuality() | Specifies JPEG image quality (when JPEG conversion is enabled). |
| setRasterizeImages() | Sets whether to convert vector graphics to raster images. |
| setRasterizationResolution() | Specifies the rasterization resolution (in DPI) when rasterization is enabled. |
The following code snippet configures image optimization settings:
// Configure image optimization settings.
PdfExportOptions options = new PdfExportOptions() {{
setRasterizeImages(true);
setRasterizationResolution(96);
setConvertImagesToJpeg(true);
setImageQuality(ImageQuality.MEDIUM);
}};
Attach Files to Exported PDF Documents
You can embed additional files into the exported PDF document.
Create an Attachment object and add it to the collection returned by the getAttachments() method.
import com.devexpress.docs.pdf;
import com.devexpress.docs.presentation.exports.*;
// Create an attachment.
Attachment attachment = new Attachment(
"attachment.pdf",
Files.readAllBytes( Path.of("input/reference.pdf"))
);
// Add the attachment to PDF export options.
PdfExportOptions options = new PdfExportOptions() {{
getAttachments().add(attachment);
}};
Specify PDF Document Properties
Document management systems and PDF viewers use document properties to identify and categorize PDF documents.
Use the getDocumentOptions() method to specify PDF metadata.
The following code snippet specifies common PDF document properties:
import com.devexpress.docs.presentation.exports.*;
// Configure PDF document properties.
PdfExportOptions options = new PdfExportOptions() {{
getDocumentOptions().setAuthor("John Doe");
getDocumentOptions().setApplication("DevExpress Presentation API");
getDocumentOptions().setKeywords("presentation, DevExpress, export, pdf");
getDocumentOptions().setProducer("Developer Express Inc.");
getDocumentOptions().setSubject("Demo Presentation");
getDocumentOptions().setTitle("Presentation Title");
}};
Specific Notes
Exported Content
The Presentation API exports slide content as images. As a result, font embedding settings do not affect PDF output.
Presentation Elements Not Included in PDF Output
The following presentation elements are not exported:
- SmartArt diagrams
- Charts
- Comments
- Fill overlay effects
PDF Compliance Standards
PDF/A and PDF/UA compliance options are not available when exporting presentations to PDF.
Security Configuration Requirements
The following combinations of modification and interactivity permissions are not supported:
// Combination #1: Not supported
PdfExportOptions options1 = new PdfExportOptions() {{
setEncryptionOptions(new EncryptionOptions("OWNER_PASSWORD", "USER_PASSWORD"){{
setModificationPermissions(DocumentModificationPermissions.ALLOWED);
setInteractivityPermissions(DocumentInteractivityPermissions.NOT_ALLOWED);
}});
}};
// Combination #2: Not supported
PdfExportOptions options2 = new PdfExportOptions() {{
setEncryptionOptions(new EncryptionOptions("OWNER_PASSWORD", "USER_PASSWORD"){{
setModificationPermissions(DocumentModificationPermissions.DOCUMENT_ASSEMBLING);
setInteractivityPermissions(DocumentInteractivityPermissions.FORM_FILL_AND_SIGN);
}});
}};
// Combination #3: Not supported
PdfExportOptions options3 = new PdfExportOptions() {{
setEncryptionOptions(new EncryptionOptions("OWNER_PASSWORD", "USER_PASSWORD"){{
setModificationPermissions(DocumentModificationPermissions.ALLOWED);
setInteractivityPermissions(DocumentInteractivityPermissions.FORM_FILL_AND_SIGN);
}});
}};
// Combination #4: Not supported
PdfExportOptions options4 = new PdfExportOptions() {{
setEncryptionOptions(new EncryptionOptions("OWNER_PASSWORD", "USER_PASSWORD"){{
setModificationPermissions(DocumentModificationPermissions.DOCUMENT_ASSEMBLING);
setInteractivityPermissions(DocumentInteractivityPermissions.ALLOWED);
}});
}};