Problems converting TIFF to PDF

I’m trying to convert a TIFF image to PDF using setImageStream. The result does not correspond to the original imgage showng some strange pattern.

Here is the source code:

            Document doc = new Document();
            Page page = doc.getPages().add();
            page.getPageInfo().getMargin().setBottom(0);
            page.getPageInfo().getMargin().setTop(0);
            page.getPageInfo().getMargin().setLeft(0);
            page.getPageInfo().getMargin().setRight(0);
            page.getPageInfo().setWidth(w);
            page.getPageInfo().setHeight(h);

            Image image1 = new Image();
            page.getParagraphs().add(image1);
            java.io.FileInputStream fs = new java.io.FileInputStream(in);
            image1.setImageStream(fs);
            fs.close();

            doc.save(out.toString());
            doc.close();

The problematic tiff file can be downloaded from this link: https://v16-test.triboni.cloud/test/img.tif

The result can be downloaded from here: https://v16-test.triboni.cloud/test/img.pdf

Other files work.

@martin.duerig

Thanks for contacting support.

We were able to replicate the issue in our environment using latest version of the API. Hence, logged it as PDFJAVA-37884 in our issue tracking system. We will further investigate the issue in details and keep you informed with the status of its correction. Please be patient and spare us little time.

We are sorry for the inconvenience.

Im having some problem as well. the size of the tiff image is reduced in the pdf a lot … looks like the pdf has some margins and it is very small compared with the Original image

@comaschi

The issue was about the wrong color pattern of TIFF image inside output PDF and it has been resolved in latest version of the API. Would you kindly share your sample TIFF image with complete code snippet that you are using. We will test the scenario in our environment and address it accordingly.

Hi Asad,
I am sending the tiffs and the Pdfs
Pease check the size page on tiff are no same in the PDF…

public static void tiffToPdf(String src, String dest) {

	if (checkLic()) {

		Document pdf = new Document();
		// Create a section in the Pdf object
		Page sec = pdf.getPages().add();
		// Create an image object in the section
		Image img = new Image();
		// Add image object into the Paragraphs collection of the section
		sec.getParagraphs().add(img);
		// Set the path of image file
		img.setFile(src);
		img.setInNewPage(true);
		// Save the Pdf
		pdf.save(dest);
		pdf.close();


	}

}

Multiff.zip (2.8 MB)

@comaschi

Could you please try using the below code snippet at your end and let us know if you still notice any issue with the sizing:

Document pdf1 = new Document();

FileStream ms = new FileStream(dataDir + "Page_1_2_3.tiff", FileMode.Open);
Bitmap myimage = new Bitmap(ms);
FrameDimension dimension = new FrameDimension(myimage.FrameDimensionsList[0]);
int frameCount = myimage.GetFrameCount(dimension);

for (int frameIdx = 0; frameIdx <= frameCount - 1; frameIdx++)
{
 Page sec = pdf1.Pages.Add();
 myimage.SelectActiveFrame(dimension, frameIdx);
 MemoryStream currentImage = new MemoryStream();
 myimage.Save(currentImage, ImageFormat.Tiff);
 if (myimage.Width > myimage.Height)
 {
  sec.PageInfo.IsLandscape = true;
 }
 else
 {
  sec.PageInfo.IsLandscape = false;
 }
 sec.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);
 sec.PageInfo.Height = myimage.Height;
 sec.PageInfo.Width = myimage.Width;
 Aspose.Pdf.Image imageht = new Aspose.Pdf.Image();
 imageht.ImageStream = currentImage;
 sec.Paragraphs.Add(imageht);
}
pdf1.Save(dataDir + "TifftoPDF.pdf");
pdf1 = new Document(dataDir + "TifftoPDF.pdf");

TifftoPDF.pdf (1.5 MB)

Bitmap class from java.awt.image.BufferedImage?
FrameDimension class?

imageht.ImageStream cannot be resolved
Page sec = pdf1.Pages.Add(); Pages cannot be resolved.
sec.PageInfo.IsLandscape = true; PageInfo cannot be resolved
sec.Paragraphs.Add(imageht); Paragraphs cannot be resolved

