Introduction
PDF documents are an essential part of many applications and websites. They are widely used for documentation, reports, invoices, etc. This article will show you how to create PDF documents using iTextSharp in a Blazor Server application.
iTextSharp is a .NET library that allows developers to create, manipulate, and extract data from PDF documents. It provides a simple API for creating PDF documents with advanced features like bookmarks, tables, and images.
What is iTextSharp?
iTextSharp is a free and open-source library for creating and manipulating PDF documents in .NET applications. It is a port of the iText library, a popular Java-based PDF library designed to provide similar functionality to .NET developers. iTextSharp allows you to create, read, and edit PDF documents and add text, images, and other elements to the document. It also supports advanced features such as encryption, digital signatures, and form filling. iTextSharp is widely used in .NET applications for generating PDF documents, such as reports, invoices, and other documents. It is a popular tool for developers because of its ease of use, flexibility, and ability to generate high-quality PDF documents with advanced features.
Prerequisites
To follow this article, you should have the following,
- A basic understanding of C# and . NET.
- Visual Studio or Visual Studio Code.
- .NET Core 6.0 or higher installed.
Step 1. Install iTextSharp
The first step is to install the iTextSharp library in your Blazor Server application. You can do this by using the NuGet Package Manager. Open Visual Studio and navigate to your project. Right-click on the project and select "Manage NuGet Packages". In the search box, type "iTextSharp" and select the iTextSharp package from the list. Click on "Install" to install the package.
Step 2. Create a PDF Document
Now, let's create a simple PDF document using iTextSharp. Add a new class file to your project and name it "PdfGenerator.cs". This class will define a method to create a PDF document.
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace BlazorApp1.Helpers
{
public class PdfGenerator
{
public static void GeneratePdf(string fileName, string title, string body)
{
//Create a new document
Document document = new Document();
//Create a PDF writer
PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));
//Open the document
document.Open();
//Add a title
Font titleFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 18);
Paragraph titleParagraph = new Paragraph(title, titleFont);
titleParagraph.Alignment = Element.ALIGN_CENTER;
document.Add(titleParagraph);
//Add some text
Font bodyFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
Paragraph bodyParagraph = new Paragraph(body, bodyFont);
bodyParagraph.Alignment = Element.ALIGN_JUSTIFIED;
document.Add(bodyParagraph);
//Close the document
document.Close();
}
}
}
In this method, we create a new document and PdfWriter objects. We then open the document, add a title and some text, and close the document.
Step 3. Call the PDF Generator
Now that we have created a method to generate PDF documents, we can call this method from our Blazor Server application. In this example, we will add a button to our Blazor Server application to generate a PDF document when clicked.
Open your Blazor Server application and add a new Razor component. Name it "PdfGenerator.razor". We will add a button to generate a PDF document in this component.
@page "/pdfgenerator"
@using BlazorApp1.Helpers
<h1>PDF Generator</h1>
<p>Click the button below to generate a PDF document.</p>
<button class="btn btn-primary" @onclick="GeneratePdf">Generate PDF</button>
@code {
private void GeneratePdf()
{
PdfGenerator.GeneratePdf("example.pdf", "Example PDF Document", "This is an example PDF document generated using iTextSharp.");
}
}
In this component, we add a button with an on-click event that calls the GeneratePdf method in the PdfGenerator class.
Step 4: Add a Download Link
Now that we have generated a PDF document, we need to add a download link to our Blazor Server application. In this example, we will add a link to the generated PDF document in the PdfGenerator component.
@page "/pdfgenerator"
@using BlazorApp1.Helpers
<h1>PDF Generator</h1>
<p>Click the button below to generate a PDF document.</p>
<button class="btn btn-primary" @onclick="GeneratePdf">Generate PDF</button>
@if (!string.IsNullOrEmpty(pdfPath))
{
<p>Click the link below to download the PDF document.</p>
<a href="@pdfPath" download>Download PDF</a>
}
@code {
private string pdfPath;
private void GeneratePdf()
{
string fileName = "example.pdf";
string title = "Example PDF Document";
string body = "This is an example PDF document generated using iTextSharp.";
PdfGenerator.GeneratePdf(fileName, title, body);
pdfPath = Path.Combine(Directory.GetCurrentDirectory(), fileName);
}
}
In this updated version of the PdfGenerator component, we add a variable called "pdfPath" that will hold the Path to the generated PDF document. We also add a conditional statement that displays a download link when the PDF document is generated.
In the GeneratePdf method, we set the value of the "pdfPath" variable to the Path of the generated PDF document. We use the Path. Combine the method to combine the current directory and the file name.
Output
Conclusion
TITextSharp is a powerful library that provides many advanced features for creating and manipulating PDF documents, and it is a great tool to have in your development toolbox. His article shows how to create a PDF document using iTextSharp in a Blazor Server application. We have created a simple PDF document with a title and some text, and we have added a button that generates the PDF document and a download link that allows the user to download the PDF document.