We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert PDF to Image with aspose imaging

Hello,

I need to convert a PDF file to an Image File, until now I used Aspose.PDF.
My problem is, I have to resize the created JPEG I can do that with Aspose.PDF passing the JpegDevice a size!
My problem is, I dont know how BIG is the original size, and i will pass the JpegDevice only the width value, and the high value should be the result of manting the relation between width and height setting only the width value.

There are not many things that I cand do on Image with Aspose.PDF library, so I tried to do the same thing using Aspose.Imaging.

On your product description you wirte Import/Export PDF, but I found nothing how to do this.

So can I export a PDF file into JPEG with Aspose.Imaging.

And what is the best way to create a new JPEG starting from a PDF setting only the WIDTH, RESOLUTION and the JPEG QUALITY for the new created JPEG??

thx
Michael



Hi Michael,


Thank you for using Aspose products.

I am afraid, Aspose.Imaging for Java API does not support loading PDF files for conversion to raster image formats such as Jpeg. The API currently supports conversion of DXF (AutoCAD) format to PDF only. If you are interested in setting different properties for Aspose.Pdf generated Jpeg image, I would be happy to share more details in this regard.

Please provide a link to point us where you have read that Aspose.Imaging APIs can import PDF files, so we could look into that information in order to make any amendments, if required.

On the image here
http://www.aspose.com/java/imaging-component.aspx
I understand I can Import AND export all this Files in each other directions!!
Se also attached file for better understanding:

So for my problem, if you can help me I am very happy:

I do this with Aspose.PDF


License licPdf = new License();
licPdf.setLicense(…);

OutputStream imageStreamThumb = new java.io.FileOutputStream("/tmp/large.jpg");

Resolution resolution = new Resolution(200);

JpegDevice jpegDeviceLarge = new JpegDevice(???, ???, resolution, 75);

Document pdfDocument = new com.aspose.pdf.Document(byte[] …);

jpegDeviceLarge.process(pdfDocument.getPages().get_Item(1), imageStreamLarge);

imageStreamLarge.close();


I have here two problems:
a)
How can I resize the JPEG by setting only the WIDTH, the HEIGHT should by calcolated in automatic mantaining the original relation between w and h.

b)
The other thing, I think you have a BUG on your libs! Stting the Resolution to 200DPI does not set the Resolution of 200DPI in the META INF of the JPEG! Open the created JPEG in a Imaging programm shows my the always the resolution of 72x72 dpi, and that is wrong!

can you help me?

thx
Michael


Hi Michael,


Thank you for writing back.

The diagram you have mentioned in your post provides a high level overview of the API features. On the same page you will find another diagram under the heading Supported File Formats that explicitly describes what file formats are supported for import and/or export Moreover, you can find the similar information in the Key Features tab.

1) As discussed earlier, Aspose.Imaging for Java cannot load PDF files, although if you wish to re-size the previously generated Jpeg images, Aspose.Imaging API can handle this task pretty well. The simplest code to re-size an existing image is as follow,

Java

com.aspose.imaging.Image image = com.aspose.imaging.Image.load(myDir + “sample.jpg”);
image.resize(newWidth, newHeight);
image.save();

The above code requires both Height and Width to re-size the image accordingly. If you wish to set a particular width or height on an image whilst maintaining the original proportions. In such a case you have to calculate the appropriate height or width by determining the ratio between the original and the desired width/height and applying that to both. You can achieve this on your own or we can perform some tests to provide you a generic formula to calculate the ratio.

2) The problem related to the resolution of Jpeg image is in relevance to the Aspose.Pdf component. I have requested my fellow colleague from the concerned department to comment on your observation. Although if you require to change the resolution of your existing images, you can opt the following approach.

Java
com.aspose.imaging.fileformats.jpeg.JpegImage image = (com.aspose.imaging.fileformats.jpeg.JpegImage)com.aspose.imaging.Image.load(myDir + "sample.jpg"); image.setResolution(dpiX, dpiY); image.save();

michael.burger@siag.it:

I do this with Aspose.PDF


License licPdf = new License();
licPdf.setLicense(…);

OutputStream imageStreamThumb = new java.io.FileOutputStream("/tmp/large.jpg");

Resolution resolution = new Resolution(200);

JpegDevice jpegDeviceLarge = new JpegDevice(???, ???, resolution, 75);

Document pdfDocument = new com.aspose.pdf.Document(byte[] …);

jpegDeviceLarge.process(pdfDocument.getPages().get_Item(1), imageStreamLarge);

imageStreamLarge.close();

I have here two problems:
a)
How can I resize the JPEG by setting only the WIDTH, the HEIGHT should by calcolated in automatic mantaining the original relation between w and h.
Hi Michael,

Thanks for using our API’s.

