参考:

           

百度云链接:https://pan.baidu.com/s/1oiMEZl9xJWVAzf_JZaw3Ww  提取码:8dna


方法1:添加引用  O2S.Components.PDFRender4NET

 

 

public enum Definition
        {
            One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
        }
        /// <summary>
        /// 将PDF转换为图片的方法
        /// </summary>
        /// <param name="pdfInputPath">PDF文件路径</param>
        /// <param name="imageOutputPath">图片输出路径</param>
        /// <param name="imageName">生成图片的名字</param>
        /// <param name="startPageNum">从PDF文档的第几页开始转换</param>
        /// <param name="endPageNum">从PDF文档的第几页开始停止转换</param>
        /// <param name="imageFormat">设置所需图片格式</param>
        /// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
        public string[] PdfToPng(string pdfInputPath, string imageOutputPath,
            string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat , int definition)
        {
            List<string> outFileList = new List<string>();
            PDFFile pdfFile = PDFFile.Open(pdfInputPath);
            if (!Directory.Exists(imageOutputPath))
            {
                Directory.CreateDirectory(imageOutputPath);
            }
            // validate pageNum
            if (startPageNum <= 0)
            {
                startPageNum = 1;
            }
            if (endPageNum > pdfFile.PageCount)
            {
                endPageNum = pdfFile.PageCount;
            }
            if (startPageNum > endPageNum)
            {
                int tempPageNum = startPageNum;
                startPageNum = endPageNum;
                endPageNum = startPageNum;
            }
            // start to convert each page
            if (endPageNum == 1)
            {
                Bitmap pageImage = pdfFile.GetPageImage(1 - 1, 56 * (int)definition);
                pageImage.Save(imageOutputPath + imageName + "." + imageFormat, imageFormat);
                pageImage.Dispose();
                outFileList.Add(imageOutputPath + imageName + "." + imageFormat);
            }
            else
            {
                for (int i = startPageNum; i <= endPageNum; i++)
                {
                    Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
                    pageImage.Save(imageOutputPath + imageName + i + "." + imageFormat, imageFormat);
                    pageImage.Dispose();
                    outFileList.Add(imageOutputPath + imageName + i + "." + imageFormat);
                }
            }
            pdfFile.Dispose();
            return outFileList.ToArray(); ;
        }

 

 

方法2:添加引用Spire.License.dll和 Spire.Pdf.dll

 

 

/// <summary>
        /// Pdf 转图片。缓存在系统目录
        /// </summary>
        /// <param name="pdfPath"></param>
        /// <param name="outPath"></param>
        /// <returns></returns>
        public  string[] ToImage(string pdfPath,string outPath)
        {
            if (!File.Exists(pdfPath)) return null;
            string err = string.Empty;
            using (FileStream fs = new FileStream(pdfPath, FileMode.Open))
            {
                return ToImage(fs, outPath,out err);
            }
        }
        public string[] ToImage(Stream s, string outPath, out string err)
        {
            try
            {
                if (outPath.Last() != '\\')
                {
                    outPath += "\\";
                }
                if (!System.IO.Directory.Exists(outPath))
                {
                    Directory.CreateDirectory(outPath);
                }

                List<string> outFileList = new List<string>();
                
                using (IDisposable pdfDocument = dynamicSpirePdf.CreatePdfDocument(s))
                {
                    int count = dynamicSpirePdf.GetCount(dynamicSpirePdf.GetPages(pdfDocument));
                    for (int i = 0; i < count; i++)
                    {
                        //将PDF页转换成bitmap图形
                        using (System.Drawing.Image bmp = dynamicSpirePdf.SaveAsImage(pdfDocument,i))
                        {
                            //将bitmap图形保存为png图片
                            var file = string.Format("{0}{1}.bmp", outPath, Guid.NewGuid().ToString("D"));
                            bmp.Save(file, System.Drawing.Imaging.ImageFormat.Bmp);
                            outFileList.Add(file);
                        }
                    }
                }
                err = string.Empty;
                return outFileList.ToArray();
            }
            catch (Exception e)
            {
                err = e.Message + Environment.NewLine + e.StackTrace;
                return null;
            }
        }