Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This Gist contains examples for Aspose.HTML for .NET in C# language.
// 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
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_Data();
// Source EPUB document
FileStream epubDocumentStream = File.OpenRead(dataDir + "input.epub");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Output file path
string outputFile = dataDir + "EPUBtoImageOutput.jpeg";
// Convert SVG to Image
Converter.ConvertEPUB(epubDocumentStream, options, outputFile);
// 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();
// Source EPUB document
FileStream fileStream = File.OpenRead(dataDir + "input.epub");
// Initialize pdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions()
{
JpegQuality = 100
};
// Output file path
string outputFile = dataDir + "EPUBtoPDF_Output.pdf";
// Convert EPUB to PDF
Converter.ConvertEPUB(fileStream, options, outputFile);
// 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();
// Source EPUB document
FileStream epubDocumentStream = File.OpenRead(dataDir + "input.epub");
// Initialize XpsSaveOptions
XpsSaveOptions options = new XpsSaveOptions()
{
BackgroundColor = System.Drawing.Color.Cyan
};
// Output file path
string outputFile = dataDir + "EPUBtoXPS_Output.xps";
// Convert EPUB to XPS
Converter.ConvertEPUB(epubDocumentStream, options, outputFile);
// 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();
// Source HTML document
HTMLDocument htmlDocument = new HTMLDocument(dataDir + "input.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
// Output file path
string outputFile = dataDir + "HTMLtoBMP_Output.bmp";
// Convert HTML to BMP
Converter.ConvertHTML(htmlDocument, options, outputFile);
// 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();
// Source HTML document
HTMLDocument htmlDocument = new HTMLDocument(dataDir + "input.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
// Output file path
string outputFile = dataDir + "HTMLtoGIF_Output.gif";
// Convert HTML to GIF
Converter.ConvertHTML(htmlDocument, options, outputFile);
// 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();
// Source HTML document
HTMLDocument htmlDocument = new HTMLDocument(dataDir + "input.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Output file path
string outputFile = dataDir + "HTMLtoJPEG_Output.jpeg";
// Convert HTML to JPEG
Converter.ConvertHTML(htmlDocument, options, outputFile);
// 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();
// Source HTML Document
HTMLDocument htmlDocument = new HTMLDocument(dataDir + "input.html");
// Initialize MHTMLSaveOptions
MHTMLSaveOptions options = new MHTMLSaveOptions();
// Set the resource handling rules
options.ResourceHandlingOptions.MaxHandlingDepth = 1;
// Output file path
string outputPDF = dataDir + "HTMLtoMHTML_Output.mht";
// Convert HTML to MHTML
Converter.ConvertHTML(htmlDocument, options, outputPDF);
// 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();
// Source HTML document
HTMLDocument htmlDocument = new HTMLDocument(dataDir + "input.html");
// Initialize PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions
{
JpegQuality = 100
};
// Output file path
string outputPDF = dataDir + "HTMLtoPDF_Output.pdf";
// Convert HTML to PDF
Converter.ConvertHTML(htmlDocument, options, outputPDF);
// 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();
// Source HTML document
HTMLDocument htmlDocument = new HTMLDocument(dataDir + "input.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Output file path
string outputFile = dataDir + "HTMLtoPNG_Output.png";
// Convert HTML to PNG
Converter.ConvertHTML(htmlDocument, options, outputFile);
// 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();
// Source HTML document
HTMLDocument htmlDocument = new HTMLDocument(dataDir + "input.html");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
// Output file path
string outputFile = dataDir + "HTMLtoPNG_Output.tif";
// Convert HTML to TIFF
Converter.ConvertHTML(htmlDocument, options, outputFile);
// 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();
// Source HTML document
HTMLDocument htmlDocument = new HTMLDocument(dataDir + "input.html");
// Initialize XpsSaveOptions
XpsSaveOptions options = new XpsSaveOptions
{
BackgroundColor = System.Drawing.Color.Cyan
};
// Output file path
string outputFile = dataDir + "input.xps";
// Convert HTML to XPS
Converter.ConvertHTML(htmlDocument, options, outputFile);
// 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();
// Convert markdown to HTML
Converter.ConvertMarkdown(dataDir + "Markdown.md", dataDir + "MarkdownToHTML.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();
// Source MHTML document
FileStream fileStream = File.OpenRead(dataDir + "sample.mht");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Output file path
string outputFile = dataDir + "MHTMLtoImage.jpeg";
// Convert SVG to Image
Converter.ConvertMHTML(fileStream, options, outputFile);
// 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();
// Source MHTML document
FileStream fileStream = File.OpenRead(dataDir + "sample.mht");
// Initialize pdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions()
{
JpegQuality = 100
};
// Output file path
string outputFile = dataDir + "MHTMLtoPDF_Output.pdf";
// Convert MHTML to PDF
Converter.ConvertMHTML(fileStream, options, outputFile);
// 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();
// Source EPUUB document
FileStream fileStream = File.OpenRead(dataDir + "sample.mht");
// Initialize XpsSaveOptions
XpsSaveOptions options = new XpsSaveOptions()
{
BackgroundColor = System.Drawing.Color.Cyan
};
// Output file path
string outputFile = dataDir + "MHTMLtoXPS_Output.xps";
// Convert MHTML to XPS
Converter.ConvertMHTML(fileStream, options, outputFile);
// 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();
// Source SVG document
SVGDocument svgDocument = new SVGDocument(dataDir + "input.svg");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Output file path
string outputFile = dataDir + "SVGtoImage_Output.jpeg";
// Convert SVG to Image
Converter.ConvertSVG(svgDocument, options, outputFile);
// 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();
// Source SVG document
SVGDocument svgDocument = new SVGDocument(dataDir + "input.svg");
// Initialize pdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions()
{
JpegQuality = 100
};
// Output file path
string outputFile = dataDir + "SVGtoPDF_Output.pdf";
// Convert SVG to PDF
Converter.ConvertSVG(svgDocument, options, outputFile);
// 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();
// Source SVG document
SVGDocument svgDocument = new SVGDocument(dataDir + "input.svg");
// Initialize XpsSaveOptions
XpsSaveOptions options = new XpsSaveOptions()
{
BackgroundColor = System.Drawing.Color.Cyan
};
// Output file path
string outputFile = dataDir + "SVGtoXPS_Output.xps";
// Convert SVG to XPS
Converter.ConvertSVG(svgDocument, options, outputFile);
// 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
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();
// Create a custom StreamProvider based on ICreateStreamProvider interface
using (MemoryStreamProvider streamProvider = new MemoryStreamProvider())
{
// Create a simple HTML document
using (HTMLDocument document = new HTMLDocument())
{
// Add your first 'hello world' to the document.
document.Body.AppendChild(document.CreateTextNode("Hello world!!!"));
// Convert HTML document to XPS by using the custom StreamProvider
Aspose.Html.Converters.Converter.ConvertHTML(document, new XpsSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
var memory = streamProvider.Streams[0];
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(dataDir + "output.xps"))
{
memory.CopyTo(fs);
}
}
}
// 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);
}
}
// 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();
// HTML template document
HTMLDocument templateHtml = new HTMLDocument(dataDir + "HTMLTemplateForJson.html");
//XML data for merging
TemplateData data = new TemplateData(dataDir + "JsonTemplate.json");
//Output file path
string templateOutput = dataDir + "MergeHTMLWithJson_Output.html";
//Merge HTML tempate with XML data
Converter.ConvertTemplate(templateHtml, data, new TemplateLoadOptions(), templateOutput);
// 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();
// HTML template document
HTMLDocument templateHtml = new HTMLDocument(dataDir + "HTMLTemplateforXML.html");
//XML data for merging
TemplateData data = new TemplateData(dataDir + "XMLTemplate.xml");
//Output file path
string templateOutput = dataDir + "HTMLTemplate_Output.html";
//Merge HTML tempate with XML data
Converter.ConvertTemplate(templateHtml, data, new TemplateLoadOptions(), templateOutput);
<html>
<head>
<title>{{Title}}</title>
<meta charset="utf-8" />
</head>
<body>
<div data_merge="{{#foreach Persons}}">
<p>Name: {{Name}} {{Surname}}</p>
<p>Address: {{Address.Number}}, {{Address.Street}}, {{Address.City}}</p>
<br/>
</div>
</body>
</html>
<html>
<head>
<title>Test JSON</title>
<meta charset="utf-8">
</head>
<body>
<DIV>
<P>Name: John Smith</P>
<P>Address: 200, Austin rd., Dallas</P>
<BR>
</DIV>
<DIV>
<P>Name: Jack Fox</P>
<P>Address: 25, Broadway, New York</P>
<BR>
</DIV>
</body>
</html>
{
"Title": "Test JSON",
"Persons": [
{
"Name": "John",
"Surname": "Smith",
"Address": {
"Number": "200",
"Street": "Austin rd.",
"City": "Dallas"
}
},
{
"Name": "Jack",
"Surname": "Fox",
"Address": {
"Number": "25",
"Street": "Broadway",
"City": "New York"
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.