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
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 using Aspose.PDF, you need to ensure that you are processing each frame of the TIFF file. The code you provided only adds the first frame because it does not iterate through all the frames in the TIFF.
Here’s an updated version of your function that handles multiple frames:
private static int addTiffAsPages(Document mergedDoc, String tiffPath, int currentPageIndex) {
try (FileInputStream imageStream = new FileInputStream(tiffPath)) {
// Load the TIFF image
TiffImage tiffImage = (TiffImage) Image.load(imageStream);
// Iterate through each frame in the TIFF
for (int i = 0; i < tiffImage.getFrameCount(); i++) {
Page newPage = mergedDoc.getPages().add();
// Get the specific frame
XImage xImage = newPage.getResources().getImages().add(tiffImage.getFrame(i));
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());
}
} catch (Exception e) {
e.printStackTrace();
}
return currentPageIndex + tiffImage.getFrameCount();
}
In this code:
- We load the TIFF image and iterate through each frame using
getFrameCount().
- For each frame, we create a new page in the PDF and add the frame as an image.
Ensure you include the necessary imports for handling images and pages in Aspose.PDF.
Can you help with the necessary imports and source for this code because I am getting the error
Cannot resolve method ‘getFrameCount’ in ‘TiffImage’
I TRIED THESE IMPORTS
import com.aspose.imaging.Image;
import com.aspose.imaging.fileformats.tiff.TiffImage;
@chetanepiq
Do you have license of Aspose.Imaging for Java as well? Please check and try to use below code snippet that only uses Aspose.PDF for Java to create a PDF with multipage TIFF image:
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 + "file_000001.tif");
java.awt.image.BufferedImage bImage = javax.imageio.ImageIO.read(new File(dataDir + "file_000001.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();
}
In case you still notice any issues, please share your sample image with us in .zip format. We will test the scenario in our environment and address it accordingly.