none
Convert PDF to any type of image RRS feed

  • Question

  • Hi guys,

    I also need to convert PDF files to Images. If the PDF is Multi-page. Just need one image that contains all of the PDF pages.

    Are there any open source solutions which are not charged like the Acrobat product?

    I tried Ghostscript but It does not meet my expectations.

    Wednesday, August 7, 2019 11:38 PM

Answers

  • Hi Donald,

    Thank you for posting here.

    For your question, you want to convert all of the PDF pages to one image.

    I make a code example, you could have a look.

           static void Main(string[] args)
            {
                PdfDocument doc = new PdfDocument();
                doc.LoadFromFile(@"D:\test.pdf");
                List<string> list = new List<string>();
                for (int i = 0; i < doc.Pages.Count; i++)
                {
                    System.Drawing.Image bmp = doc.SaveAsImage(i);
                    string fileName =string.Format("Page-{0}.png", i + 1);
                    bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                    list.Add(fileName);
                }
                Bitmap b = CombineBitmap(list.ToArray());
                b.Save("D:\\total.png");
                foreach (var item in list)
                {
                    File.Delete(item);
                }
            }
            public static System.Drawing.Bitmap CombineBitmap(string[] files)
            {
                //read all images into memory
                List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
                System.Drawing.Bitmap finalImage = null;
    
                try
                {
                    int width = 0;
                    int height = 0;
    
                    foreach (string image in files)
                    {
                        //create a Bitmap from the file and add it to the list
                        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);
    
                        //update the size of the final bitmap
                        width += bitmap.Width;
                        height = bitmap.Height > height ? bitmap.Height : height;
    
                        images.Add(bitmap);
                    }
    
                    //create a bitmap to hold the combined image
                    finalImage = new System.Drawing.Bitmap(width, height);
    
                    //get a graphics object from the image so we can draw on it
                    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
                    {
                        //set background color
                        g.Clear(System.Drawing.Color.Black);
    
                        //go through each image and draw it on the final image
                        int offset = 0;
                        foreach (System.Drawing.Bitmap image in images)
                        {
                            g.DrawImage(image,
                              new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
                            offset += image.Width;
                        }
                    }
    
                    return finalImage;
                }
                catch (Exception)
                {
                    if (finalImage != null)
                        finalImage.Dispose();
                    //throw ex;
                    throw;
                }
                finally
                {
                    //clean up memory
                    foreach (System.Drawing.Bitmap image in images)
                    {
                        image.Dispose();
                    }
                }
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 8, 2019 2:06 AM
    Moderator

All replies

  • One solution is to use a third-party library. Try ImageMagick. The original ImageMagick download page is here.

    And you also can take a look at this PDF to IMG Library

    Thursday, August 8, 2019 12:54 AM
  • Hi Donald,

    Thank you for posting here.

    For your question, you want to convert all of the PDF pages to one image.

    I make a code example, you could have a look.

           static void Main(string[] args)
            {
                PdfDocument doc = new PdfDocument();
                doc.LoadFromFile(@"D:\test.pdf");
                List<string> list = new List<string>();
                for (int i = 0; i < doc.Pages.Count; i++)
                {
                    System.Drawing.Image bmp = doc.SaveAsImage(i);
                    string fileName =string.Format("Page-{0}.png", i + 1);
                    bmp.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
                    list.Add(fileName);
                }
                Bitmap b = CombineBitmap(list.ToArray());
                b.Save("D:\\total.png");
                foreach (var item in list)
                {
                    File.Delete(item);
                }
            }
            public static System.Drawing.Bitmap CombineBitmap(string[] files)
            {
                //read all images into memory
                List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
                System.Drawing.Bitmap finalImage = null;
    
                try
                {
                    int width = 0;
                    int height = 0;
    
                    foreach (string image in files)
                    {
                        //create a Bitmap from the file and add it to the list
                        System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);
    
                        //update the size of the final bitmap
                        width += bitmap.Width;
                        height = bitmap.Height > height ? bitmap.Height : height;
    
                        images.Add(bitmap);
                    }
    
                    //create a bitmap to hold the combined image
                    finalImage = new System.Drawing.Bitmap(width, height);
    
                    //get a graphics object from the image so we can draw on it
                    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage))
                    {
                        //set background color
                        g.Clear(System.Drawing.Color.Black);
    
                        //go through each image and draw it on the final image
                        int offset = 0;
                        foreach (System.Drawing.Bitmap image in images)
                        {
                            g.DrawImage(image,
                              new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
                            offset += image.Width;
                        }
                    }
    
                    return finalImage;
                }
                catch (Exception)
                {
                    if (finalImage != null)
                        finalImage.Dispose();
                    //throw ex;
                    throw;
                }
                finally
                {
                    //clean up memory
                    foreach (System.Drawing.Bitmap image in images)
                    {
                        image.Dispose();
                    }
                }
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 8, 2019 2:06 AM
    Moderator