Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
将 HTML 转换为图像可帮助你创建可视化的网页内容,用于文档、报告、演示或发送电子邮件。例如,使用 HTML 图像转换功能,你可以快速获得基于 HTML 文档的图像,如报告、网页或图表的预览或缩略图。
Aspose.HTML for Java 库允许您将 HTML 文档转换为JPG、PNG、GIF、TIFF和BMP文件格式,并可完全控制各种转换参数。只需几个必要的步骤,即可完成任何 HTML 到图片的转换:
convertHTML() 方法,并向其传递所需的参数。本文将介绍如何将 HTML 转换为图像文件格式以及如何使用 ImageSaveOptions。让我们通过 Java 示例来了解 HTML 转换为图像的情况!
只需几行代码,您就能在 Java 应用程序中将 HTML 转换为图片!下面的 Java 代码展示了 HTML 到 JPG 的转换:
1// Convert HTML to JPG in one line using Java
2
3// Invoke the convertHTML() method to convert HTML code to image
4Converter.convertHTML(
5 "<h1>Convert HTML to JPG!</h1>",
6 ".",
7 new ImageSaveOptions(ImageFormat.Jpeg),
8 "convert-with-single-line.jpg"
9);在示例中,我们使用了
Converter 类中的convertHTML(content,baseUri,options,outputPath)方法,该方法需要四个参数:包含要转换的 HTML 代码的字符串、输入 HTML 文件的基本文件夹、定义输出图像选项(如图像格式)的 ImageSaveOptions 类实例,以及保存转换后图像的输出文件路径。
JPG 格式是常用的图像格式,因为它支持高压缩比,使您可以使用较小的文件而不会明显降低质量。这使它成为在线存储图像或通过电子邮件或其他数字媒体共享图像的理想选择。要将 HTML 转换为 JPG,应遵循以下几个步骤:
ImageFormat.Jpeg 创建一个新的
ImageSaveOptions 对象。默认图像格式为 ImageFormat.Png。convertHTML(document, options, outputPath) 方法将 HTML 保存为 JPG 图像。下面的 Java 代码片段展示了如何使用 Aspose.HTML 将 HTML 转换为 JPG:
1// Convert HTML to JPG using Java
2
3// Prepare HTML code and save it to a file
4String code = "<span>Hello,</span> <span>World!!</span>";
5try (java.io.FileWriter fileWriter = new java.io.FileWriter("jpg.html")) {
6 fileWriter.write(code);
7}
8
9// Initialize an HTML document from the file
10HTMLDocument document = new HTMLDocument("jpg.html");
11
12// Create an instance of the ImageSaveOptions class
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
14
15// Convert HTML to JPG
16Converter.convertHTML(document, options, "jpg-output.jpg");您可以从 GitHub 下载完整的示例和数据文件。
PNG 是一种流行的图像文件格式,支持无损图像压缩,因此被广泛使用。将 HTML 文件转换为 PNG 图像有多种用途,如将网页添加到 PowerPoint 演示文稿、插入博客或通过电子邮件发送。在下面的 Java 示例中,我们将逐步说明如何使用默认保存选项将 HTML 转换为 PNG:
options 对象,以便为转换过程指定不同的设置。convertHTML(document, options, outputPath) 方法将 HTML 保存为 PNG 图像。您需要将
HTMLDocument、
ImageSaveOptions 和输出文件路径作为参数传递给 convertHTML() 方法。 1// Convert HTML to PNG using Java
2
3// Prepare HTML code and save it to a file
4String code = "<span>Hello,</span> <span>World!!</span>";
5try (java.io.FileWriter fileWriter = new java.io.FileWriter("png.html")) {
6 fileWriter.write(code);
7}
8
9// Initialize an HTML document from the file
10HTMLDocument document = new HTMLDocument("png.html");
11
12// Initialize ImageSaveOptions
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
14
15// Convert HTML to PNG
16Converter.convertHTML(document, options, "png-output.png");下面的示例展示了如何使用 Aspose.HTML for Java 将 HTML 转换为 BMP。
ImageFormat.Bmp。默认情况下,格式属性为 PNG。convertHTML(document, options, outputPath) 方法将 HTML 保存为 BMP 图像。该方法需要传递
HTMLDocument、
ImageSaveOptions 和输出文件路径作为参数。 1// Convert HTML to BMP using Java
2
3// Prepare HTML code and save it to a file
4String code = "<span>Hello,</span> <span>World!!</span>";
5try (java.io.FileWriter fileWriter = new java.io.FileWriter("bmp.html")) {
6 fileWriter.write(code);
7}
8
9// Initialize an HTML document from the file
10HTMLDocument document = new HTMLDocument("bmp.html");
11
12// Initialize ImageSaveOptions
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
14
15// Convert HTML to BMP
16Converter.convertHTML(document, options, "bmp-output.bmp");下面的 Java 代码片段展示了如何使用 Aspose.HTML for Java 将 HTML 转换为 GIF。
convertHTML() 方法将 HTML 保存为 GIF 图像。 1// Convert HTML to GIF using Java
2
3// Prepare HTML code and save it to a file
4String code = "<span>Hello,</span> <span>World!!</span>";
5try (java.io.FileWriter fileWriter = new java.io.FileWriter("gif.html")) {
6 fileWriter.write(code);
7}
8
9// Initialize an HTML document from the file
10HTMLDocument document = new HTMLDocument("gif.html");
11
12// Initialize ImageSaveOptions
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
14
15// Convert HTML to GIF
16Converter.convertHTML(document, options, "gif-output.gif");下面的 Java 代码片段展示了如何使用 Aspose.HTML for Java 将 HTML 转换为 TIFF。
convertHTML(document, options, outputPath) 方法将 HTML 保存为 TIFF 图像。您需要将
HTMLDocument、
ImageSaveOptions 和输出文件路径作为参数传递给 convertHTML() 方法。 1// Convert HTML to TIFF using Java
2
3// Prepare HTML code and save it to a file
4String code = "<span>Hello,</span> <span>World!!</span>";
5try (java.io.FileWriter fileWriter = new java.io.FileWriter("tiff.html")) {
6 fileWriter.write(code);
7}
8
9// Initialize an HTML document from the file
10HTMLDocument document = new HTMLDocument("tiff.html");
11
12// Initialize ImageSaveOptions
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
14
15// Convert HTML to TIFF
16Converter.convertHTML(document, options, "tiff-output.bmp");Aspose.HTML for Java 允许使用默认或自定义保存选项将 HTML 转换为图像。通过 ImageSaveOptions 可以自定义渲染过程。您可以指定 image format、页面大小、页边距、 compression level、 media type等。
| Method | Description |
|---|---|
| setCompression(value) | Sets the Tagged Image File Format (TIFF) Compression. By default this property is Compression.LZW. |
| getCss | Gets a CssOptions object which is used for configuration of CSS properties processing. |
| setFormat(value) | Sets ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default this property is ImageFormat.Png. |
| setBackgroundColor(value) | Sets Color which will fill background of every page. Default value is Color.Transparent(Color.getTransparent()). |
| setPageSetup(value) | Gets a page setup object is used for configuration output page-set. |
| setHorizontalResolution(value) | Sets horizontal resolution for output images in pixels per inch. The default value is 300 dpi. |
| setVerticalResolution(value) | Sets vertical resolution for output images in pixels per inch. The default value is 300 dpi. |
| setSmoothingMode(value) | Sets the rendering quality for this image. |
| getText() | Gets a TextOptions object which is used for configuration of text rendering. |
下面的 Java 示例展示了如何使用 ImageSaveOptions 并创建具有自定义分辨率、页面大小和背景颜色的输出图像:
1// Convert HTML to JPG in Java with with custom background, resolution, and page size settings
2
3// Prepare HTML code and save it to a file
4String code = "<h1> Image SaveOptions </h1> " +
5 "<p>Using ImageSaveOptions Class, you can programmatically apply a wide range of " +
6 "conversion parameters such as BackgroundColor, Format, Compression, PageSetup, etc.</p>";
7
8FileHelper.writeAllText("save-options.html", code);
9
10// Initialize an HTML Document from the HTML file
11HTMLDocument document = new HTMLDocument("save-options.html");
12
13// Initialize an ImageSaveOptions object and customize save options
14ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
15options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
16options.setVerticalResolution(new Resolution(200, UnitType.AUTO));
17options.setBackgroundColor(Color.getAntiqueWhite());
18options.getPageSetup().setAnyPage(new Page(new Size(500, 250), new Margin(40, 40, 20, 20)));
19
20// Convert HTML to JPG
21Converter.convertHTML(document, options, "save-options-output.jpg");要了解有关 ImageSaveOptions 的更多信息,请阅读 Fine-Tuning Converters 一文。
将 HTML 转换为图像是一种强大的解决方案,可用于创建可视化的网页内容,用于文档、演示、预览、缩略图等。
Aspose.HTML for Java 简化了这一过程,允许您将 HTML 转换为多种图像格式,包括 JPG、PNG、BMP、GIF 和 TIFF。使用 ImageSaveOptions类,您可以自定义转换过程的关键方面,如图像分辨率、格式、背景颜色、压缩等。
本文中的 Java 示例展示了使用 Aspose.HTML for Java 以最少的代码将 HTML 转换为图像的简便性和灵活性。从快速的单行转换到更复杂的自定义场景,Java 库提供了将 HTML 高效转换为图像的所有工具。
您可以从 GitHub 下载完整的示例和数据文件。
Aspose.HTML 提供免费的在线 HTML 到图片转换器,可将 HTML 转换为高质量图片,简单快捷。只需上传、转换您的文件,几秒钟就能得到结果!
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.