In this tutorial we will learn how to create a pdf document in C# with IronPdf. We will cover the following topics:
How to create a PDF file using C#
- Download C# Create PDF Library and Open Visual Studio
- Install the Nuget Package from the Menu Bar
- Design the Windows Form and Write Code for Creating Pdf
- Add the Back-End Code for the "Clear and Close" Button
- Run the Project to See your PDF Document
IronPdf:
IronPdf is a .Net library that helps us create and read PDF documents, as well as giving us the tools to manage and adapt PDF documents for our own purposes. The following tutorial will show you how to create PDF files with IronPdf. This tutorial assumes that you already know the basics of C# and Visual Studio, and that you possess a working knowledge of HTML. We need Visual Studio for writing, compiling and running our application, C# for writing logic and code, and HTML for formatting the pdf file, including making titles, headings, adding images, paragraphs, etc. This Library fully supports .Net 5, .Net core, standard and Framework.
We can create a pdf file in C# with just a few lines of code. This is a very easy task given basic knowledge of C# and HTML. You can explore more about Ironpdf with this URL.
Creating a C# Project
I am using Visual Studio 2019, but you can use any version. Visual Studio 2019 is recommended as it provides some wonderful features.
Open Visual Studio 2019.
Click on "Create New Project".
Select "Windows Form App" from the template and then click 'Next'. The following window will pop up. Name the project. I have named it "Create Pdf using IronPdf".
Configure project:
After that, click 'Next' to bring up the next window. From the drop-down menu choose .Net Core 3.1.
Click on the 'Create' button. The project will be created as shown below:
Project:
Installing IronPdf:
To develop a solution we need to install the Nuget Package. Select "Project" from the Menu Bar, and a drop-down list will appear. Select Manage NuGet Packages from the drop-down menu and click it. The window below will display:
Select the "Browse" tab, and the following window will appear:
In the search box, type 'IronPdf' and press "Enter". The following window will appear:
Browse Nuget Package Manager:
Select and click on IronPdf. The following window will appear:
Nuget Package:
Press on the 'Install' button, and wait for the installation to finish. The following window will appear after successful installation:
Once you have pressed the 'Ok' button, you're ready to go.
The following Readme.txt file will open.
Readme File:
I recommend that you click on all of the links and learn more about this Library.
Designing the Windows Form
The project has been created and the Nuget package has now been installed. The next step is to create a Window form that allows the user to enter text. The content can be saved as a pdf document.
Open Form1 Design
On the left side of the window, click on the 'Toolbar'.
ToolBar:
We need a label to name our Application. A label can be found by searching for it on the toolbar and then dragging and dropping it onto the Form Design.
Give the label a name. I have named mine as "C# Create Pdf using IronPdf".
Drag and drop one label named "Write Text Here (Use HTML Tags for Formatting)", one Rich text Box, and three buttons (one for saving the text to a pdf file, the other for clearing the text, and the third for closing the window).
Form:
Writing Code for Creating Pdf Documents
Double-click on the "Save" Button; the following code will appear:
private void Save_Click(object sender, EventArgs e) {}
Add the namespace Ironpdf at the top of the .cs file.
using IronPdf;
The actual work starts from this point. We need a file path to save our newly created pdf file. For that I am using SaveFileDialog which will ask the user to select a file path and file name.
Add the following code inside the Save_Click Function.
private void Save_Click(object sender, EventArgs e)
{
// Code for Select the folder to save the file.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = @"D:\";
saveFileDialog1.Title = "Save Pdf File";
saveFileDialog1.DefaultExt = "pdf";
saveFileDialog1.Filter = "Pdf files (*.pdf)|*.pdf|All files (*.*)|*.*";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string filename = saveFileDialog1.FileName;
// actual code that will create Pdf files
var HtmlLine = new HtmlToPdf();
HtmlLine.RenderHtmlAsPdf(PdfText.Text).SaveAs(filename);
// MessageBox to display that file save
MessageBox.Show("File Saved Successfully!");
}
}
SaveFileDialog will open a file dialog to select the folder and file name where you want to create a pdf file.
I have set Initial Directory to D drive, but you can set it to any.
I have set Default Extension to pdf files as we are only dealing with pdf files here.
Inside the "if" condition, I have put the actual code that will create the pdf file.
Now we can see that we have managed to generate a pdf file with only two lines of code.
PdfText is the name of a Rich Text box which contains the text that will be written in a pdf file.
The filename is the file path and name which the user has selected via SaveFileDialog.
Add the Back-End Code for the "Clear and Close" Button
Double-click on the "clear" button; the following code will appear:
private void Clear_Click(object sender, EventArgs e) {}
Add the following code inside the Clear_Click function to clear the text fields.
private void Clear_Click(object sender, EventArgs e) {
PdfText.Text = "";
}
Now, double-click the "Close" button; the following code will appear:
private void Close_Click(object sender, EventArgs e) {}
Add the following code inside the Close_Click Function:
private void Close_Click(object sender, EventArgs e) {
this.Dispose();
}
Run the Project
Press Ctrl + F5 to run the Project; the following window will appear:
Write your text inside the text box.
I have written the following text:
This is Sample Pdf File
This is the demo for C# Create Pdf using Iron Pdf
Iron Pdf is a library which provides building functions for creating, reading
and manipulating pdf files with just few lines of code.
Next, click on the 'Save' button to save the file; the following window will appear:File Explorer:
Select 'Folder' and write 'File' name. I have selected D drive and set the file name as “C# Create Pdf”. Press the 'Save' button; the following message box will appear after the successful creation of a Pdf file:Message Box:
Now, let’s open the pdf file and take a look.
A file is created as shown below:Output file
Summary
In this tutorial we can see that it is very easy to read a pdf file using C#. We can easily convert our html text to a pdf document, our web link to a pdf document, as well as manage the data easily across various applications. For Full tutorial, Please visit this link
Iron Software also provides other extremely useful libraries to help us create, read, and manipulate Excel files, generate QR and bar codes, extract text from images using OCR, and much more. Our current special offer gives you the chance to purchase all five libraries for the price of two. For more details, please refer to the following link.
This brings the guide to a close. I hope you found it easy to follow and comprehend. If you have any questions, please post them in the comments section.
You can download the Project from this link
Top comments (2)
Hi, I suggest trying ZetPDF for this!zetpdf.com/
If you're looking for a reliable solution to generate PDF files in C#, you should check out ZetPDF. It’s a great SDK for .NET and allows you to create PDFs from scratch.