欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java IText (PDF),javaitextpdf

来源: javaer 分享于  点击 38295 次 点评:65

Java IText (PDF),javaitextpdf


Java IText



Java IText: Image

  • Creating an Image
  • Absolute Positioning
  • Scaling
  • Rotating

Jakob Jenkov
Last update: 2014-05-24

    

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:

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

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

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

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
Next: Java IText: Superscript + Subscript

相关文章

    暂无相关文章
相关栏目:

用户点评