使用 Aspose.HTML for Java 将 Markdown 转换为图像 – PNG、JPG、BMP、GIF、TIFF
如果您想创建可视化的内容表示,在社交媒体上分享,将其嵌入网站或博客文章,或将其用作演示文稿的一部分,那么 Markdown 到图片的转换就非常有用。Aspose.HTML for Java 库提供了大量 Markdown 转换图片的功能,如 JPG、PNG、BMP、TIFF 和 GIF。将 Markdown 转换为任何支持的图像格式时,都必须遵循以下步骤:
- 打开一个 Markdown 文件。
- 将 Markdown 文件转换为 HTML 文档
- 创建 ImageSaveOptions 对象。
- 使用 Converter 类将 HTML 文档转换为所选图像格式。
唯一的区别是使用
ImageSaveOptions 类指定输出图像格式。ImageSaveOptions(format) 构造函数用指定的格式初始化选项对象。您可以将图像格式设置为 JPG、PNG、BMP、GIF 或 TIFF。默认的 ImageFormat 是 PNG。
在本文中,您将了解如何使用 Aspose.HTML for Java 将 Markdown 转换为图像,以及如何应用
ImageSaveOptions 您可以轻松使用 Java 示例将 Markdown 转换为图像,这里详细介绍了如何将 Markdown 转换为 PNG、JPG、BMP、GIF 和 TIFF。只需将 ImageFormat 设置为 ImageSaveOptions!
将 Markdown 转换为 PNG
Converter 类的静态方法主要用于将 Markdown 文件转换为各种格式的最简单方法。只需几行代码,您就可以在 Java 应用程序中将 Markdown 转换为图像!
- 使用
convertMarkdown(
sourcePath) 方法将 Markdown 转换为 HTML 文档。 - 创建一个新的
ImageSaveOptions 对象。默认图像格式为
ImageFormat.Png。 - 使用
Converter 类的
сonvertHTML(document, options, savePath)方法可将中间 HTML 文档保存为 PNG 图像。该方法将document、options和savePath作为参数,并执行转换。
例如,如果您需要将 Markdown 文档渲染为 PNG 文件格式,下面的示例演示了如何简单地实现这一功能:
1// Convert Markdown to PNG using Java
2
3// Prepare a simple Markdown example
4String code = "### Hello, World\n\n" +
5 "[visit applications](https://products.aspose.app/html/family)";
6
7// Create a Markdown file
8try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.md")) {
9 fileWriter.write(code);
10}
11
12// Convert Markdown to HTML document
13HTMLDocument document = Converter.convertMarkdown("document.md");
14
15// Convert HTML document to PNG image file format
16Converter.convertHTML(document, new ImageSaveOptions(ImageFormat.Png), "output_md.png");保存选项 – ImageSaveOptions 类
Aspose.HTML for Java 可使用 ImageSaveOptions 以编程方式将 Markdown 转换为图像,并可完全控制各种转换选项。该功能允许用户通过设置 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. |
有关如何使用 ImageSaveOptions 自定义转换过程的详细信息,请参阅 Fine-Tuning Converters 一文。
使用 ImageSaveOptions 将 Markdown 转换为 JPG
Aspose.HTML for Java 允许使用默认或自定义保存选项将 Markdown 转换为图像。通过设置 ImageSaveOptions 类的不同选项,您可以控制输出图像的页面大小、边距、背景颜色和分辨率设置。
下面的 Java 示例展示了如何使用 ImageSaveOptions 创建具有自定义页面大小、页边距、分辨率和渲染质量的 JPG 文件:
- 使用
convertMarkdown(
sourcePath) 方法将 Markdown 转换为 HTML 文档。 - 创建一个新的
ImageSaveOptions 对象,其中包含 JPG
ImageFormat并指定所需的保存选项:- Use the
setSmoothingMode()method to set the quality of the image smoothing to high. - Use the
setHorizontalResolution()andsetVerticalResolution()methods to set the horizontal and vertical resolution of the image to 200. - Use the
setAnyPage()method to set the size of the image and margins.
- Use the
- 使用
convertHTML(document, options, savePath)方法将 Markdown 文件保存为 JPG 图像。该方法接收document、options、输出文件路径savePath并执行转换操作。
1// Convert Markdown to JPG in Java with custom settings
2
3// Convert Markdown to HTML
4HTMLDocument document = Converter.convertMarkdown("nature.md");
5
6// Initialize ImageSaveOptions
7ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
8options.setSmoothingMode(SmoothingMode.HighQuality);
9options.setHorizontalResolution(Resolution.to_Resolution(200));
10options.setVerticalResolution(Resolution.to_Resolution(200));
11options.setBackgroundColor(Color.getAliceBlue());
12options.getPageSetup().setAnyPage(new Page(new Size(600, 950), new Margin(30, 20, 10, 10)));
13
14// Convert HTML to JPG
15Converter.convertHTML(document, options, "nature-options.jpg");结论
将 Markdown 转换为 PNG、JPG、BMP、GIF 或 TIFF 等图像格式,是在演示文稿、社交媒体文章或网页中直观呈现内容的一种实用方法。Aspose.HTML for Java 提供了强大的 API,可轻松灵活地实现这一目标。使用 ImageSaveOptions类,开发人员可以自定义图像属性(如格式),从而在支持的图像格式之间进行无缝选择。此外,API 还支持对分辨率、边距、背景颜色、渲染质量等输出属性进行微调,以满足特定用户的要求。
将 Markdown 转换为图像格式涉及一个重要的中间步骤:将 Markdown 转换为 HTML。Aspose.HTML for Java 通过其 Converter类实现了这一过程,确保准确生成 HTML,为后续转换为图像奠定基础。
您可以从 GitHub 下载完整的示例和数据文件。
Aspose.HTML提供了一个免费的在线 Markdown to Image Converter,可以将Markdown高质量、方便、快速地转换为JPG。如果您需要在 PowerPoint 演示文稿中包含 Markdown 文件或通过电子邮件发送这些文件,请将它们转换为相应的图片格式,然后随意使用!只需上传、转换文件,几秒钟就能得到结果!