Skip to content

Instantly share code, notes, and snippets.

@aspose-html
Last active October 12, 2017 06:11
Show Gist options
  • Save aspose-html/27c911964bab73c47ca41f154bb7a682 to your computer and use it in GitHub Desktop.
Save aspose-html/27c911964bab73c47ca41f154bb7a682 to your computer and use it in GitHub Desktop.
This Gist contains Java code snippets for examples of Aspose.Html for Java.
Aspose.Html-for-Java
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.Html-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
String SimpleStyledFilePath = "FirstFile.html";
final File file = new File(SimpleStyledFilePath);
final FileOutputStream sw = new FileOutputStream(file);
String style = "<style>" +
".st" +
"{" +
"color: green;" +
"}" +
"</style>" +
"<div id=id1>Aspose.Html rendering Text in Black Color</div>" +
"<div id=id2 class=''st''>Aspose.Html rendering Text in Green Color</div>" +
"<div id=id3 class=''st'' style='color: blue;'>Aspose.Html rendering Text in Blue Color</div>" +
"<div id=id3 class=''st'' style='color: red;'>Aspose.Html rendering Text in Red Color</div>";
try
{ sw.write(style.getBytes());
}
finally
{
if (sw != null)
(sw).close();
}
// Create HtmlRenderer object
final com.aspose.html.rendering.HtmlRenderer renderer = new com.aspose.html.rendering.HtmlRenderer();
// Create HtmlDocument instance while passing path of already created HTML file
final com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(SimpleStyledFilePath);
// Set the page size less than document min-width. The content in the resulting file will be cropped because of element with width: 200px
com.aspose.html.rendering.xps.XpsRenderingOptions xps_options = new com.aspose.html.rendering.xps.XpsRenderingOptions();
try
{
xps_options.getPageSetup().setAnyPage(new com.aspose.html.drawing.Page(new com.aspose.html.drawing.Size(100, 100)));
String Resultnat_output = dataDir + "not-adjusted-to-widest-page_out.xps";
final com.aspose.html.rendering.xps.XpsDevice device = new com.aspose.html.rendering.xps.XpsDevice(xps_options, Resultnat_output);
try
{
// render the output using HtmlRenderer
renderer.render(device, document);
}
finally
{
if (document != null)
(document).dispose();
}
}
finally
{
if (renderer != null)
(renderer).dispose();
}
String output = dataDir + "adjusted-to-widest-page_out.xps";
final com.aspose.html.rendering.xps.XpsDevice device = new com.aspose.html.rendering.xps.XpsDevice(xps_options, output);
try
{
// Render the output
renderer.render(device, document);
}
finally
{
if (device != null)
(device).dispose();
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.Html-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
String SimpleStyledFilePath = "FirstFile.html";
final File file = new File(SimpleStyledFilePath);
final FileOutputStream sw = new FileOutputStream(file);
String style = "<style>" +
".st" +
"{" +
"color: green;" +
"}" +
"</style>" +
"<div id=id1>Aspose.Html rendering Text in Black Color</div>" +
"<div id=id2 class=''st''>Aspose.Html rendering Text in Green Color</div>" +
"<div id=id3 class=''st'' style='color: blue;'>Aspose.Html rendering Text in Blue Color</div>" +
"<div id=id3 class=''st'' style='color: red;'>Aspose.Html rendering Text in Red Color</div>";
try
{ sw.write(style.getBytes());
}
finally
{
if (sw != null)
(sw).close();
}
// File name for resultant XPS file
String Resultnat_output = dataDir + "simple-any-page_out.xps";
// Create XpxRendering Options object
com.aspose.html.rendering.xps.XpsRenderingOptions xps_options = new com.aspose.html.rendering.xps.XpsRenderingOptions();
// The PageSetup also provides different properties i.e. FirstPage, LastPage, LeftPage, RightPage and they are used to setup (PageSize, Margin) for every page.
// In most cases, usage of setup any page is enough, but in some complicated cases, you may need to fine tune page settings. It can be done either by CSS styles or by using rendering options.
// the size for drawing is in pixels
xps_options.getPageSetup().setAnyPage(new com.aspose.html.drawing.Page(new com.aspose.html.drawing.Size(200, 60)));
// Instantiate XPSDevice object while passing XPSRenderingOptions and resultant file path as arguments
final com.aspose.html.rendering.xps.XpsDevice device = new com.aspose.html.rendering.xps.XpsDevice(xps_options, Resultnat_output);
try
{
// Create HtmlRenderer object
final com.aspose.html.rendering.HtmlRenderer renderer = new com.aspose.html.rendering.HtmlRenderer();
// Create HtmlDocument instnace while passing path of already created HTML file
try
{
final com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(SimpleStyledFilePath);
try
{
// Render the output using HtmlRenderer
renderer.render(device, document);
}
finally
{
if (document != null)
(document).dispose();
}
}
finally
{
if (renderer != null)
(renderer).dispose();
}
}
finally
{
if (device != null)
(device).dispose();
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.Html-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
final File file = new File( dataDir + "input.html");
final FileOutputStream sw = new FileOutputStream(file);
try
{
sw.write("<p>this is a simple text".getBytes());
}
finally
{
if (sw != null)
(sw).close();
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.Html-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// This simple test shows how to load document from remote server
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(new com.aspose.html.Url(dataDir + "input.html"));
// Read children nodes and get length information
if (document.getBody().getChildNodes().getLength() == 0)
System.out.println("No ChildNodes found...");
// Print Document URI to console. As per information above, it has to be https://www.w3.org/TR/html5/
System.out.println("Print Document URI = " + document.getDocumentURI());
// Print domain name for remote HTML
System.out.println("Domain Name = " + document.getDomain());
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.Html-for-Java
// Load HTML file using Url instance
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(new com.aspose.html.Url("http://aspose.com/"));
// Print inner HTML of file to console
System.out.println(document.getBody().getInnerHTML());
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.Html-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Create HtmlDocument instance to load existing HTML file
com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument( dataDir + "input.html");
// Print inner HTML of file to console
System.out.println(document.getBody().getInnerHTML());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment