Hi Team,
I am converting a multipage TIFF file to PDF using Aspose for Java(we are using Aspose total). The conversion works, but the generated PDF doesn’t exactly match the original TIFF — there’s a mismatch in width and scaling (fonts and layout appear slightly different), please find the code attachment below.
private void convertTiffToPdf(File tempFile, String fileName, OutputStream outputStream, List<Long> pages)
throws IOException {
try (Document pdfDocument = new Document();
ImageInputStream imageInputStream = ImageIO.createImageInputStream(tempFile)) {
ImageReader reader = ImageIO.getImageReadersByFormatName("tiff").next();
reader.setInput(imageInputStream, false, false);
int numPages = reader.getNumImages(true);
pages.add((long) numPages);
for (int i = 0; i < numPages; i++) {
BufferedImage bufferedImage = reader.read(i);
Page page = pdfDocument.getPages().add();
com.aspose.pdf.PageInfo pageInfo = page.getPageInfo();
pageInfo.setWidth(bufferedImage.getWidth());
pageInfo.setHeight(bufferedImage.getHeight());
com.aspose.pdf.Image image = new com.aspose.pdf.Image();
File tempImageFile = File.createTempFile("tiff_page", ".png");
try {
ImageIO.write(bufferedImage, "png", tempImageFile);
try (InputStream imageInputStreamForPage = new FileInputStream(tempImageFile)) {
image.setImageStream(imageInputStreamForPage);
page.getParagraphs().add(image);
}
} finally {
bufferedImage.flush();
MediaUtils.silentlyDeleteTempFile(tempImageFile);
}
}
reader.dispose();
pdfDocument.save(outputStream);
}
}
@Debasisha
Summary
The size mismatch occurs because the TIFF pages are first converted to PNG images and the pixel dimensions of those PNGs are used directly as PDF page dimensions. PDF pages are measured in points (1 pt = 1/72 in). When the original DPI information from the TIFF is lost during the PNG conversion, the PDF engine interprets the pixel values as points, causing the pages to appear larger or smaller than the source images.
Why it happens
BufferedImage.getWidth() / getHeight() return pixel counts.
pageInfo.setWidth() and setHeight() expect values in points, not pixels.
- Converting the TIFF to PNG discards the DPI metadata that would allow the correct conversion from pixels to physical size.
- Because a TIFF is a raster image, there are no fonts to change; the perceived “font change” is simply the result of scaling the raster image.
What to do next
- Avoid the intermediate PNG conversion – load each TIFF page with Aspose.Imaging (or Java ImageIO) and pass the image data directly to Aspose.PDF.
- Read the DPI of each TIFF page using
frame.getHorizontalResolution() and frame.getVerticalResolution().
- Convert pixel dimensions to PDF points (1 pt = 1/72 in):
// pixelWidth / pixelHeight are the dimensions of the frame
// dpiX / dpiY are the horizontal and vertical DPI values
double widthPt = (pixelWidth * 72.0) / dpiX;
double heightPt = (pixelHeight * 72.0) / dpiY;
- Create a PDF page with those point dimensions and add the image without any additional scaling.
Below is a minimal, self‑contained Java example that follows these steps.
import com.aspose.pdf.*;
import com.aspose.imaging.*;
import com.aspose.imaging.fileformats.tiff.*;
import java.io.*;
public class TiffToPdfExact {
public static void convertTiffToPdf(File tiffFile, OutputStream out) throws IOException {
// Load the multi‑page TIFF
try (Image image = Image.load(tiffFile)) {
TiffImage tiff = (TiffImage) image; // safe cast for TIFF
Document pdf = new Document();
// Process each frame (page) of the TIFF
for (int i = 0; i < tiff.getFrames().size(); i++) {
ImageFrame frame = tiff.getFrames().get_Item(i);
// Retrieve DPI; fall back to a reasonable default if missing
double dpiX = frame.getHorizontalResolution();
double dpiY = frame.getVerticalResolution();
if (dpiX <= 0) dpiX = 96;
if (dpiY <= 0) dpiY = 96;
// Pixel dimensions of the frame
int pixelW = frame.getWidth();
int pixelH = frame.getHeight();
// Convert to PDF points (1 pt = 1/72 inch)
double widthPt = (pixelW * 72.0) / dpiX;
double heightPt = (pixelH * 72.0) / dpiY;
// Add a new PDF page with the calculated size
Page pdfPage = pdf.getPages().add();
pdfPage.getPageInfo().setWidth(widthPt);
pdfPage.getPageInfo().setHeight(heightPt);
// Export the frame to a memory stream.
// PNG preserves DPI metadata; you may also use BMP or TIFF.
try (ByteArrayOutputStream imgStream = new ByteArrayOutputStream()) {
frame.save(imgStream, new PngOptions());
imgStream.flush();
// Insert the image into the PDF page
com.aspose.pdf.Image pdfImg = new com.aspose.pdf.Image();
pdfImg.setImageStream(new ByteArrayInputStream(imgStream.toByteArray()));
pdfImg.setImageScale(1); // no extra scaling
pdfPage.getParagraphs().add(pdfImg);
}
}
// Save the resulting PDF
pdf.save(out);
}
}
}
Checklist before running
- Add Maven/Gradle dependencies for
aspose-pdf and aspose-imaging.
- Confirm that the source TIFF contains DPI information (most scanners embed it).
- If a page reports DPI = 0, replace it with a sensible default (e.g., 96 dpi) as shown.
- Validate the output: open the generated PDF, view the page properties, and verify that the physical dimensions match those of the original TIFF pages.
By preserving the original DPI and converting pixels to points correctly, the PDF pages will retain the same visual size as the source TIFF pages, eliminating the width/scale discrepancies you observed. If further differences appear, re‑examine the DPI values reported by the TIFF and ensure the PDF viewer respects the page size metadata.
I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.
we are using aspose java version : 22.9
@Debasisha,
Since you are utilizing the Aspose.PDF for Java API for your task, I am transferring your thread to the relevant category where a member of the Aspose.PDF team will review your issue thoroughly and provide you with better assistance shortly.
@Debasisha
Would you kindly try using the latest version of the API and in case issue still persists, we request you please share your sample TIFF image in .zip format so that we can test the scenario in our environment and address it accordingly.
@amjad.sahi we are using aspose total including (pdf and image). i am unable to upload test TIFF files. so i uploaded only one converted pdf file.
please let me know, if any other details needed.
@Debasisha,
You may zip the TIFF files and attach the zipped archive here. Aspose.PDF team will look into it soon.