Creating PDF files with C# and .NET
Sep16Written by:
2009/09/16 10:40 AM
PDF or Portable Document Format, seems to be the document format of choice on the internet. Invented by Adobe Systems and perfected over 15 years. Individuals, businesses, and government agencies around the world trust and rely on PDF to communicate their ideas and vision. Traditionally you needed Adobe Acrobat to create PDF documents. But, PDF is now a formal open standard known as ISO 32000. Maintained by the International Organization for Standardization.
PDF files are viewable and printable on virtually any platform — Mac OS, Microsoft® Windows®, UNIX®, and many mobile platforms. More than 1,800 vendors worldwide offer PDF-based solutions including creation, plug-in, consulting, training, and support tools. You can create and convert files to PDF online. There are even free PDF creators and converters that you can download. One that comes to mind is PDF995
But these tools don’t help you if you want to create PDF files in your application, and do so at runtime. There are various third party reports that have the ability to export your report to PDF. But what if you do not want to create a report?
SDK and .Net Library
Well, you can download and use the Adobe SDK. But that's a 50mb download. Not bad for our international readers, but a big chunk for South African bandwidth.
What you need is a .NET library that will do the job easily and efficiently. Enter iTextSharp. iText# (iTextSharp) is a port of the iText open source java library written entirely in C# for the .NET platform. iText# is a library that allows you to generate PDF files on the fly. It is implemented as an assembly.
iTextSharp is an open source project and can be found at sourceforge.net in the iTextSharp project.
Create PDF Document
To create a PDF document via your C# code, simply include the iTextSharp DLL as a reference, and use the methods provided.
Document document = new Document(iTextSharp.text.PageSize.A4,10,10,30,30);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath("/Test11.pdf"), FileMode.Create));
document.Open();
Paragraph p = new Paragraph("Wedsure Wedding Insurance Quote:", new Font(Font.TIMES_ROMAN, 18, Font.BOLD));
document.Add(p);
document.Close()
There you have it. A very simple PDF created. You can see more examples in the tutorial. You can add all sorts of things to your PDF document. Things like images, tables, headers and footers. As well as organise your PDF documents into sections and chapters.
Merge PDF Documents
There is also a nice neat little trick I recently learnt about. I had a project that needed to create a new PDF document. Part of the document was new information, but part of it needed to have information already contained in another PDF document. What I needed was to be able to merge the second PDF document with my newly created document. With iTextSharp this is a relatively easy task.
Document document = new Document(iTextSharp.text.PageSize.A4,10,10,30,30);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath("/Test11.pdf"), FileMode.Create));
document.Open();
Paragraph p = new Paragraph("Wedsure Wedding Insurance Quote:", new Font(Font.TIMES_ROMAN, 18, Font.BOLD));
document.Add(p)
PdfReader reader = new PdfReader(Server.MapPath(“SomePDF.pdf”));
int n = reader.NumberOfPages;
PdfImportedPage page;
PdfContentByte cb = writer.DirectContent;
for (int i = 1; i <= n; i++)
{
document.NewPage();
page = writer.GetImportedPage(reader, i);
cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
}
document.Close()
Now you have a new PDF Document merged from two, one you created and another pre-existing document.
Check out this neat little MergeEx class
New here, or perhaps you've been here a few times? Like this post? Why not subscribe to this blog and get the most up to date posts as soon as they are published.
blog comments powered by
1 comment(s) so far...
Creating PDF files with C# and .NET
PDF or Portable Document Format, seems to be the document format of choice on the internet. Invented by Adobe Systems and perfected over 15 years. Individuals, businesses, and government agencies around the world trust and rely on PDF to communicate their ideas and vision. Traditionally you needed Adobe Acrobat to create PDF documents. But, PDF is now a formal open standard known as ISO 32000. Maintained by the International Organization for Standardization. # ZillionsB.com By TrackBack on
2009/09/16 11:31 AM
|