
PDF offers to share and print read-only documents without losing documents formatting. We can easily convert PDF documents to HTML web pages and view them in any browser. In this article, we will learn how to convert PDF documents to HTML webpages using Java.
The following topics shall be covered in this article:
- Java API to Convert PDF to HTML — Free Download
- PDF to HTML Conversion using Java
- Convert Range of Pages from PDF to HTML
- Convert Specific Pages of PDF to HTML
- Password-Protected PDF to HTML Conversion in Java
- PDF to HTML Conversion with Watermark in Java
Java API to Convert PDF to HTML — Free Download
For converting PDF to HTML, we will be using GroupDocs.Conversion for Java API. It provides fast, efficient, and reliable file conversion solutions to end-users. Please either download the JAR of the API or just add the following pom.xml configuration in a Maven-based Java application.
<repository>
<id>GroupDocsJavaAPI</id>
<name>GroupDocs Java API</name>
<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
<groupId>com.groupdocs</groupId>
<artifactId>groupdocs-conversion</artifactId>
<version>21.10.1</version>
</dependency>
PDF to HTML Conversion using Java
We can easily convert PDF documents to HTML webpages programmatically by following the simple steps given below:
- Firstly, load a PDF document using the Converter class.
- Next, create an instance of the MarkupConvertOptions class.
- Then, optionally set various convert options such as FixedLayout, FixedLayoutShowBorders, etc.
- Finally, convert PDF to HTML using the Converter.Convert() method. It takes output file path and convert options as arguments.
The following code sample shows how to convert a PDF document to an HTML webpage using Java.
// This code example demonstrates how to convert a PDF document to HTML file. | |
// Initialize Convert class object | |
Converter converter = new Converter("C:\\Files\\Conversion\\sample.pdf"); | |
// Define Convert Options | |
MarkupConvertOptions options = new MarkupConvertOptions(); | |
options.setFixedLayout(true); | |
// Convert | |
String outputFile = "C:\\Files\\Conversion\\sample.html"; | |
converter.convert(outputFile, options); |

PDF to HTML Conversion using Java.
Convert Range of Pages from PDF to HTML
We can convert a range of pages of a PDF document to HTML by following the steps given below:
- Firstly, load a PDF document using the Converter class.
- Next, create an instance of the MarkupConvertOptions class.
- Then, set page number to start conversion from.
- After that, set pages count to convert total number of pages.
- Finally, convert PDF to HTML using the Converter.Convert() method.
The following code sample shows how to convert a range of pages from a PDF document to an HTML file using Java.
// This code example demonstrates how to convert a range of PDF pages to HTML file. | |
// Initialize Convert class object | |
Converter converter = new Converter("C:\\Files\\Conversion\\sample.pdf"); | |
// Define Convert Options | |
MarkupConvertOptions options = new MarkupConvertOptions(); | |
options.setPageNumber(1); // Starting page number | |
options.setPagesCount(2); // Total number of pages to convert | |
// Convert | |
String outputFile = "C:\\Files\\Conversion\\sample_N_pages.html"; | |
converter.convert(outputFile, options); |
Convert Specific Pages of PDF to HTML
We can convert specific pages of a PDF document to HTML by following the steps given below:
- Firstly, load a PDF document using the Converter class.
- Next, create an instance of the MarkupConvertOptions class.
- Then, provide specific page numbers in a comma-separated list to convert.
- Finally, convert PDF to HTML using the Converter.Convert() method.
The following code sample shows how to convert specific pages of a PDF document to an HTML file using Java.
// This code example demonstrates how to convert specific pages of a PDF document to HTML file. | |
// Initialize Convert class object | |
Converter converter = new Converter("C:\\Files\\Conversion\\sample.pdf"); | |
// Define Convert Options | |
MarkupConvertOptions options = new MarkupConvertOptions(); | |
options.setPages(Arrays.asList( 1, 3)); // Page numbers to convert | |
// Convert | |
String outputFile = "C:\\Files\\Conversion\\sample_pages.html"; | |
converter.convert(outputFile, options); |
Convert Password-Protected PDF to HTML in Java
We can also convert password-protected PDF documents to HTML webpages by following the steps given below:
- Firstly, provide password using the PdfLoadOptions class object.
- Next, load a PDF document using the Converter class with PdfLoadOptions.
- Then, create an instance of the MarkupConvertOptions class.
- Finally, convert PDF to HTML using the Converter.Convert() method.
The following code sample shows how to convert a password-protected PDF document to an HTML document using Java.
// This code example demonstrates how to convert Password-Protected PDF to HTML. | |
// Define load options | |
PdfLoadOptions loadOptions = new PdfLoadOptions(); | |
loadOptions.setPassword("12345"); | |
// Initialize Convert class object | |
Converter converter = new Converter("C:\\Files\\Conversion\\sample.pdf", loadOptions); | |
// Define Convert Options | |
MarkupConvertOptions options = new MarkupConvertOptions(); | |
// Convert | |
String outputFile = "C:\\Files\\Conversion\\sample.html"; | |
converter.convert(outputFile, options); |
PDF to HTML Conversion with Watermark in Java
We can convert PDF documents to HTML webpages and add watermarks to converted HTML files by following the steps given below:
- Firstly, load a PDF document using the Converter class.
- Next, create an instance of the WatermarkOptions class.
- Then, set various options such as Text, Color, Width, Height, Font, etc.
- Next, create an instance of the MarkupConvertOptions class.
- After that, assign WatermarkOptions to MarkupConvertOptions.
- Finally, convert PDF to HTML using the Converter.Convert() method.
The following code sample shows how to convert a PDF document to an HTML document with a watermark.
// This code example demonstrates how to convert a PDF to HTML with watermark. | |
// Initialize Convert class object | |
Converter converter = new Converter("C:\\Files\\Conversion\\sample.pdf"); | |
// Define Watermark | |
WatermarkOptions watermark = new WatermarkOptions(); | |
watermark.setText("This is a Sample watermark"); | |
watermark.setColor(Color.red); | |
watermark.setWidth(500); | |
watermark.setHeight(100); | |
watermark.setTop(0); | |
watermark.setLeft(300); | |
watermark.setBackground(true); | |
// Define Convert Options | |
MarkupConvertOptions options = new MarkupConvertOptions(); | |
options.setWatermark(watermark); | |
// Output file path | |
String outputFile = "C:\\Files\\Conversion\\sampleWithWatermark.html"; | |
// Convert | |
converter.convert(outputFile, options); |

PDF to HTML Conversion with Watermark in Java.
Get a Free License
Please try the API without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, we have learned how to convert PDF documents to HTML webpages in Java. We have also seen how to convert a password-protected PDF file to HTML and add a watermark to the converted file programmatically. Besides, you can learn more about GroupDocs.Conversion for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.