Tuesday, 27 August 2013

Convert TIFF Image to Pdf File in Java

Convert TIFF Image to Pdf File in Java

I already write article for convert .tif file to .jpg image.Now I write the post for convert TIFF Image to pdf File.Before start this coding we need iText.jar api for converting image to pdf. Here First the image converted in buffered Array then it will read pdf using Pdfwriter.

Required API:

1)iText.jar Click here


Coding:

import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.RandomAccessFileOrArray;
import com.lowagie.text.pdf.codec.TiffImage;

public class ConvertTiff2Pdf {
  public static void main(String[] args) {
 System.out.println("one");
 String arg[]={"C:/Users/user/Desktop/G4S.TIF"};//Your Tif file
 System.out.println("one2");
    if (arg.length < 1) {
      System.out
         .println("Usage: Tiff2Pdf file1.tif [file2.tif ... fileN.tif]");
      System.exit(1);
    }
    String tiff;
    String pdf;
    System.out.println("two");
    for (int i = 0; i < arg.length; i++) {
       tiff = arg[i];
       pdf = tiff.substring(0, tiff.lastIndexOf('.') + 1) + "pdf";
       Document document = new Document(PageSize.LETTER, 0, 0, 0, 0);
       try {
          PdfWriter writer = PdfWriter.getInstance(document,
             new FileOutputStream(pdf));
          int pages = 0;
          document.open();
          PdfContentByte cb = writer.getDirectContent();
          RandomAccessFileOrArray ra = null;
          int comps = 0;
          try {
             ra = new RandomAccessFileOrArray(tiff);
             comps = TiffImage.getNumberOfPages(ra);
          }
          catch (Throwable e) {
             System.out.println("Exception in " + tiff + " "
               + e.getMessage());
             continue;
          }
          System.out.println("Processing: " + tiff);
          for (int c = 0; c < comps; ++c) {
             try {
                Image img = TiffImage.getTiffImage(ra, c + 1);
                if (img != null) {
                  System.out.println("page " + (c + 1));
                  System.out.println("img.getDpiX() : "+img.getDpiX());
                  System.out.println("img.getDpiY() : "+img.getDpiY());
                  img.scalePercent(6200f / img.getDpiX(), 6200f / img.getDpiY());
                               
                  //img.scalePercent(img.getDpiX(), img.getDpiY());
                  document.setPageSize(new Rectangle(img.scaledWidth(), img.scaledHeight()));
                  img.setAbsolutePosition(0, 0);
                  cb.addImage(img);
                  document.newPage();
                  ++pages;
                }
              }
              catch (Throwable e) {
                System.out.println("Exception " + tiff + " page "
                  + (c + 1) + " " + e.getMessage());
              }
          }
          ra.close();
          document.close();
          }
          catch (Throwable e) {
             e.printStackTrace();
          }
          System.out.println("done");
       }
    }

}



Similar Posts:

Convert Tiff Image to Jpeg using java

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete