Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save aspose-com-gists/9a356b78ba78be0ac7330bd5065a4456 to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/9a356b78ba78be0ac7330bd5065a4456 to your computer and use it in GitHub Desktop.
Convert HTML, MHTML, EPUB, MD, and SVG to PDF, XPS, DOCX, PNG, JPG, and other formats

Convert HTML, XHTML, MHTML, SVG, EPUB, and Markdown to Other Formats Using C#

This gist repository contains C# code examples used in the Converting Between Formats chapter of the Aspose.HTML for .NET documentation. These examples demonstrate how to perform file format conversions using the Converter class. You will find examples for various common scenarios for HTML, XHTML, MHTML, SVG, EPUB, and Markdown conversion.

Topics Covered

  1. Convert HTML, SVG, EPUB, MHTML, and Markdown files to other formats such as PDF, DOCX, image formats, and more.
  2. Customize conversion behavior with specific options like page sizing, output format settings, and resource handling.
  3. Work with the unified Converter API to perform conversions consistently and reliably.

What's Included?

This repository provides examples for converting:

  • HTML Converter – Shows how to convert an HTML document to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, GIF, MD, and MHTML formats.
  • SVG Converter – Provides information on how to convert SVG to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, and GIF formats.
  • EPUB Converter – Demonstrates how to convert EPUB to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, and GIF formats.
  • MHTML Converter – Describes how to convert MHTML to PDF, XPS, DOCX, JPG, PNG, BMP, TIFF, and GIF formats.
  • Markdown Converter – Provides information on how to convert MD to HTML, PDF, XPS, DOCX, and various Image file formats.
  • Fine-Tuning Converters – Alternative ways to render HTML and HTML-based documents that can give you more control over the rendering process in your C# application.

Getting Started

  1. Install the latest version of Aspose.HTML for .NET using NuGet.
  2. Browse the available gists and copy the code samples you need.
  3. Configure paths, settings, and inputs to suit your environment.
  4. Run your project to see the example in action.

You can download a free trial of Aspose.HTML for .NET and use a temporary license for unrestricted access.

Documentation

Comprehensive explanations and full context for each example are available in the Converting Between Formats chapter of the official documentation.

About Aspose.HTML for .NET

Aspose.HTML for .NET is a powerful .NET library for parsing, modifying, and converting HTML and HTML-related formats (SVG, MHTML, XHTML, Markdown, EPUB). It provides built-in support for CSS, JavaScript, DOM traversal, XPath, document save options, and more.

Contributing

If you have questions or suggestions related to these examples, please use the Aspose.HTML Support Forum to get help or share feedback.

Other Related Resources

Prerequisites

To run the examples, you need:

  • .NET Platforms: .NET 5.0, .NET Core, or .NET Framework installed on your machine.
  • Supported OS: Windows, Linux, macOS.
  • The Aspose.HTML for .NET library (you can install it via NuGet: Install-Package Aspose.Html).
