WPF 显示PDF、PDF转成图片

1.NuGet 安装 O2S.Components.PDFView4NET.WPF

WPF 显示PDF、PDF转成图片_第1张图片

2.添加组件

工具箱中,空白处 右键,选择项

WPF 显示PDF、PDF转成图片_第2张图片

WPF组件 界面,选择NuGet安装库对面路径下的  O2S.Components.PDFView4NET.WPF.dll

WPF 显示PDF、PDF转成图片_第3张图片

WPF 显示PDF、PDF转成图片_第4张图片

WPF 显示PDF、PDF转成图片_第5张图片

3.引入组件命名空间,并使用


    
        
            
        

        

    

using O2S.Components.PDFView4NET;
using System.Windows;

namespace PDFView
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            PdfControl.Document = new PDFDocument() { FilePath = ".\\4.pdf" };
        }
    }
}

4.PDF转换成图片

听说有破解版(无水印),找到了两个版本两个不同O2S.Components.PDFRender4NET破解版

版本2.0.1.0 对于测试PDF,使用正常

版本4.5.1.2 对于测试PDF,使用异常

PdfToImage(".\\4.pdf", Environment.CurrentDirectory, "4", ImageFormat.Png, Definition.Eight);
/// 
        /// PDF文档所有页全部转换为图片
        /// 
        /// PDF文件流
        /// 图片输出路径
        /// 生成图片的名字
        /// 设置所需图片格式
        /// 设置图片的清晰度,数字越大越清晰
        public static void PdfToImage(string pdfStream, string imageOutputPath, string imageName, ImageFormat imageFormat, Definition definition)
        {
            PDFFile pdfFile = PDFFile.Open(pdfStream);
            int startPageNum = 1;
            int endPageNum = pdfFile.PageCount;
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                try
                {
                    Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);

                    int canKao = pageImage.Width > pageImage.Height ? pageImage.Height : pageImage.Width;
                    int newHeight = canKao > 1080 ? pageImage.Height / 2 : pageImage.Height;
                    int newWidth = canKao > 1080 ? pageImage.Width / 2 : pageImage.Width;
                    Bitmap newPageImage = new Bitmap(newWidth, newHeight);

                    Graphics g = Graphics.FromImage(newPageImage);
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                    g.DrawImage(pageImage, new Rectangle(0, 0, newWidth, newHeight),
                    new Rectangle(0, 0, pageImage.Width, pageImage.Height), GraphicsUnit.Pixel);
                    newPageImage.Save(imageOutputPath + imageName + (endPageNum >= 2 ? ("-" + i) : "") + "." + imageFormat);//+ i.ToString() imageFormat
                    g.Dispose();
                    newPageImage.Dispose();
                    pageImage.Dispose();
                }
                catch (Exception ex)
                {
                    string ss = ex.ToString();
                }
            }

            pdfFile.Dispose();
        }

        /// 
        /// 转换的图片清晰度,1最不清醒,10最清晰
        /// 
        public enum Definition
        {
            One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6, Seven = 7, Eight = 8, Nine = 9, Ten = 10
        }

5.附上官方代码库链接

PDFView4NET https://github.com/o2solutions/pdfview4net/tree/main

你可能感兴趣的:(C#,pdf,pdf,转图片,PDFRender4NET)