Free Support Forum - aspose.com

Keep aspect ratio of pages when converting Tiff to Pdf

My company recently purchased another Aspose product, and it seems to work great. We now stumbled upon a use-case for which I can’t find an apparent solution - when converting a multipage tiff file to pdf, the individual pages seem to get stretched in the resulting pdf. We use the DOM-based API, heavily borrowed from here.

Document doc = new Document()
OutputStream outputStream = new OptimizedByteArrayOutputStream()

// Create a new page & set  margins so image will fit
Page page = doc.getPages().add()
page.pageInfo.margin.bottom = 0
page.pageInfo.margin.top = 0
page.pageInfo.margin.left = 0
page.pageInfo.margin.right = 0

// Create an image object
Image image = new Image()
image.setInNewPage(true)
image.setImageStream(sourceFileContent)

// Add the image into paragraphs collection of the section
page.getParagraphs().add(image)

doc.save(outputStream)

As an example tiff file you can use this one, as we use it also in our tests.

Kind regards,
Milan

@milanovwest

Thank you for contacting support.

I have worked with the data shared by you and have tried to set page size by getting dimensions of TIFF image with Aspose.Imaging for Java API but the page size is not getting set as per your requirements. A ticket with ID PDFJAVA-37499 has been logged in our issue management system for further investigation and resolution. The issue ID has been linked with this thread so that you will receive notification as soon as the issue is resolved.

We are sorry for the inconvenience.

@milanovwest

Thank you for being patient.

We have investigated the issue reported by you and would like to share with you that we have modified following code snippet to make it work as per your requirements.

com.aspose.imaging.fileformats.tiff.TiffImage imageTemp = (com.aspose.imaging.fileformats.tiff.TiffImage)com.aspose.imaging.Image.load("c:/pdftest/sample.tif");

int framesCount = imageTemp.getFrames().length;

int maxHeight = 0;
int maxWidth = 0;


int defaultDPI = 72;    

for (int i = 0; i < framesCount; i++) {
   com.aspose.imaging.fileformats.tiff.TiffFrame frame = imageTemp.getFrames()[i];

    if (frame.getHeight() > maxHeight) {
       maxHeight = frame.getHeight();
    }

    if (frame.getWidth()> maxWidth) {
       maxWidth = frame.getWidth();
    } 
}

double verticalResolution = imageTemp.getVerticalResolution();
if (verticalResolution == 0) {
    verticalResolution = 300;
}

double horizontalResolution = imageTemp.getHorizontalResolution();
if (horizontalResolution == 0) {
    horizontalResolution = 300;
}

System.out.println(maxHeight + " " + maxWidth);
double scaledHeight = maxHeight * defaultDPI / verticalResolution ;
double scaledWidth = maxWidth * defaultDPI / horizontalResolution;

Document doc = new Document();
Page page = doc.getPages().add();
Image image = new Image();
page.getPageInfo().getMargin().setBottom(0);
page.getPageInfo().getMargin().setTop(0);
page.getPageInfo().getMargin().setLeft(0);
page.getPageInfo().getMargin().setRight(0);

//---  Same size in inches ---
page.getPageInfo().setHeight(scaledHeight); 
page.getPageInfo().setWidth(scaledWidth);

//--- Or same size in pixels ---
// page.getPageInfo().setHeight(maxHeight);
// page.getPageInfo().setWidth(maxWidth);

InputStream isImage = new FileInputStream("c:/pdftest/sample.tif");
image.setImageStream(isImage);
page.getParagraphs().add(image);
doc.save("c:/pdftest/sample_sample.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.