C#中使用Spire实现PDF转图片


  • 思路
  1. 用FreeSpire将PDF转为Image
  2. 将FreeSprie生成的警告语裁剪掉
  • 代码
        private static void TestPdfToImageBySpire()
        {
            //PDF文件路径
            string sPdfFilePath = @"D:\test.pdf";
            //图片输出路径
            string sOutPath = @"D:\zzzzz\";
            System.IO.Directory.CreateDirectory(sOutPath);
            PdfDocument doc = new PdfDocument(sPdfFilePath);
            //FreeSpire转换图片是增加在顶部的警告语高度,根据DPI不同设置不同的高度
            int iWarningTitleHeight = 45;
            for (int i = 0; i < doc.Pages.Count; i++)
            {
                Image img = doc.SaveAsImage(i, Spire.Pdf.Graphics.PdfImageType.Metafile, 300, 300);
                Rectangle srcRect = new Rectangle(0, iWarningTitleHeight, img.Width, img.Height - iWarningTitleHeight);
                Rectangle destRect = new Rectangle(0, 0, img.Width, img.Height - iWarningTitleHeight);
                using (Bitmap bitmap = new Bitmap(img.Width, img.Height - iWarningTitleHeight))
                {
                    using (Graphics g = Graphics.FromImage(bitmap))
                    {
                        //裁剪掉FreeSpire增加的警告语
                        g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);
                        bitmap.Save($"{sOutPath}{i}.jpg",ImageFormat.Jpeg);
                    }
                } 
            }
            Console.WriteLine("转换完成");
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM