Convert PDF to HTML in C++
Export PDFs to HTML and configure image folders, page splitting, SVG graphics, background images, body markup, text, and layers.
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 with default HTML options
Create HtmlSaveOptions and save the document with its default HTML export settings.
- Open the input PDF as Document.
- Create HtmlSaveOptions.
- Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/HtmlSaveOptions.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToHtml(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<HtmlSaveOptions>();
document->Save(outputFile, options);
}
}
Choose an image folder
Set SpecialFolderForAllImages to an html_images directory beside the output file.
- Open the input PDF as Document.
- Create HtmlSaveOptions.
- Set
SpecialFolderForAllImagesto the output-relative directory described above in HtmlSaveOptions. - Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/HtmlSaveOptions.h>
#include <system/io/path.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToHtmlExternalImages(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<HtmlSaveOptions>();
options->SpecialFolderForAllImages = System::IO::Path::Combine(System::IO::Path::GetDirectoryName(outputFile), u"html_images");
document->Save(outputFile, options);
}
}
Choose an SVG folder
Set SpecialFolderForSvgImages to an html_svg directory beside the output file.
- Open the input PDF as Document.
- Create HtmlSaveOptions.
- Set
SpecialFolderForSvgImagesto the output-relative directory described above in HtmlSaveOptions. - Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/HtmlSaveOptions.h>
#include <system/io/path.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToHtmlExternalSvg(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<HtmlSaveOptions>();
options->SpecialFolderForSvgImages = System::IO::Path::Combine(System::IO::Path::GetDirectoryName(outputFile), u"html_svg");
document->Save(outputFile, options);
}
}
Compress SVG graphics
Set SpecialFolderForSvgImages to html_svg_compressed beside the output file and enable CompressSvgGraphicsIfAny.
- Open the input PDF as Document.
- Create HtmlSaveOptions.
- Set
CompressSvgGraphicsIfAnytotruein HtmlSaveOptions. - Set
SpecialFolderForSvgImagesto the output-relative directory described above in HtmlSaveOptions. - Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/HtmlSaveOptions.h>
#include <system/io/path.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToHtmlCompressedSvg(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<HtmlSaveOptions>();
options->SpecialFolderForSvgImages = System::IO::Path::Combine(System::IO::Path::GetDirectoryName(outputFile), u"html_svg_compressed");
options->set_CompressSvgGraphicsIfAny(true);
document->Save(outputFile, options);
}
}
Split HTML output by page
Enable SplitIntoPages to produce page-specific HTML output.
- Open the input PDF as Document.
- Create HtmlSaveOptions.
- Set
SplitIntoPagestotruein HtmlSaveOptions. - Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/HtmlSaveOptions.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToHtmlSplitPages(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<HtmlSaveOptions>();
options->set_SplitIntoPages(true);
document->Save(outputFile, options);
}
}
Use PNG page backgrounds for raster images
Set RasterImagesSavingMode to AsEmbeddedPartsOfPngPageBackground.
- Open the input PDF as Document.
- Create HtmlSaveOptions.
- Set
RasterImagesSavingModetoHtmlSaveOptions::RasterImagesSavingModes::AsEmbeddedPartsOfPngPageBackgroundin HtmlSaveOptions. - Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/HtmlSaveOptions.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToHtmlPngBackground(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<HtmlSaveOptions>();
options->RasterImagesSavingMode = HtmlSaveOptions::RasterImagesSavingModes::AsEmbeddedPartsOfPngPageBackground;
document->Save(outputFile, options);
}
}
Export body content only
Set HtmlMarkupGenerationMode to WriteOnlyBodyContent and enable SplitIntoPages.
- Open the input PDF as Document.
- Create HtmlSaveOptions.
- Set
SplitIntoPagestotruein HtmlSaveOptions. - Set
HtmlMarkupGenerationModetoHtmlSaveOptions::HtmlMarkupGenerationModes::WriteOnlyBodyContentin HtmlSaveOptions. - Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/HtmlSaveOptions.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToHtmlBodyContent(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<HtmlSaveOptions>();
options->HtmlMarkupGenerationMode = HtmlSaveOptions::HtmlMarkupGenerationModes::WriteOnlyBodyContent;
options->set_SplitIntoPages(true);
document->Save(outputFile, options);
}
}
Preserve transparent text
Enable SaveTransparentTexts and SaveShadowedTextsAsTransparentTexts.
- Open the input PDF as Document.
- Create HtmlSaveOptions.
- Set
SaveTransparentTextstotruein HtmlSaveOptions. - Set
SaveShadowedTextsAsTransparentTextstotruein HtmlSaveOptions. - Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/HtmlSaveOptions.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToHtmlTransparentText(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<HtmlSaveOptions>();
options->SaveTransparentTexts = true;
options->SaveShadowedTextsAsTransparentTexts = true;
document->Save(outputFile, options);
}
}
Convert marked content to layers
Enable ConvertMarkedContentToLayers for HTML export.
- Open the input PDF as Document.
- Create HtmlSaveOptions.
- Set
ConvertMarkedContentToLayerstotruein HtmlSaveOptions. - Save the result with Document::Save(), passing the output path and options.
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/HtmlSaveOptions.h>
namespace ConvertingExamples
{
using namespace System;
using namespace Aspose::Pdf;
void ConvertPdfToHtmlLayers(String inputFile, String outputFile)
{
auto document = MakeObject<Document>(inputFile);
auto options = MakeObject<HtmlSaveOptions>();
options->set_ConvertMarkedContentToLayers(true);
document->Save(outputFile, options);
}
}