Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active January 14, 2025 08:16
Show Gist options
  • Save aspose-com-gists/b2199f957c72708d4d2b0de93bca3098 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/b2199f957c72708d4d2b0de93bca3098 to your computer and use it in GitHub Desktop.
This Gist contains examples for Aspose.HTML for Java.
Aspose.HTML for Java
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("FirstFile.html");
java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream("FirstFileOut.html");
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
fileOutputStream.write(bytes);
String style = "<style>\n" +
".st\n" +
"{\n" +
"color:\n" +
"green;\n" +
"}\n" +
"</style >\n" +
"<div id = id1 > Aspose.Html rendering Text in Black Color</div >\n" +
"<div id = id2 class='' st '' > Aspose.Html rendering Text in Green Color</div >\n" +
"<div id = id3 class='' st '' style = 'color: blue;' > Aspose.Html rendering Text in Blue Color</div >\n" +
"<div id = id3 class='' st '' style = 'color: red;' ><font face = 'Arial' > Aspose.Html rendering Text in Red\n" +
"Color</font ></div >\n";
fileOutputStream.write(style.getBytes(java.nio.charset.StandardCharsets.UTF_8));
String pdf_output;
// Create HtmlRenderer object
HtmlRenderer pdf_renderer = new HtmlRenderer();
// Create HtmlDocument instnace while passing path of already created HTML file
HTMLDocument html_document = new HTMLDocument("FirstFileOut.html");
// Set the page size less than document min-width. The content in the resulting file will be cropped becuase of element with width: 200px
PdfRenderingOptions pdf_options =
new PdfRenderingOptions();
PageSetup pageSetup = new PageSetup();
pageSetup.setAnyPage(new Page(new Size(100, 100)));
pageSetup.setAdjustToWidestPage(false);
pdf_options.setPageSetup(pageSetup);
pdf_output = "not-adjusted-to-widest-page_out.pdf";
PdfDevice device = new PdfDevice(pdf_options, pdf_output);
// Render the output
pdf_renderer.render(device, html_document);
// Set the page size less than document min-width and enable AdjustToWidestPage option. The page size of the resulting file will be changed according to content width
pdf_options = new PdfRenderingOptions();
pageSetup = new PageSetup();
pageSetup.setAnyPage(new Page(new Size(100, 100)));
pageSetup.setAdjustToWidestPage(true);
pdf_options.setPageSetup(pageSetup);
pdf_output = "adjusted-to-widest-page_out.pdf";
device = new PdfDevice(pdf_options, pdf_output);
// Render the output
pdf_renderer.render(device, html_document);
// Set input file name.
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("FirstFile.html");
java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream("FirstFileOut.html");
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes);
fileOutputStream.write(bytes);
String style = "<style>\n" +
".st\n" +
"{\n" +
"color: green;\n" +
"}\n" +
"</style>\n" +
"<div id=id1>Aspose.Html rendering Text in Black Color</div>\n" +
"<div id=id2 class=''st''>Aspose.Html rendering Text in Green Color</div>\n" +
"<div id=id3 class=''st'' style='color: blue;'>Aspose.Html rendering Text in Blue Color</div>\n" +
"<div id=id3 class=''st'' style='color: red;'>Aspose.Html rendering Text in Red Color</div>\n";
fileOutputStream.write(style.getBytes(java.nio.charset.StandardCharsets.UTF_8));
// Create HtmlRenderer object
HtmlRenderer renderer = new HtmlRenderer();
// Create HtmlDocument instnace while passing path of already created HTML file
HTMLDocument html_document = new HTMLDocument("FirstFileOut.html");
// Set the page size less than document min-width. The content in the resulting file will be cropped becuase of element with width: 200px
XpsRenderingOptions xps_options = new XpsRenderingOptions();
Page page = new Page(new Size(100, 100));
PageSetup pageSetup = new PageSetup();
pageSetup.setAnyPage(page);
pageSetup.setAdjustToWidestPage(false);
xps_options.setPageSetup(pageSetup);
String output = "not-adjusted-to-widest-page_out.1.xps";
XpsDevice device = new XpsDevice(xps_options, output);
// Render the output
renderer.render(device, html_document);
// Set the page size less than document min-width and enable AdjustToWidestPage option
// The page size of the resulting file will be changed according to content width
xps_options = new XpsRenderingOptions();
page = new Page(new Size(100, 100));
pageSetup = new PageSetup();
pageSetup.setAnyPage(page);
pageSetup.setAdjustToWidestPage(true);
xps_options.setPageSetup(pageSetup);
output = "not-adjusted-to-widest-page_out.2.xps";
device = new XpsDevice(xps_options, output);
// Render the output
renderer.render(device, html_document);
// Create an empty HTML document
HTMLDocument document = new HTMLDocument();
// Create a canvas element
HTMLCanvasElement canvas = (HTMLCanvasElement) document.createElement("canvas");
// with a specified size
canvas.setWidth(300);
canvas.setHeight(150);
// Append the canvas element to the document body
document.getBody().appendChild(canvas);
// Get the canvas rendering context to draw
ICanvasRenderingContext2D context = (ICanvasRenderingContext2D) canvas.getContext("2d");
// Prepare a gradient brush
ICanvasGradient gradient = context.createLinearGradient(0, 0, canvas.getWidth(), 0);
gradient.addColorStop(0, "magenta");
gradient.addColorStop(0.5, "blue");
gradient.addColorStop(1.0, "red");
// Assign the brush to the content
context.setFillStyle(gradient);
context.setStrokeStyle(gradient);
// Write the text
context.fillText("Hello, World!", 10, 90, 500);
// Fill the rectangle
context.fillRect(0, 95, 300, 20);
// Create a PDF output device
PdfDevice device = new PdfDevice("canvas.pdf");
// Render HTML5 Canvas to PDF
document.renderTo(device);
// Initialize a Configuration object
Configuration configuration = new Configuration();
// Get the User Agent Service
IUserAgentService userAgent = configuration.getService(IUserAgentService.class);
// Set a style of custom margins and create marks on it
userAgent.setUserStyleSheet(
"@page { " +
" /* Page margins should be not empty in order to write content inside the margin-boxes */ " +
" margin-top: 1cm; " +
" margin-left: 2cm; " +
" margin-right: 2cm; " +
" margin-bottom: 2cm; " +
" /* Page counter located at the bottom of the page */ " +
" @bottom-right { " +
" -aspose-content: \"Page \" currentPageNumber() \" of \" totalPagesNumber(); " +
" color: green; " +
" } " +
" /* Page title located at the top-center box */ " +
" @top-center { " +
" -aspose-content: \"Hello, World Document Title!!!\"; " +
" vertical-align: bottom; " +
" color: blue; " +
" } " +
"}"
);
// Initialize an HTML document
HTMLDocument document = new HTMLDocument("<div>Hello, World!!!</div>", ".", configuration);
// Initialize an output device
XpsDevice device = new XpsDevice("output.xps");
// Send the document to the output device
document.renderTo(device);
// Prepare a document with HTML5 Canvas inside and save it to the file "document.html"
String code = "<canvas id=myCanvas width='200' height='100' style='border:1px solid #d3d3d3;'></canvas>" +
"<script>" +
"var c = document.getElementById('myCanvas');" +
"var context = c.getContext('2d');" +
"context.font = '20px Arial';" +
"context.fillStyle = 'red';" +
"context.fillText('Hello World', 40, 50);" +
"</script>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the HTML file
HTMLDocument document = new HTMLDocument("document.html");
// Convert HTML to PDF
Converter.convertHTML(document, new PdfSaveOptions(), "output.pdf");
// Initialize an instance of HTML document from "https://httpbin.org/forms/post" url
HTMLDocument document = new HTMLDocument("https://httpbin.org/forms/post");
// Create an instance of FormEditor
FormEditor editor = FormEditor.create(document, 0);
// You can fill in the data using direct access to the input elements, like this:
InputElement custname = editor.addInput("custname");
custname.setValue("John Doe");
document.save("out.html");
// or this:
TextAreaElement comments = editor.getElement(TextAreaElement.class, "comments");
comments.setValue("MORE CHEESE PLEASE!");
// or even by performing a bulk operation, like this one:
java.util.Map<String, String> dictionary = new java.util.HashMap<>();
dictionary.put("custemail", "john.doe@gmail.com");
dictionary.put("custtel", "+1202-555-0290");
// Create an instance of FormSubmitter
FormSubmitter submitter = new FormSubmitter(editor);
// Submit the form data to the remote server
// If you need, you can specify user credentials and timeout for the request, etc.
SubmissionResult result = submitter.submit();
// Check the status of the result object
if (result.isSuccess()) {
// Check whether the result is in json format
if (result.getResponseMessage().getHeaders().getContentType().getMediaType().equals("application/json")) {
// Print result data to console
System.out.println(result.getContent().readAsString());
} else {
// Load the result data as an HTML document
Document doc = result.loadDocument();
// Inspect HTML document here
}
}
package com.aspose.html.documentation.examples.utils;
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-Java
public class MemoryStreamProvider implements java.io.Closeable {
// List of InputStream objects created during document rendering
public java.util.List<java.io.InputStream> lStream = new java.util.ArrayList<>();
@Override
public void close() throws java.io.IOException {
for (java.io.InputStream stream : lStream) {
stream.close();
}
}
}
// Create an instance of MemoryStreamProvider
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Initialize an HTMLDocument instance
HTMLDocument document = new HTMLDocument("<span>Hello, World!!</span>", ".");
// Convert HTML to JPG using the MemoryStreamProvider
Converter.convertHTML(document, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider.lStream);
// Get access to the memory stream that contains the result data
java.io.InputStream memory = streamProvider.lStream.get(0);
memory.reset();
// Flush the result data to the output file
java.nio.file.Files.copy(memory, new java.io.File("output.jpg").toPath());
// Create an empty HTML document
HTMLDocument document = new HTMLDocument();
// Create an instance of the MutationObserver class
MutationObserver observer = new MutationObserver(new MutationCallback() {
@Override
public void invoke(com.aspose.html.utils.collections.generic.IGenericList<MutationRecord> mutations, MutationObserver mutationObserver) {
for (int i = 0; i < mutations.size(); i++) {
MutationRecord record = mutations.get_Item(i);
for (Node node : record.getAddedNodes().toArray()) {
System.out.println("The '" + node + "' node was added to the document.");
}
}
}
});
// Configure options for the MutationObserver
MutationObserverInit config = new MutationObserverInit();
config.setChildList(true);
config.setSubtree(true);
config.setCharacterData(true);
// Pass to observer the target node to observe with the specified configuration
observer.observe(document.getBody(), config);
// Now, we are going to modify DOM tree to check
// Create a paragraph element and append it to the document body
Element p = document.createElement("p");
document.getBody().appendChild(p);
// Create a text and append it to the paragraph
Text text = document.createTextNode("Hello, World!");
p.appendChild(text);
System.out.println("Waiting for mutation. Press any key to continue...");
System.in.read();
HTMLDocument document = new HTMLDocument("canvas.html");
// Create an instance of HTML renderer and XPS output device
HtmlRenderer renderer = new HtmlRenderer();
PdfDevice device = new PdfDevice("canvas.output.pdf");
// Render the document to the specified device
renderer.render(device, document);
// Source EPUB document
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Output file path
String outputFile = "EPUBtoImageOutput.jpeg";
// Convert SVG to Image
Converter.convertEPUB(
fileInputStream,
options,
outputFile
);
// Source EPUB document
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Initialize pdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
options.setJpegQuality(100);
// Output file path
String outputFile = "EPUBtoPDF_Output.pdf";
// Convert EPUB to PDF
Converter.convertEPUB(
fileInputStream,
options,
outputFile
);
// Convert HTML to PDF
Converter.convertHTML("document.html", new PdfSaveOptions(), "output.pdf");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
// Call the convertEPUB() method to convert the EPUB file to BMP
Converter.convertEPUB(fileInputStream, options, "output.bmp");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
// Call the convertEPUB() method to convert the EPUB file to GIF
Converter.convertEPUB(fileInputStream, options, "output.gif");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Call the сonvertEPUB() method to convert EPUB to JPG
Converter.convertEPUB(fileInputStream, options, "input-output.jpg");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Call the сonvertEPUB() method to convert EPUB to PNG
Converter.convertEPUB(fileInputStream, options, "input-output.png");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
// Call the convertEPUB() method to convert the EPUB file to TIFF
Converter.convertEPUB(fileInputStream, options, "output.tiff");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of MemoryStreamProvider
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Convert HTML to JPG using the MemoryStreamProvider
Converter.convertEPUB(fileInputStream, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
// Get access to the memory streams that contain the resulted data
for (int i = 0; i < streamProvider.getStreams().size(); i++) {
OutputStream memory = streamProvider.getStreams().get(i);
memory.seek(0, SeekOrigin.Begin);
// Flush the page to the output file
java.io.File fs = new java.io.File("page_{" + (i + 1) + "}.jpg");
FileHelper.save(memory, fs);
}
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
options.setSmoothingMode(SmoothingMode.HighQuality);
options.setHorizontalResolution(Resolution.to_Resolution(400));
options.setVerticalResolution(Resolution.to_Resolution(400));
options.setBackgroundColor(Color.getAliceBlue());
options.getPageSetup().setAnyPage(new Page(new Size(800, 500), new Margin(30, 20, 10, 10)));
// Convert EPUB to JPG
Converter.convertEPUB(fileInputStream, options, "input-options.jpg");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Invoke the convertEPUB() method to convert EPUB to JPG
Converter.convertEPUB(fileInputStream, new ImageSaveOptions(ImageFormat.Jpeg), "convert-by-two-lines.jpg");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of the PdfSaveOptions class
PdfSaveOptions options = new PdfSaveOptions();
// Call the convertEPUB() method to convert EPUB to PDF
Converter.convertEPUB(fileInputStream, options, "output-epub-to-pdf.pdf");
// Create an instance of MemoryStreamProvider
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Convert EPUB to PDF by using the MemoryStreamProvider class
Converter.convertEPUB(fileInputStream, new PdfSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
OutputStream memory = streamProvider.getStreams().stream().findFirst().get();
memory.seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
java.io.File fs = new java.io.File("stream-provider.pdf");
FileHelper.save(memory, fs);
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions();
options.setBackgroundColor(Color.getAliceBlue());
options.getPageSetup().setAnyPage(new Page());
options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(1000), Length.fromPixels(1000)));
// Call the ConvertEPUB() method to convert EPUB to PDF
Converter.convertEPUB(fileInputStream, options, "input-options.pdf");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Invoke the convertEPUB() method to convert EPUB to PDF
Converter.convertEPUB(fileInputStream, new PdfSaveOptions(), "convert-by-two-lines.pdf");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of MemoryStreamProvider
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Convert HTML to PNG using the MemoryStreamProvider
Converter.convertEPUB(fileInputStream, new ImageSaveOptions(), streamProvider);
// Get access to the memory streams that contain the resulted data
for (int i = 0; i < streamProvider.getStreams().size(); i++) {
OutputStream memory = streamProvider.getStreams().get(i);
memory.seek(0, SeekOrigin.Begin);
// Flush the page to the output file
java.io.File fs = new java.io.File("input-page_{" + (i + 1) + "}.png");
FileHelper.save(memory, fs);
}
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of the PngSaveOptions with a custom page-size and a background-color.
ImageSaveOptions options = new ImageSaveOptions();
PageSetup pageSetup = new PageSetup();
Page anyPage = new Page();
anyPage.setSize(
new Size(
Length.fromPixels(3000),
Length.fromPixels(1000)
)
);
pageSetup.setAnyPage(anyPage);
options.setPageSetup(pageSetup);
options.setBackgroundColor(Color.getAliceBlue());
// Call the ConvertEPUB method to convert the EPUB to PNG.
Converter.convertEPUB(fileInputStream, options, "output.png");
// Open an existing EPUB file for reading
FileInputStream inputStream = new FileInputStream("input.epub");
// Convert EPUB to PNG
Converter.convertEPUB(inputStream, new ImageSaveOptions(), "convert-with-single-line.png");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions();
options.setBackgroundColor(Color.getAliceBlue());
options.setSmoothingMode(SmoothingMode.HighQuality);
options.setVerticalResolution(Resolution.to_Resolution(400));
options.setHorizontalResolution(Resolution.to_Resolution(400));
// Call the convertEPUB() method to convert EPUB to PNG
Converter.convertEPUB(fileInputStream, options, "input-options.png");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of XpsSaveOptions
XpsSaveOptions options = new XpsSaveOptions();
// Call the ConvertEPUB() method to convert EPUB to XPS
Converter.convertEPUB(fileInputStream, options, "input-output.xps");
// Open an existing EPUB file for reading
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Convert EPUB to XPS by using the MemoryStreamProvider class
Converter.convertEPUB(fileInputStream, new XpsSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
OutputStream memory = streamProvider.getStreams().stream().findFirst().get();
memory.seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
java.io.File fs = new java.io.File("stream-provider.xps");
FileHelper.save(memory, fs);
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Create an instance of XpsSaveOptions. Set up the page-size and change the background color to LightGray
XpsSaveOptions options = new XpsSaveOptions();
options.setBackgroundColor(Color.getLightGray());
options.getPageSetup().setAnyPage(new Page());
options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(500), Length.fromPixels(500)));
// Call the convertEPUB() method
Converter.convertEPUB(fileInputStream, options, "input-options.xps");
// Open an existing EPUB file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("input.epub");
// Invoke the ConvertEPUB() method to convert EPUB to XPS
Converter.convertEPUB(fileInputStream, new XpsSaveOptions(), "convert-by-two-lines.xps");
// Initialize an HTML document from a file
HTMLDocument document = new HTMLDocument("canvas.html");
// Initialize DocSaveOptions
DocSaveOptions options = new DocSaveOptions();
// Convert HTML to DOCX
Converter.convertHTML(document, options, "canvas-output.docx");
// Create an instance of MemoryStreamProvider
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Initialize an HTML document
HTMLDocument document = new HTMLDocument("<h1>Convert HTML to DOCX File Format!</h1>", ".");
// Convert HTML to DOCX using the MemoryStreamProvider
Converter.convertHTML(document, new DocSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
OutputStream memory = streamProvider.getStreams().stream().findFirst().get();
memory.seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
java.io.File fs = new java.io.File("stream-provider.docx");
FileHelper.save(memory, fs);
// Initialize an HTML document from a file
HTMLDocument document = new HTMLDocument("canvas.html");
// Initialize DocSaveOptions. Set up the pag size 600x400 pixels and margins
DocSaveOptions options = new DocSaveOptions();
options.getPageSetup().setAnyPage(new Page(new Size(600, 400), new Margin(10, 10, 10, 10)));
// Convert HTML to DOCX
Converter.convertHTML(document, options, "canvas-output-options.docx");
// Invoke the convertHTML() method to convert HTML to DOCX
Converter.convertHTML("<h1>Convert HTML to DOCX!</h1>", ".", new DocSaveOptions(), "convert-with-single-line.docx");
// Prepare HTML code and save it to a file
String code = "<span>Hello,</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("bmp.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("bmp.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
// Convert HTML to BMP
Converter.convertHTML(document, options, "bmp-output.bmp");
// Prepare HTML code and save it to a file
String code = "<span>Hello,</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("gif.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("gif.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
// Convert HTML to GIF
Converter.convertHTML(document, options, "gif-output.gif");
// Prepare HTML code and save it to a file
String code = "<span>Hello,</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("jpg.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("jpg.html");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Convert HTML to JPG
Converter.convertHTML(document, options, "jpg-output.jpg");
// Prepare HTML code and save it to a file
String code = "<span>Hello,</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("png.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("png.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Convert HTML to PNG
Converter.convertHTML(document, options, "png-output.png");
// Prepare HTML code and save it to a file
String code = "<span>Hello,</span> <span>World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("tiff.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("tiff.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
// Convert HTML to TIFF
Converter.convertHTML(document, options, "tiff-output.bmp");
// Create an instance of MemoryStreamProvider
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Initialize an HTML document
HTMLDocument document = new HTMLDocument("<h1>Convert HTML to JPG File Format!</h1>", ".");
// Convert HTML to JPG using the MemoryStreamProvider
Converter.convertHTML(document, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
// Get access to the memory stream that contains the result data
OutputStream memory = streamProvider.getStreams().stream().findFirst().get();
memory.seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
java.io.File fs = new java.io.File("stream-provider.jpg");
FileHelper.save(memory, fs);
// Prepare HTML code and save it to a file
String code = "<h1> Image SaveOptions </h1> " +
"<p>Using ImageSaveOptions Class, you can programmatically apply a wide range of " +
"conversion parameters such as BackgroundColor, Format, Compression, PageSetup, etc.</p>";
FileHelper.writeAllText("save-options.html", code);
// Initialize an HTML Document from the HTML file
HTMLDocument document = new HTMLDocument("save-options.html");
// Initialize an ImageSaveOptions object and customize save options
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
options.setVerticalResolution(new Resolution(200, UnitType.AUTO));
options.setBackgroundColor(Color.getAntiqueWhite());
options.getPageSetup().setAnyPage(new Page(new Size(500, 250), new Margin(40, 40, 20, 20)));
// Convert HTML to JPG
Converter.convertHTML(document, options, "save-options-output.jpg");
// Invoke the convertHTML() method to convert HTML code to image
Converter.convertHTML(
"<h1>Convert HTML to JPG!</h1>",
".",
new ImageSaveOptions(ImageFormat.Jpeg),
"convert-with-single-line.jpg"
);
// Prepare HTML code and save it to a file
String code = "<h1>Header 1</h1>" +
"<h2>Header 2</h2>" +
"<p>Hello, World!!</p>";
FileHelper.writeAllText("document.html", code);
// Call convertHTML() method to convert HTML to Markdown
Converter.convertHTML("document.html", MarkdownSaveOptions.getGit(), "output-git.md");
// Prepare HTML code and save it to a file
String code = "text<div markdown='inline'><code>text</code></div>";
FileHelper.writeAllText("inline.html", code);
// Call convertHTML() method to convert HTML to Markdown
Converter.convertHTML("inline.html", new MarkdownSaveOptions(), "inline-html.md");
// Output file will contain: text\r\n<div markdown="inline"><code>text</code></div>
// Prepare HTML code and save it to the file
String code = "<h1>Header 1</h1>" +
"<h2>Header 2</h2>" +
"<p>Hello, World!!</p>" +
"<a href='aspose.com'>aspose</a>";
FileHelper.writeAllText("options.html", code);
// Create an instance of SaveOptions and set up the rule:
// - only <a> and <p> elements will be converted to Markdown
MarkdownSaveOptions options = new MarkdownSaveOptions();
options.setFeatures(MarkdownFeatures.Link | MarkdownFeatures.AutomaticParagraph);
// Call the convertHTML() method to convert HTML to Markdown
Converter.convertHTML("options.html", options, "options-output.md");
// Prepare HTML code and save it to a file
String code = "<h1>Header 1</h1>\n" +
"<h2>Header 2</h2>\n" +
"<p>Hello, World!!</p>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
fileWriter.write(code);
}
// Call ConvertHTML method to convert HTML to Markdown.
Converter.convertHTML("document.html", new MarkdownSaveOptions(), "output.md");
// Prepare HTML code and save it to a file
String code = "<span>Hello, World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("document.html");
// Initialize MHTMLSaveOptions
MHTMLSaveOptions options = new MHTMLSaveOptions();
// Convert HTML to MHTML
Converter.convertHTML(document, options, "output.mht");
// Prepare HTML code with a link to another file and save it to the file as "document.html"
String code = "<span>Hello, World!!</span> " +
"<a href='document2.html'>click</a>";
FileHelper.writeAllText("document.html", code);
// Prepare HTML code and save it to the file as "document2.html"
code = "<span>Hello, World!!</span>";
FileHelper.writeAllText("document2.html", code);
// Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
MHTMLSaveOptions options = new MHTMLSaveOptions();
options.getResourceHandlingOptions().setMaxHandlingDepth(1);
// Convert HTML to MHTML
Converter.convertHTML("document.html", options, "output-options.mht");
// Invoke the convertHTML() method to convert HTML code to MHTML file
Converter.convertHTML("<h1>Hello, Word!</h1>", ".", new MHTMLSaveOptions(), "convert-with-single-line.mht");
// Invoke the ConvertHTML method to convert the HTML to PDF.
Converter.convertHTML("<span>Hello, World!!</span>", ".", new PdfSaveOptions(), "output.pdf");
// Prepare HTML code and save it to a file
String code = "<span>Hello, World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("link-to-incoming-document.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("link-to-incoming-document.html");
// Initialize PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
// Convert HTML to PDF
Converter.convertHTML(document, options, "document-output.pdf");
// Create an instance of MemoryStreamProvider
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Initialize an HTML document
HTMLDocument document = new HTMLDocument("<h1>Convert HTML to PDF File Format!</h1>", ".");
// Convert HTML to PDF using the MemoryStreamProvider
Converter.convertHTML(document, new PdfSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
OutputStream memory = streamProvider.getStreams().stream().findFirst().get();
memory.seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
java.io.File fs = new java.io.File("stream-provider.pdf");
FileHelper.save(memory, fs);
// Initialize an HTML document from a file
HTMLDocument document = new HTMLDocument("drawing.html");
// Initialize PdfSaveOptions. Set up the page-size 500x300 pixels, margins,
// resolutions and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions();
options.setHorizontalResolution(new Resolution(200, UnitType.AUTO));
options.setVerticalResolution(new Resolution(200, UnitType.AUTO));;
options.setBackgroundColor(Color.getAliceBlue());
options.setJpegQuality(100);
options.getPageSetup().setAnyPage(new Page(new Size(500, 300), new Margin(20, 10, 10, 10)));
// Convert HTML to PDF
Converter.convertHTML(document, options, "drawing-options.pdf");
// Invoke the convertHTML() method to convert the HTML to PDF
Converter.convertHTML("<h1>Convert HTML to PDF!</h1>", ".", new PdfSaveOptions(), "convert-with-single-line.pdf");
// Prepare HTML code and save it to a file
String code = "<h1> PdfSaveOptions Class</h1> " +
"<p>Using PdfSaveOptions Class, you can programmatically " +
"apply a wide range of conversion parameters " +
"such as BackgroundColor, HorizontalResolution, VerticalResolution, PageSetup, etc.</p>";
FileHelper.writeAllText("save-options.html", code);
// Initialize an HTML Document from the HTML file
HTMLDocument document = new HTMLDocument("save-options.html");
// Set up the page-size, margins and change the background color to AntiqueWhite
PdfSaveOptions options = new PdfSaveOptions();
options.setBackgroundColor(Color.getAntiqueWhite());
options.getPageSetup().setAnyPage(
new Page(
new Size(Length.fromInches(4.9f), Length.fromInches(3.5f)),
new Margin(30, 20, 10, 10)
)
);
// Convert HTML to PDF
Converter.convertHTML(document, options, "save-options-output.pdf");
// Initialize an HTML document from a file
HTMLDocument document = new HTMLDocument("nature.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Convert HTML to PNG
Converter.convertHTML(document, options, "nature-output.png");
// Prepare HTML code and save it to a file
String code = "<span>Hello, World!!</span>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
fileWriter.write(code);
}
// Initialize an HTML document from the file
HTMLDocument document = new HTMLDocument("canvas.html");
// Initialize XpsSaveOptions
XpsSaveOptions options = new XpsSaveOptions();
// Convert HTML to XPS
Converter.convertHTML(document, options, "canvas-output.xps");
// Create an instance of MemoryStreamProvider
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Initialize an HTML document
HTMLDocument document = new HTMLDocument("<h1>Convert HTML to XPS File Format!</h1>", ".");
// Convert HTML to XPS using the MemoryStreamProvider
Converter.convertHTML(document, new XpsSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
OutputStream memory = streamProvider.getStreams().stream().findFirst().get();
memory.seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
java.io.File fs = new java.io.File("stream-provider.xps");
FileHelper.save(memory, fs);
// Prepare HTML code and save it to a file
String code = "<h1> XpsSaveOptions Class</h1>\r\n" +
"<p>Using XpsSaveOptions Class, you can programmatically " +
"apply a wide range of conversion parameters " +
"such as BackgroundColor, PageSetup, etc.</p>";
FileHelper.writeAllText("save-options.html", code);
// Initialize an HTML document from the html file
HTMLDocument document = new HTMLDocument("save-options.html");
// Set up the page-size, margins and change the background color to AntiqueWhite
XpsSaveOptions options = new XpsSaveOptions();
options.setBackgroundColor(Color.getAntiqueWhite());
options.getPageSetup().setAnyPage(new Page(new Size(Length.fromInches(4.9f), Length.fromInches(3.5f)), new Margin(30, 20, 10, 10)));
// Convert HTML to XPS
Converter.convertHTML(document, options, "save-options-output.xps");
// Invoke the convertHTML() method to convert HTML to XPS
Converter.convertHTML("<h1>Convert HTML to XPS!</h1>", ".", new XpsSaveOptions(), "convert-with-single-line.xps");
// Convert Markdown to HTML
Converter.convertMarkdown("nature.md", "nature-output.html");
// Convert Markdown to HTML
HTMLDocument document = Converter.convertMarkdown("nature.md");
// Initialize PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
// Convert the HTML document to PDF file format
Converter.convertHTML(document, options, "nature-output.pdf");
// Prepare a simple Markdown example
String code = "### Hello, World! \n" +
"[visit applications](https://products.aspose.app/html/family)";
// Create a Markdown file
FileHelper.writeAllText("document.md", code);
// Convert Markdown to HTML
Converter.convertMarkdown("document.md", "document-output.html");
// Prepare a simple Markdown example
String code = "### Hello, World! \n" +
"[visit applications](https://products.aspose.app/html/family)";
// Create a Markdown file
FileHelper.writeAllText("document.md", code);
// Convert Markdown to HTML
HTMLDocument document = Converter.convertMarkdown("document.md");
// Convert HTML document to JPG image file format
Converter.convertHTML(document, new ImageSaveOptions(ImageFormat.Jpeg), "document-output.jpg");
// Prepare a simple Markdown example
String code = "### Hello, World! \n\n" +
"[visit applications](https://products.aspose.app/html/applications)";
// Create a Markdown file
FileHelper.writeAllText("document.md", code);
// Convert Markdown to HTML
HTMLDocument document = Converter.convertMarkdown("document.md");
// Convert the HTML document to PDF file format
Converter.convertHTML(document, new PdfSaveOptions(), "document-output.pdf");
// Prepare a simple Markdown example
String code = "### Hello, World\n\n" +
"[visit applications](https://products.aspose.app/html/family)";
// Create a Markdown file
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.md")) {
fileWriter.write(code);
}
// Convert Markdown to HTML document
HTMLDocument document = Converter.convertMarkdown("document.md");
// Convert HTML document to PNG image file format
Converter.convertHTML(document, new ImageSaveOptions(ImageFormat.Png), "output_md.png");
// Prepare a simple Markdown example
String code = "### Hello, World\n\n" +
"[visit applications](https://products.aspose.app/html/family)\n";
// Create a Markdown file
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.md")) {
fileWriter.write(code);
}
// Convert Markdown to HTML
Converter.convertMarkdown("document.md", "document.html");
// Convert Markdown to HTML
HTMLDocument document = Converter.convertMarkdown("nature.md");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
options.setSmoothingMode(SmoothingMode.HighQuality);
options.setHorizontalResolution(Resolution.to_Resolution(200));
options.setVerticalResolution(Resolution.to_Resolution(200));
options.setBackgroundColor(Color.getAliceBlue());
options.getPageSetup().setAnyPage(new Page(new Size(600, 950), new Margin(30, 20, 10, 10)));
// Convert HTML to JPG
Converter.convertHTML(document, options, "nature-options.jpg");
// Convert Markdown to HTML
HTMLDocument document = Converter.convertMarkdown("nature.md");
// Initialize PdfSaveOptions. Set up the resolutions, JpegQuality and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions();
options.setHorizontalResolution(Resolution.to_Resolution(200));
options.setVerticalResolution(Resolution.to_Resolution(200));
options.setBackgroundColor(Color.getAliceBlue());
options.setJpegQuality(100);
// Convert the HTML document to PDF file format
Converter.convertHTML(document, options, "nature-output.pdf");
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
// Call the convertMHTML() method to convert the MHTML file to BMP
Converter.convertMHTML(fileInputStream, options, "output.bmp");
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
// Call the ConvertMHTML method to convert the MHTML file to GIF.
Converter.convertMHTML(fileInputStream, options, "output.gif");
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Call the convertMHTML() method to convert MHTML to JPG
Converter.convertMHTML(fileInputStream, options, "sample-output.jpg");
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Call the convertMHTML() method to convert MHTML to PNG
Converter.convertMHTML(fileInputStream, options, "sample-output.png");
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
// Call the ConvertMHTML method to convert the MHTML file to TIFF.
Converter.convertMHTML(fileInputStream, options, "output.tiff");
// Open an existing MHTML file for reading
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Convert MHTML to JPG by using the MemoryStreamProvider class
Converter.convertMHTML(fileInputStream, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
// Get access to the memory streams that contain the resulted data
for (int i = 0; i < streamProvider.getStreams().size(); i++) {
OutputStream memory = streamProvider.getStreams().get(i);
memory.seek(0, SeekOrigin.Begin);
// Flush the page to the output file
java.io.File fs = new java.io.File("stream-provider.jpg");
FileHelper.save(memory, fs);
}
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Initialize the ImageSaveOptions with a custom page-size and background color
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
options.setBackgroundColor(Color.getGreen());
options.getPageSetup().setAnyPage(new Page());
options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(1000), Length.fromPixels(500)));
// Call the convertMHTML() method to convert MHTML to JPG
Converter.convertMHTML(fileInputStream, options, "sample-options.jpg");
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Invoke the convertMHTML() method to convert MHTML to JPG
Converter.convertMHTML(fileInputStream, new ImageSaveOptions(ImageFormat.Jpeg), "convert-by-few-lines.jpg");
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Create an instance of the PdfSaveOptions class
PdfSaveOptions options = new PdfSaveOptions();
// Call the convertMHTML() method to convert MHTML to PDF
Converter.convertMHTML(fileInputStream, options, "sample-output.pdf");
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Convert MHTML to PDF by using the MemoryStreamProvider class
Converter.convertMHTML(fileInputStream, new PdfSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
OutputStream memory = streamProvider.getStreams().stream().findFirst().get();
memory.seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
java.io.File fs = new java.io.File("stream-provider.pdf");
FileHelper.save(memory, fs);
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions();
options.setBackgroundColor(Color.getAliceBlue());
options.getPageSetup().setAnyPage(new Page());
options.getPageSetup().getAnyPage().setSize(new Size(Length.fromPixels(3000), Length.fromPixels(1000)));
// Call the convertMHTML() method to convert MHTML to PDF
Converter.convertMHTML(fileInputStream, options, "sample-options.pdf");
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Invoke the convertMHTML() method to convert MHTML to PDF
Converter.convertMHTML(fileInputStream, new PdfSaveOptions(), "convert-by-two-lines.pdf");
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Create an instance of MemoryStreamProvider
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Convert HTML to JPG using the MemoryStreamProvider
Converter.convertEPUB(fileInputStream, new ImageSaveOptions(ImageFormat.Png), streamProvider);
// Get access to the memory streams that contain the resulted data
for (int i = 0; i < streamProvider.getStreams().size(); i++) {
OutputStream memory = streamProvider.getStreams().get(i);
memory.seek(0, SeekOrigin.Begin);
// Flush the page to the output file
java.io.File fs = new java.io.File("page_{" + (i + 1) + "}.png");
FileHelper.save(memory, fs);
}
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Initialize the ImageSaveOptions with a custom page-size and background-color
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
PageSetup pageSetup = new PageSetup();
Page anyPage = new Page();
anyPage.setSize(
new Size(
Length.fromPixels(3000),
Length.fromPixels(1000)
)
);
pageSetup.setAnyPage(anyPage);
options.setPageSetup(pageSetup);
options.setBackgroundColor(Color.getGreen());
// Call the convertMHTML() method to convert MHTML to PNG
Converter.convertMHTML(fileInputStream, options, "sample-options.png");
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Create an instance of XpsSaveOptions
XpsSaveOptions options = new XpsSaveOptions();
// Call the convertMHTML() method to convert MHTML to XPS
Converter.convertMHTML(fileInputStream, options, "sample-output.xps");
// Create an instance of the MemoryOutputStreamProvider class
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Convert MHTML to XPS by using the MemoryStreamProvider class
Converter.convertMHTML(fileInputStream, new XpsSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
OutputStream memory = streamProvider.getStreams().stream().findFirst().get();
memory.seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
java.io.File fs = new java.io.File("stream-provider.xps");
FileHelper.save(memory, fs);
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Create an instance of XpsSaveOptions. Set up a page-size and change the background color to AliceBlue
XpsSaveOptions options = new XpsSaveOptions();
options.getPageSetup().setAnyPage(new Page(new Size(Length.fromInches(8.3f), Length.fromInches(5.8f))));
options.setBackgroundColor(Color.getAliceBlue());
// Call the convertMHTML() method to convert MHTML to XPS
Converter.convertMHTML(fileInputStream, options, "sample-options.xps");
// Open an existing MHTML file for reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream("sample.mht");
// Сonvert MHTML to PDF
Converter.convertMHTML(fileInputStream, new XpsSaveOptions(), "convert-by-two-lines.xps");
// Prepare SVG code and save it to a file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
fileWriter.write(code);
}
// Initialize an SVG document from the svg file
SVGDocument document = new SVGDocument("document.svg");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
// Convert SVG to BMP
Converter.convertSVG(document, options, "output.bmp");
// Prepare SVG code and save it to a file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
fileWriter.write(code);
}
// Initialize an SVG document from the SVG file
SVGDocument document = new SVGDocument("document.svg");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
// Convert SVG to GIF
Converter.convertSVG(document, options, "output.gif");
// Prepare SVG code and save it to a file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
fileWriter.write(code);
}
// Initialize an SVG document from the SVG file
SVGDocument document = new SVGDocument("document.svg");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Convert SVG to JPG
Converter.convertSVG(document, options, "output.jpg");
// Prepare SVG code and save it to a file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
fileWriter.write(code);
}
// Initialize an SVG document from the SVG file
SVGDocument document = new SVGDocument("document.svg");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Convert SVG to PNG
Converter.convertSVG(document, options, "output.png");
// Prepare SVG code and save it to a file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
fileWriter.write(code);
}
// Initialize an SVG document from the svg file
SVGDocument document = new SVGDocument("document.svg");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
// Convert SVG to TIFF
Converter.convertSVG(document, options, "output.tiff");
// Prepare an SVG code and save it to the file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
// Create an instance of MemoryStreamProvider
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Initialize the SVG document
SVGDocument document = new SVGDocument(code, ".");
// Convert SVG to Image by using the MemoryStreamProvider
Converter.convertSVG(document, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider.lStream);
// Get access to the memory stream that contains the result data
java.io.InputStream inputStream = streamProvider.lStream.stream().findFirst().get();
// Flush the result data to the output file
try (java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream("output.jpg")) {
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
fileOutputStream.write(buffer);
}
// Prepare SVG code and save it to a file
String code =
"<svg width=\"450\" height=\"450\" xmlns=\"http://www.w3.org/2000/svg\">" +
" <g fill=\"RoyalBlue\">" +
" <rect x=\"100\" y=\"100\" rx=\"25\" ry=\"25\" width=\"200\" height=\"56\" />" +
" <rect x=\"100\" y=\"100\" rx=\"25\" ry=\"25\" width=\"200\" height=\"56\" transform =\"rotate(90 200 128)\" />" +
" <rect x=\"100\" y=\"100\" rx=\"25\" ry=\"25\" width=\"200\" height=\"56\" transform =\"rotate(-45 200 128)\" />" +
" <rect x=\"100\" y=\"100\" rx=\"25\" ry=\"25\" width=\"200\" height=\"56\" transform =\"rotate(45 200 128)\" />" +
" </g>" +
" <circle cx=\"200\" cy=\"128\" r=\"28\" stroke=\"pink\" stroke-width=\"50\" stroke-dasharray=\"3 13\" fill=\"Orange\" />" +
" <circle cx=\"200\" cy=\"128\" r=\"5\" />" +
"</svg>";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("flower.svg")) {
fileWriter.write(code);
}
// Initialize ImageSaveOptions and set up smoothing mode, page size, and background color
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
PageSetup pageSetup = new PageSetup();
options.setSmoothingMode(SmoothingMode.HighQuality);
Page anyPage = new Page();
anyPage.setSize(new Size(Length.fromPixels(200), Length.fromPixels(200)));
pageSetup.setAnyPage(anyPage);
options.setPageSetup(pageSetup);
options.setBackgroundColor(Color.getAliceBlue());
// Call the convertSVG() method to convert the "flower.svg" file to a JPEG image
Converter.convertSVG("flower.svg", options, "flower.jpg");
// Prepare SVG code
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
// Invoke the convertSVG() method to convert SVG to image
Converter.convertSVG(code, ".", new ImageSaveOptions(ImageFormat.Jpeg), "output.jpg");
// Prepare SVG code and save it to a file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
fileWriter.write(code);
}
// Initialize an SVG document from the svg file
SVGDocument document = new SVGDocument("document.svg");
// Initialize PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
// Convert SVG to PDF
Converter.convertSVG(document, options, "output.pdf");
// Create an instance of MemoryStreamProvider
MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Prepare an SVG code
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
// Initialize an SVG document
SVGDocument document = new SVGDocument(code, ".");
// Convert SVG to PDF by using the MemoryStreamProvider
Converter.convertSVG(
document,
new PdfSaveOptions(),
streamProvider.lStream
);
// Get access to the memory stream that contains the result data
java.io.InputStream inputStream = streamProvider.lStream.stream().findFirst().get();
// Flush the result data to the output file
try (java.io.FileOutputStream fileOutputStream = new java.io.FileOutputStream("output.pdf")) {
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
fileOutputStream.write(buffer);
}
// Prepare SVG code and save it to a file
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.svg")) {
fileWriter.write(code);
}
// Set A5 as a page-size and change the background color to green
PdfSaveOptions options = new PdfSaveOptions();
PageSetup pageSetup = new PageSetup();
Page anyPage = new Page();
anyPage.setSize(new Size(Length.fromInches(8.3f), Length.fromInches(5.8f)));
pageSetup.setAnyPage(anyPage);
options.setPageSetup(pageSetup);
options.setBackgroundColor(Color.getGreen());
// Convert SVG to PDF
Converter.convertSVG("document.svg", options, "output.pdf");
// Prepare SVG code
String code = "<svg xmlns='http://www.w3.org/2000/svg'>\n" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />\n" +
"</svg>\n";
// Call the convertSVG() method to convert SVG to PDF
Converter.convertSVG(code, ".", new PdfSaveOptions(), "output.pdf");
// Create an instance of MemoryStreamProvider
final MemoryOutputStreamProvider streamProvider = new MemoryOutputStreamProvider();
// Prepare SVG code
String code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
"<circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red' />" +
"</svg>";
// Convert SVG to PNG using the MemoryStreamProvider
Converter.convertSVG(code, ".", new ImageSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
OutputStream memory = streamProvider.getStreams().stream().findFirst().get();
memory.seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
final java.io.File fs = new java.io.File("stream-provider.png");
FileHelper.save(memory, fs);
// Create an instance of the ImageSaveOptions class. Set up the SmoothingMode, resolutions, and background color
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
options.setHorizontalResolution(Resolution.to_Resolution(200));
options.setVerticalResolution(Resolution.to_Resolution(200));
options.setBackgroundColor(Color.getAliceBlue());
options.setSmoothingMode(SmoothingMode.HighQuality);
// Initialize an SVG document from a file
final SVGDocument document = new SVGDocument("flower1.svg");
// Convert SVG to PNG
Converter.convertSVG(document, options, "flower-options.png");