MULTIPLE PAGE TIFF FILE TO PDF

I want to merge a multipage tiff file to a pdf I used this function but only first frame of tiff got added , what do I need to change ,

we need to ensure that we are processing each frame of the TIFF file, can you suggest code where it will iterate properly over frames

WITH THE SOLUTION CAN YOU PLEASE SUGGEST THE REQUIRED IMPORTS

   private static int addTiffAsPages(Document mergedDoc, String tiffPath, int currentPageIndex) {
        try (FileInputStream imageStream = new FileInputStream(tiffPath)) {
            Page newPage = mergedDoc.getPages().add();
            newPage.getResources().getImages().add(imageStream);

            newPage.getContents().add(new GSave());
            
            Rectangle pageRect = newPage.getRect();
            Matrix matrix = new Matrix(new double[] {
                pageRect.getURX() - pageRect.getLLX(), 0, 0,
                pageRect.getURY() - pageRect.getLLY(),
                pageRect.getLLX(), pageRect.getLLY()
            });
            
            newPage.getContents().add(new ConcatenateMatrix(matrix));
            
            XImage ximage = newPage.getResources().getImages().get_Item(
                newPage.getResources().getImages().size()
            );
            newPage.getContents().add(new Do(ximage.getName()));
            newPage.getContents().add(new GRestore());
            

@chetanepiq

To merge a multipage TIFF file into a PDF and ensure that all frames are processed, you need to iterate through each frame of the TIFF file. Below is an example code snippet that demonstrates how to do this using Aspose.PDF for Java:

import com.aspose.pdf.Document;
import com.aspose.pdf.Page;
import com.aspose.pdf.XImage;
import com.aspose.pdf.Matrix;
import com.aspose.pdf.GSave;
import com.aspose.pdf.GRestore;
import com.aspose.pdf.ConcatenateMatrix;
import com.aspose.pdf.Rectangle;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class TiffToPdf {
    public static void main(String[] args) {
        String tiffPath = "path/to/your/multipage.tiff";
        String pdfPath = "path/to/output.pdf";
        Document mergedDoc = new Document();

        try {
            addTiffAsPages(mergedDoc, tiffPath);
            mergedDoc.save(pdfPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void addTiffAsPages(Document mergedDoc, String tiffPath) throws IOException {
        FileInputStream imageStream = new FileInputStream(tiffPath);
        BufferedImage[] images = ImageIO.read(new File(tiffPath));

        for (BufferedImage image : images) {
            Page newPage = mergedDoc.getPages().add();
            XImage xImage = newPage.getResources().getImages().add(imageStream);
            newPage.getContents().add(new GSave());

            Rectangle pageRect = newPage.getRect();
            Matrix matrix = new Matrix(new double[]{
                pageRect.getURX() - pageRect.getLLX(), 0, 0,
                pageRect.getURY() - pageRect.getLLY(),
                pageRect.getLLX(), pageRect.getLLY()
            });

            newPage.getContents().add(new ConcatenateMatrix(matrix));
            newPage.getContents().add(new Do(xImage.getName()));
            newPage.getContents().add(new GRestore());
        }
        imageStream.close();
    }
}

Required Imports:

Make sure to include the following imports in your Java file:

import com.aspose.pdf.Document;
import com.aspose.pdf.Page;
import com.aspose.pdf.XImage;
import com.aspose.pdf.Matrix;
import com.aspose.pdf.GSave;
import com.aspose.pdf.GRestore;
import com.aspose.pdf.ConcatenateMatrix;
import com.aspose.pdf.Rectangle;

Note:

  • Ensure you have the necessary Aspose.PDF library in your project.
  • The above code assumes that the TIFF file can be read as an array of BufferedImage. You may need to adjust the image reading logic based on your specific TIFF handling library.

This approach will allow you to add each frame of the TIFF file as a separate page in the PDF document.