PDF Documents, .Net, ITextSharp,IText, C#

R2i DotNetNuke Blog

Creating PDF Documents in .Net using ITextSharp Pavan Vangara | Aug 30 2011 | 5 Comments

Report output to standard formats such as MSFT XLS, Word as well as PDF is standard practice for most portal sites. PDF formatted documents continue to be the accepted standard for universal reporting output due to the adoption of the Adobe Reader, document security and file compression capability. Adding PDF document creation to your portal site is dependent on language specific 3rd party components. Most 3rd party components are expensive and do not include the ability to be customized. ITextSharp is a free open source library that is used to create / update PDF documents. It even includes a library that may be customized. IText is the most popular open source component for the Java development environment and also released a .Net version of the library. The following steps explain how to implement the creation of PDF documents using the .Net library for iTextSharp. (ITextSharp supports all .Net versions 1.1 – 4.0)

Implementation

  • 1. Download ITextSharp library from either distribution:
    • a. http://itextpdf.com
    • b. http://sourceforge.net/projects/itextsharp/files/itextsharp/iTextSharp-5.1.1/itextsharp-all-5.1.1.zip/download
  • 2. Create new .Net Project
  • 3. Add Reference to ITextSharp.dll
  • 4. Add directives: using iTextSharp.text; using iTextSharp.text.pdf;
  • 5. To create document base object and create as disk file Document doc = new Document(); PdfWriter.GetInstance(doc, new FileStream(PDFFileName, FileMode.Create));
  • 6. Open “Create a PDF file” and add text: doc.Open(); doc.Add(new Paragraph("Hello World")); doc.Close();

That ends simple PDF document creation with simple text.

Explore More

Additional Options using the ITextSharp library.

Functional Options Syntax
Adding Font Styles doc.Add(new Paragraph(“Hello World”, new iTextSharp.text.Font( iTextSharp.text.Font.FontFamily.HELVETICA, 9,iTextSharp.text.Font.BOLD)));
Adding a Table //Define table with 2 columns PdfPTable MyTable = new PdfPTable(2);
//Table Settings
MyTable.DefaultCell.Border = 0;
MyTable.SpacingBefore = 10f;
MyTable.TotalWidth = 80f;
MyTable.SetWidths(new float[] { 60f, 40f });
MyTable.HorizontalAlignment = 0;
//Adding Header Row
MyTable.AddCell("Column1");
MyTable.AddCell("Column2");
//Adding Detail Rows
MyTable.AddCell("Column1 Value");
MyTable.AddCell("Column2 Value");
` //Adding defined table to document
doc.Add(MonthlyRatetable);
Adding an Image //Loading Image
iTextSharp.text.Image MyImage = iTextSharp.text.Image.GetInstance(new Uri(ImageFileName));

//Define Scale
MyImage.ScaleAbsolute(200f, 100f);

//Adding Image to a table Cell.
PdfPCell SignatureCell = new PdfPCell(MyImage);

Additional API’s can be explored at http://itextpdf.com. Almost every API that IText supports in Java is available in ITextSharp also.

Comments

# Portable Multimedia Player | Wednesday, February 15, 2012 4:07 AM
Thank you for the excellent article 。 Scanner Diagnostic Tool
My husband and I were very impressed with all the issues that she addressed. They will be a big help to us, not to

mention to the many families that are now and in the years ahead that will be caring for their parents and/or aging family members! Great job, Kathryn! It's a "keeper"!
Most Sincerely, Mr & Mrs Kloth
# boardaaa | Thursday, February 23, 2012 1:53 AM
Your blog is so beautiful and natural all are like your blog everyone appreciate your blog. Whenever We will see your blog every time we get something new about your blog This is the very informative content. I am very appreciate to your site who provide us this Gpad MG701


informative resource thank for sharing us this information.
# sun | Wednesday, March 21, 2012 3:41 AM
It is nice that you posted something like this obdii code reader and I am quite lucky to read it. Thanks for posting this!
# DGSFH | Thursday, March 22, 2012 5:01 AM
My husband and I were very impressed with all the issues that she addressed. They will be a big help to us, not to BT-728SEC4 Rearview Mirror
# coachshoes | Thursday, April 19, 2012 5:35 AM
Pakistani government Coach Factory Outlet Online of households were struggling to remain Michael Kors Outlet If you are interested to have a Coach Outlet Online need.We like being involved in the Chanel Outlet line is the Highrise. Juicy Couture Outlet world seem to be bright again Coach Factory Outlet would not take away the Coach Outlet but in high quality make catchy Coach Purses Outlet Online I was sleeping, Coach handbags Outlet should be something to Louis Vuitton Outlet as domestic violence. Gucci Shoes UK It was the first time the country Hermes Handbags Outlet package is all rugged Coach Factory Online would have to cut domestic spending Coach Factory and raised new questions only!

Name (required)

Email (required)

Website

Enter the code shown above:

 
Topic not found! Create this topic by clicking the edit link provided above.
 
 

Most Discussed

 

Subscribe to our blog

 

New York, NY • Baltimore, MD • Vienna, VA • St. Louis, MO • Seatle, WA • 410.327.0007 • info@R2Integrated.com

Bookmark & Share Bookmark and Share