How to use Cookies with IronPDF

by Chaknith Bin

Cookies, in the context of web technology, are small pieces of data that websites store on a user's computer or device. They serve various purposes, from session management, where they help keep users logged in, to tracking and analytics, collecting data on user behavior for website improvement. However, the use of cookies has sparked discussions on privacy, leading to regulations like the GDPR and CCPA, and modern web browsers offer users control over cookie management to address these concerns.

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Apply Cookies Example

Before using the method to apply cookies, set the RequestContext property to RequestContexts.Global. Then, create the ChromeHttpLoginCredentials class and pass it to the ApplyCookies method. The renderer is now ready to be used for rendering HTML content to PDF with cookies.

:path=/static-assets/pdf/content-code-examples/how-to/cookies-apply-cookies.cs
using IronPdf;

// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global;

ChromeHttpLoginCredentials credentials = new ChromeHttpLoginCredentials() {
    NetworkUsername = "testUser",
    NetworkPassword = "testPassword"
};

string uri = "http://localhost:51169/Invoice";

// Apply cookies
renderer.ApplyCookies(uri, credentials);
Imports IronPdf

' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()

renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global

Dim credentials As New ChromeHttpLoginCredentials() With {
	.NetworkUsername = "testUser",
	.NetworkPassword = "testPassword"
}

Dim uri As String = "http://localhost:51169/Invoice"

' Apply cookies
renderer.ApplyCookies(uri, credentials)
VB   C#

RequestContexts Enum: This enum defines browser request contexts used to establish relationships between individual renders. It's essential for managing cookies and user preferences.

  • Isolated: Creates a new request context that is isolated from previous or future renders. Recommended to ensure that the current render is not affected by previous renders.
  • Global: Uses the global request context, which is shared between all renders. Useful in some cases for persisting certain browser states between renders.
  • Auto: Defaults to IronPdf.Rendering.RequestContexts.Isolated but switches to IronPdf.Rendering.RequestContexts.Global if the user has ever invoked IronPdf.ChromePdfRenderer.ApplyCookies(System.String, IronPdf.ChromeHttpLoginCredentials).

Apply Custom Cookies Example

Using custom cookies in a request requires setting the CustomCookies property. This property accepts a dictionary of key-value pairs, both as strings.

:path=/static-assets/pdf/content-code-examples/how-to/cookies-apply-custom-cookies.cs
using IronPdf;
using System;
using System.Collections.Generic;

// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

Dictionary<string, string> customCookies = new Dictionary<string, string>();

// Apply custom cookies
renderer.RenderingOptions.CustomCookies = customCookies;

var uri = new Uri("https://localhost:44362/invoice");
PdfDocument pdf = renderer.RenderUrlAsPdf(uri);
Imports IronPdf
Imports System
Imports System.Collections.Generic

' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()

Private customCookies As New Dictionary(Of String, String)()

' Apply custom cookies
renderer.RenderingOptions.CustomCookies = customCookies

Dim uri As New Uri("https://localhost:44362/invoice")
Dim pdf As PdfDocument = renderer.RenderUrlAsPdf(uri)
VB   C#
Chaknith related to Apply Custom Cookies Example

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.