Convert PowerPoint Presentations to PDF in C++
Liam Anderson
Posted on April 27, 2023
PDF is a versatile and compatible file format that can be viewed and shared across multiple platforms and devices. By converting PowerPoint presentations to PDF, you can easily share the information in the presentations with anyone, even those who don't have PowerPoint software installed on their devices. This article will illustrate how to programmatically convert PowerPoint Presentations to PDF in C++.
- Convert a PowerPoint Presentation to PDF
- Convert a PowerPoint Presentation to PDF with Multiple Slides per Page
- Convert a PowerPoint Presentation to PDF with a Specific Page Size
- Convert a Specific PowerPoint Slide to PDF
Installation
In order to convert PowerPoint presentations to PDF in C++, this article uses Spire.Presentation for C++ library.
NuGet is the easiest way to integrate Spire.Presentation for C++ into your application, and there are two ways to install Spire.Presentation for C++ through NuGet.
1.Install Spire.Presentation for C++ using Manage NuGet Packages.
- Create or open your project in Visual Studio.
- Click Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution…
- Search for “Spire.Presentation.Cpp” and click install.
2.Install Spire.Presentation for C++ using Package Manager Console.
- Create or open your project in Visual Studio.
- Click Tools -> NuGet Package Manager -> Package Manager Console.
- Type the command “Install-Package Spire.Presentation.Cpp” and press enter.
Alternatively, you can also download the package from its official site and then manually import it into your project. For more details, you can check the documentation — How to Integrate Spire.Presentation for C++ in a C++ Application.
Convert a PowerPoint Presentation to PDF in C++
To convert a PowerPoint presentation to PDF, you just need to load the presentation and then use the Presentation->SaveToFile() method to save it to PDF format. Each slide in the presentation will be converted into a separate PDF page. The detailed steps are as follows:
- Initialize an instance of the Presentation class.
- Load a PowerPoint presentation using Presentation->LoadFromFile() method.
- Save the presentation to PDF format using Presentation->SaveToFile() method.
#include "Spire.Presentation.o.h"
using namespace Spire::Presentation;
int main()
{
//Initialize an instance of the Presentation class
Presentation* ppt = new Presentation();
//Load a PowerPoint presentation
ppt->LoadFromFile(L"Sample.pptx");
//Save the presentation to PDF format
ppt->SaveToFile(L"PptToPdf.pdf", FileFormat::PDF);
delete ppt;
}
Convert a PowerPoint Presentation to PDF with Multiple Slides per Page in C++
You can use the Presentation->GetSaveToPdfOption()->SetPageSlideCount() method to specify the number of slides to be included per PDF page. The detailed steps are as follows:
- Initialize an instance of the Presentation class.
- Load a PowerPoint presentation using Presentation->LoadFromFile() method.
- Set the number of slides to be included per PDF page using the Presentation->GetSaveToPdfOption()->SetPageSlideCount() method.
- Save the presentation to PDF format using Presentation->SaveToFile() method.
#include "Spire.Presentation.o.h"
using namespace Spire::Presentation;
int main()
{
//Initialize an instance of the Presentation class
Presentation* ppt = new Presentation();
//Load a PowerPoint presentation
ppt->LoadFromFile(L"Sample.pptx");
//Specify the number of slides to be included per PDF page
ppt->GetSaveToPdfOption()->SetPageSlideCount(PageSlideCount::Two);
//Save the presentation to PDF format
ppt->SaveToFile(L"PptToPdfWithMultipleSlidesPerPage.pdf", FileFormat::PDF);
delete ppt;
}
Convert a PowerPoint Presentation to PDF with a Specific Page Size in C++
You can convert a PowerPoint presentation to PDF with a specific page size by setting the slide size of the presentation to a specific size, then save the presentation to PDF format using the Presentation->SaveToFile() method. The detailed steps are as follows:
- Initialize an instance of the Presentation class.
- Load a PowerPoint presentation using Presentation->LoadFromFile() method.
- Set the slide size type as custom using Presentation->GetSlidesSize()->SetType() method.
- Set the slide size to a specific size using Presentation->GetSlideSize()->SetSize() method.
- Set slide orientation using Presentation->GetSlideSize()->SetOrientation() method.
- Save the presentation to PDF format using Presentation->SaveToFile() method.
#include "Spire.Presentation.o.h"
using namespace Spire::Presentation;
int main()
{
//Initialize an instance of the Presentation class
Presentation* ppt = new Presentation();
//Load a PowerPoint presentation
ppt->LoadFromFile(L"Sample.pptx");
//Set slide size
ppt->GetSlideSize()->SetType(SlideSizeType::Custom);
ppt->GetSlideSize()->SetSize(new SizeF(841.68, 595.44)); //The unit of measurement is pt
//Set slide orientation
ppt->GetSlideSize()->SetOrientation(SlideOrienation::Landscape);
//Save the presentation to PDF format
ppt->SaveToFile(L"PptToPdfWithSpecificPageSize.pdf", FileFormat::PDF);
delete ppt;
}
Convert a Specific PowerPoint Slide to PDF in C++
Sometimes, you may want to convert a specific slide instead of a whole PowerPoint presentation to PDF. In this case, you can use the ISlide->SaveToFile() method to save the slide to PDF format. The detailed steps are as follows:
- Initialize an instance of the Presentation class.
- Load a PowerPoint presentation using Presentation->LoadFromFile() method.
- Access a specific slide using Presentation->GetSlides()->GetItem() method.
- Save the slide to PDF format using ISlide->SaveToFile() method.
#include "Spire.Presentation.o.h"
using namespace Spire::Presentation;
int main()
{
//Initialize an instance of the Presentation class
Presentation* ppt = new Presentation();
//Load a PowerPoint presentation
ppt->LoadFromFile(L"Sample.pptx");
//Get the third slide
ISlide* slide = ppt->GetSlides()->GetItem(2);
//Save the slide to PDF format
slide->SaveToFile(L"SlideToPdf.pdf", FileFormat::PDF);
delete ppt;
}
Posted on April 27, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.