Question

Tiff Binary Stream to PDF Stream conversion

Hi,

I need to convert the TIFF Binary stream to PDF stream.

As there is no OOTB function for this, the custom functions have to be created.

If any one have the code for converting the TIFF to PDF binary stream, please provide that.

Thanks

***Updated by moderator: Lochan to add Categories***

**Moderation Team has archived post**

This post has been archived for educational purposes. Contents and links will no longer be updated. If you have the same/similar question, please write a new post.

Correct Answer
December 29, 2017 - 4:03am

Got Java code from various sources.

The below Java code helps to get the PDF stream from TIFF stream  :

//Prepare byte array output stream

java.io.ByteArrayOutputStream outfile = new java.io.ByteArrayOutputStream();

//Create new document

com.lowagie.text.Document document = new com.lowagie.text.Document(com.lowagie.text.PageSize.A4.rotate());

try{

// Get Instance of PDF writer

com.lowagie.text.pdf.PdfWriter writer = com.lowagie.text.pdf.PdfWriter.getInstance(document,outfile); 

writer.setStrictImageSequence(true);

// Open the document

document.open(); 

//Decode the tiff encoded stream attachStream and prepare random access file or array

com.lowagie.text.pdf.RandomAccessFileOrArray ra = new com.lowagie.text.pdf.RandomAccessFileOrArray(Base64Util.decodeToByteArray(attachStream));

// count number of pages in TIFF

int pages = com.lowagie.text.pdf.codec.TiffImage.getNumberOfPages(ra); 

for(int i = 0; i< pages;i++){

//get each image for TIFF file

com.lowagie.text.Image tiff = com.lowagie.text.pdf.codec.TiffImagegetTiffImage(ra,i+1); 

// Scale the Image

tiff.scaleToFit(800,600); 

// Add each image to document

document.add(tiff); 

}

document.close();

// Capture the PDF Stream

String pdfStream = Base64Util.encodeToString(outfile.toByteArray());

outfile.flush();

}catch(Exception e){

e.printstacktrace()

}

 

The function can be prepared with this Java code and use.

If there is any best solution other than this, please post here.

Comments

Keep up to date on this post and subscribe to comments

December 29, 2017 - 4:03am

Got Java code from various sources.

The below Java code helps to get the PDF stream from TIFF stream  :

//Prepare byte array output stream

java.io.ByteArrayOutputStream outfile = new java.io.ByteArrayOutputStream();

//Create new document

com.lowagie.text.Document document = new com.lowagie.text.Document(com.lowagie.text.PageSize.A4.rotate());

try{

// Get Instance of PDF writer

com.lowagie.text.pdf.PdfWriter writer = com.lowagie.text.pdf.PdfWriter.getInstance(document,outfile); 

writer.setStrictImageSequence(true);

// Open the document

document.open(); 

//Decode the tiff encoded stream attachStream and prepare random access file or array

com.lowagie.text.pdf.RandomAccessFileOrArray ra = new com.lowagie.text.pdf.RandomAccessFileOrArray(Base64Util.decodeToByteArray(attachStream));

// count number of pages in TIFF

int pages = com.lowagie.text.pdf.codec.TiffImage.getNumberOfPages(ra); 

for(int i = 0; i< pages;i++){

//get each image for TIFF file

com.lowagie.text.Image tiff = com.lowagie.text.pdf.codec.TiffImagegetTiffImage(ra,i+1); 

// Scale the Image

tiff.scaleToFit(800,600); 

// Add each image to document

document.add(tiff); 

}

document.close();

// Capture the PDF Stream

String pdfStream = Base64Util.encodeToString(outfile.toByteArray());

outfile.flush();

}catch(Exception e){

e.printstacktrace()

}

 

The function can be prepared with this Java code and use.

If there is any best solution other than this, please post here.