C++: Convert PDF to SVG

SVG (Scalable Vector Graphics) is a vector graphics format that can be scaled up or down without losing quality, which is especially useful when images need to be placed on large billboards/stadium screens, or when small logos and icons need to be saved. By the same token, if you want to make PDF pages infinitely scalable, you can convert them to SVG format. This article will demonstrate how to convert PDF to SVG in C++ using Spire.PDF for C++.

Install Spire.PDF for C++

There are two ways to integrate Spire.PDF 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.PDF for C++ in a C++ Application

Convert a PDF File to SVG in C++

Spire.PDF for C++ offers the PdfDocument->SaveToFile() method to convert each page in a PDF file to a single SVG file. The following are the detailed steps.

  • Create a PdfDocument instance.
  • Load a sample PDF file using PdfDocument->LoadFromFile() method.
  • Convert the PDF file to SVG using PdfDocument->SaveToFile(LPCWSTR_S filename, FileFormat::SVG) method.
  • C++
#include "Spire.Pdf.o.h";

using namespace Spire::Pdf;
using namespace std;

int main()
{
	//Specify the input and output files
	wstring inputFile = L"Data\\sample1.pdf";
	wstring outputFile = L"ToSVG\\ToSVG.svg";

	//Create a PdfDcoument instance
	intrusive_ptr<PdfDocument> pdf = new PdfDocument();

	//Load a sample PDF document
	pdf->LoadFromFile(inputFile.c_str());

	//Convert the PDF document to SVG
	pdf->SaveToFile(outputFile.c_str(),FileFormat::SVG);
	pdf->Close();
}

C++: Convert PDF to SVG

Convert Specified PDF Pages to SVG in C++

Spire.PDF for C++ also allows you to convert specified pages in a PDF file to SVG files. During conversion, if you want to specify the width and height of the output SVG file, you can use the PdfDocument->GetConvertOptions()->SetPdfToSvgOptions() method. The following are the detailed steps to convert a selected PDF page to SVG with custom width and height.

  • Create a PdfDocument instance.
  • Load a sample PDF file using PdfDocument->LoadFromFile() method.
  • Specify the width and height of the output SVG file using PdfDocument->GetConvertOptions()->SetPdfToSvgOptions(float wPixel, float hPixel) method.
  • Convert selected PDF pages to SVG using PdfDocument->SaveToFile(LPCWSTR_S filename, int startIndex, int endIndex, FileFormat::SVG) method.
  • C++
#include "Spire.Pdf.o.h";

using namespace Spire::Pdf;
using namespace std;

int main()
{
	//Specify the input and output files
	wstring inputFile = L"Data\\sample1.pdf";
	wstring outputFile = L"PDFPagetoSVG.svg";

	//Create a PdfDcoument instance
	intrusive_ptr<PdfDocument> pdf = new PdfDocument();

	//Load a sample PDF document
	pdf->LoadFromFile(inputFile.c_str());

	//Specify the width and height of the output SVG file
	pdf->GetConvertOptions()->SetPdfToSvgOptions(900, 1200);

	//Convert the specified PDF page to SVG
	pdf->SaveToFile(outputFile.c_str(), 2, 2, FileFormat::SVG);
	pdf->Close();
}

C++: Convert PDF to SVG

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.