Export PowerPoint Presentations to PDF
- 8 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.*;
public class Main {
public static void main(String[] args) throws Exception {
// 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.*;
public class Main {
public static void main(String[] args) throws Exception {
// 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:
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
Call the PdfExportOptions.setEncryptionOptions(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.system.drawing.*;
import com.devexpress.docs.presentation.*;
import com.devexpress.docs.presentation.exports.*;
import java.nio.channels.FileChannel;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
// 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
Call the PdfExportOptions.getSignatureOptions() method to configure a digital signature for the exported PDF document.
The following code snippet exports a presentation to a PDF document signed with a certificate from a PKCS #12 file (certificate.pfx):
import com.devexpress.docs.pdf.HashAlgorithm;
import com.devexpress.docs.presentation.Presentation;
import com.devexpress.docs.presentation.exports.PdfExportOptions;
import java.io.*;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
// Load a presentation and a signing certificate (a PKCS #12 file).
try (Presentation presentation =
new Presentation(
Files.readAllBytes(Path.of("source/presentation.pptx")));
InputStream certificate = Files.newInputStream(Path.of("certificate.pfx"))) {
// Create PDF export options.
PdfExportOptions options = new PdfExportOptions() {{
// Specify signature settings.
getSignatureOptions().setCertificate(certificate, "CERTIFICATE_PASSWORD");
getSignatureOptions().setHashAlgorithm(HashAlgorithm.SHA_256);
getSignatureOptions().setLocation("USA");
getSignatureOptions().setContactInfo("john.doe@example.com");
}};
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
// Export the presentation to PDF.
try (OutputStream stream = Files.newOutputStream(outputDir.resolve("presentation.pdf"))) {
presentation.exportToPdf(stream, options);
}
}
}
}
Note
Replace certificate.pfx and CERTIFICATE_PASSWORD with your signing certificate and its password.
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(value) | Sets whether to convert bitmap images to JPEG format. |
| setImageQuality(value) | Sets the quality of JPEG images in the exported PDF document. |
| setRasterizeImages(value) | Sets whether to rasterize vector images during export. |
| setRasterizationResolution(value) | Sets the resolution for images that the export rasterizes. |
The following code snippet exports a presentation to PDF with image optimization settings:
import com.devexpress.docs.pdf.ImageQuality;
import com.devexpress.docs.presentation.Presentation;
import com.devexpress.docs.presentation.exports.PdfExportOptions;
import java.io.OutputStream;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
// Load an existing presentation.
try (Presentation presentation =
new Presentation(
Files.readAllBytes(Path.of("source/presentation.pptx")))) {
// Configure image optimization settings.
PdfExportOptions options = new PdfExportOptions() {{
setRasterizeImages(true);
setRasterizationResolution(96);
setConvertImagesToJpeg(true);
setImageQuality(ImageQuality.MEDIUM);
}};
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
// Export the presentation to PDF.
try (OutputStream stream = Files.newOutputStream(outputDir.resolve("presentation.pdf"))) {
presentation.exportToPdf(stream, options);
}
}
}
}
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 PdfExportOptions.getAttachments() method.
The following code snippet attaches a PDF file to the exported PDF document:
import com.devexpress.docs.pdf.Attachment;
import com.devexpress.docs.presentation.Presentation;
import com.devexpress.docs.presentation.exports.PdfExportOptions;
import java.io.OutputStream;
import java.nio.file.*;
import java.time.OffsetDateTime;
public class Main {
public static void main(String[] args) throws Exception {
// Load an existing presentation.
try (Presentation presentation =
new Presentation(
Files.readAllBytes(Path.of("source/presentation.pptx")))) {
// Create an attachment.
Attachment attachment = new Attachment(
"attachment.pdf",
Files.readAllBytes(Path.of("input/reference.pdf"))
);
attachment.setCreationDate(OffsetDateTime.now());
attachment.setModificationDate(OffsetDateTime.now());
// Add the attachment to PDF export options.
PdfExportOptions options = new PdfExportOptions() {{
getAttachments().add(attachment);
}};
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
// Export the presentation to PDF.
try (OutputStream stream = Files.newOutputStream(outputDir.resolve("presentation.pdf"))) {
presentation.exportToPdf(stream, options);
}
}
}
}
Specify PDF Document Properties
Document management systems and PDF viewers use document properties to identify and categorize PDF documents.
Call the PdfExportOptions.getDocumentOptions() method to specify PDF metadata.
If you do not specify the author, title, subject, or keywords in PDF document options, the exported PDF document gets these values from presentation document properties. To specify another source for the author, title, subject, and keywords, call the PdfExportOptions.setMetadataExportMode(value) method.
The following code snippet exports a presentation to PDF and specifies common PDF document properties:
import com.devexpress.docs.presentation.Presentation;
import com.devexpress.docs.presentation.exports.PdfExportOptions;
import java.io.OutputStream;
import java.nio.file.*;
public class Main {
public static void main(String[] args) throws Exception {
// Load an existing presentation.
try (Presentation presentation =
new Presentation(
Files.readAllBytes(Path.of("source/presentation.pptx")))) {
// 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");
}};
Path outputDir = Path.of("output");
Files.createDirectories(outputDir);
// Export the presentation to PDF.
try (OutputStream stream = Files.newOutputStream(outputDir.resolve("presentation.pdf"))) {
presentation.exportToPdf(stream, options);
}
}
}
}
Specific Notes
Presentation Elements Not Included in PDF Output
The following presentation elements are not exported:
- SmartArt diagrams
- Comments
- Fill overlay effects
Shape Hyperlinks in PDF Output
The API converts shape hyperlinks to PDF links. For URI hyperlinks, the API exports links with the http, https, or mailto scheme. Hyperlinks that move to the last viewed slide or end the slide show have no PDF counterpart, and the API does not export them.
Refer to the HyperlinkAction class description for additional information.
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:
import com.devexpress.docs.pdf.*;
import com.devexpress.docs.presentation.exports.PdfExportOptions;
public class Main {
public static void main(String[] args) throws Exception {
// 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);
}});
}};
}
}