Search code examples
c#imagepdf

How to convert PDF files to images


I need to convert PDF files to images. If the PDF file is multi-page,I just need one image that contains all of the PDF pages.

Is there an open source solution which is not charged like the Acrobat product?


Solution

  • The thread "converting PDF file to a JPEG image" is suitable for your request.

    One solution is to use a third-party library. ImageMagick is a very popular and is freely available too. You can get a .NET wrapper for it here. The original ImageMagick download page is here.

    And you also can take a look at the thread "How to open a page from a pdf file in pictureBox in C#".

    If you use this process to convert a PDF to tiff, you can use this class to retrieve the bitmap from TIFF.

    public class TiffImage
    {
        private string myPath;
        private Guid myGuid;
        private FrameDimension myDimension;
        public ArrayList myImages = new ArrayList();
        private int myPageCount;
        private Bitmap myBMP;
    
        public TiffImage(string path)
        {
            MemoryStream ms;
            Image myImage;
    
            myPath = path;
            FileStream fs = new FileStream(myPath, FileMode.Open);
            myImage = Image.FromStream(fs);
            myGuid = myImage.FrameDimensionsList[0];
            myDimension = new FrameDimension(myGuid);
            myPageCount = myImage.GetFrameCount(myDimension);
            for (int i = 0; i < myPageCount; i++)
            {
                ms = new MemoryStream();
                myImage.SelectActiveFrame(myDimension, i);
                myImage.Save(ms, ImageFormat.Bmp);
                myBMP = new Bitmap(ms);
                myImages.Add(myBMP);
                ms.Close();
            }
            fs.Close();
        }
    }
    

    Use it like so:

    private void button1_Click(object sender, EventArgs e)
    {
        TiffImage myTiff = new TiffImage("D:\\Some.tif");
        //imageBox is a PictureBox control, and the [] operators pass back
        //the Bitmap stored at that position in the myImages ArrayList in the TiffImage
        this.pictureBox1.Image = (Bitmap)myTiff.myImages[0];
        this.pictureBox2.Image = (Bitmap)myTiff.myImages[1];
        this.pictureBox3.Image = (Bitmap)myTiff.myImages[2];
    }