Convert PDF to HTML in Java
Aspose.PDF for Java supports HTML export with options for images, SVG, page splitting, transparency, and layer rendering. Use HtmlSaveOptions to control how PDF pages, resources, and markup are written to HTML output.
Convert PDF to HTML
Use this example when a PDF should be exported to a standard HTML document.
- Open the source PDF in a
Documentinstance. - Create default
HtmlSaveOptionsfor standard HTML serialization. - Call
document.save(outputFile.toString(), saveOptions)so the PDF page content is exported as HTML markup. - Save the generated HTML output.
public static void convertPdfToHtml(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
document.save(outputFile.toString(), saveOptions);
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert PDF to HTML and store images separately
Use this example when extracted images should be written as separate files during HTML export.
- Open the source PDF in a
Documentinstance. - Create
HtmlSaveOptionsand setsetSpecialFolderForAllImages(...)to a dedicated image output directory. - Call
document.save(outputFile.toString(), saveOptions)so raster images are emitted as separate resource files instead of inline-only output. - Save the HTML output together with the generated image assets.
public static void convertPdfToHtmlStoringImages(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.setSpecialFolderForAllImages(inputFile.getParent().resolve("images").toString());
document.save(outputFile.toString(), saveOptions);
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert PDF to multi-page HTML
Use this example when each PDF page should be represented separately in HTML output.
- Open the source PDF in a
Documentinstance. - Create
HtmlSaveOptionsand enablesetSplitIntoPages(true). - Call
document.save(outputFile.toString(), saveOptions)so each PDF page is written as separate HTML output. - Save the generated HTML files.
public static void convertPdfToHtmlMultiPage(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.setSplitIntoPages(true);
document.save(outputFile.toString(), saveOptions);
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert PDF to HTML and store SVG separately
Use this example when vector content should be emitted as separate SVG resources.
- Open the source PDF in a
Documentinstance. - Create
HtmlSaveOptionsand setsetSpecialFolderForSvgImages(...)to an external SVG resource directory. - Call
document.save(outputFile.toString(), saveOptions)so vector graphics are stored outside the main HTML file. - Save the HTML output and SVG assets.
public static void convertPdfToHtmlStoringSvg(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.setSpecialFolderForSvgImages(inputFile.getParent().resolve("svg_images").toString());
document.save(outputFile.toString(), saveOptions);
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert PDF to HTML with compressed SVG
Use this example when SVG output should be optimized during HTML export.
- Open the source PDF in a
Documentinstance. - Create
HtmlSaveOptionsand configure a dedicated folder for SVG resources. - Enable
setCompressSvgGraphicsIfAny(true)so SVG assets are compressed during export. - Call
document.save(outputFile.toString(), saveOptions)and save the converted HTML files.
public static void convertPdfToHtmlCompressSvg(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.setSpecialFolderForSvgImages(inputFile.getParent().resolve("svg_images").toString());
saveOptions.setCompressSvgGraphicsIfAny(true);
document.save(outputFile.toString(), saveOptions);
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert PDF to HTML with PNG page backgrounds
Use this example when page backgrounds should be rendered as PNG images in HTML output.
- Open the source PDF in a
Documentinstance. - Create
HtmlSaveOptionsand set the raster image saving mode to PNG page backgrounds. - Call
document.save(outputFile.toString(), saveOptions)so page background content is emitted as PNG-backed HTML layers. - Save the converted HTML output.
public static void convertPdfToHtmlPngBackground(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.setRasterImagesSavingMode(
HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground);
document.save(outputFile.toString(), saveOptions);
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert PDF to HTML body content only
Use this example when only the body markup is needed instead of a full HTML document shell.
- Open the source PDF in a
Documentinstance. - Create
HtmlSaveOptionsand set the markup generation mode toWriteOnlyBodyContent. - Keep
setSplitIntoPages(true)enabled when body-only output should still be page-separated. - Call
document.save(outputFile.toString(), saveOptions)and save the HTML output.
public static void convertPdfToHtmlBodyContent(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.setHtmlMarkupGenerationMode(
HtmlSaveOptions.HtmlMarkupGenerationModes.WriteOnlyBodyContent);
saveOptions.setSplitIntoPages(true);
document.save(outputFile.toString(), saveOptions);
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert PDF to HTML with transparent text rendering
Use this example when transparent text should be preserved in the HTML export.
- Open the source PDF in a
Documentinstance. - Create
HtmlSaveOptionsand enable transparent and shadowed text preservation. - Call
document.save(outputFile.toString(), saveOptions)so the transparency-related text appearance is retained in the HTML result. - Save the converted HTML output.
public static void convertPdfToHtmlTransparentTextRendering(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.setSaveTransparentTexts(true);
saveOptions.setSaveShadowedTextsAsTransparentTexts(true);
document.save(outputFile.toString(), saveOptions);
}
System.out.println(inputFile + " converted into " + outputFile);
}
Convert PDF to HTML with document layer rendering
Use this example when PDF layer visibility should be reflected in the HTML result.
- Open the source PDF in a
Documentinstance. - Create
HtmlSaveOptionsand enablesetConvertMarkedContentToLayers(true). - Call
document.save(outputFile.toString(), saveOptions)so marked PDF content is mapped into HTML layers. - Save the exported HTML files.
public static void convertPdfToHtmlDocumentLayersRendering(Path inputFile, Path outputFile) {
try (Document document = new Document(inputFile.toString())) {
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.setConvertMarkedContentToLayers(true);
document.save(outputFile.toString(), saveOptions);
}
System.out.println(inputFile + " converted into " + outputFile);
}