PDF to PNG sizing problem

Hi Aspose,

I am trying to convert a pdf document to a png image, in order to present it in a previewer. However, the size of the outcome png is slightly smaller than the pdf. How do I make the png in exactly the same dimensions as the original pdf. Here is what I do:

MemoryStream memStream = new MemoryStream();
int pageIndex = 1;

Aspose.Pdf.Document pDocument = new Aspose.Pdf.Document(path);
Aspose.Pdf.Devices.PngDevice pngDevice = new Aspose.Pdf.Devices.PngDevice(new Aspose.Pdf.Devices.Resolution(100));

pngDevice.Process(pDocument.Pages[pageIndex], memStream);

Please note, that I have tried many different pdf files, with different page sizes. Some of the pdf files I need present, have different page size on the individual pages.

How do I make the png with exact same dimensions as the pdf file, and how do I handle changing page sizes within the same document?

Thanks,
Jusbet


Hi Jusbet,

Thanks for your interest in our products.

Kindly use the below code for setting the image dimensions same as the original PDF pages.

[C#]

//open document<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Document pdfDocument = new Document(@"d:\pdffiles\helloworld.pdf");

//get page collection

PageCollection pageCollection = pdfDocument.Pages;

for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)

{

//get particular page

Aspose.Pdf.Page pdfPage = pageCollection[pageCount];

using (FileStream imageStream = new FileStream(@"d:\pdffiles\image" + pageCount + ".png", FileMode.Create))

{

//create PNG device with specified attributes

//Width, Height, Resolution, Quality

//Quality [0-100], 100 is Maximum

//create Resolution object

Resolution resolution = new Resolution(100);

PngDevice pngDevice = new PngDevice(Convert.ToInt32(pdfPage.ArtBox.Width), Convert.ToInt32(pdfPage.ArtBox.Height), resolution);

//convert a particular page and save the image to stream

pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);

//close stream

imageStream.Close();

}

}


Please feel free to contact support in case you need any further assistance.

Thanks & Regards,

Thanks Rashid,

It works fine, only need to remember, that pdfPage.ArtBox.Width is in points, and image is saved in pixels.

I have another problem related to the above. The first time I process a pdf page, everything looks correct. However, if I run a second time shortly after, I have some problems with images inside the pdf file rendered as a black box. Run the below code 2 times, first time image is correct, second time it is just a (bigger) black box:

Document pDocument = new Document(path + filename);
Page pPage = pDocument.Pages[pageNumber];
PngDevice pDevice = new PngDevice(Convert.ToInt32(pPage.ArtBox.Width * pointsToPixels), Convert.ToInt32(pPage.ArtBox.Height * pointsToPixels), new Resolution(100));
// Second time on same page render images as a black box. Also noticed, that second time, the process is must faster than first time.
pDevice.Process(pPage, "C:\\temp\\xxx.png);

Thanks in advance,
Jusbet

Hi Jusbet,

Thanks for sharing the resource file and code snippet.

I am working over this query and will get back to you soon. We are sorry for your inconvenience.

Hi Jusbet,


Thanks for your patience.

I have tested the scenario using Aspose.Pdf for .NET 7.0.0 and I am unable to notice any problem. Every-time the PDF is converted into Image format, it appears to be fine. I have tested the scenario in Visual Studio 2010 application where I have selected .Net Framework 4.0 as target platform and the application is running over Windows 7 Professional X64. Can you please share which version of product you are using. We are sorry for your inconvenience.

PS, Please note that I have made a slight change in the code where I have used following code line to initialize PngDevice object.

PngDevice
pDevice = new PngDevice(600,800, new
Aspose.Pdf.Devices.Resolution(100));
<o:p></o:p>

Hi Jusbet,


Please ignore my previous message in which I have stated that I am unable to replicate the issue related black boxes appearing in place of Images as during my further investigation, I have noticed that Image is not appearing at all in resultant PNG file. For the sake of correction, I have logged this problem as PDFNEWNET-33914 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. We are sorry for your inconvenience.

Hi Nayyerv,

I have noticed the problem in both .Net 4.0 (VS2010) and .Net 2.0 (VS 2005), both Windows 7 64 Ultimate. However, it is of high importance to us that the issue is solved for the .Net 2.0 version. Looking forward for a solution.

Thanks on advance,
Jusbet

Hi Jusbet,

In my earlier attempt of testing this scenario on Windows 7 X64 Professional in a VisualStudio 2010 application with .NET Framework 4.0 as target platform, I was able to reproduce that image present in source PDF is missing in resultant Image file. However I am not able to reproduce the issue where Black shapes are appearing in place of image. Nevertheless, I will further test this scenario over Windows XP with .NET Framework 2.0 and will keep you updated with my findings. We are sorry for your inconvenience.

PS, Can you please share which version of Aspose.Pdf for .NET you are using ?

Hi Nayyervv,

I am using the version 7.0.0 of the Aspose.Pdf.

Br,
Jusbet

Hi Jusbet,


I have again tested the scenario using Aspose.Pdf for .NET 7.0.0 in VisualStudio 2005 project (where the target platform of my application is set to .Net Framework 2.0), running over Windows-XP SP3 and as per my observations, the resultant PNG contains a very small image of bear present in source PDF document. I am afraid I am still unable to notice that black boxes appearing in place of image, in resultant PNG files. We will continue with our investigations and will keep you posted with the status of correction. Please be patient and spare us little time. We are sorry for your inconvenience.

PS, For your reference, I have also attached the resultant PNG image which I have generated over Windows XP.

Hi Codewarrior,

Sory for my late reply.

I am able to reproduce the problem here every time. First time presented with correct image, second time is a black box.

Have you tried running the process two times with different output size? I am using the library to present the users pdf file directly on the screen, and where I notice the problem is when you change the size.

Unfortuatly, this is becomming a problem for us, as it this behaviour is permanent. Hope you will be able to reproduce the problem, so a solution can come closer.

Br,

Jusbet

Hi Jusbet,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for the feedback.

I am able to reproduce your mentioned issue after an initial test (When running the process twice). Your issue has been registered in our issue tracking system with issue id: PDFNEWNET-34102. You will be notified via this forum thread regarding any updates against your issue.

Sorry for the inconvenience,

Hi Aspose,

Any news on this?

Br,

Jusbet Bonilla

Hi Jusbet,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for being patient.

I am afraid; your reported issue is not fixed yet. I have asked the development team to share an ETA / update regarding your reported issue. As soon as I get a feedback, I will update you via this thread.

Sorry for the inconvenience,

Hi Jusbet,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Sorry for the delay in response.

I have received a feedback from our developer team and your reported issues are scheduled to be fixed / release in Aspose.Pdf for .NET v7.6 (expected in early December 2012). This is the current plan of development and we will try our best to provide the fix as per the above schedule.

Thank you for being patient,

The issues you have found earlier (filed as PDFNEWNET-33914;PDFNEWNET-34102) have been fixed in Aspose.Pdf for .NET 7.6.0.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.