Convert a group of TIFF images into a PDF file

Source: Internet
Author: User
Package com.netcode.pdf;

Import java. Io .*;

Import java. AWT. image. renderedimage;
Import java. AWT. image. bufferedimage;
Import java. AWT. image. Raster;
Import java. util. hashtable;

Import com. lowagie. Text. Document;
Import com. lowagie. Text. image;
Import com.lowagie.text.html. Contents contentbyte;
Import com.lowagie.text=. writable writer;
Import com. Sun. Media. Jai. codec .*;
Import com. Sun. Media. Jai. codecimpl .*;
Import com. Sun. Media. Jai. codec. seekablestream;
Import com. Sun. Media. Jai. codec. fileseekablestream;
Import com. Sun. Media. Jai. codec. imagedecoder;
Import com. Sun. Media. Jai. codec. imagecodec;

Public class extends topdf {
Document document;
Pdfcontentbyte CB;

Public writable topdf (string filename, string [] strimages) throws exception {
Document = new document ();
Fileoutputstream rech = new fileoutputstream (filename );
Response writer = Response writer. getinstance (document, rech );
Document. open ();
CB = writer. getdirectcontent ();
For (INT I = 0; I <strimages. length; ++ I ){
Addimage (strimages [I]);
}

Document. Close ();
}

Public void addimage (string strimagename) throws exception {
System. Out. println (strimagename );
File file = new file (strimagename );
Seekablestream stream = new fileseekablestream (strimagename );
Catalog directory dir = new catalog directory (stream, 0 );
String [] names = imagecodec. getdecodernames (Stream );
Imagedecoder dec = imagecodec. createimagedecoder (Names [0], stream,
Null );
Int Total = dec. getnumpages ();
For (int K = 0; k <total; ++ K ){
Renderedimage rI = dec. decodeasrenderedimage (k );
Raster Ra = Ri. getdata ();
Bufferedimage Bi = new bufferedimage (Ri. getcolormodel (), raster
. Createwritableraster (Ri. getsamplemodel (), RA
. Getdatabuffer (), null), false, new hashtable ());
Image IMG = image. getinstance (Bi, null, true );

Long h = 0;
Long W = 0;
Long ifdoffset = dir. getifdoffset ();
While (ifdoffset! = 0l ){
Dir = new catalog directory (stream, ifdoffset, 0 );
Ifdoffset = dir. getnextifdoffset ();
H = dir. getfieldaslong (incluimagedecoder. pai_image_length );
W = dir. getfieldaslong (incluimagedecoder. pai_image_width );
}
Float percent = 100;
Int Pos = 0;
If (W> 895)
Percent = (595 + 18) * 100/W );
If (h> 842)
Pos = (INT) (842-H x percent/100 );
Else
Position = (INT) (842-h );
System. Out. println (percent );
System. Out. println (POS );
IMG. scalepercent (percent );
IMG. setabsoluteposition (0, POS );
System. Out. println ("Image:" + k );

CB. addimage (IMG );
Document. Newpage ();
}
Stream. Close ();
File. Delete ();
}

Public static void main (string [] ARGs ){
Try {
String [] temp = {"1. Tiff", "2. Tiff", "3. Tiff "};
Export topdf build = new export topdf ("temptid", temp );

} Catch (exception e ){
E. printstacktrace ();
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.