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

Working with fonts and text - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Adding text to the document is quite easy. The most basic unit of text is the Chunk, which is simply a “chunk” of text that must have a consistent appearance. Here, we create a simple Chunk:


Chunk boo = new Chunk("Boo!");


Content must be manually added to the page:


doc.Add(boo);


We can stylize the text if we need to. Below, we make the text bold and big:


boo.Font.SetStyle(Font.BOLD);

boo.Font.Size = 72;


As you can see, the Font property, which is of the Font type, represents the text's font. Above, we make it bold and give it a size of 72 points. To set the style, we can either pass an appropriate integer value (these values can be found in Font, like Font.Bold), or we can pass a string to achieve the same effect:


boo.Font.SetStyle("bold");


Fonts can also be created and passed into the constructor when creating a Chunk:


Font small = new Font(Font.TIMES_ROMAN, 5);

Chunk smallText = new Chunk("This is small.", small);


Above, we create a font by specifying the font family (Times Roman) and a size (five points). However, we can specify several more properties in the constructor, such as the font style and the color. Below, we create a red, italicized font:


Font redItalic = new Font(Font.HELVETICA, Font.DEFAULTSIZE,

 Font.ITALIC, Color.RED);


Fonts can also be quickly and easily created using the GetFont method of the FontFactory class:


Chunk hey = new Chunk("Hey.", FontFactory.GetFont("Courier", 12, Font.ITALIC));


There's also a Phrase class, which is a bit higher level than the Chunk class, but is created similarly:


Phrase p = new Phrase("This is a phrase.");




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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- 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: