Created
June 4, 2024 10:29
-
-
Save conholdate-gists/9ece2ce3fd04f97a90f19f46dccacd5a to your computer and use it in GitHub Desktop.
Convert HTML to Image in Java | HTML to JPG PNG BMP TIFF Photo Converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Initialize an HTML document from the file | |
var document = new com.aspose.html.HTMLDocument("spring.html"); | |
// Initialize ImageSaveOptions | |
var options = new com.aspose.html.saving.ImageSaveOptions(com.aspose.html.rendering.image.ImageFormat.Jpeg); | |
// Convert HTML to JPG | |
com.aspose.html.converters.Converter.convertHTML(document, options, "spring-output.jpg"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Prepare HTML code and save it to a file | |
var code = "<h1> Image SaveOptions </h1>\r\n" + | |
"<p>Using ImageSaveOptions Class, you can programmatically apply a wide range of conversion parameters such as BackgroundColor, Format, Compression, PageSetup, etc.</p>\r\n"; | |
try (java.io.PrintWriter out = new java.io.PrintWriter("spring.html")) { | |
out.println(code); | |
} | |
// Initialize an HTML document from the file | |
var document = new com.aspose.html.HTMLDocument("spring.html"); | |
// Set up the page-size and margins | |
var options = new com.aspose.html.saving.ImageSaveOptions(com.aspose.html.rendering.image.ImageFormat.Png); | |
var page = new com.aspose.html.drawing.Page(new com.aspose.html.drawing.Size(400, 250), new com.aspose.html.drawing.Margin(40, 40, 20, 20)); | |
options.getPageSetup().setAnyPage(page);; | |
options.setBackgroundColor(com.aspose.html.drawing.Color.getAntiqueWhite()); | |
// Convert HTML to PNG | |
com.aspose.html.converters.Converter.convertHTML(document, options, "spring-output.png"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment