The Brave Programmer - Blogging and coding
Not for the faint hearted
 

Blog Posts From The Brave Programmer

Minimize

Creating PDF files with C# and .NET

Sep16

Written by:
2009/09/16 10:40 AM RssIcon

Create PDF Documents in 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.

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.

del.icio.us Tags: ,,,,


Tags:
Categories:
blog comments powered by Disqus

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
 
Blog Updates Via E-mail
 Blog Updates Via E-mail
Minimize

Do you want to receive blog updates via e-mail. Then just click on the link below. You will be redirected to Google's feed burner, where you can fill out a form. Supplying your e-mail address.

The subscription is managed entirely by Google's Feedburner. We cannot and do not collect your email address.

Subscribe to The Brave Programmer by Email

Print  
 

 

Latest Comments
 Latest Comments
Minimize
Powered by Disqus

Sign up with Disqus to enjoy a  surprise box of features

Print  
 
Blog Roll
 Blog Roll
Minimize
Print  
 
Categories
 Categories
Minimize
Print  
 
<h1>Search Blogs From The Brave Programmer</h1>
 

Search Blogs From The Brave Programmer

Minimize
Print  
 
Archive
 Archive
Minimize
Archive
<March 2017>
SunMonTueWedThuFriSat
2627281234
567891011
12131415161718
19202122232425
2627282930311
2345678
Monthly
Go
Print  
 
<h1>News Feeds (RSS)</h1>
 

News Feeds (RSS)

Minimize
Print  
 

Follow robertbravery on Twitter

Blog Engage Blog Forum and Blogging Community, Free Blog Submissions and Blog Traffic, Blog Directory, Article Submissions, Blog Traffic

View Robert Bravery's profile on LinkedIn

Mybyte

 

Robert - Find me on Bloggers.com

Tags
 Tags
Minimize
Print  
 
Contact Us Now
 Contact Us Now
Minimize
 

Email  us now or call us on 082-413-1420,  to host your website.

We design and develop websites. We develop websites that make a difference. We do Dotnetnuke Module development.

Web Masters Around The World
Power By Ringsurf
Print