Object Reference¶
Developer Notice: The Iron PDF Object Reference Has Moved.¶
IronPdf.HtmlToPdf¶
HtmlToPdf is your go-to class for creating PDFs in .Net
It can work from HTMl string or fetch URL (with all of their supporting assets).
A custom System.Net.WebCient can optionally be assign to manage requests to render webpages with special security provisions.
Examples¶
This should help you get started. IntelliSense should take it from there!
using IronPdf;
//..
HtmlToPdf HtmlToPdf = new IronPdf.HtmlToPdf();
HtmlToPdf.PrintOptions.EnableJavaScript = true;
// Many more PrintOptions available - check IntelliSense
HtmlToPdf.RenderHtmlAsPdf("<p>html</p>").SaveAs("Path/FileName.Pdf");
//or
HtmlToPdf.RenderUrlAsPdf("http://ironpdf.com").SaveAs("FileName.Pdf");
//or
HtmlToPdf.RenderUrlAsPdf(new Uri("http://ironpdf.com")).SaveAs("FileName.Pdf");
You can also save the result as a Stream or Byte array for advanced usage. See IronPdf.PdfResource.
IronPdf.HtmlToPdf Reference¶
- class
IronPdf.HtmlToPdf allows you to create PDF files from any web page or HTML Snippet
Example:
HtmlToPdf myHtmlToPdf = new IronPdf.HtmlToPdf();
myHtmlToPdf.PrintOptions.Title = "A Great PDF Title";
myHtmlToPdf.PrintOptions.EnableJavaScript = true;
myHtmlToPdf.RenderHtmlAsPdf("<p>html</p>").SaveAs("Path\File.Pdf");
Public Functions
-
IronPdf.HtmlToPdf.HtmlToPdf()
IronPdf.HtmlToPdf allows you to create PDF files from any web page or HTML Snippet
-
IronPdf.HtmlToPdf.HtmlToPdf(PdfPrintOptions PrintOptions)
IronPdf.HtmlToPdf allows you to create PDF files from any web page or HTML Snippet
- Parameters
PrintOptions
-Sets PDF output options such as Paper-Size, Dpi, Headers and Footers using an instance of the IronPDF.PDFPrintOptions Class.
-
PdfResource IronPdf.HtmlToPdf.RenderUrlAsPdf(Uri Url, WebClient CustomWebClient)
Renders the URL as PDF binary data.
Note: Custom WebClient and PDFPrinting options can be set optionally. Example:
HtmlToPdf myHtmlToPdf = new IronPdf.HtmlToPdf();
Uri myUri = new Uri("http://www.example.com");
myHtmlToPdf.RenderUrlAsPdf(myUri).SaveAs("Path\File.Pdf");
- Return
- PdfResource
- Parameters
Url
-An absolute Uri.
CustomWebClient
-A custom System.Net.WebClient for fetching the url. This allows prgramatic control of cookies, credentials and proxy settings.
- Exceptions
-
PdfResource IronPdf.HtmlToPdf.RenderUrlAsPdf(string Url, WebClient CustomWebClient)
Renders the URL as PDF binary data.
Note: Custom WebClient and PDFPrinting options can be set optionally. Example:
HtmlToPdf myHtmlToPdf = new IronPdf.HtmlToPdf();
myHtmlToPdf.RenderUrlAsPdf("http://www.example.com").SaveAs("Path\File.Pdf");
- Return
- PdfResource
- Parameters
Url
-An absolute Uri as a string.
CustomWebClient
-A custom System.Net.WebClient for fetching the url. This allows prgramatic control of cookies, credentials and proxy settings.
- Exceptions
-
PdfResource IronPdf.HtmlToPdf.RenderHtmlAsPdf(string Html, Uri BaseUrl)
Creates a PDF file from an HTML string, and returns it as an IronPdf.PdfResource
Note: Custom WebClient and PDFPrinting options can be set optionally. Example:
HtmlToPdf myHtmlToPdf = new IronPdf.HtmlToPdf();
myHtmlToPdf.RenderHtmlAsPdf("<p>html</p>").SaveAs("Path\File.Pdf");
- Return
- A PdfResource
- Parameters
Html
-The HTML to be turned into a PDF as a string
BaseUrl
-Setting the BaseURL property gives the HTML a relative content link, links, css etc…
Public Members
-
PdfPrintOptions IronPdf.HtmlToPdf.PrintOptions
Sets PDF output options such as Paper-Size, Dpi, Headers and Footers
-
IronPdf.AspxToPdf¶
AspxToPdf is a static class used to turn any ASPX web form into a PDF. The look and feel remain the same - but the output becomes a PDF.
This is simply achieved by adding AspxToPdf in your ASPX page code-behind. Normally this is a file named like Default.aspx.cs.
Just call IronPdf.AspxToPdf.RenderThisPageAsPdf() in your of your ASPX Form onLoad event.
Examples¶
Basic Example
private void Form1_Load(object sender, EventArgs e)
{
//..
IronPdf.AspxToPdf.RenderThisPageAsPDF();
}
Advanced Example
using IronPdf;
private void Form1_Load(object sender, EventArgs e)
{
//..
var PrintOptions = new PdfPrintOptions(){ Dpi=100 };
PrintOptions.SetHeaderText("{page} of {total-pages}”);
AspxToPdf.RenderThisPageAsPDF(AspxToPdf.FileBehaviour.Attachment, "FileName.pdf", PrintOptions);
}
IronPdf.AspxToPdf Reference¶
- class
Renders any .Net Web Page (ASPX) into a PDF Document. Simply add it to the Page_Load event.
Example:
protected void Page_Load(object sender, EventArgs e){
IronPdf.AspxToPdf.RenderThisPageAsPDF();
}
Public Types
Public Static Functions
-
static void IronPdf.AspxToPdf.RenderThisPageAsPDF(FileBehaviour PDFBehaviour, String PdfFileName, PdfPrintOptions PrintOptions)
Automagically renders any ASPX page into PDF instead of HTML. Use it in the Page_Load Event.
Example:
protected void Page_Load(object sender, EventArgs e){
IronPdf.AspxToPdf.RenderThisPageAsPDF(FileBehaviour.Attachment, "MyPdf.pdf", new PdfPrintOptions(){ Dpi = 300 });
}
- Parameters
PDFBehaviour
-Specifies if the PDF file should be downloaded as an attachment, or displayed directly in the browser (if an pdf browser plug-in is present).
PdfFileName
-The file-name of the PDF. If no name is set - a suitable name will be automatically assigned chosen based on the HTML title, PrintOptions or name of the ASPX page.
PrintOptions
-Sets PDF output options such as Pdf Title, Paper-Size, Dpi, Headers and Footers
-
IronPdf.PdfPrintOptions¶
PdfPrintOptions is a class used to fine-tune the behavior of Pdf rendering by any the following methods:
- IronPdf.AspxToPdf.RenderThisPageAsPDF
- IronPdf.HtmlToPdf.RenderUrlAsPdf
- IronPdf.HtmlToPdf.RenderHtmlAsPdf
PdfPrintOptions covers almost every pdf setting we can imagine, including editable PDF forms, javascript, custom headers and footers with {mail-merge} fields, paper sizes including custom sizes, margins …the whole kitchen sink.
Examples with HtmlToPdf¶
To make coding easier - there are 2 ways you can do set the PdfPrintOptions:
- Construct HtmlToPdf with a PdfPrintOptions parameter. E.g.
HtmlToPdf myPdfMaker = new HtmlToPdf(PdfPrintOptions);
- HtmlToPdf has a property called PrintOptions which is an instance of PdfPrintOptions. This allows for on the fly settings changes. E.g.
HtmlToPdf myPdfMaker = new IronPdf.HtmlToPdf();
myPdfMaker.PrintOptions.GrayScale = true;
myPdfMaker.RenderHtmlAsPdf("<p>hello world</p>").SaveAs("test.pdf");
myPdfMaker.PrintOptions.Zoom = 200;
myPdfMaker.RenderHtmlAsPdf(@“<p>hello world</p>“).SaveAs(“test2.pdf”);
Example with AspxToPdf¶
For AspxToPdf your PdfPrintOptions are added as a final additional argument in the RenderThisPageAsPDF function call.
IronPdf.PdfPrintOptions PrintOptions = new PrintOptions(){ Dpi = 300 };
IronPdf.AspxToPdf.RenderThisPageAsPDF(AspxToPdf.FileBehaviour.Attachment, “FileName.pdf”, PrintOptions);
IronPdf.AspxToPdf Reference¶
- class
PDF output options for IronPdf. Specifies options such as Paper-Size, Dpi, Headers and Footers.
Example:
PdfPrintOptions myOptions = new PdfPrintOptions() ){ GrayScale = true , Dpi=150, MarginTop = 15 , PaperKind = System.Drawing.Printing.PaperKind.Letter };
myOptions.SetFooter( "Page {page} of {total-pages}");
Public Types
- enum type
IronPdf::PdfPrintOptions
::
PaperOrientation
¶Portrait or Landscape
Values:
Public Functions
void IronPdf.PdfPrintOptions.SetCustomPaperSize(int width, int height)
Set an output paper size for PDF pages. Dimensions are in millimeters.
void IronPdf.PdfPrintOptions.SetHeaderAsHtml(string Html)
Sets the header for every PDF page as HTML. This HTML is treated as a sperate HTML document, with its own javascript context and stylesheet.
Using an HTML header replaces any text headers. Note: Ensure that MarginTop is set to a high enough value to render your HTML. Merge meta-data data into your html using any of these placeholder strings: {page} {total-pages} {url} {date} {time} {html-title} {pdf-title}
- Parameters
Html
-The HTML for the header.
void IronPdf.PdfPrintOptions.SetHeader(string LeftText, string CenterText, string RightText, Font HeaderFont, bool UseLineSeperator)
Sets the header text for the PDF document.
Merge meta-data data into your header using any of these placeholder strings: {page} {total-pages} {url} {date} {time} {html-title} {pdf-title}
- Parameters
LeftText
-The left text.
CenterText
-The center text.
RightText
-The right text.
HeaderFont
-The header font.
UseLineSeperator
-if set to
true
adds a header divider line to the page .
void IronPdf.PdfPrintOptions.SetFooter(string LeftText, string CenterText, string RightText, Font FooterFont, bool UseLineSeperator)
Sets the footer text for the PDF document.
Merge meta-data data into your footer using any of these placeholder strings: {page} {total-pages} {url} {date} {time} {html-title} {pdf-title}
- Parameters
LeftText
-The left text.
CenterText
-The center text.
RightText
-The right text.
FooterFont
-System.Drawing.Font The footer typeface.
UseLineSeperator
-if set to
true
adds a footer divider line to the page .
void IronPdf.PdfPrintOptions.SetFooterAsHtml(string Html)
Sets the footer for every PDF page as HTML. This HTML is treated as a sperate HTML document, with its own javascript context and stylesheet.
Using an HTML footer replaces any existing text footer. Note: Ensure that MarginBottom is set to a high enough value to render your HTML. Merge meta-data data into your html using any of these placeholder strings: {page} {total-pages} {url} {date} {time} {html-title} {pdf-title}
- Parameters
Html
-The HTML for the header.
Public Members
string IronPdf.PdfPrintOptions.LicenseKey
Removes watermarks. Get Licensed at http://ironpdf.com/license
System.Text.Encoding IronPdf.PdfPrintOptions.InputEncoding
Standard Paper Sizes
The input character encoding as a string;
int IronPdf.PdfPrintOptions.Zoom
The zoom level in %
int IronPdf.PdfPrintOptions.Dpi
Printing output Dpi. 300 is standard for most print jobs. Higher resolutions produce clearer images an text, but also larger PDF files.
bool IronPdf.PdfPrintOptions.GrayScale
Outputs a black-and-white PDF
int IronPdf.PdfPrintOptions.JpegQuality
Quality of any image that must be re-sampled. 0-100
int IronPdf.PdfPrintOptions.MarginLeft
Paper margin in millimeters. Set to zero for commercial printing applications.
int IronPdf.PdfPrintOptions.MarginBottom
Paper margin in millimeters. Set to zero for commercial printing applications.
int IronPdf.PdfPrintOptions.MarginTop
Paper margin in millimeters. Set to zero for commercial printing applications.
int IronPdf.PdfPrintOptions.MarginRight
Paper margin in millimeters. Set to zero for commercial printing applications.
System.Drawing.Printing.PaperKind IronPdf.PdfPrintOptions.PaperKind
Set an output paper size for PDF pages. System.Drawing.Printing.PaperKind.
Use SetCustomPaperSize(int width, int height) for custom sizes.
PaperOrientation IronPdf.PdfPrintOptions.PDFPaperOrientation
The PDF paper orientation.
bool IronPdf.PdfPrintOptions.PrintHtmlBackgrounds
Prints background-colors and images from HTML
bool IronPdf.PdfPrintOptions.UseScreenCSSMediaType
Enables Media=”screen” Css Styles and StyleSheets
Note: By setting AllowScreenCss=false, IronPDF prints using css for media=”print” only.
bool IronPdf.PdfPrintOptions.EnableJavaScript
Enables JavaScript and Json to be executed for 100ms before the page is rendered. Ideal for printing from Ajax / Angular Applications.
Note: By default - IronPDF disables all JavaScript.
bool IronPdf.PdfPrintOptions.OutputForms
Turns all HTML forms elements into editable PDF forms.
string IronPdf.PdfPrintOptions.Title
PDF Document Name and Title meta-data. Not required.
bool IronPdf.PdfPrintOptions.FitToPaperWidth
Where possible, fits the PDF content to 1 page width.
int IronPdf.PdfPrintOptions.RenderDelay
Milliseconds to wait after HTML is rendered before printing. This can use useful when considering the rendering of javascript, ajax or animations.
int IronPdf.PdfPrintOptions.FirstPageNumber
First page number to be used in PDF headers and footers.
Font IronPdf.PdfPrintOptions.HeaderFont
System.Drawing.Font used to render the PDF header.
Font IronPdf.PdfPrintOptions.FooterFont
System.Drawing.Font used to render the PDF header.
IronPdf.PdfResource¶
All of IronPdf.HtmlToPdf’s rendering methods output an instance of a IronPDF.PdfResource.
We decided to work this way to make you job as a coder easier.
PdfResource can automatically save a file for you, but it can also return an ByteArray (byte[]) as raw binary data or a System.IO.MemoryStream.
This is useful if you want to post-process you pdf, save to a database, or do something innovative we haven’t even thought of yet!
Examples¶
using IronPdf;
HtmlToPdf myPdfMaker = new IronPdf.HtmlToPdf();
PdfResource myPdf = myPdfMaker.RenderHtmlAsPdf("<p>hello world</p>");
//Now you have 3 ways to use the PDF data stored in myPdf:
PdfResource.SaveAs(@"C:\path\mypdf.pdf");
// or
System.IO.MemoryStream stream = PdfResource.Stream;
//or
byte[] data = BinaryData;
IronPdf.PdfResource Reference¶
- class
A PDF File generated by IronPDF. It can be saved to a file, or accessed programmatically as a Stream or ByteArray.
Example:
HtmlToPdf HtmlToPdf = new IronPdf.HtmlToPdf();
PdfResource res = HtmlToPdf.RenderHtmlAsPdf("<p>html</p>");
res.SaveAs("Path\File.Pdf");
Public Functions
-
bool IronPdf.PdfResource.SaveAs(string FileName)
Saves the PDF to the local file-system
- Return
- Bool - True if success, False if failure (check path is valid)
- Parameters
FileName
-Full local file path to save the PDF document.
Property
-
property
IronPdf::PdfResource::BinaryData
Gets the PDF as a ByteArray ( byte[] )
The PDF as a ByteArray
-
property
IronPdf::PdfResource::Stream
Gets the PDF as a System.IO.MemoryStream
The PDF as a System.IO.MemoryStream
-