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

Working with paragraphs - 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


However, Chunk is a bit low-level. More often, you'll be using Paragraph, which represents a paragraph of text. A paragraph has space before it, and it can be indented. Unlike a Chunk, a paragraph's text does not have to be consistent. That is, portions of the text can be underlined, struck through, etc. Here's a basic Paragraph:


Paragraph hello = new Paragraph("Hello. This is a paragraph.");


A Paragraph can be styled just like a Chunk:


hello.Font.sestyle(Font.ITALIC);


However, applying styles to specific sections of text within a Paragraph is a bit trickier. To do this, we must break the Paragraph into Chunk objects and then style each Chunk object. Here, we create a paragraph with multiple styled sections of text:


Paragraph fancy = new Paragraph();


Chunk bold = new Chunk("This ");

bold.Font.SetStyle(Font.BOLD);

fancy.Add(bold);


Chunk italics = new Chunk("is a");

italics.Font.SetStyle(Font.ITALIC);

fancy.Add(italics);


Chunk big = new Chunk("fancy");

big.Font.Size = 20;

big.Font.SetStyle(Font.BOLD);

fancy.Add(big);


Chunk struck = new Chunk("paragraph.");

struck.Font.SetStyle(Font.STRIKETHRU);

fancy.Add(struck);


A paragraph will automatically have some spacing, but it's possible to add more:


Paragraph spaced =

 new Paragraph("This has a lot of space around it.");

spaced.SpacingBefore = 72;

spaced.SpacingAfter = 72;

spaced.IndentationLeft = 72;

spaced.IndentationRight = 72;


Indenting the paragraph by setting the IndentationLeft property indents the entire paragraph, though. Often, you'll want to indent only the first line of a paragraph. This can be done by setting the FirstLineIndent property:


Paragraph p = new Paragraph();

p.FirstLineIndent = 36;


While paragraphs are left-aligned by default, the alignment can be changed using the Alignment property:


Paragraph p = new Paragraph();

p.Alignment = Element.ALIGN_LEFT;

p.Alignment = Element.ALIGN_CENTER;

p.Alignment = Element.ALIGN_RIGHT;

p.Alignment = Element.ALIGN_JUSTIFIED;


The lines in a paragraph will naturally need some space between them, called a leading. The default leading will often do fine, but sometimes the leading must be modified. This can be done by setting the Leading property:


Paragraph spacy = new Paragraph();

spacy.Leading = 72;


The leading can also be specified in the constructor:


Paragraph spacy2 = new Paragraph(72);

Paragraph spacy3 = new Paragraph(72,

 "The leading is an inch.");


The leading can also be calculated by multiplying the font size by a number. This feature can be used, for example, to double space a paragraph:


Paragraph doubleSpaced = new Paragraph();

doubleSpaced.MultipliedLeading = 2;




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