locked
How to Convert Pdf Pages to Images RRS feed

  • Question

  • User-1049489927 posted

    Hi

         I am working on C#.Net web(asp.net)application, in my application we have a requiremnt of converting PDF Page to JPG Images,we have full licensed version acrobat 7.0 sdk

    i am using the following code convert from pdf file to image.

    private bool Converter(string Input, string Output)
            {
                try
                {
                    try
                    {
                        Acrobat.CAcroPDDoc pdfDoc;
                        Acrobat.CAcroPDPage pdfPage;
                        Acrobat.CAcroRect pdfRect;
                        Acrobat.CAcroPoint pdfPoint;

     

                        string inputFile = Input;

     

                        pdfDoc = (Acrobat.CAcroPDDoc)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.PD Doc", "");

     

                        int ret = pdfDoc.Open(inputFile);

     

                        if (ret == 0)
                        {

     

                        }

     

                        // Get the number of pages (to be used later if you wanted to store that information)
                        int pageCount = pdfDoc.GetNumPages();
                        if (pageCount == 0)
                        {
                            Console.WriteLine("Pages not found");
                            return false;
                        }
                        
                       
                        for (int i = 0; i < pageCount; i++)
                        {
                           
                            // Get the first page
                            pdfPage = (Acrobat.CAcroPDPage)pdfDoc.AcquirePage(i);
                            pdfPoint = (Acrobat.CAcroPoint)pdfPage.GetSize();
                            pdfRect = (Acrobat.CAcroRect)Microsoft.VisualBasic.Interaction.CreateObject("AcroExch.Rec t", "");

     


                            pdfRect.Left = 0;
                            pdfRect.right = pdfPoint.x;
                            pdfRect.Top = 0;
                            pdfRect.bottom = pdfPoint.y;
                            pdfPage.CopyToClipboard(pdfRect, 0, 0, 100);

     

                            System.Windows.Forms.IDataObject clipboardData = System.Windows.Forms.Clipboard.GetDataObject();
                            if (clipboardData.GetDataPresent(DataFormats.Bitmap))
                            {
                                string FileName = string.Empty;
                                if (i.ToString().Length == 1)
                                    FileName = "0" + i.ToString();
                                else
                                    FileName = i.ToString();

     

                                Bitmap pdfBitmap = (Bitmap)clipboardData.GetData(DataFormats.Bitmap);
                                Image Img = (Image)pdfBitmap;
                                                        
                                Img.Save(Output + "\\Pages\\" + FileName + ".jpg");
                               
                            }
                        }

     

     

     


                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message.ToString());
                        return false;
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }

    But i am getting error like "object reference not set to an instance of an object"

    at this line:  clipboardData.GetDataPresent(DataFormats.Bitmap

    please can some help me regarding this,i have very urgent.

    Regards,

    Babu


    Saturday, October 10, 2009 5:32 AM

All replies

  • User-1460196090 posted

    Hi sathyam,

    Please take a look at the following tutorial: http://www.codeproject.com/KB/library/pdftoimages.aspx

    Regards,
    Hajan

    Saturday, October 10, 2009 10:27 PM
  • User-1049489927 posted

    Hi Hajan,

    Thanq for response.The article that u shown is excellent,but i want use the above code.

    please any suggestions

    Satyam.P

    Sunday, October 11, 2009 4:18 AM
  • User-1049489927 posted

    Hi Hajan

    It's working fine for me.

    I have added AspCompact="true" @Page Directive for My code.now it's working great.

    anyway thanx for ur help

    Satyam

    Monday, October 12, 2009 2:34 AM
  • User-1460196090 posted

    Hi Hajan

    It's working fine for me.

    I have added AspCompact="true" @Page Directive for My code.now it's working great.

    anyway thanx for ur help

    Satyam

    Ahhh I see... :)

    Great you got it working...

    Regards,
    Hajan

    Monday, October 12, 2009 7:50 AM
  • User-1049489927 posted

    Hi all,

    It's working fine when it was in developer system. but when i hosted  the application in IIS , it's not working.

    It is giving the error like :"Cannot Create Activex Component".

    Can some one please help me.


    Friday, October 16, 2009 2:37 AM
  • User-1267536501 posted

    Hi i am also facing the same problem and getting error like "object reference not set to an instance of an object". I have a window service to convert pdf file into jpg format and i am unable to add AspCompact="true" @Page Directive in the service. Anyone can give me a hand? Many thanks if you can help me.

    Tuesday, July 30, 2013 6:35 AM
  • User-1267536501 posted

    Hi i am also facing the same problem and getting error like "object reference not set to an instance of an object". I have a window service to convert pdf file into jpg format and i am unable to add AspCompact="true" @Page Directive in the service. Anyone can give me a hand? Many thanks if you can help me.

    Tuesday, July 30, 2013 6:36 AM