Using apose.pdf-20.10.jar

@comaschi

We are sorry for the confusion.

The code snippet in our previous post was about .NET API. Please check following code snippet which uses Aspose.Imaging along with the Aspose.PDF in order to generate PDF file from TIFF with correct height and width:

public void TiffToPdf()
    {
        System.out.println("Starting");
        try {
            long totalStart = System.currentTimeMillis();
            com.aspose.imaging.fileformats.tiff.TiffImage image = (com.aspose.imaging.fileformats.tiff.TiffImage)com.aspose.imaging.Image.load(new ByteArrayInputStream(FileUtils.readFileToByteArray(new File(dataDir + "Page_1_2_3.tiff"))));
            Document document = new Document();
            for (com.aspose.imaging.fileformats.tiff.TiffFrame frame : image.getFrames()) {
                com.aspose.pdf.Image pdfImage = frameToImage(frame);
                addImageToPage(document, pdfImage, frame.getWidth(), frame.getHeight(), frame.getHorizontalResolution(), frame.getVerticalResolution());
            }

            document.save(dataDir + "output.pdf");

            // release resources
            document.freeMemory();
            document.dispose();
            long totalEnd = System.currentTimeMillis();
            System.out.println("Total time taken was "+ (totalEnd-totalStart)/1000 + " seconds for conversion");
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    private com.aspose.pdf.Image frameToImage(com.aspose.imaging.fileformats.tiff.TiffFrame frame) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        com.aspose.imaging.imageoptions.TiffOptions options = new com.aspose.imaging.imageoptions.TiffOptions(com.aspose.imaging.fileformats.tiff.enums.TiffExpectedFormat.TiffNoCompressionRgb);
        frame.save(stream, options);

        com.aspose.pdf.Image image = new com.aspose.pdf.Image();
        image.setImageStream(new ByteArrayInputStream(stream.toByteArray()));
        return image;
    }

    private Page addImageToPage(Document document, com.aspose.pdf.Image image, int width, int height, double horizontalResolution, double verticalResolution) {
        Page page = document.getPages().add();
        width = (int) (width * (72.0 / horizontalResolution));
        height = (int) (height * (72.0 / verticalResolution));

        page.getPageInfo().setMargin(new MarginInfo(0,0,0,0));
        page.getPageInfo().setWidth(width);
        page.getPageInfo().setHeight(height);

        page.getParagraphs().add(image);
        return page;
    }

com.aspose.pdf.internal.imaging should be the same … correct?

Asad, It works as expected … THANK YOU!!!

Asad,
Is there any chance to compress in the same conversion process?
I saw that I have to compress the pdf after the document is saved using OptimizationOptions.
It should be upload twice the document in memory and I’d like to avoid to do that for performance perspective

Thanks
Diego

@comaschi

The API keeps document content and updated collections in the memory until it is saved. The final structure and size of the document is decided at the time of saving. Which is why optimizing the document without saving may give undesired results. However, you can use Document.ProcessParagraphs() method which process and allocates all memory content to the document in process. Please try to use below code snippet before saving the document:

document.processParagraphs();
// Set RemoveUsedObject option
com.aspose.pdf.optimization.OptimizationOptions optimizeOptions = new com.aspose.pdf.optimization.OptimizationOptions();
optimizeOptions.setRemoveUnusedObjects(true);
optimizeOptions.setSubsetFonts(true);
optimizeOptions.setLinkDuplcateStreams(true);
optimizeOptions.getImageCompressionOptions().setCompressImages(true);
optimizeOptions.getImageCompressionOptions().setImageQuality(50);
optimizeOptions.getImageCompressionOptions().setResizeImages(true);
// Optimize PDF document using OptimizationOptions
document.optimizeResources(optimizeOptions);
document.save(dataDir + "output.pdf");

output.pdf (531.7 KB)
output_notopt.pdf (1.5 MB)

Thank you Asad!!!
Works Great!!!

1 Like