Creating PDF Files in ASP.NET using iTextSharp
iText is a library that allows you to create and
manipulate PDF documents. It enables developers looking to enhance web and
other applications with dynamic PDF document generation and/or manipulation.
Developers can use iText to:
(i)
Serve PDF to a browser
(ii)
Generate dynamic documents from XML files
or databases
(iii)
Use PDF's many interactive features
(iv)
Add bookmarks, page numbers, watermarks,
etc
(v)
Split, concatenate, and manipulate PDF
pages
(vi)
Automate filling out of PDF forms
(vii)
Add digital signatures to a PDF file
iText is available in Java as well as in C#.
Find
some links below that will help you to explore this:
Official Website: http://itextpdf.com/itext.php
Video Tutorials: http://itextpdf.com/summit.php
Assemblies Download: http://sourceforge.net/projects/itextsharp/files/latest/download
So, now if you want to use this in ASP.NET application,
I have created sample solution for you, just follow the steps to understand it.
Step
1
First of all you need to download the components (dll)
from here.
Step
2
Now, open the Visual Studio and create and simple
website and add a ‘Bin’ folder in the project.
Step
3
If you done with above, right click on the ‘Bin’ folder
and select ‘Add Reference’ and in the appeared dialog box click on Browse button
and locate the dll we have saved in step 1.
Step
4
Now in the code-behind add two namespaces using
iTextSharp.text; and using iTextSharp.text.pdf; and add a folder named ‘PDF_Files’
on the root. Remember, if you are using hosting then you need to allow Read and
Write permission on this folder.
Step
5
Okay, use the following code-behind codes:
var doc1 = new iTextSharp.text.Document();
string path = Server.MapPath("PDF_Files");
PdfWriter.GetInstance(doc1, new FileStream(path + "/Doc1.pdf", FileMode.Create));
doc1.Open();
doc1.Add(new Paragraph("My sample
text goes here."));
doc1.Close();
Step
6
If you done with above, run the project and test it, find
the complete code altogether.
Code-Behind
using System;
using
System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
using iTextSharp.text;
using
iTextSharp.text.pdf;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var doc1 = new iTextSharp.text.Document();
string path = Server.MapPath("PDF_Files");
PdfWriter.GetInstance(doc1, new FileStream(path + "/Doc1.pdf", FileMode.Create));
doc1.Open();
doc1.Add(new Paragraph("My sample
text goes here."));
doc1.Close();
}
}
If you want to explore it more, please visit here http://itextpdf.com/summit.php.
I hope you like it. Thanks.
No comments:
Post a Comment