Free Support Forum - aspose.com

Aspose Java issue with Tiff to Pdf

I’m trying to convert the following tiff to a pdf.

./Aspose_Slides_Java/Examples/src/main/resources/com/aspose/slides/examples/Presentation/PresentationToTIFF/customsize/demo.tiff (I can't upload the tiff as the file update for the forum prevents adding tiff attachments)


From another forum post I've got two version but both have issues;

Using the document version I get two pages in the PDF, using the pdf version I get the 1st page of the TIFF in the 1st page of the PDF but not a 2nd page for the 2nd page in the TIFF.

Also the width and height don't match the original TIFF. How do I change each pdf page to match that of the TIFF width and size?


Using Document;

public ConvertTiffToPdfViaDocument(final String sourceImageFile, final String outputPdfFile) {

// instantiate Document object

com.aspose.pdf.Document document = new com.aspose.pdf.Document();

// add page to PDF file

document.getPages().add();

// create Image object

com.aspose.pdf.Image image = new com.aspose.pdf.Image();

// load TIFF image

image.setFile(sourceImageFile);

// add image to paragraphs collection of first page

document.getPages().get_Item(1).getParagraphs().add(image);

// save resultant PDF

document.save(outputPdfFile);

}


Using PDF;

public ConvertTiffToPdfViaPDF(final String sourceImageFile, final String outputPdfFile) {

aspose.pdf.Pdf pdf = new aspose.pdf.Pdf();

aspose.pdf.Section section = pdf.getSections().add();

aspose.pdf.Image image = new aspose.pdf.Image(section);

// java.net.URL url = new java.net.URL(“file:///D:/pdftest/TIF.tif”);

// BufferedImage bufferImage=ImageIO.read(url);

image.getImageInfo().setFile(sourceImageFile);

image.getImageInfo().setTiffFrame(-1);

image.getImageInfo().setImageFileType(aspose.pdf.ImageFileType.Tiff);

section.getParagraphs().add(image);

pdf.save(outputPdfFile);

}


Hi John,


Thanks for contacting support.

com.aspose.pdf.Document is the recommended approach for PDF file creation as well as manipulation. Now concerning to setting PDF pages width equal to TIFF image dimensions, once you have loaded TIFF image to com.aspose.pdf.Image object, you can use its Width and Height information to set PDF file page dimensions. In case you still face any issue, please share the resource TIFF image (rename the file extension or archive it and try uploading to forums), so that we can test the scenario in our environment. We are sorry for your inconvenience.

com.aspose.pdf.Image doesn’t seam to have any methods that return the image size.

image.getFixHeight() and image.getFixWidth() are the only two that mention Height and Width but they are both returning 0.0.

Regarding the tiff that I’m trying to convert to pdf, it’s the one that is the supplied by the Aspose_Slides_Java github project.

https://raw.githubusercontent.com/asposeslides/Aspose_Slides_Java/blob/master/Examples/src/main/resources/com/aspose/slides/examples/Presentation/PresentationToTIFF/customsize/demo.tiff

Hi John,


Yes you are correct. Unless the PDF file is saved, the image or other paragraph objects are not placed inside the document, so if you try to get their properties, a value of 0 is being returned. However even calling the methods after file save, the value of 0 is being returned. For the sake of correction, I have logged it as PDFNEWJAVA-35156 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.