loveForEver loveForEver - 2 months ago 51
C# Question

how to convert pdf files to image

I have some files. It's format is pdf
Now there is a project, this project needs convert the pdf file to Image
if the pdf is Multi-page´╝îI just need one image contains all pdf pages.
I get some answer from google,but some of tools is charge

so, how to solve it with C#?
thank you very much!

I know,use the Acrobat.dll in C# can solve this problem.
but it must installation the adobe acrobat and it is not free.

Answer

The following thread is suitable for your request. converting pdf file to an jpeg image

One solution is to use a third party library. ImageMagick is a very popular, 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 this 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];
}