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