How do i convert a Multipage TIFF to a PDF

Hi

How do i convert a Multipage TIFF to a PDF

@david.hancock.imagef

Thanks for contacting support.

Please use following code snippet, in order to convert multi-page TIFF into PDF.

Document pdf1 = new Document();
MemoryStream ms = new MemoryStream();
new FileStream(dataDir + @"WO02006172A1.tif", FileMode.Open).CopyTo(ms);
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);

 Aspose.Pdf.Image imageht = new Aspose.Pdf.Image();
 imageht.ImageStream = currentImage;
 sec.Paragraphs.Add(imageht);
}
pdf1.Save(dataDir + "TifftoPDF.pdf");

In case you face any issue, please share your sample TIFF file with us, so that we can test the scenario in our environment and address it accordingly.

Hello,

using this code i can only convert the first page to PDF. Is it possible that there is an issue with the code ?

@panosk

Thanks for getting back to us.

We have tested above code snippet with one of our sample TIFF images and found it correct. Would you please share your sample TIFF image with us. We will test the scenario in our environment and address it accordingly.

How can I do the same in Aspose.Pdf Java? I’m using 19.4 version.

@pjanardhan

Please use following Java code to achieve functionality:

try {
     Document doc = new Document();
     // Add a page to pages collection of document
     Page page = doc.getPages().add();
     // Load the source image file to Stream object
     FileInputStream fs = new FileInputStream(dataDir + "input.TIF");

     java.awt.image.BufferedImage bImage = null;
     bImage = javax.imageio.ImageIO.read(new File(dataDir + "input.TIF"));

     page.getPageInfo().setHeight(bImage.getHeight());
     page.getPageInfo().setWidth(bImage.getWidth());
     // Set margins so image will fit, etc.
     page.getPageInfo().getMargin().setBottom(0);
     page.getPageInfo().getMargin().setTop(0);
     page.getPageInfo().getMargin().setLeft(0);
     page.getPageInfo().getMargin().setRight(0);
     Image image1 = new Image();
     // Add the image into paragraphs collection of the section
     page.getParagraphs().add(image1);
     // Set the image file stream
     image1.setImageStream(fs);

     doc.save(dataDir + "output.pdf");
}catch (Exception ex){
     ex.printStackTrace();
}

Assuming the source tiff is “input.TIF”, what is “12-1735-0616-8-003.TIF”?

@upacheco

We are sorry for the confusion.

Both names should be of same TIFF image. We have now edited the code snippet in our previous reply.

java.lang.NullPointerException on page.getPageInfo().setHeight(bImage.getHeight());

@comaschi

Would you please share some sample file and code snippet with more details of the issue you are facing. We will test the scenario in our environment and address it accordingly.

same code that you posted

public static void tiffToPdf(String src, String dest) {
	try {
		Document doc = new Document();
		// Add a page to pages collection of document
		Page page = doc.getPages().add();
		// Load the source image file to Stream object
		FileInputStream fs = new FileInputStream(src);

		java.awt.image.BufferedImage bImage = null;
		bImage = javax.imageio.ImageIO.read(new File(src));

		page.getPageInfo().setHeight(bImage.getHeight());
		page.getPageInfo().setWidth(bImage.getWidth());
		// Set margins so image will fit, etc.
		page.getPageInfo().getMargin().setBottom(0);
		page.getPageInfo().getMargin().setTop(0);
		page.getPageInfo().getMargin().setLeft(0);
		page.getPageInfo().getMargin().setRight(0);
		Image image1 = new Image();
		// Add the image into paragraphs collection of the section
		page.getParagraphs().add(image1);
		// Set the image file stream
		image1.setImageStream(fs);

		doc.save(dest);
	} catch (Exception ex) {
		ex.printStackTrace();
	}
	
}

@asad.ali
Hey there! I found the problem javax.imageio.ImageIO.read for TIFF files are supported in JV9.
so the solution for previous JV version is the extension jai-imageio

@comaschi

It is nice to hear that you managed to resolve your issue. Please keep using our API and in case you need further assistance, please feel free to let us know.

Thanks in advance for help us in this regards!
We are also using same code to convert TIF to PDF. Below is the error.
Exception in thread “main” com.aspose.pdf.internal.ms.System.z113: FrameworkException: Not enough memory to process jpeg file.
We are using following versions.

com.aspose
aspose-pdf
16.11.0


com.aspose
aspose-imaging
20.2
jdk16
jar

@NaeemJamil

Would you please try using latest version of the API and also try increasing Java Heap Size enough to process large files. In case you still face any issue, please share your sample file with us. We will test the scenario in our environment and address it accordingly.