Skip to content

Instantly share code, notes, and snippets.

@aspose-html
Last active October 18, 2018 17:55
Show Gist options
  • Save aspose-html/0e4f6e3708f31a774c6c3888ed52d7a1 to your computer and use it in GitHub Desktop.
Save aspose-html/0e4f6e3708f31a774c6c3888ed52d7a1 to your computer and use it in GitHub Desktop.
This Gist contains CSharp code snippets for examples of Aspose.HTML for .NET.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Aspose.Html.Examples.CSharp.QuickStart
{
public class ApplyMeteredLicense
{
public static void Run()
{
// ExStart:ApplyMeteredLicense
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_Data();
// set metered public and private keys
Aspose.Html.Metered metered = new Aspose.Html.Metered();
// Access the setMeteredKey property and pass public and private keys as parameters
metered.SetMeteredKey("*****", "*****");
// Load the document from disk.
HTMLDocument document = new HTMLDocument(dataDir + "input.html");
// Print inner HTML of file to console
Console.WriteLine(document.Body.InnerHTML);
// ExEnd:ApplyMeteredLicense
}
}
}
Aspose.HTML-for-.NET
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
String SimpleStyledFilePath = dataDir + "FirstFile.html";
using (FileStream fs = File.Create(SimpleStyledFilePath))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(
@"<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;'><font face='Arial'>Aspose.Html rendering Text in Red Color</font></div>
");
}
string pdf_output;
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer pdf_renderer = new Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instnace while passing path of already created HTML file
using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(SimpleStyledFilePath))
{
// Set the page size less than document min-width. The content in the resulting file will be cropped becuase of element with width: 200px
Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions
{
PageSetup =
{
AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)),
AdjustToWidestPage = false
},
};
pdf_output = dataDir + "not-adjusted-to-widest-page_out.pdf";
using (Aspose.Html.Rendering.Pdf.PdfDevice device = new Aspose.Html.Rendering.Pdf.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 Aspose.Html.Rendering.Pdf.PdfRenderingOptions
{
PageSetup =
{
AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)),
AdjustToWidestPage = true
},
};
pdf_output = dataDir + "adjusted-to-widest-page_out.pdf";
using (Aspose.Html.Rendering.Pdf.PdfDevice device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, pdf_output))
{
// Render the output
pdf_renderer.Render(device, html_document);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
// Set input file name.
String SimpleStyledFilePath = dataDir + "FirstFile.html";
using (FileStream fs = File.Create(SimpleStyledFilePath))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(
@"<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>
");
}
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instnace while passing path of already created HTML file
using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(SimpleStyledFilePath))
{
// Set the page size less than document min-width. The content in the resulting file will be cropped becuase of element with width: 200px
Aspose.Html.Rendering.Xps.XpsRenderingOptions xps_options = new Aspose.Html.Rendering.Xps.XpsRenderingOptions
{
PageSetup =
{
AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)),
AdjustToWidestPage = false
},
};
string output = dataDir + "not-adjusted-to-widest-page_out.xps";
using (Aspose.Html.Rendering.Xps.XpsDevice device = new Aspose.Html.Rendering.Xps.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 Aspose.Html.Rendering.Xps.XpsRenderingOptions
{
PageSetup =
{
AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)),
AdjustToWidestPage = true
},
};
output = dataDir + "adjusted-to-widest-page_out.xps";
using (Aspose.Html.Rendering.Xps.XpsDevice device = new Aspose.Html.Rendering.Xps.XpsDevice(xps_options, output))
{
// render the output
renderer.Render(device, html_document);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new HTMLDocument(dataDir + "canvas.html"))
{
// Create an instance of HTML renderer and XPS output device
using (var renderer = new Rendering.HtmlRenderer())
using (var device = new Aspose.Html.Rendering.Pdf.PdfDevice(dataDir + "canvas.pdf"))
{
// Render the document to the specified device
renderer.Render(device, document);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
using (var document = new Aspose.Html.HTMLDocument("<p>my first paragraph</p>", dataDir))
{
// Create MarkdownSaveOptions object
var options = new Aspose.Html.Saving.MarkdownSaveOptions();
// Set the rules: only <a>, <img> and <p> elements will be converted to markdown.
options.Features = MarkdownFeatures.Link | MarkdownFeatures.Image | MarkdownFeatures.AutomaticParagraph;
document.Save(dataDir + "Markdown.md", options);
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new Aspose.Html.HTMLDocument("<p>my first paragraph</p>" +
"<p>my second paragraph</p>", dataDir))
{
document.Save(dataDir + "Markdown.md", Saving.HTMLSaveFormat.Markdown);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
using (var document = new Aspose.Html.HTMLDocument("<p>my first paragraph</p>", dataDir))
{
// Save to markdown by using default for the GIT formatting model
document.Save(dataDir + "Markdown.md", Saving.MarkdownSaveOptions.Git);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new Aspose.Html.HTMLDocument("<p>my first paragraph</p>", dataDir))
{
// Save to MHTML format
document.Save(dataDir + "document.mht", HTMLSaveFormat.MHTML);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
using (var document = new Aspose.Html.HTMLDocument("<p>my first paragraph</p>", dataDir))
{
var options = new Aspose.Html.Saving.MHTMLSaveOptions();
// Set the resource handling rules
options.ResourceHandlingOptions.MaxHandlingDepth = 1;
// Save to MHTML format
document.Save(dataDir + "document.mht", options);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
String InputHtml = dataDir + "input.html";
using (FileStream fs = File.Create(InputHtml))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(
@"<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;'><font face='Arial'>Aspose.Html rendering Text in Red Color</font></div>");
}
// File name for resultant PDF file
string Resultant_output = dataDir + "simple-any-page_out.pdf";
// Create PdfRendering Options object
Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions();
// 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
pdf_options.PageSetup.AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(400, 100));
// Instantiate PdfDevice object while passing PdfRenderingOptions and resultant file path as arguments
using (Aspose.Html.Rendering.Pdf.PdfDevice pdf_device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, Resultant_output))
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instance while passing path of already created HTML file
using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(InputHtml))
{
// Render the output using HtmlRenderer
renderer.Render(pdf_device, html_document);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_Data();
Aspose.Html.Rendering.Image.ImageRenderingOptions pdf_options = new Aspose.Html.Rendering.Image.ImageRenderingOptions();
// Instantiate PdfDevice object while passing PdfRenderingOptions and resultant file path as arguments
using (Aspose.Html.Rendering.Image.ImageDevice pdf_device = new Aspose.Html.Rendering.Image.ImageDevice(pdf_options, dataDir + "Aspose_HTML.png"))
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instance while passing path of already created HTML file
using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(dataDir + "input.html"))
{
// Render the output using HtmlRenderer
renderer.Render(pdf_device, html_document);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_Data();
Aspose.Html.Rendering.Image.ImageRenderingOptions img_options = new Aspose.Html.Rendering.Image.ImageRenderingOptions();
// Instantiate PdfDevice object while passing PdfRenderingOptions and resultant file path as arguments
using (Aspose.Html.Rendering.Image.ImageDevice img_device = new Aspose.Html.Rendering.Image.ImageDevice(img_options, dataDir + "Aspose_HTML.png"))
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instance while passing path of already created HTML file
using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(dataDir + "input.html"))
{
// Render the output using HtmlRenderer
renderer.Render(img_device, html_document);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_Data();
Aspose.Html.Rendering.Image.ImageRenderingOptions pdf_options = new Aspose.Html.Rendering.Image.ImageRenderingOptions();
// Instantiate PdfDevice object while passing PdfRenderingOptions and resultant file path as arguments
using (Aspose.Html.Rendering.Image.ImageDevice pdf_device = new Aspose.Html.Rendering.Image.ImageDevice(pdf_options, dataDir + "Aspose_HTML.tiff"))
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instance while passing path of already created HTML file
using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(dataDir + "input.html"))
{
// Render the output using HtmlRenderer
renderer.Render(pdf_device, html_document);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_Data();
Aspose.Html.Rendering.Image.ImageRenderingOptions pdf_options = new Aspose.Html.Rendering.Image.ImageRenderingOptions();
pdf_options.Format = ImageFormat.Tiff;
// Instantiate PdfDevice object while passing PdfRenderingOptions and resultant file path as arguments
using (Aspose.Html.Rendering.Image.ImageDevice pdf_device = new Aspose.Html.Rendering.Image.ImageDevice(pdf_options, dataDir + "Aspose_HTML.tiff"))
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instance while passing path of already created HTML file
using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(dataDir + "input.html"))
{
// Render the output using HtmlRenderer
renderer.Render(pdf_device, html_document);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
String InputHtml = dataDir + "FirstFile.html";
using (FileStream fs = File.Create(InputHtml))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(
@"<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>
");
}
// File name for resultant XPS file
string Resultnat_output = dataDir + "simple-any-page_out.xps";
// Create XpxRendering Options object
Aspose.Html.Rendering.Xps.XpsRenderingOptions xps_options = new 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.PageSetup.AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(200, 60));
// Instantiate XPSDevice object while passing XPSRenderingOptions and resultant file path as arguments
using (Aspose.Html.Rendering.Xps.XpsDevice device = new Aspose.Html.Rendering.Xps.XpsDevice(xps_options, Resultnat_output))
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instnace while passing path of already created HTML file
using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(InputHtml))
{
// Render the output using HtmlRenderer
renderer.Render(device, html_document);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
// Simple SVG file
File.WriteAllText( dataDir + "my.svg",
"<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"140\" width=\"500\">" +
"<ellipse cx=\"200\" cy=\"80\" rx=\"100\" ry=\"50\" style=\"fill:yellow;stroke:purple;stroke-width:2\" />" +
"</svg>");
// Create the new SVG document
Aspose.Html.Dom.Svg.SVGDocument document = new Aspose.Html.Dom.Svg.SVGDocument( dataDir + "my.svg");
// Simple navigation and inspection the element properties
Aspose.Html.Dom.Svg.SVGEllipseElement ellipse = (Aspose.Html.Dom.Svg.SVGEllipseElement)document.GetElementsByTagName("ellipse").First();
Console.WriteLine(ellipse.Style.CSSText); // fill: yellow; stroke: purple; stroke-width: 2;
// Create the new SVG renderer & XPS device
using (Aspose.Html.Rendering.SvgRenderer renderer = new Aspose.Html.Rendering.SvgRenderer())
using (Aspose.Html.Rendering.Xps.XpsDevice device = new Aspose.Html.Rendering.Xps.XpsDevice(dataDir + "my.xps"))
{
// Render to the output device
renderer.Render(device, document);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
String outputHtml = dataDir + "SimpleDocument.html";
// Create an instance of HTMLDocument
var document = new HTMLDocument();
// Add image
if (document.CreateElement("img") is HTMLImageElement img)
{
img.Src = "http://via.placeholder.com/400x200";
img.Alt = "Placeholder 400x200";
img.Title = "Placeholder image";
document.Body.AppendChild(img);
}
// Add ordered list
var orderedListElement = document.CreateElement("ol") as HTMLOListElement;
for (int i = 0; i < 10; i++)
{
var listItem = document.CreateElement("li") as HTMLLIElement;
listItem.TextContent = $" List Item {i + 1}";
orderedListElement.AppendChild(listItem);
}
document.Body.AppendChild(orderedListElement);
// Add table 3x3
var table = document.CreateElement("table") as HTMLTableElement;
var tBody = document.CreateElement("tbody") as HTMLTableSectionElement;
for (var i = 0; i < 3; i++)
{
var row = document.CreateElement("tr") as HTMLTableRowElement;
row.Id = "trow_" + i;
for (var j = 0; j < 3; j++)
{
var cell = document.CreateElement("td") as HTMLTableCellElement;
cell.Id = $"cell{i}_{j}";
cell.TextContent = "Cell " + j;
row.AppendChild(cell);
}
tBody.AppendChild(row);
}
table.AppendChild(tBody);
document.Body.AppendChild(table);
//Save the document to disk
document.Save(outputHtml);
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
String InputHtml = dataDir + "input.html";
using (FileStream fs = File.Create(InputHtml))
using (StreamWriter sw = new StreamWriter(fs))
{
// Write sample HTML tags to HTML file
sw.Write(@"<p>this is a simple text");
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
var document = new HTMLDocument();
// you can subscribe to the event 'OnLoad'
document.OnLoad += (sender, @event) =>
{
// manipulate with document here
};
document.Navigate(dataDir + "input.html");
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
var document = new HTMLDocument();
// subscribe to the event 'OnReadyStateChange' that will be fired once document is completely loaded
document.OnReadyStateChange += (sender, @event) =>
{
if (document.ReadyState == "complete")
{
// manipulate with document here
}
};
document.Navigate(dataDir + "input.html");
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(new Aspose.Html.Url(@"https://www.w3.org/TR/html5/"));
// Read children nodes and get length information
if (document.Body.ChildNodes.Length == 0)
Console.WriteLine("No ChildNodes found...");
// Print Document URI to console. As per information above, it has to be https://www.w3.org/TR/html5/
Console.WriteLine("Print Document URI = " + document.DocumentURI);
// Print domain name for remote HTML
Console.WriteLine("Domain Name = " + document.Domain);
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
String InputHtml = "http://aspose.com/";
// Load HTML file using Url instance
Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(new Aspose.Html.Url(InputHtml));
// Print inner HTML of file to console
Console.WriteLine(document.Body.InnerHTML);
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
// Create an empty document
using (HTMLDocument document = new HTMLDocument())
{
// Create a Canvas element
var canvas = (HTMLCanvasElement)document.CreateElement("canvas");
// Set the canvas size
canvas.Width = 300;
canvas.Height = 150;
// Append created element to the document
document.Body.AppendChild(canvas);
// Initialize a canvas 2D context
var context = (ICanvasRenderingContext2D)canvas.GetContext("2d");
// Prepare a gradient brush
var gradient = context.CreateLinearGradient(0, 0, canvas.Width, 0);
gradient.AddColorStop(0, "magenta");
gradient.AddColorStop(0.5, "blue");
gradient.AddColorStop(1.0, "red");
// Set the previously prepared brush to the fill and stroke properties
context.FillStyle = gradient;
context.StrokeStyle = gradient;
// Fill a rectange
context.FillRect(0, 95, 300, 20);
// Write a text
context.FillText("Hello World!", 10, 90, 500);
// Create an instance of HTML renderer and XPS output device
using (var renderer = new HtmlRenderer())
using (var device = new XpsDevice(dataDir + "canvas.xps"))
{
// Render the document to the specified device
renderer.Render(device, document);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
String InputHtml = dataDir + "input.html";
// Create HtmlDocument instance to load existing HTML file
Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(InputHtml);
// Print inner HTML of file to console
Console.WriteLine(document.Body.InnerHTML);
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_Data();
// Set metered public and private keys
Aspose.Html.Metered metered = new Aspose.Html.Metered();
// Access the setMeteredKey property and pass public and private keys as parameters
metered.SetMeteredKey("*****", "*****");
// Load the document from disk.
HTMLDocument document = new HTMLDocument(dataDir + "input.html");
// Print inner HTML of file to console
Console.WriteLine(document.Body.InnerHTML);
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_Data();
// set metered public and private keys
Aspose.Html.Metered metered = new Aspose.Html.Metered();
// Access the setMeteredKey property and pass public and private keys as parameters
metered.SetMeteredKey("*****", "*****");
// Load the document from disk.
HTMLDocument document = new HTMLDocument(dataDir + "input.html");
// Print inner HTML of file to console
Console.WriteLine(document.Body.InnerHTML);
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
// Initialize configuration object and set up the page-margins for the document
Configuration configuration = new Configuration();
configuration.GetService<IUserAgentService>().UserStyleSheet = @"
@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: ""Document's title"";
vertical-align: bottom;
}
}";
// Initialize an empty document
using (HTMLDocument document = new HTMLDocument(configuration))
{
// Initialize an output device
using (XpsDevice device = new XpsDevice(dataDir + "output_out.xps"))
{
// Send the document to the output device
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new Aspose.Html.HTMLDocument("<style>p { color: green; }</style><p>my first paragraph</p>", @"c:\work\"))
{
var options = new PdfRenderingOptions()
{
PageSetup =
{
AnyPage = new Page(new Size(500, 500), new Margin(50, 50, 50, 50))
},
Encryption = new PdfEncryptionInfo("user", "p@wd", PdfPermissions.PrintDocument, PdfEncryptionAlgorithm.RC4_128)
};
using (PdfDevice device = new PdfDevice(options, dataDir + @"document_out.pdf"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new Aspose.Html.HTMLDocument("<style>p { color: green; }</style><p>my first paragraph</p>", @"c:\work\"))
{
// Initialize rendering optionss and set jpeg as output format
var options = new ImageRenderingOptions(ImageFormat.Jpeg);
// Set the size and margin property for all pages.
options.PageSetup.AnyPage = new Page(new Size(500, 500), new Margin(50, 50, 50, 50));
// If the document has an element which size is bigger than predefined by user page size output pages will be will be adjusted.
options.PageSetup.AdjustToWidestPage = true;
using (ImageDevice device = new ImageDevice(options, dataDir + @"document_out.jpg"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new Aspose.Html.HTMLDocument("<style>p { color: green; }</style><p>my first paragraph</p>", @"c:\work\"))
{
using (ImageDevice device = new ImageDevice(dataDir + @"document_out.png"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new Aspose.Html.HTMLDocument("<style>p { color: green; }</style><p>my first paragraph</p>", @"c:\work\"))
{
using (XpsDevice device = new XpsDevice(new XpsRenderingOptions()
{
PageSetup =
{
AnyPage = new Page(new Size(500, 500), new Margin(50, 50, 50, 50))
}
}, dataDir + @"document_out.xps"))
{
document.RenderTo(device);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
// The MutationObserver interface provides the ability to watch for changes being made to the DOM tree.
// Create an empty document
using (var document = new HTMLDocument())
{
// Create a WaitHandle for purpose described below
var @event = new ManualResetEvent(false);
// Create an observer instance
var observer = new MutationObserver((mutations, mutationObserver) =>
{
var mutation = mutations[0];
Console.WriteLine(mutation.AddedNodes[0]);
@event.Set();
});
// Options for the observer (which mutations to observe)
var config = new MutationObserverInit
{
ChildList = true,
Subtree = true
};
// Start observing the target node
observer.Observe(document.DocumentElement, config);
// An example of user modifications
var p = document.CreateElement("p");
document.DocumentElement.AppendChild(p);
// Since, mutations are working in the async mode you should wait a bit. We use WaitHandle for this purpose.
@event.WaitOne();
// Later, you can stop observing
observer.Disconnect();
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
// The MutationObserver interface provides the ability to watch for changes being made to the DOM tree.
// Create an empty document
using (var document = new HTMLDocument())
{
// Create a WaitHandle for purpose described below
var @event = new ManualResetEvent(false);
// Create an observer instance
var observer = new MutationObserver((mutations, mutationObserver) =>
{
var mutation = mutations[0];
Console.WriteLine(mutation.AddedNodes[0]);
@event.Set();
});
// Options for the observer (which mutations to observe)
var config = new MutationObserverInit
{
ChildList = true,
Subtree = true
};
// Start observing the target node
observer.Observe(document.DocumentElement, config);
// An example of user modifications
var p = document.CreateElement("p");
document.DocumentElement.AppendChild(p);
// Since, mutations are working in the async mode you should wait a bit. We use WaitHandle for this purpose.
@event.WaitOne();
// Later, you can stop observing
observer.Disconnect();
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var fs = File.OpenRead(dataDir + "document.epub"))
using (var device = new Aspose.Html.Rendering.Xps.XpsDevice(dataDir + "document_out.xps"))
using (var renderer = new Aspose.Html.Rendering.EpubRenderer())
{
renderer.Render(device, fs);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new Aspose.Html.HTMLDocument("<style>p { color: green; }</style><p>my first paragraph</p>", @"c:\work\"))
{
using (HtmlRenderer renderer = new HtmlRenderer())
using (ImageDevice device = new ImageDevice(dataDir + @"document_out.png"))
{
renderer.Render(device, document);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
// Create an instance of the HTML document
using (var document = new Aspose.Html.HTMLDocument())
{
// Async loading of the external html file
document.Navigate( dataDir + "input.html");
// Create a renderer and output device
using (HtmlRenderer renderer = new HtmlRenderer())
using (ImageDevice device = new ImageDevice(dataDir + @"document.png"))
{
// Delay the rendering indefinitely until there are no scripts or any other internal tasks to execute
renderer.Render(device, -1, document);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_Data();
// Create an instance of the HTML document
using (var document = new Aspose.Html.HTMLDocument())
{
// Async loading of the external html file
document.Navigate(dataDir + "input.html");
// Create a renderer and output device
using (HtmlRenderer renderer = new HtmlRenderer())
using (ImageDevice device = new ImageDevice(dataDir + @"document.png"))
{
// Delay rendering for 5 seconds
// Note: document will be rendered into device if there are no scripts or any internal tasks to execute
renderer.Render(device, TimeSpan.FromSeconds(5), document);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var fs = File.OpenRead(dataDir + "document.mht"))
using (var device = new Aspose.Html.Rendering.Xps.XpsDevice(dataDir + "document_out.xps"))
using (var renderer = new Aspose.Html.Rendering.MhtmlRenderer())
{
renderer.Render(device, fs);
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new Aspose.Html.HTMLDocument("<style>p { color: green; }</style><p>my first paragraph</p>", @"c:\work\"))
using (var document2 = new Aspose.Html.HTMLDocument("<style>p { color: blue; }</style><p>my first paragraph</p>", @"c:\work\"))
{
using (HtmlRenderer renderer = new HtmlRenderer())
using (XpsDevice device = new XpsDevice(dataDir + @"document_out.xps"))
{
renderer.Render(device, document, document2);
}
}
// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
string dataDir = RunExamples.GetDataDir_Data();
using (var document = new Aspose.Html.Dom.Svg.SVGDocument("<svg xmlns='http://www.w3.org/2000/svg'><circle cx='50' cy='50' r='40'/></svg>", @"c:\work\"))
{
using (SvgRenderer renderer = new SvgRenderer())
using (ImageDevice device = new ImageDevice(dataDir + @"document_out.png"))
{
renderer.Render(device, document);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Aspose.Html.Examples.CSharp.Conversion
{
public class HTMLtoPNG
{
public static void Run()
{
// ExStart:HTMLToPNG
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_Data();
Aspose.Html.Rendering.Image.ImageRenderingOptions pdf_options = new Aspose.Html.Rendering.Image.ImageRenderingOptions();
// Instantiate PdfDevice object while passing PdfRenderingOptions and resultant file path as arguments
using (Aspose.Html.Rendering.Image.ImageDevice pdf_device = new Aspose.Html.Rendering.Image.ImageDevice(pdf_options, dataDir + "Aspose_HTML.png"))
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instance while passing path of already created HTML file
using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(dataDir + "input.html"))
{
// Render the output using HtmlRenderer
renderer.Render(pdf_device, html_document);
}
// ExEnd:HTMLToPNG
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Aspose.Html.Examples.CSharp.Conversion
{
public class HTMLtoTIFF
{
public static void Run()
{
// ExStart:HTMLToTIFF
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_Data();
Aspose.Html.Rendering.Image.ImageRenderingOptions pdf_options = new Aspose.Html.Rendering.Image.ImageRenderingOptions();
// Instantiate PdfDevice object while passing PdfRenderingOptions and resultant file path as arguments
using (Aspose.Html.Rendering.Image.ImageDevice pdf_device = new Aspose.Html.Rendering.Image.ImageDevice(pdf_options, dataDir + "Aspose_HTML.tiff"))
// Create HtmlRenderer object
using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
// Create HtmlDocument instance while passing path of already created HTML file
using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(dataDir + "input.html"))
{
// Render the output using HtmlRenderer
renderer.Render(pdf_device, html_document);
}
// ExEnd:HTMLToTIFF
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment