ankita p - 2011-02-01

Hi,
I tried to extract the images from pdf by using pdf clown(c#) but the extracted images are inverted.Please help me if you know the answers.

The code is pasted below..

else if (content is it.stefanochizzolini.clown.documents.contents.objects.GraphicsObject)
                {
                    /*=============================================================================
                     *  TO EXTRACT THE GRAPHICAL OBJECT WITHIN THE PDF
                     =============================================================================*/
                    ContentScanner.GraphicsObjectWrapper objectWrapper = level.CurrentWrapper;
                    //ContentScanner.GraphicsObjectWrapper object1 = level.CurrentWrapper;
                    if (objectWrapper == null)
                    {
                        continue;
                    }
                  

                    /*
                      NOTE: Images can be represented on a page either as
                      external objects (XObject) or inline objects.
                    */
                    SizeF? imageSize = null; // Image native size.
                    /*if (objectWrapper is PdfDataObject)
                    {
                     ContentScanner.GraphicsObjectWrapper gobjectwrapper=(ContentScanner.GraphicsObjectWrapper)gobjectwrapper;
                        it.stefanochizzolini.clown.objects.PdfDataObject pdobjt=gobjectwrapper
                    }*/
                    //if(objectWrapper is Image)
                    //{
                    //    }
                   

                    if (objectWrapper is ContentScanner.XObjectWrapper)
                    {
                        ContentScanner.XObjectWrapper xObjectWrapper = (ContentScanner.XObjectWrapper)objectWrapper;
                        it.stefanochizzolini.clown.documents.contents.xObjects.XObject Xobject = xObjectWrapper.XObject;
                        // Is the external object an image?
                        if (Xobject is it.stefanochizzolini.clown.documents.contents.xObjects.ImageXObject)
                        {
                            txtOutput.Text = txtOutput.Text + Environment.NewLine +
                              "External Image '" + xObjectWrapper.Name + "' (" + Xobject.BaseDataObject + ")"; // Image key and indirect reference.

                            imageSize = Xobject.Size; // Image native size.

                           PdfDataObject dataObject = Xobject.BaseDataObject;

                            PdfDictionary header = ((PdfStream)dataObject).Header;
                            if (header.ContainsKey(PdfName.Type) && header.Equals(PdfName.XObject) && header.Equals(PdfName.Image))
                            {
                                if (header.Equals(PdfName.DCTDecode)) // JPEG image.
                                {
                                    // Get the image data (keeping it encoded)!
                                IBuffer body = ((PdfStream)dataObject).GetBody(false);
                                   
                                    
                                    // Export the image!
                                  
                                    ExportImage(
                                      body,
                                      txtOutputPath.Text + System.IO.Path.DirectorySeparatorChar + "Image_" + (index++) + ".jpg"
                                      );
                                }
                            }

                        }
                      
//Exproting the image

private void ExportImage(IBuffer data, string outputPath)
        {
            FileStream outputStream;
            try
            { outputStream = new FileStream(outputPath, FileMode.CreateNew); }
            catch (Exception e)
            { throw new Exception(outputPath + " file couldn't be created.", e); }

            try
            {
                BinaryWriter writer = new BinaryWriter(outputStream);
                //ImageConverter ic = new ImageConverter();
                //Image img = (Image)ic.ConvertFrom(data);
              
                writer.Write(data.ToByteArray());
                writer.Close();
                outputStream.Close();
            }
            catch (Exception e)
            { throw new Exception(outputPath + " file writing has failed.", e); }

            Console.WriteLine("Output: " + outputPath);

        }