Click here to Skip to main content
15,892,746 members
Articles / Web Development / ASP.NET

Create/Read Advance PDF Report using iTextSharp in C# .NET: Part I

Rate me:
Please Sign up or sign in to vote.
4.80/5 (120 votes)
27 Nov 2013CPOL9 min read 805.8K   58.1K   217  
Create/Read/Write Advance PDF Report using iTextSharp.DLL in Desktop, Mobile, Web Application
using System;
using System.IO;
using System.Diagnostics;
using iTextSharp.text;
using iTextSharp.text.pdf;


namespace iTextSharp.tutorial.Chapter1
{
	/// <summary>
	/// Example 3 shows the Following:
	/// 1. Setting with Page Margins
	/// 2. Setting with the Text/Paragraph Alignment
	/// </summary>
	public class Example3
	{
		public Example3()
		{
			Document document = new Document(PageSize.A5, 36, 72, 108, 180);
			string appRootDir = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName;
			try
			{
				// Step 1: Creating System.IO.FileStream object
				using (FileStream fs = new FileStream(appRootDir + "/PDFs/" + "Chapter1_Example3.pdf", FileMode.Create, FileAccess.Write, FileShare.None))
				// Step 2: Creating iTextSharp.text.Document object
				// Adding PageSize and Margins during Document creation
				// NOTE: In iTextSharp library, 72 points = 1 inch
				// Left Margin:		36pt	=> 0.5 inch
				// Right Margin:	72pt	=> 1 inch
				// Top Margin:		108pt	=> 1.5 inch
				// Bottom Margini:	180pt	=> 2.5 inch
				using (Document doc = new Document(PageSize.A4, 36, 72, 108, 180))
				// Step 3: Creating iTextSharp.text.pdf.PdfWriter object
				// It helps to write the Document to the Specified FileStream
				using (PdfWriter writer = PdfWriter.GetInstance(doc, fs))
				{
					// Step 4: Openning the Document
					doc.Open();

					// Step 5: Adding a paragraph
					// NOTE: When we want to insert text, then we've to do it through creating paragraph
					// Creating iTextSharp.text.Paragraph object
					Paragraph para = new Paragraph("Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World");
					// Setting paragraph's text alignment using iTextSharp.text.Element class
					para.Alignment = Element.ALIGN_JUSTIFIED;
					// Adding this 'para' to the Document object
					doc.Add(para);

					// Step 6: Closing the Document
					doc.Close();
				}
			}
			// Catching iTextSharp.text.DocumentException if any
			catch (DocumentException de)
			{
				throw de;
			}
			// Catching System.IO.IOException if any
			catch (IOException ioe)
			{
				throw ioe;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer National Informatics Centre (NIC)
India India
Hello! Myself Debopam Pal. I've completed my MCA degree from West Bengal University of Technology at 2013. I'm from India. I’ve started to work with MS Technologies in 2013 specially in C# 4.0, ASP.NET 4.0. I've also worked in PHP 5. Now I work in JAVA/J2EE, Struts2. Currently I'm involved in a e-Governance Project since Jan, 2014. In my leisure time I write Blog, Articles as I think that every developer should contribute something otherwise resource will be finished one day. Thank you for your time.

Visit: Linkedin Profile | Facebook Profile | Google+ Profile | CodeProject Profile

Comments and Discussions