Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert HTML to PDF in C#, use
Converter.ConvertHTML() with an HTML source,
PdfSaveOptions, and an output path or stream provider. Aspose.HTML for .NET can convert HTML from a file, string, stream, or URL, and PdfSaveOptions lets you control page setup, margins, background color, JPEG quality, document information, and encryption.
This guide shows the core HTML to PDF workflows for C# applications: a one-line conversion, a file-based conversion, custom PDF save options, and output stream handling with ICreateStreamProvider. Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.
For simple input, the static
Converter class provides a short path from HTML content to a PDF file. The example below passes HTML code, a base URI, default PdfSaveOptions, and the output file path to ConvertHTML().
1// Convert HTML to PDF using C#
2
3// Invoke the ConvertHTML() method to convert HTML to PDF
4Converter.ConvertHTML(@"<h1>Convert HTML to PDF!</h1>", ".", new PdfSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.pdf"));Use this pattern when your HTML is already available as a string and default PDF settings are enough. For file input, custom options, or stream output, use the workflows below.
A typical file-based conversion uses an
HTMLDocument instance, a PdfSaveOptions object, and Converter.ConvertHTML(). You can load HTML from a local file, HTML code, stream, or URL. For more loading patterns, see
Creating an HTML Document.
The workflow is:
HTMLDocument constructor.PdfSaveOptions instance.Converter.ConvertHTML(document, options, savePath) to write the PDF file.The next example uses the sample file
spring.html. The following C# code converts spring.html to spring-output.pdf.
1// Convert HTML to PDF in C#
2
3// Prepare a path to a source HTML file
4string documentPath = Path.Combine(DataDir, "spring.html");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "spring-output.pdf");
8
9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Initialize PdfSaveOptions
13PdfSaveOptions options = new PdfSaveOptions();
14
15// Convert HTML to PDF
16Converter.ConvertHTML(document, options, savePath);After conversion, the PDF output preserves the layout and visual content of the source HTML:

PdfSaveOptions, and output path to Converter.ConvertHTML().HTMLDocument, create PdfSaveOptions, and call Converter.ConvertHTML(document, options, savePath).HTMLDocument from the URL, then convert the loaded document with PDF save options.PdfSaveOptions.PageSetup; see
Resize Document During Conversion.ICreateStreamProvider and pass it to the conversion workflow.Use
PdfSaveOptions when the output PDF needs custom rendering settings. The options object is passed to ConvertHTML() and controls how the HTML document is rendered to PDF.
| Option | Use it to control |
|---|---|
JpegQuality | JPEG compression quality for images in the PDF output. The default value is 95. |
Css | CSS processing behavior through CssOptions. |
DocumentInfo | Metadata for the generated PDF document. |
BackgroundColor | The color used to fill the page background. The default value is transparent. |
PageSetup | Page size, margins, and page layout settings. |
HorizontalResolution and VerticalResolution | Output resolution values used by the rendering process. The default value is 300 dpi for each property. |
Encryption | PDF encryption settings when the output must be protected. |
The following example creates a PDF with custom page size, margins, background color, image quality, and resolution settings.
1// Convert HTML to PDF in C# with custom page settings
2
3// Prepare a path to a source HTML file
4string documentPath = Path.Combine(DataDir, "drawing.html");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "drawing-options.pdf");
8
9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Initialize PdfSaveOptions. Set up the page-size 600x300 pixels, margins, resolutions and change the background color to AliceBlue
13PdfSaveOptions options = new PdfSaveOptions()
14{
15 HorizontalResolution = 200,
16 VerticalResolution = 200,
17 BackgroundColor = System.Drawing.Color.AliceBlue,
18 JpegQuality = 100
19};
20options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(600, 300), new Margin(20, 10, 10, 10));
21
22// Convert HTML to PDF
23Converter.ConvertHTML(document, options, savePath);In this example:
JpegQuality controls JPEG compression quality for images.BackgroundColor changes the PDF page background.HorizontalResolution and VerticalResolution set rendering resolution values.PageSetup.AnyPage defines the page size and margins used for the output pages.Use ICreateStreamProvider when the conversion output should be written through streams instead of only to a file path. This is useful for memory-based workflows, remote storage, databases, or services that need to decide where the rendered output is stored.
The MemoryStreamProvider class below implements ICreateStreamProvider and keeps the generated streams in memory.
1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
2
3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
4{
5 // List of MemoryStream objects created during the document rendering
6 public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
7
8 public Stream GetStream(string name, string extension)
9 {
10 // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11 MemoryStream result = new MemoryStream();
12 Streams.Add(result);
13 return result;
14 }
15
16 public Stream GetStream(string name, string extension, int page)
17 {
18 // 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.)
19 MemoryStream result = new MemoryStream();
20 Streams.Add(result);
21 return result;
22 }
23
24 public void ReleaseStream(Stream stream)
25 {
26 // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27 }
28
29 public void Dispose()
30 {
31 // Releasing resources
32 foreach (MemoryStream stream in Streams)
33 stream.Dispose();
34 }
35}Some output formats produce one file, such as PDF and XPS. Image formats such as JPG or PNG can produce multiple output streams when the rendered document has multiple pages.
The following example converts HTML to PDF with MemoryStreamProvider, reads the generated memory stream, and saves it to a PDF file.
1// Convert HTML to PDF in C# using memory stream
2
3// Create an instance of MemoryStreamProvider
4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
5
6// Initialize an HTML document
7using HTMLDocument document = new HTMLDocument(@"<h1>Convert HTML to PDF File Format!</h1>", ".");
8
9// Convert HTML to PDF using the MemoryStreamProvider
10Converter.ConvertHTML(document, new PdfSaveOptions(), streamProvider);
11
12// Get access to the memory stream that contains the result data
13MemoryStream memory = streamProvider.Streams.First();
14memory.Seek(0, SeekOrigin.Begin);
15
16// Flush the result data to the output file
17using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.pdf")))
18{
19 memory.CopyTo(fs);
20}DocSaveOptions.Use the online HTML to PDF Converter for quick manual conversion. Use Aspose.HTML for .NET when HTML to PDF conversion must run inside a C# application, service, or document pipeline.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.