Question

PDF Stream to Tiff Stream conversion

Hi,

I need to convert the PDF stream to TIFF 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 PDF to TIFF binary stream, please provide that.

Thanks

Correct Answer
January 31, 2018 - 9:19am

Got Java code.

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

  1. java.io.File tiffFile=null;
  2.  
  3. try{
  4. tiffFile =java.io.File.createTempFile("TiffFile",".TIFF");
  5.  
  6. java.io.FileInputStream fis=new java.io.FileInputStream(tiffFile);
  7.  
  8. org.apache.pdfbox.pdmodel.PDDocument document = null;
  9. document = org.apache.pdfbox.pdmodel.PDDocument.load(Base64Util.decodeToByteArray(FileData));
  10. org.apache.pdfbox.rendering.PDFRenderer pdfRenderer = new org.apache.pdfbox.rendering.PDFRenderer(document);
  11. int pageCount=document.getPages().getCount();
  12. int pageCounter=0;
  13. java.util.ArrayList<java.awt.image.BufferedImage> bImgList=new java.util.ArrayList<java.awt.image.BufferedImage>();
  14.  
  15. for(pageCounter=0;pageCounter<pageCount;++pageCounter)
  16. {
  17. java.awt.image.BufferedImage bim = pdfRenderer.renderImageWithDPI(pageCounter, 196, org.apache.pdfbox.rendering.ImageType.BINARY);
  18. bImgList.add(bim);
  19. }
  20.  
  21. java.util.Iterator<javax.imageio.ImageWriter> writers=javax.imageio.ImageIO.getImageWritersByFormatName("TIFF");
  22. javax.imageio.ImageWriter imageWriter = writers.next();
  23. javax.imageio.ImageWriteParam param = imageWriter.getDefaultWriteParam();
  24. param.setTilingMode(javax.imageio.ImageWriteParam.MODE_DEFAULT);
  25. //Compression Mode
  26. param.setCompressionMode(javax.imageio.ImageWriteParam.MODE_EXPLICIT);
  27. param.setCompressionType("CCITT T.4");
  28.  
  29. javax.imageio.stream.ImageOutputStream ios = javax.imageio.ImageIO.createImageOutputStream(tiffFile);
  30. imageWriter.setOutput(ios);
  31. imageWriter.prepareWriteSequence(null);
  32.  
  33.  
  34. for (java.awt.image.BufferedImage image : bImgList)
  35. {
  36.  
  37. javax.imageio.metadata.IIOMetadata metadata = imageWriter.getDefaultImageMetadata(new javax.imageio.ImageTypeSpecifier(image), param);
  38. imageWriter.writeToSequence(new javax.imageio.IIOImage(image, null, metadata), param);
  39.  
  40. }
  41. java.io.ByteArrayOutputStream baos=new java.io.ByteArrayOutputStream();
  42.  
  43. int bt;
  44. while((bt=fis.read())!=-1)
  45. {
  46. baos.write(bt);
  47. }
  48. byte[] filebteary= baos.toByteArray();
  49. EBDR=Base64Util.encodeToString(filebteary);
  50.  
  51. imageWriter.endWriteSequence();
  52. imageWriter.dispose();
  53. fis.close();
  54. ios.flush();
  55. ios.close();
  56. document.close();
  57. baos.close();
  58.  
  59.  
  60. }
  61. catch(Exception e)
  62. {
  63. e.printStackTrace();
  64. }
  65.  
  66. finally
  67. {
  68. tiffFile.delete();
  69. }

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

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

Thanks.

Comments

Keep up to date on this post and subscribe to comments

January 31, 2018 - 9:19am

Got Java code.

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

  1. java.io.File tiffFile=null;
  2.  
  3. try{
  4. tiffFile =java.io.File.createTempFile("TiffFile",".TIFF");
  5.  
  6. java.io.FileInputStream fis=new java.io.FileInputStream(tiffFile);
  7.  
  8. org.apache.pdfbox.pdmodel.PDDocument document = null;
  9. document = org.apache.pdfbox.pdmodel.PDDocument.load(Base64Util.decodeToByteArray(FileData));
  10. org.apache.pdfbox.rendering.PDFRenderer pdfRenderer = new org.apache.pdfbox.rendering.PDFRenderer(document);
  11. int pageCount=document.getPages().getCount();
  12. int pageCounter=0;
  13. java.util.ArrayList<java.awt.image.BufferedImage> bImgList=new java.util.ArrayList<java.awt.image.BufferedImage>();
  14.  
  15. for(pageCounter=0;pageCounter<pageCount;++pageCounter)
  16. {
  17. java.awt.image.BufferedImage bim = pdfRenderer.renderImageWithDPI(pageCounter, 196, org.apache.pdfbox.rendering.ImageType.BINARY);
  18. bImgList.add(bim);
  19. }
  20.  
  21. java.util.Iterator<javax.imageio.ImageWriter> writers=javax.imageio.ImageIO.getImageWritersByFormatName("TIFF");
  22. javax.imageio.ImageWriter imageWriter = writers.next();
  23. javax.imageio.ImageWriteParam param = imageWriter.getDefaultWriteParam();
  24. param.setTilingMode(javax.imageio.ImageWriteParam.MODE_DEFAULT);
  25. //Compression Mode
  26. param.setCompressionMode(javax.imageio.ImageWriteParam.MODE_EXPLICIT);
  27. param.setCompressionType("CCITT T.4");
  28.  
  29. javax.imageio.stream.ImageOutputStream ios = javax.imageio.ImageIO.createImageOutputStream(tiffFile);
  30. imageWriter.setOutput(ios);
  31. imageWriter.prepareWriteSequence(null);
  32.  
  33.  
  34. for (java.awt.image.BufferedImage image : bImgList)
  35. {
  36.  
  37. javax.imageio.metadata.IIOMetadata metadata = imageWriter.getDefaultImageMetadata(new javax.imageio.ImageTypeSpecifier(image), param);
  38. imageWriter.writeToSequence(new javax.imageio.IIOImage(image, null, metadata), param);
  39.  
  40. }
  41. java.io.ByteArrayOutputStream baos=new java.io.ByteArrayOutputStream();
  42.  
  43. int bt;
  44. while((bt=fis.read())!=-1)
  45. {
  46. baos.write(bt);
  47. }
  48. byte[] filebteary= baos.toByteArray();
  49. EBDR=Base64Util.encodeToString(filebteary);
  50.  
  51. imageWriter.endWriteSequence();
  52. imageWriter.dispose();
  53. fis.close();
  54. ios.flush();
  55. ios.close();
  56. document.close();
  57. baos.close();
  58.  
  59.  
  60. }
  61. catch(Exception e)
  62. {
  63. e.printStackTrace();
  64. }
  65.  
  66. finally
  67. {
  68. tiffFile.delete();
  69. }

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

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

Thanks.

December 7, 2018 - 12:39am
Response to Krishna_Chaitanya

Hi Krishna Chaitanya,

I have used the above code to convert pdf to tiff in activity(java methods), but am unable to convert the pdf. It is executing till the number of pages and throwing error in "javax.imageio.ImageWriter imageWriter = writers.next();" as "java.util.NoSuchElementException". Do we have any alternate solution to convert pdf to tiff?

Thanks in Advance.

 

Regards,

Mathumitha