Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Sep 24, 2014 5:31 am

Hi All,

I have been trying the demo to convert PDF to Image.

So, I would like to ask how can we give specific dimension for image while creating from PDF. I would like to generate the image with the below dimensions.

Width :- 796
Height :- 1030

I have tried, but it is not giving the exact dimensions.

Regards,
David

davidfernandes
 
Posts: 7
Joined: Fri Sep 12, 2014 5:51 am

Wed Sep 24, 2014 8:23 am

Dear David,

Thanks for your inquiry.
Please try the following code:
Code: Select all
PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("..\\..\\Sample3-1.pdf");
            Image image = doc.SaveAsImage(0,PdfImageType.Metafile);
            int width = 796;   
            int height =1030 ;
            Bitmap bitmap = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bitmap);
            g.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
           
            bitmap.Save("sample2.png", ImageFormat.Png);


Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Wed Sep 24, 2014 11:29 am

amy.zhao

Hey thanks a lot for the reply. It has helped me.

One more thing, what are the different properties/methods of spire which will help for image formatting
For e.g. maintain image quality, color after conversion etc ?

Thanks,
David

davidfernandes
 
Posts: 7
Joined: Fri Sep 12, 2014 5:51 am

Thu Sep 25, 2014 2:28 am

Dear David,

Please try this line: bitmap.SetResolution(float, float); to set resolution of image so that have a high quality.
All code:
Code: Select all
  PdfDocument doc = new PdfDocument();
            doc.LoadFromFile("..\\..\\Sample3-1.pdf");
            Image image = doc.SaveAsImage(0, PdfImageType.Metafile);
            int width = 796;
            int height = 1030;
            Bitmap bitmap = new Bitmap(width, height);
   
            bitmap.SetResolution(300, 300);

            Graphics g = Graphics.FromImage(bitmap);
            g.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);

            bitmap.Save("sample2.png", ImageFormat.Png);


Welcome to write to us for further inquiry.

Best wishes,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Sun Sep 28, 2014 9:46 am

Dear David,

Does my code help?
Thanks for your feedback.

Please feel free to contact us if you need any help.

Best wishes,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 2766
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF