WPF 显示PDF、PDF转成图片

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

2.添加组件

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

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

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

cs 复制代码
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PDFView"
        xmlns:PageViewWPF="clr-namespace:O2S.Components.PDFView4NET.WPF;assembly=O2S.Components.PDFView4NET.WPF"
        x:Class="PDFView.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <PageViewWPF:PDFPageView x:Name="PdfControl" HorizontalAlignment="Left" Height="auto" VerticalAlignment="Top" Width="auto" PageDisplayLayout="Custom"  WorkMode="PanAndScan" HighlightFormFields="True"/>

    </Grid>
</Window>
cs 复制代码
using O2S.Components.PDFView4NET;
using System.Windows;

namespace PDFView
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    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破解版

百度网盘地址:链接:https://pan.baidu.com/s/1onJG6pkmXAR-pT1ZzeIy4A

提取码:dlfe

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

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

cs 复制代码
PdfToImage(".\\4.pdf", Environment.CurrentDirectory, "4", ImageFormat.Png, Definition.Eight);
cs 复制代码
/// <summary>
        /// PDF文档所有页全部转换为图片
        /// </summary>
        /// <param name="pdfInputPath">PDF文件流</param>
        /// <param name="imageOutputPath">图片输出路径</param>
        /// <param name="imageName">生成图片的名字</param>
        /// <param name="imageFormat">设置所需图片格式</param>
        /// <param name="definition">设置图片的清晰度,数字越大越清晰</param>
        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();
        }

        /// <summary>
        /// 转换的图片清晰度,1最不清醒,10最清晰
        /// </summary>
        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

相关推荐
阿幸软件杂货间17 小时前
最新PDF版本!Acrobat Pro DC 2025,解压即用版
pdf·adobe acrobat·acrobat
星空的资源小屋19 小时前
网易UU远程,免费电脑远程控制软件
人工智能·python·pdf·电脑
会飞的小菠菜1 天前
如何一次性将多个PPT幻灯片批量转换成PDF文档
pdf·powerpoint·ppt·批量·格式转换
somethingGoWay1 天前
wpf .netcore 导出pdf文件
pdf·wpf·.netcore
小白电脑技术1 天前
PDF教程|如何把想要的网页保存下来?
pdf·电脑
我没想到原来他们都是一堆坏人2 天前
通过Gen AI SDK调用gemini 2.5 pro,单独上传pdf文件 | ai agent 开发笔记 2025.9.2 Day 2
ai·google·pdf·sdk·gemini
AI视觉网奇2 天前
麒麟系统 doc转pdf
linux·运维·pdf
CodeCraft Studio2 天前
国产化PDF处理控件Spire.PDF教程:如何在 Java 中通过模板生成 PDF
java·python·pdf·spire.pdf·java创建pdf·从html创建pdf
janthinasnail3 天前
使用Docker安装Stirling-PDF(PDF工具)
docker·pdf
海斗星河万里长3 天前
ConvertAPI:PDF转Word的便捷之选
pdf