当前位置: 首页 > news >正文

WPF 显示PDF、PDF转成图片

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

2.添加组件

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

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

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

<Windowxmlns="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>
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,使用异常

PdfToImage(".\\4.pdf", Environment.CurrentDirectory, "4", ImageFormat.Png, Definition.Eight);
/// <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() imageFormatg.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

http://www.lryc.cn/news/262723.html

相关文章:

  • CODESYS的Robotics_PickAndPlace_without_Depictor例程解释
  • 通过全流量分析Web业务性能好坏
  • 【C语言】自定义类型——枚举、联合体
  • 大模型自定义算子优化方案学习笔记:CUDA算子定义、算子编译、正反向梯度实现
  • 【密码学基础】Diffie-Hellman密钥交换协议
  • 最新AI绘画Midjourney绘画提示词Prompt教程
  • AI助力DevOps新时代
  • Spring之容器:IOC(2)
  • Spring 依赖查找知识点总结
  • html5新增特性
  • 4、APScheduler: 详解Scheduler种类用法、常见错误与解决方法【Python3测试任务管理总结】
  • 微服务实战系列之ZooKeeper(实践篇)
  • C++ 开发中为什么要使用继承
  • 2020蓝桥杯c组纸张大小
  • 【Image】图像处理
  • JAVA对文档加密
  • EmbedAI:一个可以上传文件训练自己ChatGPT的AI工具,妈妈再也不用担心我的GPT不会回答问题
  • runCatching异常捕获onSuccess/onFailure返回函数,Kotlin
  • IDEA报错处理
  • 使用动画曲线编辑器打造炫酷的3D可视化ACE
  • 使用 React 和 ECharts 创建地球模拟扩散和飞线效果
  • http状态码(一)400报错
  • 【深度学习目标检测】五、基于深度学习的安全帽识别(python,目标检测)
  • 芒果RT-DETR改进实验:深度集成版目标检测 RT-DETR 热力图来了!支持自定义数据集训练出来的模型
  • c语言实验八
  • ArcGIS Pro SDK文件选择对话框
  • ACT、NAT、NATPT和EASY-IP
  • HTML实现每天单词积累
  • 【ECMAScript笔记二】运算符分类,流程控制(顺序结构、分支结构、循环结构)
  • ShenYu网关注册中心之Zookeeper注册原理