Convert PDF to other file formats in C++
Export PDFs to EPUB, TeX, text, XPS, XML, Markdown, and MobiXML.
Configure the Aspose.PDF for C++ SDK before running these functions. Pass an existing input file and a writable destination path; create any required output directories first. Each function is an independent example, and its options do not carry over to other examples.
Convert PDF to EPUB
Use SaveFormat::Epub with the SDK’s default EPUB export settings.
- Open the PDF as a Document.
- Select the SaveFormat shown below.
- Export the document with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/SaveFormat.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToEpub(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
document->Save(outputFile, SaveFormat::Epub);
}
}
Convert PDF to TeX
Save the PDF with TeXSaveOptions.
- Open the input PDF as Document.
- Create TeXSaveOptions.
- Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/TeXSaveOptions.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToTex(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<TeXSaveOptions>();
document->Save(outputFile, options);
}
}
Export text from the first page
Use TextDevice to process only page 1 into the output stream. The input must contain at least one page; this example does not extract text from subsequent pages.
- Open the input PDF and create a TextDevice.
- Open the output FileStream in create mode.
- Pass Pages[1] and the stream to TextDevice::Process(), then close the stream.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Devices/TextDevice.h>
#include <system/io/file_stream.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Devices;
void ConvertPdfToText(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto device = MakeObject<TextDevice>();
auto output = MakeObject<System::IO::FileStream>(outputFile, System::IO::FileMode::Create);
device->Process(document->get_Pages()->idx_get(1), output);
output->Close();
}
}
Convert PDF to XPS
Set XpsSaveOptions::UseEmbeddedTrueTypeFonts to true before saving.
- Open the input PDF as Document.
- Create XpsSaveOptions.
- Set
UseEmbeddedTrueTypeFontstotruein XpsSaveOptions. - Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/XpsSaveOptions.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToXps(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<XpsSaveOptions>();
options->set_UseEmbeddedTrueTypeFonts(true);
document->Save(outputFile, options);
}
}
Convert tagged PDF to XML
Use SaveFormat::Xml with a tagged PDF input. This is a document export example, not an OCR workflow or an arbitrary XML schema mapping.
- Open the PDF as a Document.
- Select the SaveFormat shown below.
- Export the document with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/SaveFormat.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToXml(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
document->Save(outputFile, SaveFormat::Xml);
}
}
Convert PDF to Markdown
Set ResourcesDirectoryName to images and UseImageHtmlTag to true in MarkdownSaveOptions.
- Open the input PDF as Document.
- Create MarkdownSaveOptions.
- Set
ResourcesDirectoryNametou"images"in MarkdownSaveOptions. - Set
UseImageHtmlTagtotruein MarkdownSaveOptions. - Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/PdfToMarkdown/MarkdownOptions/MarkdownSaveOptions.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToMarkdown(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<MarkdownSaveOptions>();
options->set_ResourcesDirectoryName(u"images");
options->set_UseImageHtmlTag(true);
document->Save(outputFile, options);
}
}
Convert PDF to MobiXML
Use SaveFormat::MobiXml to export MobiXML content.
- Open the PDF as a Document.
- Select the SaveFormat shown below.
- Export the document with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/SaveFormat.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToMobiXml(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
document->Save(outputFile, SaveFormat::MobiXml);
}
}