Home arrow Java & J2EE arrow Page 4 - Creating Simple PDF Files With iTextSharp

A short example - Java

Have you ever wondered how to generate PDF documents in .NET? Thankfully, there's a port of the iText library for .NET, called iTextSharp. Moreover, since C# and Java share a number of similarities, iText code in Java can be easily converted into C# in order to work with iTextSharp. In this article, we'll take a look at the iTextSharp library, using it for PDF generation and manipulation in .NET.

  1. Creating Simple PDF Files With iTextSharp
  2. Working with fonts and text
  3. Working with paragraphs
  4. A short example
By: Peyton McCullough
Rating: starstarstarstarstar / 20
June 17, 2008

print this article



In order to recap everything before we move on, let's create a simple application that generates a very basic PDF document. Nothing fancy is needed here. We'll create a few paragraphs of text and style each of them a bit differently. This application will create a PDF in the file 1.pdf:

using System;

using System.IO;

using iTextSharp.text;

using iTextSharp.text.pdf;

class Example1


 static void Main(string[] args)


 // Create and open a document

Document doc = new Document();


 new FileStream("1.pdf", FileMode.Create));


 // Create a string to use for paragraphs

 string text = "This is a paragraph. It is represted" +

 " by a Paragraph object in the iTextSharp " +

 "library. Here, we're creating paragraphs with " +

 "various styles in order to test out iTextSharp." +

 " This paragraph will take up multiple lines " +

 "and allow for a more complete example.";

 // Add a basic paragraph

doc.Add(new Paragraph(text));

 // Add a paragraph that's underlined and indented

Paragraph p2 = new Paragraph(text);

p2.IndentationLeft = 36;



 // Add a paragraph that's underlined and in bold

doc.Add(new Paragraph(text,




 // Add a really big paragraph

doc.Add(new Paragraph(text,

 new Font(Font.HELVETICA, 36)));

 // Add a green, centered paragraph

Paragraph p5 = new Paragraph(text,

 new Font(Font.TIMES_ROMAN,


p5.Alignment = Element.ALIGN_CENTER;


 // Add a double-spaced paragraph with

 // an indented first line

Paragraph p6 = new Paragraph(text);

p6.FirstLineIndent = 36;

p6.MultipliedLeading = 2;


 // Add a paragraph with multiple styles

 // Each word gets bigger

Paragraph ascending = new Paragraph();

ascending.SpacingBefore = 72;

 for (int i = 1; i <= 5; i++)


ascending.Add(new Chunk("Hello",

 new Font(Font.HELVETICA, i * 5)));



 // Close the document




The example is pretty straightforward. Everything should be familiar to you, and, moreover, comments explain the different sections of code. When you run the above code, the file 1.pdf will be created. If you open the new PDF file, you should see multiple paragraphs with various styles.

You've looked at two basic text objects, fonts, and a document creation in the iTextSharp library, but, of course, the library supports more features. Besides adding text, you can add graphics and metadata to a PDF document. You can even enhance the text parts of a document with features such as columns.

>>> More Java & J2EE Articles          >>> More By Peyton McCullough

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: