Java Tutorial/PDF/Tiff Image

Материал из Java эксперт
Версия от 05:21, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Get DpiX and DpiY for tiff image

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Image tiff = Image.getInstance("dog.tiff");
    document.add(tiff);
    document.add(new Paragraph("DPI X: " + tiff.getDpiX() + "; DPI Y: " + tiff.getDpiY()));
    
    document.close();
  }
}





Get width and height of tiff image

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Image tiff = Image.getInstance("dog.tiff");
    document.add(tiff);
    document.add(new Paragraph("Original width: " + tiff.width() + "; original height: "
        + tiff.height()));
    
    document.close();
  }
}





Image with 360 Dpi scaled

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    Image tiff = Image.getInstance("dog.tiff");
    document.add(tiff);
    tiff.scalePercent(72f / tiff.getDpiX() * 100);
    document.add(new Paragraph("Show the image with 360 Dpi (scaled " + (7200f / tiff.getDpiX())
        + "%):"));
        
    document.add(new Paragraph("Scaled width: " + tiff.scaledWidth() + "; scaled height: "
        + tiff.scaledHeight()));        
    document.add(tiff);
    document.close();
  }
}





Load tiff image

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    document.add(new Paragraph("dog.tiff"));
    Image img1 = Image.getInstance("dog.tiff");
    System.out.println(img1.getClass().getName());
    document.add(img1);
    
    
    document.close();
  }
}





Multiple-page Tiff

import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.RandomAccessFileOrArray;
import com.lowagie.text.pdf.codec.TiffImage;
public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();
    document.add(Image.getInstance("dog.tif"));
    RandomAccessFileOrArray ra = new RandomAccessFileOrArray("dog.tif");
    int pages = TiffImage.getNumberOfPages(ra);
    document.add(new Paragraph("There are " + pages + " pages in the tiff file."));
    for (int i = 0; i < pages;) {
      ++i;
      document.add(TiffImage.getTiffImage(ra, i));
    }
    document.close();
  }
}