When you need to share or present your PowerPoint presentations on different computers/devices, you may occasionally find that some content cannot be displayed properly. To avoid such incompatibility issues, a common method is to convert your PowerPoint document to PDF to ensure document integrity. In this article, you will learn how to convert a PowerPoint Presentation to PDF in C++ using Spire.Presentation for C++.
- Convert an Entire PowerPoint Presentation to PDF in C++
- Convert a Specific PowerPoint Slide to PDF in C++
- Convert a PowerPoint to PDF with Specific Page Size in C++
Install Spire.Presentation for C++
There are two ways to integrate Spire.Presentation for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.
Integrate Spire.Presentation for C++ in a C++ Application
Convert an Entire PowerPoint Presentation to PDF in C++
The Presentation->SaveToFile(LPCWSTR_S fileName, FileFormat::PDF) method allows you to convert each slide in PowerPoint to a PDF page. The following are the steps to convert a whole PowerPoint presentation to PDF.
- Create a Presentation object.
- Load a PowerPoint presentation using Presentation->LoadFromFile() method.
- Save it to PDF using Presentation->SaveToFile(LPCWSTR_S fileName, FileFormat::PDF) method.
- C++
#include "Spire.Presentation.o.h" using namespace std; using namespace Spire::Presentation; int main() { //Specify the input and output file paths std::wstring inputFile = L"Data\\sample.pptx"; std::wstring outputFile = L"Output\\PowerPointToPDF.pdf"; //Create a Presentation object Presentation* ppt = new Presentation(); //Load a PowerPoint document from disk ppt->LoadFromFile(inputFile.c_str()); //Save the document to PDF ppt->SaveToFile(outputFile.c_str(), FileFormat::PDF); delete ppt; }
Convert a Specific PowerPoint Slide to PDF in C++
If you only want to convert a particular slide to PDF, you can use the ISlide->SaveToFile(LPCWSTR_S fileName, FileFormat::PDF) method provided by Spire.Presentation for C++. The following are the detailed steps.
- Create a Presentation object.
- Load a PowerPoint presentation using Presentation->LoadFromFile() method.
- Get a specified slide by index using Presentation->GetSlides()->GetItem(slideIndex) method.
- Save the slide to PDF using ISlide->SaveToFile(LPCWSTR_S fileName, FileFormat::PDF) method.
- C++
#include "Spire.Presentation.o.h" using namespace std; using namespace Spire::Presentation; int main() { //Specify the input and output file paths std::wstring inputFile = L"Data\\sample.pptx"; std::wstring outputFile = L"Output\\SlideToPDF.pdf"; //Create a Presentation object Presentation* ppt = new Presentation(); //Load a PowerPoint document from disk ppt->LoadFromFile(inputFile.c_str()); //Get the second slide ISlide* slide = ppt->GetSlides()->GetItem(1); //Save the second slide to PDF slide->SaveToFile(outputFile.c_str(), FileFormat::PDF); delete ppt; }
Convert a PowerPoint to PDF with Specific Page Size in C++
Spire.Presentation for C++ also allows you to set a desired slide size and orientation for a PowerPoint document before converting it to PDF. The following are the steps to convert a PowerPoint to PDF with Specific Page Size (A4 slide size = 10.83x7.05 inch).
- Create a Presentation object.
- Load a PowerPoint presentation using Presentation->LoadFromFile() method.
- Set the slide size of the PowerPoint document using Presentation->GetSlideSize()->SetType() method.
- Set the slide orientation of the PowerPoint document using Presentation->GetSlideSize()->SetOrientation() method.
- Save the document to PDF using Presentation->SaveToFile(LPCWSTR_S fileName, FileFormat::PDF) method.
- C++
#include "Spire.Presentation.o.h" using namespace std; using namespace Spire::Presentation; int main() { //Specify the input and output file paths std::wstring inputFile = L"Data\\sample.pptx"; std::wstring outputFile = L"Output\\ToPdfWithSpecificPageSize.pdf"; //Create a Presentation object Presentation* ppt = new Presentation(); //Load a PowerPoint document from disk ppt->LoadFromFile(inputFile.c_str()); //Set the slide size to A4 ppt->GetSlideSize()->SetType(SlideSizeType::A4); //Set the slide orientation to Landscape ppt->GetSlideSize()->SetOrientation(SlideOrienation::Landscape); //Save the document to PDF ppt->SaveToFile(outputFile.c_str(), FileFormat::PDF); delete ppt; }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.