Cannot add TIFF image into PDF

Hi,


I need to convert a TIFF image into PDF. I found some example how to do that, but in the end I get the exception below. Can you help me solving that issue?

Pdf pdf1 = new Pdf();
//Create a section in the Pdf object
Section sec1 = pdf1.getSections().add();
//Create an image object in the section
aspose.pdf.Image img1 = new aspose.pdf.Image(sec1);
//Add image object into the Paragraphs collection of the section
sec1.getParagraphs().add(img1);
img1.getImageInfo().setTitle(sysObject.getObjectName());
//Set the path of image file

img1.getImageInfo().setFile(“tiff.tiff”);
img1.getImageInfo().setFixWidth(sec1.getPageInfo().getPageWidth() - sec1.getPageInfo().getMargin().getLeft() - sec1.getPageInfo().getMargin().getRight());

//Save the Pdf
pdf1.save(“pdf.pdf”);


Exception:
java.util.NoSuchElementException
at javax.imageio.spi.FilterIterator.next(ServiceRegistry.java:808)
at javax.imageio.ImageIO$ImageReaderIterator.next(ImageIO.java:502)
at javax.imageio.ImageIO$ImageReaderIterator.next(ImageIO.java:487)
at aspose.pdf.figure.Bitmap.readFrames(Unknown Source)
at aspose.pdf.figure.Bitmap.a(Unknown Source)
at aspose.pdf.figure.Bitmap.(Unknown Source)
at aspose.pdf.xml.ImageBase.a(Unknown Source)
at aspose.pdf.xml.ImageGenerator.processImage(Unknown Source)
at aspose.pdf.xml.SectionGenerator.processSection(Unknown Source)
at aspose.pdf.xml.DocumentBase.a(Unknown Source)
at aspose.pdf.xml.GeneratorXml.generateDocument(Unknown Source)
at aspose.pdf.Pdf.save(Unknown Source)

Hi Mariusz,


Thanks for using our products.

Can you please share the source TIFF image so that we can test the scenario at our end. We are sorry for this inconvenience.

Attached. Does Aspose.PDF requires JAI?

I added jai_core.jar and jai_codec.jar into the classpath (v.1.1.3) and now it fails with a different exception.


java.lang.NoClassDefFoundError: com/sun/media/imageio/plugins/tiff/TIFFDirectory
at aspose.pdf.figure.Bitmap.readFrames(Unknown Source)
at aspose.pdf.figure.Bitmap.a(Unknown Source)
at aspose.pdf.figure.Bitmap.(Unknown Source)
at aspose.pdf.xml.ImageBase.a(Unknown Source)
at aspose.pdf.xml.ImageGenerator.processImage(Unknown Source)
at aspose.pdf.xml.SectionGenerator.processSection(Unknown Source)
at aspose.pdf.xml.DocumentBase.a(Unknown Source)
at aspose.pdf.xml.GeneratorXml.generateDocument(Unknown Source)
at aspose.pdf.Pdf.save(Unknown Source)

TIFFDirectory file is present in jai_codec.jar, but in a different location
jai_codec.jar\com\sun\media\jai\codec\TIFFDirectory

Finally I added jai_core.jar, jai_codec.jar and jai_imageio.jar into the classpath and there is no more issue, but the produced PDF is corrupted. Check the attached files.

There is actually some internal exception in the log during the TIFF conversion:


bitmap IR: tif
dpiX: 0.0
dpiY: 0.0
bitmap IR: tif
dpiX: 0.0
dpiY: 0.0
bitmap IR: tif
dpiX: 0.0
dpiY: 0.0
bitmap IR: tif
dpiX: 0.0
dpiY: 0.0
bitmap IR: tif
dpiX: 0.0
dpiY: 0.0
bitmap IR: tif
dpiX: 0.0
dpiY: 0.0
bitmap IR: tif
dpiX: 0.0
dpiY: 0.0
bitmap IR: tif
dpiX: 0.0
dpiY: 0.0
bitmap IR: tif
dpiX: 0.0
dpiY: 0.0
bitmap IR: tif
dpiX: 0.0
dpiY: 0.0
bitmap IR: tif
image = BufferedImage@3834a1c8: type = 0 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@4d2ef4c6 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 1903 height = 899 #numDataElements 3 dataOff[0] = 0; frameCount = 10
image = BufferedImage@3834a1c8: type = 0 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_ColorSpace@4d2ef4c6 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 1903 height = 899 #numDataElements 3 dataOff[0] = 0
getFrame: 0; frames.size(): 10
;;; Resolution: 1903.0*899.0
java.lang.Exception: Invalid scale value (should be a float number greater than 0):0.0
at aspose.pdf.Image.setImageScale(Unknown Source)
at aspose.pdf.xml.ParagraphGenerator.getParagraphPosition(Unknown Source)
at aspose.pdf.xml.ImageGenerator.processImage(Unknown Source)
at aspose.pdf.xml.SectionGenerator.processSection(Unknown Source)
at aspose.pdf.xml.DocumentBase.a(Unknown Source)
at aspose.pdf.xml.GeneratorXml.generateDocument(Unknown Source)
at aspose.pdf.Pdf.save(Unknown Source)



If I remove scaling:

img1.getImageInfo().setFixWidth(
sec1.getPageInfo().getPageWidth() - sec1.getPageInfo().getMargin().getLeft()
- sec1.getPageInfo().getMargin().getRight());

then PDF is not generated with the following exception:

