用 Java 保存 HTML 文档

在转换和保存过程中, aspose.html.Saving 包由数据类提供,用于描述特定的保存选项。在处理 HTML 文件或从头开始创建 HTML 文档后,您可以使用 HTMLDocument.save() 方法之一保存更改。Java 库提供了 [SaveOptions]https://reference.aspose.com/html/java/com.aspose.html.saving/saveoptions/ 和 ResourceHandlingOptions 类,允许您设置保存操作的选项。

Aspose.HTML for Java 库有两种不同的创建输出文件的概念:

  • 第一种构想基于创建 HTML 类文件作为输出,即 HTML、MHTML、Markdown 和 SVG 文件。 SaveOptions 是这种方法的基类,有助于处理脚本、样式、图像等相关资源的保存过程。
  • 第二个概念可用来直观地表示 HTML 的结果。这个概念的基类是 RenderingOptions;它有专门的方法来指定页面大小、页边距、分辨率、用户样式等。

本文仅介绍如何使用 SaveOptions 类和 save() 方法保存 HTML 文档。要了解有关渲染机制的更多信息,请参阅 微调转换器 一文。

保存 HTML

完成 HTML 更改后,您可能需要使用 HTMLDocument.save() 方法之一保存文档。下面的示例是保存 HTML 文件最简单的方法:

 1// Save HTML to a file using Java
 2
 3// Initialize an empty HTML document
 4HTMLDocument document = new HTMLDocument();
 5
 6// Create a text node and add it to the document
 7Text text = document.createTextNode("Hello, World!");
 8document.getBody().appendChild(text);
 9
10// Save the HTML document to a file
11document.save("save-to-file.html");

上面的示例非常简单。不过,在实际应用中,你往往需要对保存过程进行额外的控制。接下来的几节将介绍如何使用资源处理选项或将文档保存为不同格式。

将 HTML 保存到文件

下面的代码片段展示了如何使用 ResourceHandlingOptions 来管理链接文档。我们从头开始创建一个简单的 HTML 文件,示例中的链接文档名为 “linked.html”。

为了应用保存选项,我们使用 HTMLSaveOptions() 构造函数创建一个 HTMLSaveOptions 类的实例。然后,我们使用 getResourceHandlingOptions() 方法,将保存选项中链接资源的最大处理深度设为 1,这意味着保存 HTML 文档时将包括主文档和深度为 1 的任何直接链接资源。 最后,我们调用文档对象的 save(path, options) 方法以及指定的保存选项。

 1// Save HTML with a linked resources using Java
 2
 3// Prepare a simple HTML file with a linked document
 4java.nio.file.Files.write(
 5        java.nio.file.Paths.get("save-with-linked-file.html"),
 6        "<p>Hello, World!</p><a href='linked.html'>linked file</a>".getBytes());
 7
 8// Prepare a simple linked HTML file
 9java.nio.file.Files.write(java.nio.file.Paths.get("linked.html"),
10        "<p>Hello, linked file!</p>".getBytes());
11
12// Load the "save-with-linked-file.html" into memory
13HTMLDocument document = new HTMLDocument("save-with-linked-file.html");
14
15// Create an instance of the HTMLSaveOptions class
16HTMLSaveOptions options = new HTMLSaveOptions();
17
18// The following line with the value "0" cuts off all other linked HTML-files while saving this instance
19// If you remove this line or change the value to "1", the "linked.html" file will be saved as well to the output folder
20options.getResourceHandlingOptions().setMaxHandlingDepth(1);
21
22// Save the document with the save options
23document.save("save-with-linked-file_out.html", options);

将 HTML 保存为 MHTML

有时,您需要将 HTML 文档保存为单一的 MHTML 文件。在这种情况下,MHTML文档就非常有用,因为它是一个网页存档文件,可以将所有内容都保存在其中。HTMLSaveFormat 指定了文档的保存格式,可以是 HTML、MHTML 和 MD 格式。下面的示例展示了如何使用 save(path, saveFormat) 方法将 HTML 保存为 MHTML 格式。

 1// Save HTML as MHTML using Java
 2
 3// Prepare a simple HTML file with a linked document
 4java.nio.file.Files.write(
 5        java.nio.file.Paths.get("document.html"),
 6        "<p>Hello, World!</p><a href='linked-file.html'>linked file</a>".getBytes());
 7
 8// Prepare a simple linked HTML file
 9java.nio.file.Files.write(
10        java.nio.file.Paths.get("linked-file.html"),
11        "<p>Hello, linked file!</p>".getBytes());
12
13// Load the "document.html" into memory
14HTMLDocument document = new HTMLDocument("document.html");
15
16// Save the document to MHTML format
17document.save("save-to-MTHML.mht", HTMLSaveFormat.MHTML);

HTML to File 示例一样,您也可以使用 MHTMLSaveOptions 并自定义所需的处理选项。

将 HTML 保存为 Markdown

Markdown 是一种使用纯文本语法的标记语言。要使用 Aspose.HTML 创建 Markdown 文件,请参阅以下示例:

 1// Save HTML as a Markdown file using Java
 2
 3// Prepare HTML code
 4String html_code = "<H2>Hello, World!</H2>";
 5
 6// Initialize a document from a string variable
 7HTMLDocument document = new HTMLDocument(html_code, ".");
 8
 9// Save the document as a Markdown file
10document.save("save-to-MD.md", HTMLSaveFormat.Markdown);

Aspose.HTML for Java 除了 HTML 到 Markdown 的保存功能外,还提供 HTML 到 Markdown 的转换器。有关如何使用 Markdown 转换器的更多信息,请访问 HTML to Markdown 一文。

保存 SVG

通常,SVG 文档是 HTML 文件的一部分;它表示页面上的矢量数据:图像、图标、表格等。不过,SVG 也可以从网页中提取出来,您可以像处理 HTML 文档一样处理它。由于 SVGDocumentHTMLDocument 基于相同的 WHATWG DOM 标准,因此对这两种文档的所有操作,如加载、读取、编辑、转换和保存,都是相似的。因此,所有您可以看到对 HTMLDocument 进行操作的示例也适用于 SVGDocument

要将更改保存为 SVG,请使用下面的方法:

 1// Save HTML as SVG using Java
 2
 3// Prepare SVG code
 4String code = "<svg xmlns='http://www.w3.org/2000/svg' height='200' width='300'>" +
 5        "<g fill='none' stroke-width= '10' stroke-dasharray='30 10'>" +
 6        "<path stroke='red' d='M 25 40 l 215 0' />" +
 7        "<path stroke='black' d='M 35 80 l 215 0' />" +
 8        "<path stroke='blue' d='M 45 120 l 215 0' />" +
 9        "</g>" +
10        "</svg>";
11
12// Initialize an SVG instance from the content string
13SVGDocument document = new SVGDocument(code, ".");
14
15// Save the SVG file to disk
16document.save("save-to-SVG.svg");

结论

  1. aspose.html.Saving包允许开发人员使用 SaveOptions 和 ResourceHandlingOptions 等高级选项自定义和优化保存 HTML 和 SVG 文档的过程。使用这些类,您可以有效地管理链接资源、选择特定的输出格式并自定义保存过程,以满足应用程序的需要。

  2. Aspose.HTML for Java 允许您以各种格式保存 HTML 文档,包括 HTML、MHTML 和 Markdown,提供精确控制和简便性。

  3. 本文中的示例展示了保存操作的简单性和强大功能。请查看 微调转换器 一文,进一步了解渲染选项和其他高级功能。

您可以从 GitHub 下载完整的示例和数据文件。

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.