When using Aspose.Pdf for Java to convert PDF pages to JPEG format, you may consider using an over load of com.aspose.pdf.devices.JpegDevice(…) which takes Width, Height, Resolution and Quality parameters as arguments. So as per your requirement, you can load the PDF file into Document object, get PDF file Width and Height information and use the same information as argument to JpegDevice(…) constructor.

But I am afraid in current release of Aspose.Pdf for Java, there seems to be an issue while getting page dimensions information. For all the PDF files, a similar value of Page Height and Width is being returned. For
the sake of correction, I have logged it in our issue tracking system as PDFNEWJAVA-34178.

[Java]

Document pdfDocument = new com.aspose.pdf.Document(“c:/pdftest/sample.pdf”);<o:p></o:p>

System.out.println("page Height = " +pdfDocument.getPageInfo().getHeight());

System.out.println("page Width = " +pdfDocument.getPageInfo().getWidth());

com.aspose.pdf.devices.Resolution resolution = new com.aspose.pdf.devices.Resolution(300);

com.aspose.pdf.devices.JpegDevice jpegDeviceLarge = new com.aspose.pdf.devices.JpegDevice((int)pdfDocument.getPageInfo().getWidth(), (int)pdfDocument.getPageInfo().getHeight(), resolution, 100);

jpegDeviceLarge.process(pdfDocument.getPages().get_Item(1), "c:/pdftest/ImageConversion.jpeg");

michael.burger@siag.it:
b)
The other thing, I think you have a BUG on your libs! Stting the Resolution to 200DPI does not set the Resolution of 200DPI in the META INF of the JPEG! Open the created JPEG in a Imaging programm shows my the always the resolution of 72x72 dpi, and that is wrong!
I have tested this scenario and have observed that whatever value we specify for Resolution object, the resultant image is always in 96 dpi. For the sake of correction, I have separately logged it in our issue tracking system as PDFNEWJAVA-34179. We will investigate these issues in details and will keep you updated on the status of a correction.

We are sorry for this inconvenience.

So for now I can’t calculate me the right dimension, neither I can save a image with the right resolution?

Right?

Hi Michael,


Yes your understanding is correct. For both scenarios, I have separately logged problems in our issue tracking system. As soon as we have some further updates regarding their resolution, we will let you know within forum thread.

Sorry for my hard words

But in German we say something like that:

If Aspose.Words is like heaven, Aspose.Pdf is like hell.

I do not use many function with .Pdf but I found uncountable errors, for really basic functions!

:frowning:

Ok there is the problem with Aspose PDF and resolution!

But if I try to change the meta information for the resolution by hand as you described, it doesn’t work to:


image.setResolution(200,200);


Image has always resoltuion by 72x72

Doing this

com.aspose.imaging.fileformats.jpeg.JpegImage imageLarge = (com.aspose.imaging.fileformats.jpeg.JpegImage)com.aspose.imaging.Image.load("/tmp/x/ddaLarge.jpg");
imageLarge.setResolution(200, 200);
System.out.println(imageLarge.getSize().getWidth());
System.out.println(imageLarge.getSize().getHeight());
System.out.println(imageLarge.getVerticalResolution());
System.out.println(imageLarge.getHorizontalResolution());
imageLarge.save(new FileOutputStream("/tmp/x/ddaLargeNew.jpg"));


getVerticalResoltuion() and getHorizontalResoltuion returns 200
BUT opening the image with any programm says me the resoltuio is 72x72
so I think you do not write the resolution meta inf to the right way?

Hi Michael,


Please provide the sample image so we could evaluate the problem further in order to propose a solution with Aspose.Imaging for Java APIs.

Sorry for the inconvenience.

The File ddaLarge.jpg in the attachement is a JPEG createt by a Resolution of 200dpi,
but as we know the created Image does always have 96dpi

So as suggested by you I use Aspose.Imaging to manipulate the Resolution by that

