Top
ArticleCity.comArticle Categories C# Generate PDF (Code Example)

C# Generate PDF (Code Example)

Photo from Unsplash

Originally posted on: https://ironpdf.com/docs/questions/generate-pdf-in-csharp/

We are always looking for a way to generate PDF documents and work with them in C# projects more quickly, accurately, and efficiently.

Having easy-to-use functions from a library allows us to focus more on the work, and less on the time-heavy details of trying to generate PDF files, whether in .NET Core or .NET Framework.

Here are a few examples of how to generate PDF C# project tasks that you can use in your work, including generate PDF from HTML String, generate PDF from ASPX Input, and more.

 


 

Step 1

1. Install the Free C# Library

To make the most out of this tutorial, install the IronPDF C# HTML to PDF library. You can use it free for development in your project and implement the steps below yourself. With it, you can create PDF files using C# and will not need other Nuget packages. It can even convert HTML to PDF with image, form, data, pages, CSS, text, header, footer, images supported in a single solution.

Access the software by direct file download or through the latest NuGet Package install to load into Visual Studio and your project.

 PM > Install-Package IronPdf

 


 

How to Tutorial

2. Work with .NET Framework & .NET Core

The IronPDF library simplifies PDF file tasks into easy-to-use and understandable .NET methods. This enhances developer productivity and speeds up development in .NET Core or .NET. IronPDF is compatible with any .NET Framework project from Version 4 upward, and .NET Core from version 2 upwards. Use it when you need to create PDF file content using C# .NET

 


 

3. Follow Easy Naming Conventions to Create a PDF

What makes the IronPDF ‘PDF’ library so special when generating PDF Documents in .NET?

First of all: speed. Not only does IronPDF create a PDF document for .NET quicker, but it also increases developer productivity because of the easy naming convention employed for methods in the IronPDF .NET PDF library. Using Html to PDF technology, you can create PDF documents using C# predictably and easily inside web applications and all other .NET project types.

Secondly, ease of use. As mentioned in the previous point, the IronPDF .NET PDF library is very easy to work with. It is easy to identify method names and understand their purpose. .NET developers will not struggle to get generate PDF in .NET with this PDF document library in either VB.NET or using C#.

 


 

4. Generate PDF from HTML String

How easy is it to generate a PDF with IronPDF and .NET?

Creating a PDF is pretty easy. Here is a very quick example of how to generate a PDF from an HTML input string.

  1. /**
  2. PDF from HTML String
  3. anchor-generate-pdf-from-html-string
  4. **/
  5. private void HTMLString()
  6. {
  7. // Render any HTML fragment or document to HTML
  8. var Renderer = new IronPdf.ChromePdfRenderer();
  9. var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPDF</h1>");
  10. var OutputPath = "ChromePdfRenderer.pdf";
  11. PDF.SaveAs(OutputPath);
  12. }

 

 

The above C# code uses 4 lines of code to create PDF file documents from an HTML string.

Note the aptly named methods:

  • ChromePdfRenderer
  • RenderHtmlAsPdf
  • SaveAs

Any newbie and expert alike will be able to identify, access, and use them in their VB.NET or C# code to generate PDFs.

 


 

5. Generate PDF from ASPX

Another example of ease of use:

The following code makes use of IronPDF to generate a PDF directly from an ASPX file without using iTextSharp for PdfSharp.

  1. /**
  2. PDF from ASPX
  3. anchor-generate-pdf-from-aspx
  4. **/
  5. protected void Page_Load(object sender, EventArgs e)
  6. {
  7. IronPdf.AspxToPdf.RenderThisPageAsPdf();
  8. }

 

 

 


 

6. JavaScript Support

Features such as Responsiveness as well as the support of JavaScript make IronPDF extremely handy and highly sought-after. With a PDF file format library that is able to comprehend both CSS and JavaScript, you won’t need any other .NET PDF package, only IronPDF.

 


 

7. Generate PDF with Custom Viewport

Here are a few examples of Responsiveness and utilizing JavaScript inside a converted PDF document.

The following code allows you to programmatically set the size of your response Viewport when generating your PDF document.

How to: Create PDF file using “Html to PDF” from an HTML String. You can convert HTML into an HTML file using C#. You can then save it to disk or server as web content using the IronPDF API using C#.

  1. /**
  2. Set Viewport
  3. anchor-generate-pdf-with-custom-viewport
  4. **/
  5. using IronPdf;
  6. IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
  7. //Choose screen or print CSS media
  8. Renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Screen;
  9. //Set the width of the responsive virtual browser window in pixels
  10. Renderer.ViewPortWidth = 1280;
  11. // Render an HTML document or snippet as a string
  12. Renderer.RenderHTMLFileAsPdf("Assets/Responsive.html");

 

 

IronPDF supports JavaScript quite nicely via the Chromium rendering engine. One caveat though, you may have to add a delay to a page render to allow JavaScript time to execute whilst generating PDFs using C#. This is done by using the following few lines of code for the settings:

  1. Renderer.RenderingOptions.EnableJavaScript = true;
  2. Renderer.RenderingOptions.RenderDelay = 500; //milliseconds
No Comments

Sorry, the comment form is closed at this time.