Converting tif image to pdf gives a wrong color

Hi,


I’am making an application were different image types are converted to a pdf. But when I do that every (so far) image will end up in de pdf with other colors. See the attached files. (it happens also with jpg and bmp files).

I’am using the following code:

public void aposeImageToPdfTest() throws IOException {
try (InputStream inputStream = this.getClass().getResourceAsStream(“test image_tif.tif”)) {
com.aspose.imaging.Image image = com.aspose.imaging.Image.load(inputStream);
BufferedImage bufferedImage = com.aspose.imaging.extensions.ImageExtensions.toJava(image);
LOGGER.debug(“Image info height:” + bufferedImage.getHeight() + " width:" + bufferedImage.getWidth());

com.aspose.pdf.Image pdfImage = new com.aspose.pdf.Image();
pdfImage.setBufferedImage(bufferedImage);

com.aspose.pdf.Document doc = new com.aspose.pdf.Document();
com.aspose.pdf.Page page = doc.getPages().add();
page.getParagraphs().add(pdfImage);

doc.save(new File(tmpDir, “test.pdf”).getAbsolutePath(), SaveFormat.Pdf);
}
}

Hi Gerard,

Thank you for your inquiry.

This is to update you that Raster images (TIFF, PNG, JPEG etc) to PDF conversion is not supported by Aspose.Imaging. Converting raster images to PDF can be done using Aspose.Pdf. Consider the following code snippet to convert image to PDF.

// Instantiate Document Object
com.aspose.pdf.Document doc = new com.aspose.pdf.Document();
// Add a page to pages collection of document
com.aspose.pdf.Page page = doc.getPages().add();
// Load the source image file to Stream object
java.io.FileInputStream fs = new java.io.FileInputStream("test_image_tif.tif");
// 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);
page.setCropBox(new com.aspose.pdf.Rectangle(0, 0, 400, 400));
// Create an image object
com.aspose.pdf.Image image1 = new com.aspose.pdf.Image();
// Add the image into paragraphs collection of the section
page.getParagraphs().add(image1);
// Set the image file stream
image1.setImageStream(fs);
// Save resultant PDF file
doc.save("F:\\Ctrash\\Input\\test_image_tif.pdf");

Following is the related URL to the online documentation:


Hi Ikram,


Thank you for your reply.

I want to scale the image based on its size so it fits correctly in the pdf. To calculate the scale I need the size of the image. How can I get the size of an image from a com.aspose.pdf.Image object?

With kind regards,

Gerard van der Hoorn
Hi Gerard,

Thank you for writing us back.

Again refer to the online documentation link Convert an Image to PDF. You can use the BufferedImage class to get the image dimensions. Following is the sample code snippet for your reference.

// create BufferedImage instance
java.awt.image.BufferedImage bufferedImage = javax.imageio.ImageIO.read(new java.io.File("source.gif"));
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();