com.aspose.imaging.fileformats.jpeg.JpegImage imageLarge = (com.aspose.imaging.fileformats.jpeg.JpegImage)com.aspose.imaging.Image.load("/tmp/x/ddaLarge.jpg");
System.out.println("before -> " + imageLarge.getVerticalResolution());
System.out.println("after -> " + imageLarge.getHorizontalResolution());
imageLarge.setResolution(200, 200);
System.out.println(“before -> " + imageLarge.getVerticalResolution());
System.out.println(“after -> " + imageLarge.getHorizontalResolution());
imageLarge.save();


com.aspose.imaging.fileformats.jpeg.JpegImage imageTest = (com.aspose.imaging.fileformats.jpeg.JpegImage)com.aspose.imaging.Image.load(”/tmp/x/ddaLarge.jpg”);
System.out.println(imageTest.getVerticalResolution());
System.out.println(imageTest.getHorizontalResolution());
imageTest.dispose();

It doesn’t work!

Hi Michael,


You are right, the above code as well as other variations to set the resolution of a given Jpeg image aren’t working as expected. I am performing some more tests, and I will log appropriate ticket for correction purposes.
michael.burger@siag.it:
Sorry for my hard words

But in German we say something like that:

If Aspose.Words is like heaven, Aspose.Pdf is like hell.

I do not use many function with .Pdf but I found uncountable errors, for really basic functions!

:(
Hi Michael,

Aspose.Pdf for Java and Aspose.Words for Java are two separate API's with different rendering engines. Nevertheless, with every new release of Aspose.Pdf for Java, we are trying our level best to make this API robust and error free. Once again, please accept our humble apologies for this inconvenience.

I tried a workaround converting the PDF in a TIFF and using Imaging for converting the TIFF to JPEG!

The createt TIFF has a Resoltuion of 200 SO I WAS HAPPY!
But than converting the file from TIFF to JPEG the created jpeg is as usual only 72x72 (or 96)dpi

So two different libs and 3 different methods to get a JPEG with right resoltuion, but none of them works!

code=>
TiffSettings tiffSettings = new TiffSettings();
tiffSettings.setCompression(CompressionType.LZW);
tiffSettings.setDepth(ColorDepth.Default);
tiffSettings.setSkipBlankPages(false);


TiffDevice tiffDevice = new TiffDevice(new Resolution(200), tiffSettings);
tiffDevice.process(pdfDocument, 1, 1, “/tmp/x/ddaTIFF.tif”);


com.aspose.imaging.fileformats.tiff.TiffImage imageConv = (com.aspose.imaging.fileformats.tiff.TiffImage)com.aspose.imaging.Image.load("/tmp/x/ddaTIFF.tif");

JpegOptions jpegOptions = new com.aspose.imaging.imageoptions.JpegOptions();
imageConv.save("/tmp/x/ddaTIFF.jpg", jpegOptions);

Hi Michael,


First of all, please accept our sincere apologies for all the inconvenience caused to you.

As discussed in previous post, there seems to be some problem in JpegOptions class that does not allowing to set custom resolution for existing Jpeg images. We have logged an investigative ticket (IMAGING-34119) in our bug tracking system to probe further into this matter, and to provide a solution at earliest. Please spare us little time to properly investigate the problem cause on our end. In the meanwhile, we will keep you posted with updates in this regard.

Yes, but I do not know what do you do in your libs!

My first error in this thread wos the conversion from PDF to JPEG!
Using ONLY Aspose.PDF and not including Aspose.Imaging by using the class JpegDevice which comes from com.aspose.pdf.devices!!!


So for my (becouse I don’t know and I can’t know how you do it) this are two different errors!!

If you use in your PDF lib your own Imaging lib and use the same methots so resolving one resolves the errors on BOTH libs!!!
But I don’t know what you do in your sources!




Hi Michael,


As discussed in our first post, the feature of PDF conversion to raster images is beyond the scope of Aspose.Imaging APIs. This feature is already supported by Aspose.Pdf component so we will not implement it in Aspose.Imaging APIs. Moreover, Aspose.Pdf uses a subset of Aspose.Imaging APIs for basic imaging functionality at it’s core. We have recently passed on the latest build of Aspose.Imaging to the Aspose.Pdf development team and they should evaluate and incorporate the Jpeg related features into Aspose.Pdf component. Hopefully with future releases of Aspose.Pdf, you will be able to achieve both of your goals, i.e; re-size & set custom resolution for Jpeg images without needing Aspose.Imaging at your end.
michael.burger@siag.it:
Yes, but I do not know what do you do in your libs!

My first error in this thread wos the conversion from PDF to JPEG!
Using ONLY Aspose.PDF and not including Aspose.Imaging by using the class JpegDevice which comes from com.aspose.pdf.devices!!!
Hi Michael,

Thanks for sharing the details. We do understand that when converting PDF files to JPEG format, the image resolution is coming as 96. The problem has already been logged in our issue tracking system.


michael.burger@siag.it:
So for my (becouse I don't know and I can't know how you do it) this are two different errors!!

If you use in your PDF lib your own Imaging lib and use the same methots so resolving one resolves the errors on BOTH libs!!!
Thanks for the suggestion. Please note both API's are separate and they have different implementations for image processing. However, I will intimate the development team to look into this point as it might help us during the resolution of these problems.

PS, as you have stated earlier, when converting TIFF image to JPEG, the resultant images have dpi 96 so using current version/approach of Aspose.Imaging will not help. Nevertheless, the issue will be resolved in Aspose.Pdf or in Aspose.Imaging and same approach can be incorporated in Aspose.Pdf. However the development team needs to decide the appropriate approach.

The issues you have found earlier (filed as PDFNEWJAVA-34179) have been fixed in Aspose.Pdf for Java 9.1.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.