2014年10月25日 星期六

itext-images

-http://tutorials.jenkov.com/java-itext/image.html


You can do a lot with images in IText, including scaling, rotating, masking, absolute positioning, borders, alignment etc. I'll only go through the basics here. Get the "IText in Action" book if you want the full story on images. Here is a list of the topics covered in this text:
  1. Creating an Image
  2. Absolute Positioning
  3. Scaling
  4. Rotating

Creating an Image

The com.itextpdf.text.Image is used to add images to IText PDF documents. You can load images either from file or from a URL, like this:
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;

public class ImageExample {
  public static void main(String[] args) {
    Document document = new Document();

    try {
        PdfWriter.getInstance(document,
                new FileOutputStream("Image.pdf"));
        document.open();

        Image image1 = Image.getInstance("watermark.png");
        document.add(image1);

        
            String imageUrl = "http://jenkov.com/images/" +
            "20081123-20081123-3E1W7902-small-portrait.jpg";

            Image image2 = Image.getInstance(new URL(imageUrl));
        document.add(image2);

        document.close();
    } catch(Exception e){
      e.printStackTrace();
    }
  }
}
Here is what the generated document looks like:
An IText Image example
An IText Image example

Absolute Positioning

You set the absolute position of an image using the setAbsolutePosition() method. Do so before adding the image to the document. This method takes two parameters: X and Y coordinate of the lower left corner of the image. Also keep in mind, that the origin coordinate system in a PDF document is the lower left corner of the document. Not the uppper left corner, like on a screen.
Here is a code example:
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.net.URL;

public class Image2Example {
    public static void main(String[] args) {
        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("Image2.pdf"));
            document.open();

            String imageUrl = "http://jenkov.com/images/" +
                    "20081123-20081123-3E1W7902-small-portrait.jpg";

            Image image = Image.getInstance(new URL(imageUrl));
            image.setAbsolutePosition(500f, 650f);
            document.add(image);

            document.close();
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}
Here is what the resulting document looks like:
An IText Image at an absolute position
An IText Image at an absolute position

Scaling

You can scale images using one of these Image methods:
scaleAbsolute()
scaleAbsoluteWidth()
scaleAbsoluteHeight()
scalePercentage()
scaleToFit()
Here is a simple example:
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.net.URL;

public class Image3Example {
  public static void main(String[] args) {
    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
            new FileOutputStream("Image3.pdf"));
      document.open();

      String imageUrl = "http://jenkov.com/images/" +
              "20081123-20081123-3E1W7902-small-portrait.jpg";

      Image image = Image.getInstance(new URL(imageUrl));
      image.scaleAbsolute(150f, 150f);
      document.add(image);

      Image image2 = Image.getInstance(new URL(imageUrl));
      image2.scalePercent(300f);
      document.add(image2);

      document.close();
    } catch(Exception e){
       e.printStackTrace();
    }
  }
}
Here is what the resulting document looks like:
Two IText Image's scaled
Two IText Image's scaled

Rotating

You can rotate images in IText PDF documents too, using these methods:
setRotationDegrees()
setRotation()
Here is a simple example:
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.net.URL;

public class Image4Example {
  public static void main(String[] args) {
    Document document = new Document();

    try {
      PdfWriter.getInstance(document,
            new FileOutputStream("Image4.pdf"));
      document.open();

      String imageUrl = "http://jenkov.com/images/" +
              "20081123-20081123-3E1W7902-small-portrait.jpg";

      Image image = Image.getInstance(new URL(imageUrl));
      image.setRotationDegrees(45f);
      document.add(image);

      document.close();
    } catch(Exception e){
      e.printStackTrace();
    }
  }
}
Here is what the resulting document looks like:
An IText Image rotated
An IText Image rotated

沒有留言:

張貼留言