Home arrow Java & J2EE arrow Page 4 - Adding Images With iTextSharp

An example - Java

Often, text alone can communicate a message, but sometimes graphics are needed. Even when they aren't, text alone can be a bit dull to read. In this article, we'll take a look at incorporating graphics into a PDF document with iTextSharp.

TABLE OF CONTENTS:
  1. Adding Images With iTextSharp
  2. Adding padding and a border
  3. Manipulating an Image
  4. An example
By: Peyton McCullough
Rating: starstarstarstarstar / 13
July 01, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Let's put together what we've covered so far in a short example. In this example, we'll create something that looks like this:



Note that this image is scaled down.

We'll use the ASP Free logo. Download it and place it in a directory in which the example will execute (the output directory of the Visual Studio project-you can add it to the project and set it to be copied to the output directory in the Properties Window):


In order to achieve the result we want, we'll need to create three instances of the image. All three will have to be rotated and will need borders. Two will need to be positioned manually and will be located beneath the text. The third will have text wrapped around it and will be positioned automatically. It will also be enlarged.


using System;

using System.IO;


using iTextSharp.text;

using iTextSharp.text.pdf;


class Images1

{

 public static void Main()

{

 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.";


 Document doc = new Document(PageSize.LETTER,

72, 72, 72, 72);

 PdfWriter.GetInstance(doc,

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

doc.Open();


 // Get instances of the images

 string imagePath = Path.Combine(

System.Environment.CurrentDirectory,

 "aspfreelogo.JPG");

 Image logoLeft = Image.GetInstance(imagePath);

 Image logoBottom = Image.GetInstance(imagePath);

 Image logoRight = Image.GetInstance(imagePath);


 // Add some text

doc.Add(new Paragraph(text));

doc.Add(new Paragraph(text));


 // Add the top-left image

logoLeft.Alignment = Image.UNDERLYING;

logoLeft.Rotation = (float)Math.PI / -3;

logoLeft.Border = Image.RIGHT_BORDER;

logoLeft.BorderWidth = 1;

logoLeft.BorderColor = Color.RED;

logoLeft.SetAbsolutePosition(72, 72 * 9);

doc.Add(logoLeft);


 // Add the top-right image

logoRight.Alignment = Image.UNDERLYING;

logoRight.Rotation = (float)Math.PI / 3;

logoRight.Border = Image.LEFT_BORDER;

logoRight.BorderWidth = 1;

logoRight.BorderColor = Color.BLUE;

logoRight.SetAbsolutePosition(72 * 4, 72 * 9);

doc.Add(logoRight);


 // Add the bottom-left image

 // (using degrees to rotate it!)

logoBottom.ScalePercent(150);

logoBottom.Alignment = Image.TEXTWRAP;

logoBottom.RotationDegrees = 45;

logoBottom.Border = Image.RIGHT_BORDER;

logoBottom.BorderWidth = 1;

logoBottom.BorderColor = Color.GREEN;

doc.Add(logoBottom);


 // Add some more text

doc.Add(new Paragraph(text));


doc.Close();

}

}

Nothing in the above example should be unfamiliar to you. We've covered everything there is, and each section of the code includes a comment.

That concludes basic image addition and transformation with the iTextSharp library. Now, you should be able to add images to a PDF document with spacing, borders, and so forth. Images can be placed automatically or manually. The images can also be transformed according to your needs.



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