Aspose.HTML for .NET – Converting Between Formats
// Convert EPUB to BMP using C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-bmp/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-output.bmp");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
// Call the ConvertEPUB() method to convert EPUB to BMP
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to BMP in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-bmp/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "input-options.bmp");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp)
{
UseAntialiasing = true,
HorizontalResolution = 400,
VerticalResolution = 400,
BackgroundColor = System.Drawing.Color.AliceBlue
};
// Call the ConvertEPUB() method to convert EPUB to BMP
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to DOCX using C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-docx/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-output.docx");
// Create an instance of the DocSaveOptions class
DocSaveOptions options = new DocSaveOptions();
// Call the ConvertEPUB() method to convert EPUB to DOCX
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to DOCX using C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-docx/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Invoke the ConvertEPUB() method to convert EPUB to DOCX
Converter.ConvertEPUB(stream, new DocSaveOptions(), Path.Combine(OutputDir, "convert-by-two-lines.docx"));
// Convert EPUB to DOCX in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-docx/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "stream-provider.docx");
// Convert EPUB to DOCX by using the MemoryStreamProvider class
Converter.ConvertEPUB(stream, new DocSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(savePath))
{
memory.CopyTo(fs);
}
// Convert EPUB to DOCX with custom settings using C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-docx/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-options.docx");
// Create an instance of DocSaveOptions. Set A5 as a page-size
DocSaveOptions options = new DocSaveOptions();
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(Length.FromInches(8.3f), Length.FromInches(5.8f)));
// Call the ConvertEPUB() method to convert EPUB to DOCX
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to DOCX with custom page size using C#
// Learn more: https://docs.aspose.com/html/net/renderers/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Create an instance of EPUB Renderer
using EpubRenderer renderer = new EpubRenderer();
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "convert-epub-options.docx");
// Create the instance of Rendering Options and set a custom page-size
DocRenderingOptions options = new DocRenderingOptions();
options.PageSetup.AnyPage = new Page(new Size(800, 400));
// Create an instance of the DocDevice class
using DocDevice device = new DocDevice(options, savePath);
// Render EPUB to DOCX
renderer.Render(device, stream);
// Convert EPUB to GIF using C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-gif/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-output.gif");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
// Call the ConvertEPUB() method to convert EPUB to GIF
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to GIF in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-gif/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "input-options.gif");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif)
{
UseAntialiasing = true,
HorizontalResolution = 400,
VerticalResolution = 400,
BackgroundColor = System.Drawing.Color.AliceBlue
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(800, 500), new Margin(30, 20, 10, 10));
// Call the ConvertEPUB() method to convert EPUB to GIF
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to JPG using C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-jpg/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Invoke the ConvertEPUB() method to convert EPUB to JPG
Converter.ConvertEPUB(stream, new ImageSaveOptions(ImageFormat.Jpeg), Path.Combine(OutputDir, "convert-by-two-lines.jpg"));
// Convert EPUB to JPG using C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-jpg/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-options.jpg");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
{
UseAntialiasing = true,
HorizontalResolution = 400,
VerticalResolution = 400,
BackgroundColor = System.Drawing.Color.AliceBlue
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(800, 500), new Margin(30, 20, 10, 10));
// Call the ConvertEPUB() method to convert EPUB to JPG
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to JPG in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-jpg/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Convert HTML to JPG using the MemoryStreamProvider
Converter.ConvertEPUB(stream, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
// Get access to the memory streams that contain the resulted data
for (int i = 0; i < streamProvider.Streams.Count; i++)
{
MemoryStream memory = streamProvider.Streams[i];
memory.Seek(0, System.IO.SeekOrigin.Begin);
// Flush the page to the output file
using (FileStream fs = File.Create(Path.Combine(OutputDir, $"input-page_{i + 1}.jpg")))
{
memory.CopyTo(fs);
}
}
// Convert EPUB to PDF using C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-pdf/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-output.pdf");
// Create an instance of the PdfSaveOptions class
PdfSaveOptions options = new PdfSaveOptions();
// Call the ConvertEPUB() method to convert EPUB to PDF
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to PDF using C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-pdf/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Invoke the ConvertEPUB() method to convert EPUB to PDF
Converter.ConvertEPUB(stream, new PdfSaveOptions(), Path.Combine(OutputDir, "convert-by-two-lines.pdf"));
// Convert EPUB to PDF in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-pdf/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "stream-provider.pdf");
// Convert EPUB to PDF by using the MemoryStreamProvider class
Converter.ConvertEPUB(stream, new PdfSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(savePath))
{
memory.CopyTo(fs);
}
// Convert EPUB to PDF with custom page size using C#
// Learn more: https://docs.aspose.com/html/net/renderers/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Create an instance of EPUB Renderer
using EpubRenderer renderer = new EpubRenderer();
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "convert-epub.pdf");
// Create the instance of Rendering Options and set a custom page-size
PdfRenderingOptions options = new PdfRenderingOptions();
options.PageSetup.AnyPage = new Page(new Size(800, 400));
// Create an instance of the PdfDevice class
using PdfDevice device = new PdfDevice(options, savePath);
// Convert EPUB to PDF
renderer.Render(device, stream);
// Convert EPUB to PDF in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-pdf/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-options.pdf");
// Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions()
{
PageSetup =
{
AnyPage = new Page()
{
Size = new Aspose.Html.Drawing.Size(Length.FromPixels(1000), Length.FromPixels(1000))
}
},
BackgroundColor = System.Drawing.Color.AliceBlue
};
// Call the ConvertEPUB() method to convert EPUB to PDF
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to PNG in C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-png/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-output.png");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions();
// Call the ConvertEPUB() method to convert EPUB to PNG
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to PNG in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-png/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-options.png");
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions()
{
UseAntialiasing = true,
HorizontalResolution = 400,
VerticalResolution = 400,
BackgroundColor = System.Drawing.Color.AliceBlue
};
// Call the ConvertEPUB() method to convert EPUB to PNG
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to PNG using C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-png/
// Invoke the ConvertEPUB() method to convert EPUB to PNG
Converter.ConvertEPUB(File.OpenRead(DataDir + "input.epub"), new ImageSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.png"));
// Convert EPUB to TIFF using C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-tiff/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-output.tiff");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
// Call the ConvertEPUB() method to convert EPUB to TIFF
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to TIFF in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-tiff/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-options.tiff");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff)
{
Compression = Compression.None,
UseAntialiasing = true,
HorizontalResolution = 400,
VerticalResolution = 400,
BackgroundColor = System.Drawing.Color.AliceBlue
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(800, 500), new Margin(30, 20, 10, 10));
// Call the ConvertEPUB() method to convert EPUB to TIFF
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to XPS in C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-xps/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-output.xps");
// Create an instance of XpsSaveOptions
XpsSaveOptions options = new XpsSaveOptions();
// Call the ConvertEPUB() method to convert EPUB to XPS
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to XPS using C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-xps/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Invoke the ConvertEPUB() method to convert EPUB to XPS
Converter.ConvertEPUB(stream, new XpsSaveOptions(), Path.Combine(OutputDir, "convert-by-two-lines.xps"));
// Convert EPUB to XPS in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-xps/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "stream-provider.xps");
// Convert EPUB to XPS by using the MemoryStreamProvider class
Converter.ConvertEPUB(stream, new XpsSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(savePath))
{
memory.CopyTo(fs);
}
// Convert EPUB to XPS in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-xps/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-options.xps");
// Create an instance of XpsSaveOptions. Set up the page-size and change the background color to LightGray
XpsSaveOptions options = new XpsSaveOptions()
{
PageSetup =
{
AnyPage = new Page()
{
Size = new Aspose.Html.Drawing.Size(Length.FromPixels(500), Length.FromPixels(500))
}
},
BackgroundColor = System.Drawing.Color.LightGray
};
// Call the ConvertEPUB() method to convert EPUB to XPS
Converter.ConvertEPUB(stream, options, savePath);
// Convert EPUB to JPG using C#
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-jpg/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "input-output.jpg");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Call the ConvertEPUB() method to convert EPUB to JPG
Converter.ConvertEPUB(stream, options, savePath);
// Convert HTML from URL to PDF using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-pdf/
// Initialize an HTML document from a URL
using HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/files/document.html");
// Initialize PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "document.pdf");
// Convert HTML to PDF
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to BMP using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-bmp/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "bmp.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "bmp-output.bmp");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
// Convert HTML to BMP
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to BMP in C# with custom background, resolution, and antialiasing settings
// Learn more: https://docs.aspose.com/html/net/convert-html-to-bmp/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "bmp.html");
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "bmp-output-options.bmp");
// Initialize an HTML Document from the html file
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp)
{
UseAntialiasing = false,
HorizontalResolution = 350,
VerticalResolution = 350,
BackgroundColor = System.Drawing.Color.Beige
};
// Convert HTML to BMP
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to DOCX using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-docx/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "canvas.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "canvas-output.docx");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize DocSaveOptions
DocSaveOptions options = new DocSaveOptions();
// Convert HTML to DOCX
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to DOCX in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-html-to-docx/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Initialize an HTML document
using 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
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.docx")))
{
memory.CopyTo(fs);
}
// Convert HTML to DOCX in C# with custom page size and margins
// Learn more: https://docs.aspose.com/html/net/convert-html-to-docx/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "canvas.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "canvas-output-options.docx");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize DocSaveOptions. Set up the page-size 600x400 pixels and margins
DocSaveOptions options = new DocSaveOptions();
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(600, 400), new Margin(10, 10, 10, 10));
// Convert HTML to DOCX
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to DOCX in C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-docx/
// Invoke the ConvertHTML() method to convert HTML to DOCX
Converter.ConvertHTML(@"<h1>Convert HTML to DOCX!</h1>", ".", new DocSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.docx"));
// Convert HTML to GIF using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-gif/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "spring.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "spring-output.gif");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
// Convert HTML to GIF
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to GIF in C# with custom background, resolution, and antialiasing settings
// Learn more: https://docs.aspose.com/html/net/convert-html-to-gif/
string documentPath = Path.Combine(OutputDir, "convert-to-gif.html");
string savePath = Path.Combine(OutputDir, "convert-to-gif-options.gif");
// Prepare HTML code and save it to a file
string code = "<h1> HTML to GIF Converter </h1>\r\n" +
"<p> GIF is a popular image format that supports animated images and frequently used in web publishing. HTML to GIF conversion allows you to save an HTML document as a GIF image. </p>\r\n";
File.WriteAllText(documentPath, code);
// Initialize an HTML Document from the html file
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif)
{
UseAntialiasing = false,
HorizontalResolution = 100,
VerticalResolution = 100,
BackgroundColor = System.Drawing.Color.MistyRose
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 200), new Margin(30, 20, 10, 10));
// Convert HTML to GIF
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to JPG using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-jpg/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "spring.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "spring-output.jpg");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Convert HTML to JPG
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to JPG in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-html-to-jpg/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Initialize an HTML document
using 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
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.jpg")))
{
memory.CopyTo(fs);
}
// Convert HTML to JPG in C# with with custom background, resolution, and antialiasing settings
// Learn more: https://docs.aspose.com/html/net/convert-html-to-jpg/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "color.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "color-output-options.jpg");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
{
UseAntialiasing = true,
HorizontalResolution = 200,
VerticalResolution = 200,
BackgroundColor = System.Drawing.Color.AliceBlue
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 500), new Margin(30, 20, 10, 10));
// Convert HTML to JPG
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to JPG in C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-jpg/
// Invoke the ConvertHTML() method to convert the HTML code to JPG image
Converter.ConvertHTML(@"<h1>Convert HTML to JPG!</h1>", ".", new ImageSaveOptions(ImageFormat.Jpeg), Path.Combine(OutputDir, "convert-with-single-line.jpg"));
// Convert HTML to Markdown in C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-markdown/
// Prepare HTML code and save it to a file
string code = "<h1>Header 1</h1>" +
"<h2>Header 2</h2>" +
"<p>Convert HTML to Markdown</p>";
File.WriteAllText("convert.html", code);
// Call ConvertHTML() method to convert HTML to Markdown
Converter.ConvertHTML("convert.html", new MarkdownSaveOptions(), Path.Combine(OutputDir, "convert.md"));
// Convert HTML to Markdown in C# using Git syntax
// Learn more: https://docs.aspose.com/html/net/convert-html-to-markdown/
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "output-git.md");
// Prepare HTML code and save it to the file
string code = "<h1>Header 1</h1>" +
"<h2>Header 2</h2>" +
"<p>Hello, World!!</p>";
File.WriteAllText(Path.Combine(OutputDir, "document.html"), code);
// Call ConvertHTML() method to convert HTML to Markdown
Converter.ConvertHTML(Path.Combine(OutputDir, "document.html"), MarkdownSaveOptions.Git, savePath);
// Convert HTML to MHTML in C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-mhtml/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "drawing.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "drawing-output.mht");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize MHTMLSaveOptions
MHTMLSaveOptions options = new MHTMLSaveOptions();
// Convert HTML to MHTML
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to MHTML with custom settings using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-mhtml/
// 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>";
File.WriteAllText("document.html", code);
// Prepare HTML code and save it to the file as 'document2.html'
code = @"<span>Hello, World!!</span>";
File.WriteAllText("document2.html", code);
string savePath = Path.Combine(OutputDir, "output-options.mht");
// Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
MHTMLSaveOptions options = new MHTMLSaveOptions()
{
ResourceHandlingOptions =
{
MaxHandlingDepth = 1
}
};
// Convert HTML to MHTML
Converter.ConvertHTML("document.html", options, savePath);
// Convert HTML to MHTML using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-mhtml/
// Invoke the ConvertHTML() method to convert HTML to MHTML
Converter.ConvertHTML(@"<h1>Hellow, Word!</h1>", ".", new MHTMLSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.mht"));
// Convert HTML to PDF in C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-pdf/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "spring.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "spring-output.pdf");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
// Convert HTML to PDF
Converter.ConvertHTML(document, options, savePath);
// Flatten PDF during HTML to PDF conversion using C#
// Learn more: https://docs.aspose.com/html/net/flatten-pdf/
// Prepare a path to an HTML source file
string sourcePath = Path.Combine(DataDir, "SampleHtmlForm.html");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(sourcePath);
// Prepare PDF save options
PdfSaveOptions options = new PdfSaveOptions
{
// Flatten all form fields
FormFieldBehaviour = FormFieldBehaviour.Flattened
};
// Prepare a path to the result file
string resultPath = Path.Combine(OutputDir, "form-flattened.pdf");
// Convert HTML to PDF
Converter.ConvertHTML(document, options, resultPath);
// Convert HTML to PDF using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-pdf/
// Invoke the ConvertHTML() method to convert HTML to PDF
Converter.ConvertHTML(@"<h1>Convert HTML to PDF!</h1>", ".", new PdfSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.pdf"));
// Convert HTML to PDF using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-pdf/
Aspose.Html.Converters.Converter.ConvertHTML(@"<span>Hello, World!!</span>", ".", new Aspose.Html.Saving.PdfSaveOptions(), "output.pdf");
// Convert HTML to PDF in C# with password protection and custom permissions
// Learn more: https://docs.aspose.com/html/net/convert-html-to-pdf/
// Initialize an HTML document from a URL
using HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/files/document.html");
// Initialize PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
// Configure PDF encryption settings
options.Encryption = new PdfEncryptionInfo(
ownerPassword: "owner123",
userPassword: "user123",
permissions: PdfPermissions.PrintDocument | PdfPermissions.ExtractContent,
encryptionAlgorithm: PdfEncryptionAlgorithm.RC4_128
);
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "document-with-password.pdf");
// Convert HTML to PDF
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to PDF in C# with custom page settings
// Learn more: https://docs.aspose.com/html/net/convert-html-to-pdf/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "drawing.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "drawing-options.pdf");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize PdfSaveOptions. Set up the page-size 600x300 pixels, margins, resolutions and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions()
{
HorizontalResolution = 200,
VerticalResolution = 200,
BackgroundColor = System.Drawing.Color.AliceBlue,
JpegQuality = 100
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(600, 300), new Margin(20, 10, 10, 10));
// Convert HTML to PDF
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to PDF in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-html-to-pdf/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Initialize an HTML document
using 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
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.pdf")))
{
memory.CopyTo(fs);
}
// Render HTML to PDF with custom page settings using C#
// Learn more: https://docs.aspose.com/html/net/renderers/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "file.html");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Create an instance of HTML Renderer
using HtmlRenderer renderer = new HtmlRenderer();
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "convert-html-options.pdf");
// Create the instance of Rendering Options and set a custom page-size
PdfRenderingOptions options = new PdfRenderingOptions();
options.PageSetup.AnyPage = new Page(new Size(600, 200));
options.Encryption = new PdfEncryptionInfo(
"user_pwd",
"owner_pwd",
PdfPermissions.PrintDocument,
PdfEncryptionAlgorithm.RC4_128);
// Create an instance of PDF device
using PdfDevice device = new PdfDevice(options, savePath);
// Render HTML to PDF
renderer.Render(device, document);
// Convert HTML to PDF with custom metadata using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-pdf/
// Load an HTML document from file or string
using (HTMLDocument document = new HTMLDocument("<h1>Hello, PDF!</h1>", "."))
{
// Initialize PDF rendering options
PdfSaveOptions options = new PdfSaveOptions();
// Set PDF document information (metadata)
options.DocumentInfo.Title = "Sample PDF Title";
options.DocumentInfo.Author = "Aspose.HTML for .NET";
options.DocumentInfo.Subject = "HTML to PDF Example";
options.DocumentInfo.Keywords = "Aspose, HTML, PDF, conversion";
// Set output file path
string outputPath = Path.Combine(OutputDir, "document.pdf");
// Convert HTML to PDF
Converter.ConvertHTML(document, options, outputPath);
}
Console.WriteLine("PDF created successfully.");
// Convert HTML to PNG in C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-png/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "nature.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "nature-output.png");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Convert HTML to PNG
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to PNG using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-png/
// Invoke the ConvertHTML() method to convert HTML to PNG
Converter.ConvertHTML(@"<h1>Convert HTML to PNG!</h1>", ".", new ImageSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.png"));
// Convert HTML to PNG in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-html-to-png/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Initialize an HTML document
using HTMLDocument document = new HTMLDocument(@"<h1>Convert HTML to PNG File Format!</h1>", ".");
// Convert HTML to JPG using the MemoryStreamProvider
Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Png), streamProvider);
// Get access to the memory stream that contains the result data
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.png")))
{
memory.CopyTo(fs);
}
// Convert HTML to PNG in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-html-to-png/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "nature.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "nature-output-options.png");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions()
{
UseAntialiasing = false,
HorizontalResolution = 100,
VerticalResolution = 100,
BackgroundColor = System.Drawing.Color.Beige
};
// Convert HTML to PNG
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to TIFF using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-tiff/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "nature.html");
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "nature-output.tiff");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
// Convert HTML to TIFF
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to TIFF with custom settings using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-tiff/
string documentPath = Path.Combine(DataDir, "nature.html");
string savePath = Path.Combine(OutputDir, "nature-output-options.tiff");
// Initialize an HTML Document from the html file
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff)
{
Compression = Compression.None,
BackgroundColor = System.Drawing.Color.Bisque,
HorizontalResolution = 150,
VerticalResolution = 150,
UseAntialiasing = true,
};
// Convert HTML to TIFF
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to XPS in C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-xps/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "canvas.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "canvas-output.xps");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize XpsSaveOptions
XpsSaveOptions options = new XpsSaveOptions();
// Convert HTML to XPS
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to XPS using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-xps/
// Invoke the ConvertHTML() method to convert the HTML code to XPS
Converter.ConvertHTML(@"<h1>Convert HTML to XPS!</h1>", ".", new XpsSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.xps"));
// Convert HTML to XPS in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-html-to-xps/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Initialize an HTML document
using HTMLDocument document = new HTMLDocument(@"<h1>Convert HTML to XPS File Format!</h1>", ".");
// Convert HTML to XPS using MemoryStreamProvider
Converter.ConvertHTML(document, new XpsSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.xps")))
{
memory.CopyTo(fs);
}
// Convert inline HTML elements to Markdown using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-markdown/
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "inline-html.md");
// Prepare HTML code and save it to the file
string code = "text<div markdown='inline'><code>text</code></div>";
File.WriteAllText(Path.Combine(OutputDir, "inline.html"), code);
// Call ConvertHTML() method to convert HTML to Markdown
Converter.ConvertHTML(Path.Combine(OutputDir, "inline.html"), new MarkdownSaveOptions(), savePath);
// Output file will contain: text\r\n<div markdown="inline"><code>text</code></div>
// Convert Markdown to HTML in C#
// Learn more: https://docs.aspose.com/html/net/convert-ai-markdown-to-html/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(DataDir, "nature.md");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "nature-output.html");
// Convert Markdown to HTML
Converter.ConvertMarkdown(sourcePath, savePath);
// Convert Markdown to styled HTML with injected CSS and semantic head structure using stream input
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-html/
// AI response in Markdown format with the text, code example and link
string markdownContent = "# How to Convert a Markdown File to HTML\n" +
"Markdown is a lightweight markup language used for formatting text. If you need to convert a Markdown file to an HTML document, you can use **Aspose.HTML for .NET**.\n\n" +
"## Steps to Convert\n" +
"1. Load the Markdown file.\n" +
"2. Convert it to an HTML document.\n" +
"3. Save the output file.\n\n" +
"## Example Code\n" +
"```csharp\n" +
"// Convert a Markdown file to HTML\n" +
"Converter.ConvertMarkdown(\"input.md\", \"output.html\");\n" +
"```\n\n" +
"For more details, refer to the [official documentation](https://docs.aspose.com/html/net/convert-markdown-to-html/).\n";
// Create a memory stream from the Markdown string
using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(markdownContent)))
{
// Convert Markdown to HTML
HTMLDocument document = Converter.ConvertMarkdown(stream, "");
// Ensure the document has a <head> element
HTMLHeadElement head = document.QuerySelector("head") as HTMLHeadElement;
if (head == null)
{
head = document.CreateElement("head") as HTMLHeadElement;
document.DocumentElement.InsertBefore(head, document.Body);
}
// Create a <style> element with CSS styles
string cssStyles = "body { font-family: Open Sans, sans-serif; max-width: 800px; margin: auto; padding: 20px; background-color: #f9f9f9; }\n" +
"h1, h2 { color: #333; }\n" +
"p, li { font-size: 14px; }\n" +
"code, pre { font-family: Consolas, monospace; color: #f8f8f2; background-color: #272822; padding: 10px; border-radius: 5px; display: block; }\n";
HTMLStyleElement styleElement = document.CreateElement("style") as HTMLStyleElement;
styleElement.TextContent = cssStyles;
// Append the <style> element to the <head>
head.AppendChild(styleElement);
// Save the resulting HTML file
string outputPath = Path.Combine(OutputDir, "chartAnswer.html");
document.Save(outputPath);
// Print the HTML content to the console
Console.WriteLine(document.DocumentElement.OuterHTML);
Console.WriteLine("Conversion completed. HTML saved at " + outputPath);
}
// Convert Markdown to BMP using C#
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-image/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(DataDir, "document.md");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "output.bmp");
// Convert Markdown to HTML
using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
// Convert HTML document to BMP image file format
Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Bmp), savePath);
// Convert Markdown to DOCX using C#
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-docx/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(OutputDir, "document.md");
// Prepare a simple Markdown example
string code = "### Hello, World!" +
"\r\n" +
"Convert Markdown to DOCX!";
// Create a Markdown file
File.WriteAllText(sourcePath, code);
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "document-output.docx");
// Convert Markdown to HTML document
using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
// Convert HTML document to DOCX file format
Converter.ConvertHTML(document, new DocSaveOptions(), savePath);
// Convert Markdown to DOCX in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-docx/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(DataDir, "nature.md");
// Prepare a path for converted DOCX file saving
string savePath = Path.Combine(OutputDir, "nature-output.docx");
// Convert Markdown to HTML
using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
// Initialize DocSaveOptions. Set up the page-size 500x1000 pixels and margins
DocSaveOptions options = new DocSaveOptions();
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 1000), new Margin(20, 20, 10, 10));
// Convert the HTML document to DOCX file format
Converter.ConvertHTML(document, options, savePath);
// Convert Markdown to GIF using C#
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-image/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(DataDir, "document.md");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "output.gif");
// Convert Markdown to HTML
using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
// Convert HTML document to GIF image file format
Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Gif), savePath);
// Convert Markdown to HTML using C#
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-html/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(OutputDir, "document.md");
// Prepare a simple Markdown example
string code = "### Hello, World!" +
"\r\n" +
"[visit applications](https://products.aspose.app/html/)";
// Create a Markdown file
File.WriteAllText(sourcePath, code);
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "document-output.html");
// Convert Markdown to HTML document
Converter.ConvertMarkdown(sourcePath, savePath);
// Convert Markdown to JPG using C#
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-image/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(OutputDir, "document.md");
// Prepare a simple Markdown example
string code = "### Hello, World!" +
"\r\n" +
"[visit applications](https://products.aspose.app/html/family)";
// Create a Markdown file
File.WriteAllText(sourcePath, code);
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "document-output.jpg");
// Convert Markdown to HTML
using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
// Convert HTML document to JPG image file format
Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Jpeg), savePath);
// Convert Markdown to JPG in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-image/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(DataDir, "nature.md");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "nature-options.jpg");
// Convert Markdown to HTML
using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
// Initialize ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
{
UseAntialiasing = true,
HorizontalResolution = 200,
VerticalResolution = 200,
BackgroundColor = System.Drawing.Color.AliceBlue
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(600, 950), new Margin(30, 20, 10, 10));
// Convert HTML to JPG
Converter.ConvertHTML(document, options, savePath);
// Convert Markdown to PDF using C#
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-pdf/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(OutputDir, "document.md");
// Prepare a simple Markdown example
string code = "### Hello, World!" +
"\r\n" +
"[visit applications](https://products.aspose.app/html/applications)";
// Create a Markdown file
File.WriteAllText(sourcePath, code);
// Convert Markdown to HTML
using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
// Prepare a path for converted PDF file saving
string savePath = Path.Combine(OutputDir, "document-output.pdf");
// Convert the HTML document to PDF file format
Converter.ConvertHTML(document, new PdfSaveOptions(), savePath);
// Convert Markdown to PDF in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-pdf/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(DataDir, "nature.md");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "nature-output.pdf");
// Convert Markdown to HTML
using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
// Initialize PdfSaveOptions. Set up the resolutions, JpegQuality and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions()
{
HorizontalResolution = 200,
VerticalResolution = 200,
BackgroundColor = System.Drawing.Color.AliceBlue,
JpegQuality = 100
};
// Convert the HTML document to PDF file format
Converter.ConvertHTML(document, options, savePath);
// Convert Markdown to PNG using C#
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-image/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(DataDir, "document.md");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "output.png");
// Convert Markdown to HTML
using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
// Convert HTML document to PNG image file format
Converter.ConvertHTML(document, new ImageSaveOptions(), savePath);
// Convert Markdown to TIFF using C#
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-image/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(DataDir, "document.md");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "output.tiff");
// Convert Markdown to HTML
using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
// Convert HTML document to TIFF image file format
Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Tiff), savePath);
// Convert Markdown to XPS in C#
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-xps/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(OutputDir, "document.md");
// Prepare a simple Markdown example
string code = "### Hello, World!" +
"\r\n" +
"[visit applications](https://products.aspose.app/html/applications)";
// Create a Markdown file
File.WriteAllText(sourcePath, code);
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "document-output.xps");
// Convert Markdown to HTML
using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
// Convert the HTML document to XPS file format
Converter.ConvertHTML(document, new XpsSaveOptions(), savePath);
// Convert Markdown to XPS in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-markdown-to-xps/
// Prepare a path to a source Markdown file
string sourcePath = Path.Combine(DataDir, "nature.md");
// Prepare a path for converted PDF file saving
string savePath = Path.Combine(OutputDir, "nature-output.xps");
// Convert Markdown to HTML
using HTMLDocument document = Converter.ConvertMarkdown(sourcePath);
// Initialize XpsSaveOptions. Set up the resilutions, page-size, margins and change the background color to AntiqueWhite
XpsSaveOptions options = new XpsSaveOptions()
{
HorizontalResolution = 200,
VerticalResolution = 200,
BackgroundColor = System.Drawing.Color.AntiqueWhite
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(Length.FromInches(5.0f), Length.FromInches(10.0f)), new Margin(30, 20, 10, 10));
// Convert HTML to XPS file format
Converter.ConvertHTML(document, options, savePath);
// Convert MHTML to BMP using C#
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-image/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Create an instance of ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
// Call the ConvertMHTML() method to convert MHTML to BMP
Converter.ConvertMHTML(stream, options, Path.Combine(OutputDir, "sample-output.bmp"));
// Convert MHTML to DOCX using C#
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-docx/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "sample-output.docx");
// Create an instance of DocSaveOptions
DocSaveOptions options = new DocSaveOptions();
// Call the ConvertMHTML() method to convert MHTML to DOCX
Converter.ConvertMHTML(stream, options, savePath);
// Convert MHTML to DOCX in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-docx/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "stream-provider.docx");
// Convert MHTML to DOCX by using the MemoryStreamProvider class
Converter.ConvertMHTML(stream, new DocSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(savePath))
{
memory.CopyTo(fs);
}
// Convert MHTML to DOCX in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-docx/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "sample-options.docx");
// Create an instance of DocxSaveOptions and set A5 as a page size
DocSaveOptions options = new DocSaveOptions();
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(Length.FromInches(8.3f), Length.FromInches(5.8f)));
// Call the ConvertMHTML() method to convert MHTML to DOCX
Converter.ConvertMHTML(stream, options, savePath);
// Convert MHTML to DOCX in C#
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-docx/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Invoke the ConvertMHTML() method to convert MHTML to DOCX
Converter.ConvertMHTML(stream, new DocSaveOptions(), Path.Combine(OutputDir, "convert-by-two-lines.docx"));
// Convert MHTML to GIF in C#
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-image/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Create an instance of ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
// Call the ConvertMHTML() method to convert MHTML to GIF
Converter.ConvertMHTML(stream, options, Path.Combine(OutputDir, "sample-output.gif"));
// Convert MHTML to PNG with custom page settings using C#
// Learn more: https://docs.aspose.com/html/net/renderers/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Create an instance of MHTML Renderer
using MhtmlRenderer renderer = new MhtmlRenderer();
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "convert-mhtml.png");
// Create the instance of Rendering Options and set a custom page-size and background color
ImageRenderingOptions options = new ImageRenderingOptions();
options.PageSetup.AnyPage = new Page(new Size(200, 100));
options.BackgroundColor = System.Drawing.Color.Bisque;
// Create an instance of ImageDevice
using ImageDevice device = new ImageDevice(options, savePath);
// Convert MHTML to PNG
renderer.Render(device, stream);
// Convert MHTML to JPG using C#
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-image/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Create an instance of ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Call the ConvertMHTML() method to convert MHTML to JPG
Converter.ConvertMHTML(stream, options, Path.Combine(OutputDir, "sample-output.jpg"));
// Convert MHTML to JPG in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-image/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "stream-provider.jpg");
// Convert MHTML to JPG by using the MemoryStreamProvider class
Converter.ConvertMHTML(stream, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
// Get access to the memory streams that contain the resulted data
for (int i = 0; i < streamProvider.Streams.Count; i++)
{
MemoryStream memory = streamProvider.Streams[i];
memory.Seek(0, SeekOrigin.Begin);
// Flush the page to the output file
using (FileStream fs = File.Create(savePath))
{
memory.CopyTo(fs);
}
}
// Convert MHTML to JPG in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-image/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "sample-options.jpg");
// Initailize the ImageSaveOptions with a custom page-size and a background color
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
{
PageSetup =
{
AnyPage = new Page()
{
Size = new Aspose.Html.Drawing.Size(Length.FromPixels(1000), Length.FromPixels(500))
}
},
BackgroundColor = System.Drawing.Color.Beige
};
// Call the ConvertMHTML() method to convert MHTML to JPG
Converter.ConvertMHTML(stream, options, savePath);
// Convert MHTML to JPG in C#
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-image/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Invoke the ConvertMHTML() method to convert MHTML to JPG
Converter.ConvertMHTML(stream, new ImageSaveOptions(ImageFormat.Jpeg), Path.Combine(OutputDir, "convert-by-two-lines.jpg"));
// Convert MHTML to PDF in C#
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-pdf/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "sample-output.pdf");
// Create an instance of PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
// Call the ConvertMHTML() method to convert MHTML to PDF
Converter.ConvertMHTML(stream, options, savePath);
// Flatten PDF during MHTML to PDF conversion using C#
// Learn more: https://docs.aspose.com/html/net/flatten-pdf/
// Prepare a path to an MHTML source file
string sourcePath = Path.Combine(DataDir, "SampleHtmlForm.mhtml");
// Initialize PDF save options
PdfSaveOptions options = new PdfSaveOptions
{
// Flatten all form fields
FormFieldBehaviour = FormFieldBehaviour.Flattened
};
// Prepare a path to the result file
string resultPath = Path.Combine(OutputDir, "document-flattened.pdf");
// Convert MHTML to PDF
Converter.ConvertMHTML(sourcePath, options, resultPath);
// Convert MHTML to PDF in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-pdf/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "sample-options.pdf");
// Create an instance of PdfSaveOptions. Set up the page-size and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions()
{
PageSetup =
{
AnyPage = new Page()
{
Size = new Aspose.Html.Drawing.Size(Length.FromPixels(3000), Length.FromPixels(1000))
}
},
BackgroundColor = System.Drawing.Color.AliceBlue
};
// Call the ConvertMHTML() method to convert MHTML to PDF
Converter.ConvertMHTML(stream, options, savePath);
// Convert MHTML to PDF in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-pdf/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "stream-provider.pdf");
// Convert MHTML to PDF by using the MemoryStreamProvider class
Converter.ConvertMHTML(stream, new PdfSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(savePath))
{
memory.CopyTo(fs);
}
// Convert MHTML to PDF with custom page settings using C#
// Learn more: https://docs.aspose.com/html/net/renderers/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Create an instance of MHTML Renderer
using MhtmlRenderer renderer = new MhtmlRenderer();
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "convert-mhtml-options.pdf");
// Create the instance of Rendering Options and set a custom page-size and background color
PdfRenderingOptions options = new PdfRenderingOptions();
options.PageSetup.AnyPage = new Page(new Size(600, 200));
options.BackgroundColor = System.Drawing.Color.Azure;
// Create an instance of PDF device
using PdfDevice device = new PdfDevice(options, savePath);
// Convert MHTML to PDF
renderer.Render(device, stream);
// Convert MHTML to PDF using C#
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-pdf/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Invoke the ConvertMHTML() method to convert MHTML to PDF
Converter.ConvertMHTML(stream, new PdfSaveOptions(), Path.Combine(OutputDir, "convert-by-two-lines.pdf"));
// Convert MHTML to PNG using C#
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-image/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Create an instance of ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Png);
// Call the ConvertMHTML() method to convert MHTML to PNG
Converter.ConvertMHTML(stream, options, Path.Combine(OutputDir, "sample-output.png"));
// Convert MHTML to TIFF in C#
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-image/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "sample-options.tiff");
// Create an instance of ImageSaveOptions
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
// Call the ConvertMHTML() method to convert MHTML to TIFF
Converter.ConvertMHTML(stream, options, savePath);
// Convert MHTML to XPS in C#
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-xps/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "sample-output.xps");
// Create an instance of XpsSaveOptions
XpsSaveOptions options = new XpsSaveOptions();
// Call the ConvertMHTML() method to convert MHTML to XPS
Converter.ConvertMHTML(stream, options, savePath);
// Convert MHTML to XPS in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-xps/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "stream-provider.xps");
// Convert MHTML to XPS by using the MemoryStreamProvider class
Converter.ConvertMHTML(stream, new XpsSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(savePath))
{
memory.CopyTo(fs);
}
// Convert MHTML to XPS using C#
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-xps/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Invoke the ConvertMHTML() method to convert the MHTML code to XPS
Converter.ConvertMHTML(stream, new XpsSaveOptions(), Path.Combine(OutputDir, "convert-by-two-lines.xps"));
// Convert MHTML to XPS in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-mhtml-to-xps/
// Open an existing MHTML file for reading
using FileStream stream = File.OpenRead(DataDir + "sample.mht");
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "sample-options.xps");
// Create an instance of XpsSaveOptions. Set up the page-size and change the background color to AliceBlue
XpsSaveOptions options = new XpsSaveOptions();
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(Length.FromInches(8.3f), Length.FromInches(5.8f)));
options.BackgroundColor = System.Drawing.Color.AliceBlue;
// Call the ConvertMHTML() method to convert MHTML to XPS
Converter.ConvertMHTML(stream, options, savePath);
// Convert SVG to BMP using C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-bmp/
// Prepare SVG code
string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
"<circle cx ='100' cy ='100' r ='50' fill='none' stroke='red' stroke-width='10' />" +
"</svg>";
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "circle.bmp");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp);
// Convert SVG to BMP
Converter.ConvertSVG(code, ".", options, savePath);
// Convert SVG to BMP in C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-bmp/
// Invoke the ConvertSVG() method for SVG to BMP conversion
Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new ImageSaveOptions(ImageFormat.Bmp), Path.Combine(OutputDir, "convert-with-single-line.bmp"));
// Convert SVG to BMP in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-bmp/
// Prepare a path to a source SVG file
string documentPath = Path.Combine(DataDir, "flower1.svg");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "flower-options.bmp");
// Initialize an SVG document from the file
using SVGDocument document = new SVGDocument(documentPath);
// Initialize ImageSaveOptions. Set up the SmoothingMode, resolutions, and change the background color to AliceBlue
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Bmp)
{
HorizontalResolution = 200,
VerticalResolution = 200,
BackgroundColor = System.Drawing.Color.AliceBlue,
UseAntialiasing = true,
};
// Convert SVG to BMP
Converter.ConvertSVG(document, options, savePath);
// Convert SVG to DOCX in C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-docx/
// Prepare SVG code
string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
"<circle cx ='100' cy ='100' r ='50' fill='none' stroke='red' stroke-width='10' />" +
"</svg>";
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "circle.docx");
// Initialize DocSaveOptions
DocSaveOptions options = new DocSaveOptions();
// Convert SVG to DOCX
Converter.ConvertSVG(code, ".", options, savePath);
// Convert SVG to DOCX using C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-docx/
// Invoke the ConvertSVG() method to convert SVG to DOCX
Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new DocSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.docx"));
// Convert SVG to DOCX in C# with custom page settings
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-docx/
// Prepare a path to a source SVG file
string documentPath = Path.Combine(DataDir, "shapes.svg");
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "shapes-options.docx");
// Initialize an SVG document from the file
using SVGDocument document = new SVGDocument(documentPath);
// Initialize DocSaveOptions. Set up the page-size and margins
DocSaveOptions options = new DocSaveOptions();
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 500), new Margin(30, 10, 10, 10));
// Convert SVG to DOCX
Converter.ConvertSVG(document, options, savePath);
// Convert SVG to GIF using C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-gif/
// Prepare SVG code
string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
"<circle cx ='100' cy ='100' r ='55' fill='pink' stroke='red' stroke-width='8' />" +
"</svg>";
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "circle.gif");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif);
// Convert SVG to GIF
Converter.ConvertSVG(code, ".", options, savePath);
// Convert SVG to GIF in C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-gif/
// Invoke the ConvertSVG() method for SVG to GIF conversion
Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new ImageSaveOptions(ImageFormat.Gif), Path.Combine(OutputDir, "convert-with-single-line.gif"));
// Convert SVG to GIF in C# with custom background, resolution, and antialiasing settings
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-gif/
// Prepare a path to a source SVG file
string documentPath = Path.Combine(DataDir, "gradient.svg");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "gradient-options.gif");
// Initialize an SVG document from the file
using SVGDocument document = new SVGDocument(documentPath);
// Initialize ImageSaveOptions. Set up the SmoothingMode, resolutions, and change the background color to AliceBlue
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Gif)
{
UseAntialiasing = true,
HorizontalResolution = 200,
VerticalResolution = 200,
BackgroundColor = System.Drawing.Color.AliceBlue
};
// Convert SVG to GIF
Converter.ConvertSVG(document, options, savePath);
// Convert SVG to JPG in C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-jpg/
// Prepare SVG code
string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
"<circle cx ='100' cy ='100' r ='55' fill='green' stroke='red' stroke-width='10' />" +
"</svg>";
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "circle.jpg");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
// Convert SVG to JPG
Converter.ConvertSVG(code, ".", options, savePath);
// Convert SVG to JPG using C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-jpg/
// Invoke the ConvertSVG() method for SVG to JPG conversion
Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new ImageSaveOptions(ImageFormat.Jpeg), Path.Combine(OutputDir, "convert-with-single-line.jpg"));
// Convert SVG to JPG in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-jpg/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// 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 JPG using the MemoryStreamProvider
Converter.ConvertSVG(code, ".", new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
// Get access to the memory stream that contains the result data
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.jpg")))
{
memory.CopyTo(fs);
}
// Convert SVG to JPG in C# with custom background, resolution, and antialiasing settings
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-jpg/
// Prepare a path to a source SVG file
string documentPath = Path.Combine(DataDir, "flower.svg");
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "flower-options.jpg");
// Initialize an SVG document from the file
using SVGDocument document = new SVGDocument(documentPath);
// Create an instance of the ImageSaveOptions class. Set up the resolutions, SmoothingMode and change the background color to AliceBlue
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
{
UseAntialiasing = true,
HorizontalResolution = 200,
VerticalResolution = 200,
BackgroundColor = System.Drawing.Color.AliceBlue
};
// Convert SVG to JPG
Converter.ConvertSVG(document, options, savePath);
// Convert SVG to PDF using C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-pdf/
// Prepare SVG code
string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
"<circle cx ='100' cy ='100' r ='50' fill='none' stroke='red' stroke-width='5' />" +
"</svg>";
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "circle.pdf");
// Initialize PdfSaveOptions
PdfSaveOptions options = new PdfSaveOptions();
// Convert SVG to PDF
Converter.ConvertSVG(code, ".", options, savePath);
// Convert SVG to PDF in C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-pdf/
// Invoke the ConvertSVG() method for SVG to PDF conversion
Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new PdfSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.pdf"));
// Convert SVG to PDF in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-pdf/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// 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 PDF using the MemoryStreamProvider
Converter.ConvertSVG(code, ".", new PdfSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.pdf")))
{
memory.CopyTo(fs);
}
// Convert SVG to PDF in C# with custom page settings
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-pdf/
// Prepare a path to a source SVG file
string documentPath = Path.Combine(DataDir, "aspose.svg");
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "aspose-options.pdf");
// Initialize an SVG document from the file
using SVGDocument document = new SVGDocument(documentPath);
// Initialize PdfSaveOptions. Set up the page-size, margins, resolutions, JpegQuality, and change the background color to AliceBlue
PdfSaveOptions options = new PdfSaveOptions()
{
HorizontalResolution = 200,
VerticalResolution = 200,
BackgroundColor = System.Drawing.Color.AliceBlue,
JpegQuality = 100
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 500), new Margin(30, 10, 10, 10));
// Convert SVG to PDF
Converter.ConvertSVG(document, options, savePath);
// Convert SVG to PNG in C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-png/
// Prepare SVG code
string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
"<circle cx ='100' cy ='100' r ='60' fill='none' stroke='red' stroke-width='10' />" +
"</svg>";
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "circle.png");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions();
// Convert SVG to PNG
Converter.ConvertSVG(code, ".", options, savePath);
// Convert SVG to PNG using C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-png/
// Invoke the ConvertSVG() method for SVG to PNG conversion
Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new ImageSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.png"));
// Convert SVG to PNG in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-png/
// Prepare a path to a source SVG file
string documentPath = Path.Combine(DataDir, "flower1.svg");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "flower-options.png");
// Initialize an SVG document from the file
using SVGDocument document = new SVGDocument(documentPath);
// Create an instance of the ImageSaveOptions class. Set up the SmoothingMode, resolutions, and change the background color to AliceBlue
ImageSaveOptions options = new ImageSaveOptions()
{
HorizontalResolution = 200,
VerticalResolution = 200,
BackgroundColor = System.Drawing.Color.AliceBlue,
UseAntialiasing = true,
};
// Convert SVG to PNG
Converter.ConvertSVG(document, options, savePath);
// Convert SVG to PNG in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-png/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// 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
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.png")))
{
memory.CopyTo(fs);
}
// Convert SVG to TIFF in C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-tiff/
// Prepare SVG code
string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
"<circle cx ='100' cy ='100' r ='50' fill='pink' stroke='red' stroke-width='10' />" +
"</svg>";
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "circle.tiff");
// Create an instance of the ImageSaveOptions class
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff);
// Convert SVG to TIFF
Converter.ConvertSVG(code, ".", options, savePath);
// Convert SVG to TIFF using C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-tiff/
// Invoke the ConvertSVG() method for SVG to TIFF conversion
Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new ImageSaveOptions(ImageFormat.Tiff), Path.Combine(OutputDir, "convert-with-single-line.tiff"));
// Convert SVG to TIFF in C# with custom background, resolution, and compression settings
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-tiff/
// Prepare a path to a source SVG file
string documentPath = Path.Combine(DataDir, "gradient.svg");
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "gradient-options.tiff");
// Initialize an SVG document from the file
using SVGDocument document = new SVGDocument(documentPath);
// Initialize ImageSaveOptions. Set up the compression, resolutions, and change the background color to AliceBlue
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Tiff)
{
Compression = Compression.None,
HorizontalResolution = 200,
VerticalResolution = 200,
BackgroundColor = System.Drawing.Color.AliceBlue
};
// Convert SVG to TIFF
Converter.ConvertSVG(document, options, savePath);
// Convert SVG to XPS in C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-xps/
// Prepare SVG code
string code = "<svg xmlns='http://www.w3.org/2000/svg'>" +
"<circle cx ='100' cy ='100' r ='60' fill='none' stroke='red' stroke-width='10' />" +
"</svg>";
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "circle.xps");
// Initialize XpsSaveOptions
XpsSaveOptions options = new XpsSaveOptions();
// Convert SVG to XPS
Converter.ConvertSVG(code, ".", options, savePath);
// Convert SVG to XPS using C#
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-xps/
// Invoke the ConvertSVG() method to convert SVG to XPS
Converter.ConvertSVG(Path.Combine(DataDir, "shapes.svg"), new XpsSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.xps"));
// Convert SVG to XPS in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-xps/
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// 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 XPS using the MemoryStreamProvider
Converter.ConvertSVG(code, ".", new XpsSaveOptions(), streamProvider);
// Get access to the memory stream that contains the result data
MemoryStream memory = streamProvider.Streams.First();
memory.Seek(0, SeekOrigin.Begin);
// Flush the result data to the output file
using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.xps")))
{
memory.CopyTo(fs);
}
// Convert SVG to XPS in C# with custom page settings
// Learn more: https://docs.aspose.com/html/net/convert-svg-to-xps/
// Prepare a path to a source SVG file
string documentPath = Path.Combine(DataDir, "aspose.svg");
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "aspose-options.xps");
// Initialize an SVG document from the file
using SVGDocument document = new SVGDocument(documentPath);
// Initialize XpsSaveOptions. Set up the page-size 500x500 pixels, margins, resolutions and change the background color to AliceBlue
XpsSaveOptions options = new XpsSaveOptions()
{
HorizontalResolution = 200,
VerticalResolution = 200,
BackgroundColor = System.Drawing.Color.AliceBlue
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 500), new Margin(30, 10, 10, 10));
// Convert SVG to XPS
Converter.ConvertSVG(document, options, savePath);
// Convert EPUB to PNG in C# using memory stream
// Learn more: https://docs.aspose.com/html/net/convert-epub-to-png/
// Open an existing EPUB file for reading
using FileStream stream = File.OpenRead(DataDir + "input.epub");
// Create an instance of MemoryStreamProvider
using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
// Convert HTML to PNG using the MemoryStreamProvider
Converter.ConvertEPUB(stream, new ImageSaveOptions(), streamProvider);
// Get access to the memory streams that contain the resulted data
for (int i = 0; i < streamProvider.Streams.Count; i++)
{
MemoryStream memory = streamProvider.Streams[i];
memory.Seek(0, System.IO.SeekOrigin.Begin);
// Flush the page to the output file
using (FileStream fs = File.Create(Path.Combine(OutputDir, $"input-page_{i + 1}.png")))
{
memory.CopyTo(fs);
}
}
// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
// Learn more: https://docs.aspose.com/html/net/convert-html-to-pdf/
class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
{
// List of MemoryStream objects created during the document rendering
public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
public Stream GetStream(string name, string extension)
{
// This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
MemoryStream result = new MemoryStream();
Streams.Add(result);
return result;
}
public Stream GetStream(string name, string extension, int page)
{
// This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
MemoryStream result = new MemoryStream();
Streams.Add(result);
return result;
}
public void ReleaseStream(Stream stream)
{
// Here you can release the stream filled with data and, for instance, flush it to the hard-drive
}
public void Dispose()
{
// Releasing resources
foreach (MemoryStream stream in Streams)
stream.Dispose();
}
}
// Merge HTML to PDF with custom page settings using C#
// Learn more: https://docs.aspose.com/html/net/renderers/
// Prepare HTML code
string code1 = @"<br><span style='color: green'>Hello, World!!</span>";
string code2 = @"<br><span style='color: blue'>Hello, World!!</span>";
string code3 = @"<br><span style='color: red'>Hello, World!!</span>";
// Create three HTML documents to merge later
using HTMLDocument document1 = new HTMLDocument(code1, ".");
using HTMLDocument document2 = new HTMLDocument(code2, ".");
using HTMLDocument document3 = new HTMLDocument(code3, ".");
// Create an instance of HTML Renderer
using HtmlRenderer renderer = new HtmlRenderer();
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "merge-html-options.pdf");
// Create the instance of Rendering Options and set a custom page-size
PdfRenderingOptions options = new PdfRenderingOptions();
options.PageSetup.AnyPage = new Page(new Size(400, 100));
options.BackgroundColor = System.Drawing.Color.Azure;
// Create an instance of PDF device
using PdfDevice device = new PdfDevice(options, savePath);
// Merge all HTML documents into PDF
renderer.Render(device, document1, document2, document3);
// Merge HTML to PDF using C#
// Learn more: https://docs.aspose.com/html/net/renderers/
// Prepare HTML code
string code1 = @"<br><span style='color: green'>Hello, World!!</span>";
string code2 = @"<br><span style='color: blue'>Hello, World!!</span>";
string code3 = @"<br><span style='color: red'>Hello, World!!</span>";
// Create three HTML documents to merge later
using HTMLDocument document1 = new HTMLDocument(code1, ".");
using HTMLDocument document2 = new HTMLDocument(code2, ".");
using HTMLDocument document3 = new HTMLDocument(code3, ".");
// Create an instance of HTML Renderer
using HtmlRenderer renderer = new HtmlRenderer();
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "merge-html.pdf");
// Create an instance of PDF device
using PdfDevice device = new PdfDevice(savePath);
// Merge all HTML documents into PDF
renderer.Render(device, document1, document2, document3);
// Merge SVG to PDF using C#
// Learn more: https://docs.aspose.com/html/net/renderers/
// Initialize an SVG document from the file
using SVGDocument document1 = new SVGDocument(Path.Combine(DataDir, "shapes.svg"));
using SVGDocument document2 = new SVGDocument(Path.Combine(DataDir, "aspose.svg"));
// Create an instance of SVG Renderer
using SvgRenderer renderer = new SvgRenderer();
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "merge-svg.pdf");
// Create the instance of Rendering Options and set a custom page-size
PdfRenderingOptions options = new PdfRenderingOptions();
options.PageSetup.AnyPage = new Page(new Size(600, 500));
// Create an instance of the Pdfdevice class
using PdfDevice device = new PdfDevice(options, savePath);
// Merge all SVG documents into PDF
renderer.Render(device, document1, document2);
// Render HTML to PDF and adjust to the widest page with C#
// Learn more: https://docs.aspose.com/html/net/rendering-options/
// Prepare HTML code
string code = @"<style>
div { page-break-after: always; }
</style>
<div style='border: 1px solid red; width: 300px'>First Page</div>
<div style='border: 1px solid red; width: 500px'>Second Page</div>";
// Initialize an HTML document from the HTML code
using HTMLDocument document = new HTMLDocument(code, ".");
// Create the instance of Rendering Options and set a custom page-size
PdfRenderingOptions options = new PdfRenderingOptions();
options.PageSetup.AnyPage = new Page(new Size(400, 200));
// Enable auto-adjusting for the page size
options.PageSetup.AdjustToWidestPage = true;
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "output-widest-page-size.pdf");
// Create the PdfDevice and specify options and output file
using PdfDevice device = new PdfDevice(options, savePath);
// Render HTML to PDF
document.RenderTo(device);
// Render HTML to DOCX using C#
// Learn more: https://docs.aspose.com/html/net/rendering-device/
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "document.docx");
// Load a document from 'https://docs.aspose.com/html/files/document.html' web page
using HTMLDocument document = new HTMLDocument("https://docs.aspose.com/html/files/document.html");
// Create an instance of the DocRenderingOptions class
DocRenderingOptions docOptions = new DocRenderingOptions();
// Create the DocDevice object and specify the output file to render
using DocDevice device = new DocDevice(docOptions, savePath);
// Render HTML to DOCX
document.RenderTo(device);
// Render HTML to JPG using C#
// Learn more: https://docs.aspose.com/html/net/rendering-device/
// Prepare path to a source HTML file
string documentPath = Path.Combine(DataDir, "spring.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "spring-output.jpg");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Create an instance of the ImageRenderingOptions class
ImageRenderingOptions imageOptions = new ImageRenderingOptions(ImageFormat.Jpeg);
// Create the Image Device and specify the output file to render
using ImageDevice device = new ImageDevice(imageOptions, savePath);
// Render HTML to JPG
document.RenderTo(device);
// Render HTML to PDF using C#
// Learn more: https://docs.aspose.com/html/net/rendering-device/
// Prepare HTML code
string code = @"<span>Hello, World!!</span>";
// Prepare a path to save a converted file
string savePath = Path.Combine(OutputDir, "document.pdf");
// Initialize an HTML document from the HTML code
using HTMLDocument document = new HTMLDocument(code, ".");
// Create a PDF Device and specify the output file to render
using PdfDevice device = new PdfDevice(savePath);
// Render HTML to PDF
document.RenderTo(device);
// Render HTML to PDF in C# with custom page size
// Learn more: https://docs.aspose.com/html/net/rendering-options/
string code = @"<span>Hello, World!!</span>";
// Initialize an HTML document from HTML code
using HTMLDocument document = new HTMLDocument(code, ".");
// Create an instance of PdfRenderingOptions and set a custom page size
PdfRenderingOptions options = new PdfRenderingOptions()
{
PageSetup =
{
AnyPage = new Page(new Size(Length.FromInches(4),Length.FromInches(2)))
}
};
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "file-with-custom-page-size.pdf");
// Create an instance of the PdfDevice and specify the options and output file to render
using PdfDevice device = new PdfDevice(options, savePath);
// Render HTML to PDF
document.RenderTo(device);
// Render HTML to PDF in C# with custom resolution
// Learn more: https://docs.aspose.com/html/net/rendering-options/
// Prepare HTML code and save it to a file
string code = @"
<style>
p
{
background: #a7d3fe;
}
@media(min-resolution: 300dpi)
{
p
{
/* high resolution screen color */
background: orange
}
}
</style>
<p>Hello, World!!</p>";
File.WriteAllText("document.html", code);
// Prepare a path to save output file
string savePath1 = Path.Combine(OutputDir, "output_resolution_50.pdf");
string savePath2 = Path.Combine(OutputDir, "output_resolution_300.pdf");
// Create an instance of HTML document
using (HTMLDocument document = new HTMLDocument("document.html"))
{
// Create options for low-resolution screens
PdfRenderingOptions options1 = new PdfRenderingOptions()
{
HorizontalResolution = 50,
VerticalResolution = 50
};
// Create an instance of PDF device
using PdfDevice device1 = new PdfDevice(options1, savePath1);
// Render HTML to PDF
document.RenderTo(device1);
// Create options for high-resolution screens
PdfRenderingOptions options2 = new PdfRenderingOptions()
{
HorizontalResolution = 300,
VerticalResolution = 300
};
// Create an instance of PDF device
using PdfDevice device2 = new PdfDevice(options2, savePath2);
// Render HTML to PDF
document.RenderTo(device2);
}
// Render HTML to PDF with timeout settings using C#
// Learn more: https://docs.aspose.com/html/net/renderers/
// Prepare HTML code
string code = @"
<script>
var count = 0;
setInterval(function()
{
var element = document.createElement('div');
var message = (++count) + '. ' + 'Hello, World!! I know how to use Renderers!';
var text = document.createTextNode(message);
element.appendChild(text);
document.body.appendChild(element);
}, 1000);
</script>";
// Initialize an HTML document based on prepared HTML code
using HTMLDocument document = new HTMLDocument(code, ".");
// Create an instance of HTML Renderer
using HtmlRenderer renderer = new HtmlRenderer();
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "output-timeout.pdf");
// Create an instance of the PdfDevice class
using PdfDevice device = new PdfDevice(savePath);
// Render HTML to PDF
renderer.Render(device, TimeSpan.FromSeconds(5), document);
// Render HTML to PNG with custom resolution using C#
// Learn more: https://docs.aspose.com/html/net/rendering-options/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "spring.html");
// Create an instance of HTML document
using HTMLDocument document = new HTMLDocument(documentPath);
// Prepare path to save output file
string savePath1 = Path.Combine(OutputDir, "output_resolution_50.png");
string savePath2 = Path.Combine(OutputDir, "output_resolution_300.png");
// Create options for low-resolution screens
ImageRenderingOptions options1 = new ImageRenderingOptions()
{
HorizontalResolution = 50,
VerticalResolution = 50
};
// Create an instance of Image device
using ImageDevice device1 = new ImageDevice(options1, savePath1);
// Render HTML to PNG
document.RenderTo(device1);
// Create options for high-resolution screens
ImageRenderingOptions options2 = new ImageRenderingOptions()
{
HorizontalResolution = 300,
VerticalResolution = 300
};
// Create an instance of Image device
using ImageDevice device2 = new ImageDevice(options2, savePath2);
// Render HTML to PNG
document.RenderTo(device2);
// Render HTML to XPS using C#
// Learn more: https://docs.aspose.com/html/net/rendering-device/
// Prepare path to a source HTML file
string documentPath = Path.Combine(DataDir, "spring.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "spring.xps");
// Initialize an HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Create an instance of the XpsDevice and specify the output file to render
using XpsDevice device = new XpsDevice(savePath);
// Render HTML to XPS
document.RenderTo(device);
// Render SVG to PDF using C#
// Learn more: https://docs.aspose.com/html/net/renderers/
// Initialize an SVG document from the file
using SVGDocument document = new SVGDocument(Path.Combine(DataDir, "shapes.svg"));
// Create an instance of SVG Renderer
using SvgRenderer renderer = new SvgRenderer();
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "rendering-svg.pdf");
// Create the instance of Rendering Options and set a custom page-size
PdfRenderingOptions options = new PdfRenderingOptions();
options.PageSetup.AnyPage = new Page(new Size(600, 500));
// Create an instance of the Pdfdevice class
using PdfDevice device = new PdfDevice(options, savePath);
// Render SVG to PDF
renderer.Render(device, document);
// Convert HTML to DOCX in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-html-to-docx/
string documentPath = Path.Combine(OutputDir, "save-options.html");
string savePath = Path.Combine(OutputDir, "save-options-output.docx");
// Prepare HTML code and save it to a file
string code = "<h1>DocSaveOptions Class</h1>\r\n" +
"<p>Using DocSaveOptions Class, you can programmatically apply a wide range of conversion parameters.</p>\r\n";
File.WriteAllText(documentPath, code);
// Initialize an HTML Document from the html file
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize DocSaveOptions. Set A5 as a page-size
DocSaveOptions options = new DocSaveOptions();
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(Length.FromInches(8.3f), Length.FromInches(5.8f)));
// Convert HTML to DOCX
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to JPG in C# with custom settings
// Learn more: https://docs.aspose.com/html/net/convert-html-to-jpg/
string documentPath = Path.Combine(OutputDir, "save-options.html");
string savePath = Path.Combine(OutputDir, "save-options-output.jpg");
// Prepare HTML code and save it to a file
string code = "<h1> Image SaveOptions </h1>\r\n" +
"<p>Using ImageSaveOptions Class, you can programmatically apply a wide range of conversion parameters such as BackgroundColor, Format, Compression, PageSetup, etc.</p>\r\n";
File.WriteAllText(documentPath, code);
// Initialize an HTML Document from the html file
using HTMLDocument document = new HTMLDocument(documentPath);
// Set up the page-size 500x250 pixels, margins and change the background color to AntiqueWhite
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
{
BackgroundColor = System.Drawing.Color.AntiqueWhite
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 250), new Margin(40, 40, 20, 20));
// Convert HTML to JPG
Converter.ConvertHTML(document, options, savePath);
// Convert selective HTML tags to Markdown using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-markdown/
// Prepare a path for converted file saving
string savePath = Path.Combine(OutputDir, "options-output.md");
// 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>";
File.WriteAllText(Path.Combine(OutputDir, "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.Features = MarkdownFeatures.Link | MarkdownFeatures.AutomaticParagraph;
// Call the ConvertHTML() method to convert the HTML to Markdown
Converter.ConvertHTML(Path.Combine(OutputDir, "options.html"), options, savePath);
// Convert HTML to PDF with custom settings using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-pdf/
string documentPath = Path.Combine(OutputDir, "save-options.html");
string savePath = Path.Combine(OutputDir, "save-options-output.pdf");
// Prepare HTML code and save it to a file
string code = "<h1> PdfSaveOptions Class</h1>\r\n" +
"<p>Using PdfSaveOptions Class, you can programmatically apply a wide range of conversion parameters such as BackgroundColor, HorizontalResolution, VerticalResolution, PageSetup, etc.</p>\r\n";
File.WriteAllText(documentPath, code);
// Initialize an HTML Document from the html file
using HTMLDocument document = new HTMLDocument(documentPath);
// Set up the page-size, margins and change the background color to AntiqueWhite
PdfSaveOptions options = new PdfSaveOptions()
{
BackgroundColor = System.Drawing.Color.AntiqueWhite
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(Length.FromInches(4.9f), Length.FromInches(3.5f)), new Margin(30, 20, 10, 10));
// Convert HTML to PDF
Converter.ConvertHTML(document, options, savePath);
// Convert HTML to XPS with custom settings using C#
// Learn more: https://docs.aspose.com/html/net/convert-html-to-xps/
string documentPath = Path.Combine(OutputDir, "save-options.html");
string savePath = Path.Combine(OutputDir, "save-options-output.xps");
// 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>\r\n";
File.WriteAllText(documentPath, code);
// Initialize an HTML Document from the html file
using HTMLDocument document = new HTMLDocument(documentPath);
// Set up the page-size, margins and change the background color to AntiqueWhite
XpsSaveOptions options = new XpsSaveOptions()
{
BackgroundColor = System.Drawing.Color.AntiqueWhite
};
options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(Length.FromInches(4.9f), Length.FromInches(3.5f)), new Margin(30, 20, 10, 10));
// Convert HTML to XPS
Converter.ConvertHTML(document, options, savePath);
// Render HTML to PDF with custom background color using C#
// Learn more: https://docs.aspose.com/html/net/rendering-options/
// Prepare path to a source HTML file
string documentPath = Path.Combine(DataDir, "spring.html");
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "spring.pdf");
// Create an instance of HTML document
using HTMLDocument document = new HTMLDocument(documentPath);
// Initialize options with 'Azure' as a background-color
PdfRenderingOptions options = new PdfRenderingOptions()
{
BackgroundColor = System.Drawing.Color.Azure
};
// Create an instance of PDF device
using PdfDevice device = new PdfDevice(options, savePath);
// Render HTML to PDF
document.RenderTo(device);
// Render HTML to DOCX in C# with custom page settings
// Learn more: https://docs.aspose.com/html/net/rendering-options/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "nature.html");
// Initialize the HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Create an instance of Rendering Options and set a custom page size
DocRenderingOptions options = new DocRenderingOptions();
options.PageSetup.AnyPage = new Page(new Size(Length.FromInches(8), Length.FromInches(10)));
options.FontEmbeddingRule = (FontEmbeddingRule.Full);
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "nature-options.docx");
// Create an instance of the DocDevice and specify options and output file
using DocDevice device = new DocDevice(options, savePath);
// Render HTML to DOCX
document.RenderTo(device);
// Render HTML to JPG with custom resolution and antialiasing settings with C#
// Learn more: https://docs.aspose.com/html/net/rendering-options/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "color.html");
// Initialize the HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Create an instance of Rendering Options
ImageRenderingOptions options = new ImageRenderingOptions(ImageFormat.Jpeg)
{
// Disable smoothing mode
UseAntialiasing = false,
// Set the image resolution as 75 dpi
VerticalResolution = Resolution.FromDotsPerInch(75),
HorizontalResolution = Resolution.FromDotsPerInch(75),
};
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "color-options.jpg");
// Create an instance of the ImageDevice and specify options and output file
using ImageDevice device = new ImageDevice(options, savePath);
// Render HTML to JPG
document.RenderTo(device);
// Render HTML to PDF with custom page size using C#
// Learn more: https://docs.aspose.com/html/net/rendering-options/
// Prepare HTML code
string code = @"<style>div { page-break-after: always; }</style>
<div>First Page</div>
<div>Second Page</div>
<div>Third Page</div>
<div>Fourth Page</div>";
// Initialize an HTML document from the HTML code
using HTMLDocument document = new HTMLDocument(code, ".");
// Create the instance of Rendering Options and set a custom page size
PdfRenderingOptions options = new PdfRenderingOptions();
options.PageSetup.SetLeftRightPage(
new Page(new Size(400, 150)),
new Page(new Size(400, 50))
);
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "output-custom-page-size.pdf");
// Create the PDF Device and specify options and output file
using PdfDevice device = new PdfDevice(options, savePath);
// Render HTML to PDF
document.RenderTo(device);
// Render HTML to PDF with password protection using C#
// Learn more: https://docs.aspose.com/html/net/rendering-options/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "document.html");
// Initialize the HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Create the instance of Rendering Options
PdfRenderingOptions options = new PdfRenderingOptions();
// Set the permissions to the file
options.Encryption = new PdfEncryptionInfo(
"user_pwd",
"owner_pwd",
PdfPermissions.PrintDocument,
PdfEncryptionAlgorithm.RC4_128);
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "output-options.pdf");
// Create an instance of the PdfDevice and specify options and output file
using PdfDevice device = new PdfDevice(options, savePath);
// Render HTML to PDF
document.RenderTo(device);
// Render HTML to XPS with custom page size using C#
// Learn more: https://docs.aspose.com/html/net/rendering-options/
// Prepare a path to a source HTML file
string documentPath = Path.Combine(DataDir, "document.html");
// Initialize the HTML document from the file
using HTMLDocument document = new HTMLDocument(documentPath);
// Create an instance of Rendering Options
XpsRenderingOptions options = new XpsRenderingOptions();
options.PageSetup.AnyPage = new Page(new Size(Length.FromInches(5), Length.FromInches(2)));
// Prepare a path to save the converted file
string savePath = Path.Combine(OutputDir, "document-options.xps");
// Create an instance of the XpsDevice and specify options and output file
using XpsDevice device = new XpsDevice(options, savePath);
// Render HTML to XPS
document.RenderTo(device);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment