C#pdf文件转单个图片

        /// <summary>
        /// PDF转图片
        /// </summary>
        /// <param name="dicomFile">PDF文件路径</param>
        /// <param name="destJpgFile">保存图片路径</param>
        /// <returns></returns>
        public static string ImportJPG(string dicomFile, string destJpgFile)
        {
            string JPGFilePath = string.Empty;
            try
            {
                PdfiumViewer.PdfDocument pdf = PdfiumViewer.PdfDocument.Load(dicomFile);
                int pdfpage = pdf.PageCount;
                var pagesizes = pdf.PageSizes;

                Size size = new Size();
                size.Height = (int)pagesizes[0].Height;
                size.Width = (int)pagesizes[0].Width;

                Bitmap tmpBmp = new Bitmap(size.Width, size.Height);

                Graphics g = Graphics.FromImage(tmpBmp);
                g.Clear(SystemColors.AppWorkspace);

                for (int i = 1; i <= pdfpage; i++)
                {

                    System.Drawing.Image image = pdf.Render(i - 1, size.Width, size.Height, 600, 600, PdfRenderFlags.Annotations);
                    g.DrawImage(image, 0, (i - 1) * size.Height, size.Width, size.Height);
                    image.Dispose();
                }
                tmpBmp.Save(destJpgFile);
                JPGFilePath = destJpgFile;
                tmpBmp.Dispose();
                g.Dispose();
                pdf.Dispose();

            }
            catch (Exception ex)
            {
                JPGFilePath = ex.Message;
            }
            return JPGFilePath;

        }

 引用的dll:https://files.cnblogs.com/files/jiangge23/PdfiumViewer.zip?t=1683595153&download=true

posted @ 2023-05-09 09:12  RC城  阅读(405)  评论(0编辑  收藏  举报