IronPDF Library

IronPDF allows developers to create, edit and extract PDF content within .NET Framework Projects.

Introduction
 
IronPDF allows developers to create, edit and extract PDF content within .NET Core and Framework Projects. Using IronPDF library we can easily design and generate the PDF document.
 
First, we need to install IronPDF package for our application. Please go to package manager console and run the below command.
  1. Install-Package IronPdf -Version 2019.7.0  
Create PDF File using HTML fragment
 
In your Visual Studio create one simple class.
  1. using IronPdf;  
Code Snippet
  1. public static void HtmlToPdfExample()  
  2.        {  
  3.   
  4.            // Render any HTML fragment or document to HTML  
  5.            var Renderer = new HtmlToPdf();  
  6.            var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");  
  7.            var OutputPath = "HtmlToPDF.pdf";  
  8.            PDF.SaveAs(OutputPath);  
  9.   
  10.            // This neat trick opens our PDF file so we can see the result  
  11.            System.Diagnostics.Process.Start(OutputPath);  
  12.        }  
Output will be like this -- please check the below snapshot.
 
 
 
Create PDF using HTML File
 
This library helps us convert HTML files to PDF files. While converting it will consider CSS effects also. Please use the below code for generating PDF using an HTML file.
 
Code Snippet
  1. public static void HtmlFileToPdfExample() {  
  2.  // Create a PDF from an existing HTML    
  3.  var Renderer = new HtmlToPdf();  
  4.   
  5.  Renderer.PrintOptions.MarginTop = 50; //millimeters    
  6.  Renderer.PrintOptions.MarginBottom = 50;  
  7.  Renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print;  
  8.   
  9.  Renderer.PrintOptions.Header = new SimpleHeaderFooter() {  
  10.   CenterText = "{pdf-title}",  
  11.    DrawDividerLine = true,  
  12.    FontSize = 16  
  13.  };  
  14.   
  15.  Renderer.PrintOptions.Footer = new SimpleHeaderFooter() {  
  16.   LeftText = "{date} {time}",  
  17.    RightText = "Page {page} of {total-pages}",  
  18.    DrawDividerLine = true,  
  19.    FontSize = 14  
  20.  };  
  21.   
  22.  string adress = @ "C:\Users\mmadansb\Downloads\templated-hielo\index.html";  
  23.   
  24.  var PDF = Renderer.RenderHTMLFileAsPdf(adress);  
  25.  var OutputPath = "Invoice.pdf";  
  26.  PDF.SaveAs(OutputPath);  
  27.   
  28.  // This neat trick opens our PDF file so we can see the result    
  29.  System.Diagnostics.Process.Start(OutputPath);  
  30. } 
Create PDF using URL
 
By rendering URL we can also create a PDF document. Please use the below code for creating a PDF from URL.
 
Code Snippet
  1. public static void UrlToPdfExample()  
  2.        {  
  3.   
  4.            // Create a PDF from a web page  
  5.            var Renderer = new HtmlToPdf();  
  6.            var PDF = Renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Portable_Document_Format");  
  7.            PDF.SaveAs("wikipedia.pdf");  
  8.   
  9.            // This neat trick opens our PDF file so we can see the result  
  10.            System.Diagnostics.Process.Start("wikipedia.pdf");  
  11.   
  12.        }  
Summary
 
In this blog, we discussed PDF conversion using IronPDF Library. We can generate PDF in three ways those are,
  1. Create PDF File using HTML fragment
  2. Create PDF using HTML File
  3. Create PDF using URL
I hope it's helpful. Eat-> Code->Sleep->Repeat.