Java
  Home arrow Java arrow Page 4 - 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 - An example


    (Page 4 of 4 )

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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