DEV Community

Parvesh Kumar
Parvesh Kumar

Posted on

IronPdf in .Net

**Setting up IronPDF in .NET: **IronPDF is a powerful and easy-to-use .NET library for generating, editing, and transforming PDFs from HTML and images. Here’s a step-by-step guide—which includes tips and best practices on configuration, image use, performance, and combining PDFs.

  1. Installation First, install IronPDF via NuGet in your .NET project: dotnet add package IronPdf Or via Visual Studio’s NuGet package manager. Tip: Ensure your project targets a supported .NET version (e.g., .NET 6/7/8).

You need to have 4 packages if you are adding package > 2025.10


  1. Setup Options in Program.cs (or Startup.cs) Configure IronPDF settings at application startup. For best performance in server scenarios, preconfigure the IronPdf options in your Program.cs:

`var ironpdfLicenceKey = builder.Configuration.GetSection("IronPdf");
// Optional: Set temp folder path BEFORE initializing
Installation.TempFolderPath = ironpdfLicenceKey.GetSection("TempFolderPath").Value;

License.LicenseKey = ironpdfLicenceKey.GetSection("LicenseKey").Value;

// Initialize IronPdf AFTER setting temp folder path
Installation.Initialize();

// Optional: Set global temp file directory for faster processing
IronPdf.Installation.TempFolderPath = "D:\IronPdfTemp";

builder.Services.AddSingleton(_ =>
{
var renderingOptions = new ChromePdfRenderOptions
{
FitToPaperMode = IronPdf.Engines.Chrome.FitToPaperModes.AutomaticFit,
MarginTop = 5,
MarginLeft = 5,
MarginRight = 5,
MarginBottom = 10,
PaperSize = IronPdf.Rendering.PdfPaperSize.Letter,
HtmlFooter = new HtmlHeaderFooter
{
HtmlFragment = "Page {page} of {total-pages}"
},
EnableJavaScript = true,
};
renderingOptions.SetCustomPaperSizeInInches(8.5, 11);
return new ChromePdfRenderer
{
RenderingOptions = renderingOptions
};
});`

Why Temp Path?

IronPDF processes intermediate files in the background. Customizing to a fast, dedicated temp folder (e.g., an SSD directory) can improve performance and prevent permission issues.


  1. How to Transform HTML to PDF

IronPDF shines at converting HTML (with CSS and even JavaScript) to PDF:

using (PdfDocument pdf = _renderer.RenderRazorViewToPdf(_viewRenderService,
template,
new RazorViewTemplateWrapper<TModel>()
{
Model = queryResult.Value,
ViewData = new Dictionary<string, object>
{
["Paper"] = "letter"
},
HomePath = url
}))
{
Response.Headers["Content-Disposition"] = "inline";
return new FileStreamResult(pdf.Stream, "application/pdf");
} using (PdfDocument pdf = _renderer.RenderRazorViewToPdf(_viewRenderService,
template,
new RazorViewTemplateWrapper<TModel>()
{
Model = queryResult.Value,
ViewData = new Dictionary<string, object>
{
["Paper"] = "letter"
},
HomePath = url
}))
{
Response.Headers["Content-Disposition"] = "inline";
return new FileStreamResult(pdf.Stream, "application/pdf");
}

You can also load HTML from files, URLs, or streams.


  1. How to convert Images to pdf?** All about image handling?**

You can use image to Pdf option
var pdf = IronPdf.ImageToPdfConverter.ImageToPdf(resizedImage, IronPdf.Imaging.ImageBehavior.FitToPageAndMaintainAspectRatio, _renderer.RenderingOptions);

What if you are getting different size of the pages in Images?
Remember to set FitToPageAndMaintainAspectRatio

How to compress the images to reduce the size of the pdf?
pdf. CompressImages(!string.IsNullOrEmpty(config.Value.CompressImageQuality) ? Convert.ToInt32(config.Value.CompressImageQuality) : 100);


  1. Combining Different PDFs—How & Cautions You can append or merge multiple PDFs with IronPDF using Merge option foreach (var pdfBytes in pdfs) { allPdf.Add(new IronPdf.PdfDocument(pdfBytes)); } var mergedPdf = IronPdf.PdfDocument.Merge(allPdf); How to keep same size for Pdf after merge? Resize PDF?

`double a4Width = 8.5; // in mm
double a4Height = 11; // in mm

for (int i = 0; i < mergedPdf.PageCount; i++)
{
var page = mergedPdf.Pages[i];

 // Scale content and set new page size
 page.Resize(a4Width, a4Height, MeasurementUnit.Inch);
Enter fullscreen mode Exit fullscreen mode

}`


Final Tips

  1. Pdf generation is memory intensive. Always deploy Pdf generation as separate service.
  2. Always review IronPDF’s official documentation for deeper configuration options.
  3. If deploying in a server/cloud scenario, preconfigure the temp and license settings at startup.
  4. For any issues with rendering, ensure all resource URLs (CSS, images, fonts) are reachable and permissions are correctly set.

Top comments (0)