java.lang.NullPointerException
at com.aspose.ms.imagecodecs.tiff.spi.metadata.TiffImageMetadata.getStandardDocumentNode(Unknown Source)
at javax.imageio.metadata.IIOMetadata.getStandardTree(Unknown Source)
at com.aspose.ms.imagecodecs.tiff.spi.metadata.TiffImageMetadata.getAsTree(Unknown Source)
at com.sun.media.imageio.plugins.tiff.TIFFDirectory.createFromMetadata(TIFFDirectory.java:224)
at aspose.pdf.figure.Bitmap.readFrames(Unknown Source)
at aspose.pdf.figure.Bitmap.a(Unknown Source)
at aspose.pdf.figure.Bitmap.(Unknown Source)
at aspose.pdf.xml.ImageBase.a(Unknown Source)
at aspose.pdf.xml.ImageGenerator.processImage(Unknown Source)
at aspose.pdf.xml.SectionGenerator.processSection(Unknown Source)
at aspose.pdf.xml.DocumentBase.a(Unknown Source)
at aspose.pdf.xml.GeneratorXml.generateDocument(Unknown Source)
at aspose.pdf.Pdf.save(Unknown Source)

More finding!


I had to add two lines:

img1.getImageInfo().setSystemImage(ImageIO.read(file));
img1.getImageInfo().setTiffFrame(-1);

so in the end it is:
img1.getImageInfo().setSystemImage(ImageIO.read(file));
img1.getImageInfo().setFile(file.getAbsolutePath());
img1.getImageInfo().setImageFileType(ImageFileType.Tiff);
img1.getImageInfo().setTiffFrame(-1);

In the result I can create a PDF from a single page TIFF images, but I cannot scale it. Is there anyway to scale the document size to be the same as TIFF size?

Unfortunately it doesn’t work for multi page TIFFs. My TIFF with 10 pages has been converted into PDF with 4 pages. And again, image scaling is a must in that case.

Any help appreciated.

Thank you,
Mariusz

Hi Mariusz,

Thanks for sharing the details.

When converting TIFF image to PDF format, JAI is required for conversion. Furthermore, in order to convert Image files to PDF format, you need to specify the ImageFileType as you have stated in above code lines.

In the result I can create a PDF from a single page TIFF images, but I cannot scale it. Is there anyway to scale the document size to be the same as TIFF size?

In order to scale the image, please pass correct (non-negative) values to setFixWidth(…) and setFixHeight(…) methods.

Unfortunately it doesn’t work for multi-page TIFFs. My TIFF with 10 pages has been converted into PDF with 4 pages. And again, image scaling is a must in that case.

I think you are using the component in trial mode. When using the product in trial mode, there is a limitation of converting specific frames of TIFF image. You may consider requesting a 30 days temporary license to test our component without any limitations. For further details, please visit Get a temporary license

We are sorry for your inconvenience.

I’m not using the trial version, We have licences for all your products. And even with JAI and all the code you mentioned it doesn’t work. See my previous comments. Anyway, because this product seems to be very unstable and unreliable we decided to use iText and ImageMagick instead. Aspose.Words and Aspose.Cells handle the conversion to PDF very well. With Aspose.Slides we have strange issues, but with Aspose.Pdf we already got 10 different exceptions and no answer what to do with them :frowning:

Hi,


Sorry for the delayed response.

We have gone through your earlier posts in which you have indicated the problems/exceptions that you have been facing during TIFF to PDF conversion. However in your last posts, you have indicated that you have been able to resolve exceptions by adding JAI into class path of application. Furthermore, we are testing the scenario of TIFF to PDF conversion where only first four pages are being converted and images are not being scaled in resultant file.

We are sorry for this delay and inconvenience.

mariuszpala:
I need to convert a TIFF image into PDF. I found some example how to do that, but in the end I get the exception below. Can you help me solving that issue?

Exception:
java.util.NoSuchElementException
at javax.imageio.spi.FilterIterator.next(ServiceRegistry.java:808)
at javax.imageio.ImageIO$ImageReaderIterator.next(ImageIO.java:502)
at javax.imageio.ImageIO$ImageReaderIterator.next(ImageIO.java:487)
at aspose.pdf.figure.Bitmap.readFrames(Unknown Source)
at aspose.pdf.figure.Bitmap.a(Unknown Source)
at aspose.pdf.figure.Bitmap.(Unknown Source)
at aspose.pdf.xml.ImageBase.a(Unknown Source)
at aspose.pdf.xml.ImageGenerator.processImage(Unknown Source)
at aspose.pdf.xml.SectionGenerator.processSection(Unknown Source)
at aspose.pdf.xml.DocumentBase.a(Unknown Source)
at aspose.pdf.xml.GeneratorXml.generateDocument(Unknown Source)
at aspose.pdf.Pdf.save(Unknown Source)
Hi Mariusz,

Thanks for your patience.

I have tested the scenario and I am able to
notice the above stated problem. For the sake of correction, I have logged this issue
as PDFNEWJAVA-33706 in our issue tracking system. We will
further look into the details of this problem and will keep you updated on the
status of correction. Please be patient and spare us little time. We are sorry
for this inconvenience.

Hi Mariusz,


Thanks for your patience. Please note your above reported issue has been already fixed in Aspose.Pdf for Java 9.7.0 in result of some other fix. Please download and try latest version of Aspose.Pdf for Java it will help you to add TIFF image into PDF document.

Please feel free to contact us for any further assistance.

Best Regards,