Hi,
I am trying to convert image to PDF using Aspose.Pdf 17.6 and next recommendations: How to set PDF page matching the image size?.
I am using next code:
var doc = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = doc.Pages.Add();
FileStream fs = new FileStream(inputFileFullName, FileMode.Open, FileAccess.Read);
byte[] tmpBytes = new byte[fs.Length];
fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length));
MemoryStream mystream = new MemoryStream(tmpBytes);
Bitmap b = new Bitmap(mystream);
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;
page.PageInfo.Width = b.Width;
page.PageInfo.Height = b.Height;
page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
page.Paragraphs.Add(image);
image.ImageStream = mystream;
doc.Save(outputFileFullName);
mystream.Close();
The size of image in result PDF file does not match to the size of source image (see attached files).
Could you please help to understand the reason?
Best regards
Valeriy
jpeg.jpeg (9.4 KB)
jpeg.jpeg.pdf (31.9 KB)
@uaprogrammer
Thanks for contacting support.
The reason of PDF file size increases is when you convert an Image into PDF, the Aspose.Pdf API does not compromise the quality of the image. Furthermore, the size of PDF increase due to stored Graphics States of the image inside PDF.
However in order to compress or reduce the file size you need to change the image quality. Please check the following code snippet to optimize the PDF document with reducing the image quality inside it. In below code I have set the image quality reduced to 10%.
pdf.OptimizeResources(new Document.OptimizationOptions()
{
LinkDuplcateStreams = true,
RemoveUnusedObjects = true,
RemoveUnusedStreams = true,
CompressImages = true,
ImageQuality = 10
});
pdf.Save(dataDir + "jpeg.jpeg.pdf");
For your reference, I have also attached an output jpeg.jpeg.pdf (11.9 KB)
, generated by above code snippet. You may check the size of the PDF document has been reduced. You may also visit “Optimize PDF Document” section in our API documentation for more details. In case of any further assistance please feel free to contact us.
Best Regards,
Asad Ali
Hi Asad,
Unfortunately I can not open the link to the result of your conversion (I’m getting HTTP 500 error message). However I think I was not clear enough in explanation of the problem.
The problem is not in the size of PDF file but is in size of image on the PDF page. The width and height of image on PDF page is much more bigger than the width and height of image itself.
Our previous code, which used the Generator functionality of previous versions of Aspose generated next file: jpeg.jpeg_previous.pdf (11.1 KB)
How can we get the same result using the mentioned above code example?
Thanks in advance,
Valeriy
@uaprogrammer
Thanks for adding more details to the scenario.
I have tested the scenario again and as per my observations, new approach (DOM) is adding Image with actual height and width inside PDF document, unless you change its dimensions before adding it into PDF. As in the above code snippet, you were adjusting Page Height and Width according to image by setting Page.Cropbox
property. So when you will compare the resultant file with image properties, you will notice that the height and width of the image is not changed.
Height and width of the page, generated by new approach, were 2.35in and 4.14in respectively. If we calculate image dimensions in inches (72 points = 1 inch), we will get same height and width as of the PDF page. For your reference, I have also attached an screenshot of calculations Image_vs_Page_dimensions.png.
Furthermore, would you please share the code snippet which you were using to add image inside PDF with old Aspose.Pdf.Generator approach. We will test and compare the results of the scenario in our environment and address it accordingly.
Best Regards,
Asad Ali
Hi,
We used next code for Generator approach:
var pdf = new Aspose.Pdf.Generator.Pdf();
var section = pdf.Sections.Add();
var newImage = new Aspose.Pdf.Generator.Image(section);
section.Paragraphs.Add(newImage);
var existedImage = System.Drawing.Image.FromFile(inputFileFullName);
newImage.ImageInfo.SystemImage = existedImage;
newImage.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg;
pdf.Save(outputFileFullName);
Best regards,
Valeriy
@uaprogrammer
Thanks for sharing code snippet.
I have observed the difference between the output(s), generated by old and new approach(s) and logged an investigation ticket as PDFNET-43057 in our issue tracking system. Product team will further look into this and share their feedback. As soon as we have some updates on this ticket, we will let you know. Please be patient and spare us little time.
We are sorry for the inconvenience.
Best Regards,
Asad Ali
Hello Asad!
Can I ask you about priority for this issue fix?
This defect seems to be quite major from our perspective and it wasn’t present in the previous version of Aspose library. So we hope that you would set high priority for its fix.
Can you please share the plans on this defect correction? Can we expect to get it fixed within nearest future?
Thanks in advance,
Liza
@uaprogrammer
Thanks for contacting support.
As your issue has been posted under Normal/Free Support Model, so its priority level is normal and lower than those of, which are logged under Enterprise/Priority Support Model.
We are sorry that you have faced this issue while using our API, though please note that it has been logged for investigation purpose in our internal issue tracking system and our product team will investigate it as per their development schedule.
I am afraid that currently we are not in position to share any reliable ETA as there are other pending issues in the queue which were logged prior to yours. Though we have recorded your concerns and intimated relevant team about them. As soon as we have some definite updates regarding investigation of the issue, we will let you know.
Your patience and comprehension is highly appreciated in this regard. Please spare us little time.
We are sorry for the inconvenience.
@uaprogrammer
Thanks for your patience.
Our product team has further investigated the issue and as per their findings, new generator result is correct where image dimensions are 298x169 pixels
inside PDF, whereas old generator result was incorrect as it was squeezing the image.
Moreover, you may set FixHeight
and FixWidth
properties of Image
object in your code to keep the desired dimensions of the image inside PDF. In case if you have any further inquiry, please feel free to let us know.
Hi,
I have taken a look at the properties of images in file, converted to PDF with DOM functionality (see properties here jpeg.jpeg.pdf-Image Parameters.jpeg (235.5 KB)) and in the file of the previous version, converted to PDF with Generator functionality (see screenshot here jpeg.jpeg_previous.pdf-Image Parameters.jpeg (204.8 KB)).
Unfortunately they are in Russuan, thus I am giving you translations of selected areas:
it is stated: “size 298 x169 pix (105.13 x 59.62 mm)”, “resolution: 2.83 pix/mm - horizontal, 2.83 pix/mm - vertical” in jpeg.jpeg.pdf-Image Parameters.jpeg file,
and it is stated: “size 298 x169 pix (25.23 x 14.31 mm)”, “resolution: 11.81 pix/mm - horizontal, 11.81 pix/mm - vertical” in jpeg.jpeg_previous.pdf-Image Parameters.jpeg file.
Thus, as you can see the sizes of images in pixels are the same but the resolution was changed and as result, the size of images in mm was changed.
Why the new conversion uses the different resolution?
How can I get in DOM functionality the same resolution, which was used in Generator functionality?
Best regards
@uaprogrammer
Thanks for adding more details to the scenario.
We have shared relevant details with our product team and as soon as they share some feedback in this regard, we will let you know. Please spare us little time.
We are sorry for this inconvenience.
@uaprogrammer,
Thanks for your patience.
In order to fulfill your requirement, please try using following code snippet with image.IsApplyResolution = true option :
string inFile = GetInputPath("43057.jpeg");
string outFile = GetOutputPath("43057.pdf");
var doc = new Aspose.Pdf.Document();
Aspose.Pdf.Page page = doc.Pages.Add();
FileStream fs = new FileStream(inFile, FileMode.Open, FileAccess.Read);
byte[] tmpBytes = new byte[fs.Length];
fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length));
MemoryStream mystream = new MemoryStream(tmpBytes);
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
page.Paragraphs.Add(image);
image.ImageStream = mystream;
image.IsApplyResolution = true;
doc.Save(outFile);
mystream.Close();
Hi,
Using of IsApplyResolution property really helped us to fix issue with raster images. Thank you very much.
However we still have an issue with emf and wmf formats. They are converted slightly different comparing to the results of conversion with Generator functionality.
This is an example of .emf image EMF.zip (16.0 KB) conversion to pdf with current DOM functionality EMF.emf_current.pdf (154.0 KB) and with previous Generator functionality EMF.emf_previous.pdf (37.7 KB). As you can see circles become ellipses in current functionality.
And this is an example of .wmf image wmf.zip (182.2 KB) conversion to pdf with current DOM functionality wmf.wmf_current.pdf (10.7 KB) and with previous Generator functionality wmf.wmf_previous.pdf (2.5 KB)
. As you can see the quality of the image in previous functionality is better.
Best regards
Valeriy
@uaprogrammer
Thanks for your inquiry.
Would you please share the old Aspose.Pdf.Generator based code, you are using to add EMF and WMF images into PDF document. We will test the scenario in our environment and address it accordingly.
Hi,
We used next code for conversion:
var pdf = new Aspose.Pdf.Generator.Pdf();
var section = pdf.Sections.Add();
var newImage = new Aspose.Pdf.Generator.Image(section);
section.Paragraphs.Add(newImage);
newImage.ImageInfo.File = inputFileFullName;
newImage.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Emf;
pdf.Save(outputFileFullName);
According for .wmf we used the same code with setting of correct type:
newImage.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Wmf;
Best regards
Valeriy
@uaprogrammer
Thanks for sharing sample code snippet.
We have observed that the image in the PDF got squeezed due to default margin settings of Page, which was why the circles in the image turned as ellipses in the output. After setting the page margins as zero, the output seems fine. Please check following code snippet in order to obtain correct output.
var pdf = new Aspose.Pdf.Document();
var pdfImageSection = pdf.Pages.Add();
FileStream stream = new FileStream(dataDir + "emf.emf", FileMode.Open);
var image = new Aspose.Pdf.Image { ImageStream = stream };
image.IsApplyResolution = true;
pdfImageSection.Paragraphs.Add(image);
// remove extra white space in four corners by setting page margin information
pdfImageSection.PageInfo.Margin.Left = pdfImageSection.PageInfo.Margin.Right = pdfImageSection.PageInfo.Margin.Top = pdfImageSection.PageInfo.Margin.Bottom = 0f;
pdf.Save(dataDir + "EMF_With_DOM.pdf");
EMF_With_DOM.pdf (154.1 KB)
Furthermore, we have also observed that with old Aspose.Pdf.Generator code, the image looked fine and unchanged in the output, along with the default page margins. So we have logged an issue as PDFNET-43530 in our issue tracking system.
We have also observed the image quality difference, between the outputs, generated by both approaches and logged a ticket against this issue as PDFNET-43531 in our issue tracking system. We will further look into the logged tickets and keep you updated with the status. Please be patient and spare us little time.
We are sorry for the inconvenience.
Hi,
Would you be so kind to provide us with status on the PDFNET-43531 issue?
Best regards,
Oleh
@uaprogrammer
We are afraid that earlier logged issue could not get resolved due to other high priority issues in the queue. We will surely inform you as soon as there are some certain updates regarding its resolution. Please spare us little time.
We are sorry for the inconvenience.
Hi,
Can you share updates regarding this issue?
BR
Oleh
@uaprogrammer
We regret to share that earlier logged issues are not yet resolved. We will let you know as soon as we have some updates regarding their resolution. Please give us some time.
We are sorry for the inconvenience.