How To Create PDF File In C# Using IronPdf
Photo from Unsplash
Originally Posted On: https://www.c-sharpcorner.com/article/how-to-create-pdf-file-in-c-sharp-using-ironpdf/
Software developers must know how to create a pdf file dynamically through C# code, as this is considered a need in almost all projects in today’s world. You may come across various problems where you need to create a Pdf file such as generating a report from user input, saving user text as pdf, extracting HTML content and converting it into pdf, etc.
You might be thinking that it would be a complex task. But thanks to IronPdf, It’s as easy as calculating 2+3.
You can create a pdf file from C# with just 2 lines of code.
Explore more about Ironpdf from this URL.
I have prepared a step-by-step demo for you, to show how we can create pdf document using C#.
Prerequisite Knowledge
You must have the following basic knowledge to understand it easily,
- Basic Knowledge of C# Programming
- Basic Knowledge of C# GUI Controls
- Basic Knowledge of HTML for formatting the pdf document.
If you don’t have this knowledge, don’t worry, you will still be able to understand it easily.
I have used the window Form App for the demonstration. However, you can use Asp.Net, console app, WPF App, or any other of your choice.
This Library is supported by both C# and VB.Net
Step 1 – Create Visual Studio Project
I am using Visual Studio 2019, You can use any. Visual Studio 2019 is recommended as it provides 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 popup. Name the project. I have named it “Create Pdf using IronPdf”.
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,
Step 2 – Install Nuget Package of IronPdf
To develop a solution, we need to install Nuget Package. Select “Project” from the Menu Bar, a Drop Down List appears. Select Manage NuGet Packages from the drop-down menu and click it. The window below will display.
Select the Browse tab, the following window will appear.
In the Search Box, type ‘IronPdf’ and press Enter. The following window will appear.
Select and Click on IronPdf. The following Window will appear.
Press on the ‘Install’ button, and wait for the installation to finish. The following window will appear after successful Installation.
Once you’ve pressed the ‘Ok’ button, you’re ready to go.
Following Readme.txt file will open.
I recommend that you click on all of the links and learn more about this Library.
Step 3 – Design Window Form
The project has been created, and the Nuget package has 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’.
We need a label to name our Application. A label can be found by searching for it from the toolbar and then dragging and dropping it onto the Form Design.
Give the label a name. I have named it 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).
Step 4 – Add the Back End Code for Save Button
Double Click on Save Button, the following code will appear.
Add the namespace of Ironpdf on the top of the .cs file.
Now, the actual work will start from here. We need a File path where we have to save our newly created pdf file. For that I am using SaveFile Dialog, that will ask user to select file path and file name.
Add Following Code inside the Save_Click Function.
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.
You can see that, With only two lines of code, we have managed to generate pdf file.
PdfText is the name of a Rich text box in which we have our text to be written in a pdf file.
The filename is the file path and name which the user has selected via save file dialogue.
Step 5 – Add the Back End Code for Clear and Close Button
Double Click on the clear button, the following code will appear.
Add the following code inside the Clear_Click function to clear the text feilds.
Now, Double click on Close Button, the following code will appear.
Add the following code inside the Close_Click Function.
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.
<h1> This is Sample Pdf File </h1>
<p> This is the demo for C# Create Pdf using Iron Pdf </p>
<p> Iron Pdf is a library which provides building functions for creating, reading <br>
and manipulating pdf files with just few lines of code. </p>
Now Click on the ‘Save’ button to save the file, the following window will appear.
Select ‘Folder’ and write ‘File’ name. I have select D drive and set the file name as “C# Create Pdf”. Press the ‘Save’ button, the following message box will appear after successful creation of Pdf file.
Now, Let’s open the pdf file and see.
A file is created as shown below,
This brings the guide to a close. I hope you found it simple to follow and comprehend. If you have any questions, please post them in the comments section.