";
5
6try (java.io.FileWriter fileWriter = new java.io.FileWriter($o("document.html"))) {
7 fileWriter.write(code);
8}
9
10// Create an instance of the Configuration class
11Configuration configuration = new Configuration();
12
13// Add ErrorMessageHandler to the chain of existing message handlers
14INetworkService network = configuration.getService(INetworkService.class);
15LogMessageHandler logHandler = new LogMessageHandler();
16network.getMessageHandlers().addItem(logHandler);
17
18// Initialize an HTML document with specified configuration
19// During the document loading, the application will try to load the image and we will see the result of this operation in the console
20HTMLDocument document = new HTMLDocument($o("document.html"), configuration);
21
22// Convert HTML to PNG
23Converter.convertHTML(document, new ImageSaveOptions(), $o("output.png"));
```
--------------------------------
### Save Webpage with Default Save Options
Source: https://docs.aspose.com/html/java/website-to-html
This example demonstrates how to save a single web page to a local file using the default settings of the Aspose.HTML for Java API. By default, only resources from the same domain as the specified URL will be downloaded.
```APIDOC
## Save Webpage with Default Save Options
### Description
Saves a web page from a given URL to a local file using default resource handling options. Resources from external domains are not downloaded.
### Method
`document.save(savePath)`
### Endpoint
N/A (Local file operation)
### Parameters
#### Path Parameters
- `savePath` (String) - Required - The local file path where the HTML content will be saved.
#### Query Parameters
None
#### Request Body
None
### Request Example
```java
// Initialize an HTML document from a URL
final HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/net/message-handlers/");
// Prepare a path to save the downloaded file
String savePath = "root/result.html";
// Save the HTML document to the specified file
document.save(savePath);
```
### Response
#### Success Response (200)
- `void` - The operation saves the file locally without returning any specific value.
```
--------------------------------
### Get Accessibility Criterion and Techniques in Java
Source: https://docs.aspose.com/html/java/web-accessibility-rules
This Java code demonstrates how to use the Aspose.HTML library to retrieve a specific WCAG success criterion by its code. It initializes the WebAccessibility object, navigates through principles and guidelines, and then fetches the criterion. The code prints the criterion's code, description, and level, followed by all its sufficient techniques.
```java
/*
* Get accessibility criterion and its sufficient techniques in Java
*/
// Initialize a webAccessibility container
WebAccessibility webAccessibility = new WebAccessibility();
// Get the principle by code
Principle principle = webAccessibility.getRules().getPrinciple("1");
// Get guideline
Guideline guideline = principle.getGuideline("1.1");
// Get criterion by code
Criterion criterion = guideline.getCriterion("1.1.1");
if (criterion != null) {
System.out.println(String.format("%s: %s - %s",
criterion.getCode(),
criterion.getDescription(),
criterion.getLevel()
));
// @output: 1.1.1:Non-text Content - A
// Get all Sufficient Techniques and write to console
for (IRule technique : criterion.getSufficientTechniques())
System.out.println(String.format("%s: %s",
technique.getCode(),
technique.getDescription()
));
}
```
--------------------------------
### Convert Markdown to HTML and then to PDF using Java
Source: https://docs.aspose.com/html/java/convert-markdown-to-html
This Java example illustrates converting a Markdown file to an HTML document in memory using Converter.convertMarkdown, then saving this HTML document as a PDF file. It utilizes PdfSaveOptions for PDF specific settings and requires the Aspose.HTML for Java library.
```java
// Convert Markdown to PDF using Java
// Convert Markdown to HTML
HTMLDocument document = Converter.convertMarkdown($i("nature.md"));
// Initialize PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
// Convert the HTML document to PDF file format
Converter.convertHTML(document, options, $o("nature-output.pdf"));
```
--------------------------------
### Render HTML to PDF and Adjust to Widest Page in Java
Source: https://docs.aspose.com/html/java/fine-tuning-converters
This Java code snippet shows how to convert HTML to PDF while adjusting the page size to fit the widest content. It defines HTML content with different widths, initializes an HTMLDocument, configures page setup options to enable auto-adjustment, and renders to PDF.
```java
// Render HTML to PDF and adjust to the widest page with Java
// Prepare HTML code
String code = " \n" +
" This is bold text inside the div.
"); // Append the div to the body document.getBody().appendChild(divElement); // Get the inner HTML of the div String innerHtmlContent = divElement.getInnerHTML(); System.out.println("Inner HTML of the div: " + innerHtmlContent); // Output: Inner HTML of the div:This is bold text inside the div.
// Clean up the document document.dispose(); } } ``` -------------------------------- ### Convert HTML to PDF using Aspose.HTML for Java Source: https://docs.aspose.com/html/java/faq This snippet demonstrates the basic conversion of an HTML file to a PDF document using Aspose.HTML for Java. It requires the `Converter` class and `PdfSaveOptions`. The input is an HTML file path and the output is a PDF file path. ```java Converter.convertHTML(Input("document.html"), new PdfSaveOptions(), Output("output.pdf")); ``` -------------------------------- ### Save HTML to MHTML Source: https://docs.aspose.com/html/java/save-a-document This example shows how to convert and save an HTML document into a single MHTML (MHT) file. MHTML is a web-page archive format that stores all resources within a single file. ```APIDOC ## Save HTML to MHTML ### Description This endpoint converts an HTML document into an MHTML file. MHTML is a web archive format that encapsulates the HTML content and its associated resources into a single file. ### Method POST (Implicit - used within a Java application context) ### Endpoint Document.save(path, saveFormat) ### Parameters #### Request Body - **document** (HTMLDocument) - The HTML document object to save. - **path** (string) - The file path where the MHTML file will be saved. - **saveFormat** (HTMLSaveFormat) - The format to save the document in. Use `HTMLSaveFormat.MHTML` for MHTML. ### Request Example ```java // Prepare a simple HTML file with a linked document java.nio.file.Files.write( java.nio.file.Paths.get($o("document.html")), "Hello, World!
linked file".getBytes()); // Prepare a simple linked HTML file java.nio.file.Files.write( java.nio.file.Paths.get($o("linked-file.html")), "Hello, linked file!
".getBytes()); // Load the "document.html" into memory HTMLDocument document = new HTMLDocument($o("document.html")); // Save the document to MHTML format document.save($o("save-to-MTHML.mht"), HTMLSaveFormat.MHTML); ``` ### Response #### Success Response (200) - **None** (The operation saves a file to the specified path.) #### Response Example (No explicit response body, file is saved to disk.) ``` -------------------------------- ### Create HTML for Color Contrast Check in Java Source: https://docs.aspose.com/html/java/check-color-contract This Java code snippet demonstrates how to create an HTML file with specific CSS styles to visually represent good and bad color contrast. This file is then used as input for accessibility checks. ```html