Hi Aspose.Word Team,
I am currently testing a Pdf generation process by using Aspose.Words API for manipulating the word document template and convert it to a Pdf file.
In the word document template, I am doing a bookmark manipulation, where two images are inserted into the word document template. The two images are BarCode (by using Aspose.Barcode) and GraphChart (by using Aspose.Chart).
However, I had a problem with the Graph chart picture when converting the word document template into pdf file. The problem was, the Graph Chart picture was blurred.
Then, I discussed this issue in the Aspose.chart forum, suspecting that there was a problem with the Graph Chart image generation. They told me to use the EMF format so the quality will show better, but the result was still the same (i.e. the picture was blurry). Yesterday, Aspose.Chart team provides me with an example source code that proves the Graph chart picture generation works fine.
I have tested the pdf generation and found out that the Aspose.Chart team were using an older Aspose.Word API (i.e. ver 184.108.40.206), and the Graph chart picture is in excellent quality.
Then, I retry to generate the pdf by using the latest version of Aspose.Word (ver 220.127.116.11), again, the Graph Chart became fuzzy.
I have attached the example codes.
Thank you very much for your helps and supports.
Hi Aspose.Word Team,
Thank you for your inquiry.
The graph chart is a metafile with resolution set to 133 DPI. Since version 18.104.22.168 logic has changed. Aspose.Words in the current implementation always converts metafiles to PNG on PDF export. You can see the image file in the same directory with main XML if you export to Aspose.Pdf XML format. Resulting raster image quality is dependent on the output resolution.
- If resolution is not specified in the image itself then output resolution is taken from the following property:
- If resolution is specified as in your case then the minimum of these two values is taken.
We don’t see the reason to scale images with higher resolution than specified in the file and what is intended for them. This behavior is by design. You can remove resolution properties from the metafile before you insert it to the document. If both vertical and horizontal resolution are zeroes you can easily control quality by increasing PdfExportMetafileResolution property. Default is 300 DPI. This should be enough in most cases.
Another way is converting metafiles to raster externally, before you insert the images.
I expect one reasonable question. That’s great but you can ask why MS Word renders the metafile without blur effect. Most probably it completely ignores image resolution and uses some default. But we give the possibility to control resolution of individual images.
Please let me know if I can help you further.
Thank you very much for your reply and explanation.
So how do I make the quality of the GraphChart similar to the ver 22.214.171.124? because I don’t need to set any properties in the pdf generation in ver 126.96.36.199.
and because the GraphChart metafile resolution is specified, then (in ver 188.8.131.52), it will take the minimum of those two values (as you mentioned above).
Thus, is there any way to make the GraphChart image has the same picture quality as ver 184.108.40.206?
Unfortunately, using .NET classes you can only get horizontal and vertical resolution from metafile, not set these values. So the easiest way is rasterizing the metafile with resolution you’d like to have. See .NET classes Image and Bitmap for that purpose. It’s good to start with 300 DPI.
- Create source Image from file or stream containing the metafile.
- Create destination Bitmap with size and resolution you need. For Bitmap it is possible to set resolution (SetResolution).
- Copy Image to Bitmap with one of methods of Graphics class.
- Save the new Bitmap or add it directly to the document.
No success, the image still blur. I think I didn’t do it properly.
I follow you instruction, and this is my Script:
Dim chartBitmap_source As Bitmap = New Bitmap("…/…/chart.emf")
Dim chartBitmap As Bitmap = New Bitmap(300, 300)
Dim v_part As System.Drawing.Rectangle = New System.Drawing.Rectangle(0, 0, 300, 300)
Dim g As Graphics = Graphics.FromImage(chartBitmap)
Do you have an example script for the steps that you told me?
Thank You very much for your helps.
Basically you can start with this:
Image srcImage = Image.FromFile("chart.emf"); Bitmap dstImage = new Bitmap(newWidth, newHeight, newFormat); dstImage.SetResolution(newResolution, newResolution); using (Graphics gr = Graphics.FromImage(dstImage)) gr.DrawImage(srcImage, 0, 0, dstImage.Width, dstImage.Height); dstImage.Save("chart.png", ImageFormat.Png);
Play with newWidth, newHeight, newFormat and newResolution variables. This is just an idea and similar code is utilized inside Aspose.Words.
It’s better to set newWidth and newHeight to values corresponding to destination size if you can determine it. In Aspose.Words destination image size in points is used for these parameters. I mean image in terms of MS Word and Aspose.Words, not GDI+. But you can experiment with any values. For newResolution it’s good to start with 300 DPI as I already advised. newFormat can be anything you’d like to have. But note that DrawImage won’t work with indexed formats. You can start with PixelFormat.Format32bppArgb and probably also experiment with others.
I think that would be good to ask in Aspose.Chart forum whether you can control destination image resolution or whether they can provide this feature.
It works perfectly!
Thank you very much for your helps and supports, I’ll ask Aspose.Chart if they can provide the new functionality.
After some circumstances I managed to change default behavior regarding image resolution. From now metafiles will be scaled to the resolution given in SaveOptions regardless of their “built-in” resolution. I think that’s exactly what you asked about. Once any other cases come up important we’ll consider additional parameterization.
The issue is logged as #5277 in our defect database and implemented in the current codebase. New implementation will be available with the next hotfix in about 2-3 weeks.
The issues you have found earlier (filed as 5277) have been fixed in this update.