Java
  Home arrow Java arrow Adding Images With iTextSharp
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Adding Images With iTextSharp
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2008-07-01

    Table of Contents:
  • Adding Images With iTextSharp
  • Adding padding and a border
  • Manipulating an Image
  • An example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Adding Images With iTextSharp


    (Page 1 of 4 )

    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.

    Adding Images

    The first thing we'll take a look at is adding existing images into a PDF document. The library supports various images types, each represented by its own class and derived from the Image class, which is abstract. We can import an image using the GetInstance static method of the Image class. This will create an instance of the specified image. There are a number of versions of this method that suit different purposes (for example, creating an image through the image's data), but the easiest one to use accepts a string-the filename of the image to retrieve:

    Image logo = Image.GetInstance("aspfreelogo.JPG");

    This will return the image aspfreelogo.JPG and will return an instance of the Jpeg class. The image can then be added to the page through Document.Add:

    doc.Add(logo);

    By default, the image is placed on the left side of the page. However, this can be changed through the Alignment property. Instead of placing the image on the left side, you can place it in the center or on the right side:

    logo.Alignment = Element.ALIGN_CENTER;

    logo.Alignment = Element.ALIGN_RIGHT;

    Of course, in some cases, you probably don't want the image occupying a whole chunk of space by itself. Instead, you may want text wrapped around the image. This can be arranged using the Alignment property as well:

    logo.Alignment = Image.TEXTWRAP;

    In other cases, you may want the image to be behind the text:

    logo.Alignment = Image.UNDERLYING;

    The above two settings can be combined with the settings for left, center, and right alignment by using the bitwise OR operator. The following line of code will place the image on the right side of the page, with text wrapped around it:

    logo.Alignment = Element.ALIGN_RIGHT | Image.TEXTWRAP;

    So far, we've been placing the image wherever we happen to be on the current page, just like we'd place a paragraph. However, sometimes it's necessary to place an image at a specific location on the page. The SetAbsolutePosition method can be used to do this. It accepts two float values, which, together, form a coordinate on the current page. This coordinate is the location of the lower left corner of the image. So to place an image an inch from the left of the page and an inch from the bottom, we'd do this:

    logo.SetAbsolutePosition(72, 72);

    The image is still added to the page as before, using the Add method.

    More Java Articles
    More By Peyton McCullough


       · Hello, all,This is yet another article on using the iTextSharp library to create...
       · Thanks for this tutorial.I would just need to know one more thing : is it possible...
       · You mean to flip the image (mirror it) horizontally?Yes, it's possible, and...
       · Yes, it's what I meant (flipping the image horizontally or mirror it).For now, I...
     

       

    JAVA ARTICLES

    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT