Java
  Home arrow Java arrow Page 3 - Adding Images With iTextSharp
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 
Mobile Linux 
App Generation ROI 
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 - Manipulating an Image


    (Page 3 of 4 )

    Images don't have to be added to the document as-is. Sometimes they will need to be altered or transformed in some way. The library provides a few ways to transform images. We'll take a look at two of them here.

    One of the most basic ways to manipulate an image is to rotate it. Rotation is possible with iTextSharp using two properties: Rotation and RotationDegrees. The first property can be used to rotate the image by an angle in radians. For example, an image could be rotated a quarter-turn counterclockwise like this:

    logo.Rotation = (float)Math.PI / 2;

    Of course, most people will be more comfortable using degrees. The RotationDegrees property can be used to rotate an image by an angle in degrees. The same quarter-turn could be done using RotationDegrees like this:

    logo.RotationDegrees = 90;

    Images can also be resized, or scaled, using iTextSharp. There are three ways that an image can be scaled. The first is to scale an image to a certain height, width, or both. The ScaleAbsolute method can be used to give the image both a new height and a new width. To make an image 72 points tall and 36 points wide, we'd do this:

    logo.ScaleAbsolute(36, 72);

    The height or width of an image can be modified using the ScaleAbsoluteHeight and ScaleAbsoluteWidth methods:

    logo.ScaleAbsoluteHeight(72);

    logo.ScaleAbsoluteWidth(72);

    If you're not careful, though, scaling an image to a specific height or width can distort the image. The ScalePercent method can remedy this. We can pass it a percentage, which will be used to scale the image. To make the image half of its original size, we'd do this:

    logo.ScalePercent(50);

    Alternatively, the height and the width can be scaled separately. To make the image half as wide, but just as tall, we'd do this:

    logo.ScalePercent(50, 100);

    As you can see, the first parameter is the percentage to scale the width by, and the second is the percentage to scale the height.

    The third way to scale an image is to scale it to fit within a given rectangle using the ScaleToFit method. This accepts a width and a height, just like ScaleAbsolute, but it will not distort the image. The image will be scaled to fit within the rectangle but won't necessarily fill up the rectangle. So, if the original image is a square, and we use ScaleToFit with 72 as the width and 100 as the height, it will end up with both a width and a height of 72:


    logo.ScaleToFit(72, 100);

    The new dimensions can be checked with the ScaledWidth and ScaledHeight properties.

    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 1 hosted by Hostway
    Stay green...Green IT