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.

Here's an example of applying cookies using IronPDF:

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

// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer()
{
    // Set the rendering context to Global, sharing context across multiple renderers
    RenderingOptions = { RequestContext = IronPdf.Rendering.RequestContexts.Global }
};

// Define network credentials to access the resource at the specified URI
var credentials = new ChromeHttpLoginCredentials
{
    NetworkUsername = "testUser",
    NetworkPassword = "testPassword"
};

// Define the URI of the resource to render
string uri = "http://localhost:51169/Invoice";

// Apply network credentials/cookies to the renderer for accessing the resource
renderer.ApplyHttpLoginCredentials(uri, credentials);
Imports IronPdf



' Instantiate ChromePdfRenderer

Private renderer As New ChromePdfRenderer() With {

	.RenderingOptions = { RequestContext = IronPdf.Rendering.RequestContexts.Global }

}



' Define network credentials to access the resource at the specified URI

Private credentials = New ChromeHttpLoginCredentials With {

	.NetworkUsername = "testUser",

	.NetworkPassword = "testPassword"

}



' Define the URI of the resource to render

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



' Apply network credentials/cookies to the renderer for accessing the resource

renderer.ApplyHttpLoginCredentials(uri, credentials)
$vbLabelText   $csharpLabel

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.

Here's an example of applying custom cookies using IronPDF:

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

// Instantiate ChromePdfRenderer, which is used for rendering PDFs from URLs, HTML, etc.
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a dictionary to hold custom cookies that might be necessary for accessing certain pages
Dictionary<string, string> customCookies = new Dictionary<string, string>
{
    // Add custom cookies if necessary using the following format:
    // {"CookieName", "CookieValue"}
};

// Apply the custom cookies to the rendering options of the renderer
renderer.RenderingOptions.CustomCookies = customCookies;

// Define the URI of the webpage you want to render as a PDF
// Make sure the URL is correct and accessible
Uri uri = new Uri("https://localhost:44362/invoice");

// Use the renderer to render the URL as a PDF document
// The RenderUrlAsPdf method will try to access the given URL, apply the renderer's options, including custom cookies, and create a PdfDocument
PdfDocument pdf = renderer.RenderUrlAsPdf(uri);

// The variable 'pdf' now contains the rendered PDF document
// You can save it or do further processing as needed, for example:
// pdf.SaveAs("invoice.pdf");
' Import necessary namespaces

Imports IronPdf

Imports System

Imports System.Collections.Generic



' Instantiate ChromePdfRenderer, which is used for rendering PDFs from URLs, HTML, etc.

Private renderer As New ChromePdfRenderer()



' Create a dictionary to hold custom cookies that might be necessary for accessing certain pages

Private customCookies As New Dictionary(Of String, String)



' Apply the custom cookies to the rendering options of the renderer

renderer.RenderingOptions.CustomCookies = customCookies



' Define the URI of the webpage you want to render as a PDF

' Make sure the URL is correct and accessible

Dim uri As New Uri("https://localhost:44362/invoice")



' Use the renderer to render the URL as a PDF document

' The RenderUrlAsPdf method will try to access the given URL, apply the renderer's options, including custom cookies, and create a PdfDocument

Dim pdf As PdfDocument = renderer.RenderUrlAsPdf(uri)



' The variable 'pdf' now contains the rendered PDF document

' You can save it or do further processing as needed, for example:

' pdf.SaveAs("invoice.pdf");
$vbLabelText   $csharpLabel

Frequently Asked Questions

What are cookies in the context of web technology?

Cookies are small pieces of data stored on a user's computer or device by websites. They are used for session management, tracking, and analytics, among other purposes.

How can cookies be used in PDF rendering?

In IronPDF, cookies can be applied during PDF rendering by using the RequestContext property and the ApplyCookies method to incorporate session or custom cookies into the rendering process.

What is the RequestContext property?

The RequestContext property in IronPDF defines the browser request context, determining how cookies and other browser states are shared or isolated between renders.

What does the ApplyCookies method do?

The ApplyCookies method in IronPDF applies cookies to the rendering process, allowing HTML content to be rendered with specific session or custom cookies.

How can custom cookies be applied?

Custom cookies can be applied in IronPDF by setting the CustomCookies property, which accepts a dictionary of key-value pairs representing cookie names and values.

What is the purpose of the ChromeHttpLoginCredentials class?

The ChromeHttpLoginCredentials class in IronPDF is used to pass login credentials when applying cookies, enabling authentication for sessions during the rendering process.

What are the different RequestContexts?

IronPDF provides three RequestContexts: Isolated, Global, and Auto. Isolated creates a new request context for each render, Global shares contexts across renders, and Auto switches based on prior cookie application.

Why is cookie management important in web applications?

Cookie management is crucial for maintaining user sessions, personalizing user experiences, and ensuring compliance with privacy regulations like GDPR and CCPA.

Chaknith related to Apply Custom Cookies Example
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.