Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active January 14, 2021 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/5e71e1be8d062c0f8359bf90d5c71a04 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/5e71e1be8d062c0f8359bf90d5c71a04 to your computer and use it in GitHub Desktop.
Convert HTML File Webpage or String to Image like JPG, PNG, TIFF, BMP, GIF, etc.
// Initialize an HTML document from the html file
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("document.html");
try {
// Initialize ImageSaveOptions
com.aspose.html.saving.ImageSaveOptions options = new com.aspose.html.saving.ImageSaveOptions(com.aspose.html.rendering.image.ImageFormat.Bmp);
// Convert HTML to BMP
com.aspose.html.converters.Converter.convertHTML(document, options, "output.bmp");
} finally {
if (document != null) {
document.dispose();
}
}
// Load input HTML document
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("document.html");
try {
// Initialize ImageSaveOptions
com.aspose.html.saving.ImageSaveOptions options = new com.aspose.html.saving.ImageSaveOptions(com.aspose.html.rendering.image.ImageFormat.Jpeg);
// Convert HTML to output JPG image
com.aspose.html.converters.Converter.convertHTML(document, options, "output.jpg");
} finally {
if (document != null) {
document.dispose();
}
}
// Initialize an HTML document from the html file
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("document.html");
try {
// Initialize ImageSaveOptions
com.aspose.html.saving.ImageSaveOptions options = new com.aspose.html.saving.ImageSaveOptions(com.aspose.html.rendering.image.ImageFormat.Png);
// Convert HTML to PNG
com.aspose.html.converters.Converter.convertHTML(document, options, "output.png");
} finally {
if (document != null) {
document.dispose();
}
}
// Initialize an HTML document from the html file
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(dataDir + "document.html");
try {
// Set up the page-size 3000x1000 pixels and change the background color to green
com.aspose.html.saving.ImageSaveOptions options = new com.aspose.html.saving.ImageSaveOptions(com.aspose.html.rendering.image.ImageFormat.Tiff);
com.aspose.html.rendering.PageSetup pageSetup = new com.aspose.html.rendering.PageSetup();
com.aspose.html.drawing.Page anyPage = new com.aspose.html.drawing.Page();
anyPage.setSize(
new com.aspose.html.drawing.Size(
com.aspose.html.drawing.Length.fromPixels(3000),
com.aspose.html.drawing.Length.fromPixels(1000)
)
);
pageSetup.setAnyPage(anyPage);
options.setPageSetup(pageSetup);
// Set background color for output image
options.setBackgroundColor(com.aspose.html.drawing.Color.getGreen());
// Call the ConvertHTML to convert 'document.html' into tiff image
com.aspose.html.converters.Converter.convertHTML(dataDir + "document.html", options, dataDir + "output.tiff");
} finally {
if (document != null) {
document.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment