基于WPF實(shí)現(xiàn)PDF的顯示與轉(zhuǎn)換
1.NuGet安裝
O2S.Components.PDFView4NET.WPF

2.添加組件
工具箱中,空白處 右鍵,選擇項(xiàng)

WPF組件 界面,選擇NuGet安裝庫(kù)對(duì)面路徑下的 O2S.Components.PDFView4NET.WPF.dll



3.引入組件命名空間并使用
<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>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轉(zhuǎn)換成圖片
聽說有破解版(無水?。业搅藘蓚€(gè)版本兩個(gè)不同O2S.Components.PDFRender4NET破解版

版本2.0.1.0 對(duì)于測(cè)試PDF,使用正常
版本4.5.1.2 對(duì)于測(cè)試PDF,使用異常
PdfToImage(".\\4.pdf", Environment.CurrentDirectory, "4", ImageFormat.Png, Definition.Eight);/// <summary>
/// PDF文檔所有頁(yè)全部轉(zhuǎn)換為圖片
/// </summary>
/// <param name="pdfInputPath">PDF文件流</param>
/// <param name="imageOutputPath">圖片輸出路徑</param>
/// <param name="imageName">生成圖片的名字</param>
/// <param name="imageFormat">設(shè)置所需圖片格式</param>
/// <param name="definition">設(shè)置圖片的清晰度,數(shù)字越大越清晰</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>
/// 轉(zhuǎn)換的圖片清晰度,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.官方代碼庫(kù)鏈接
PDFView4NET https://github.com/o2solutions/pdfview4net/tree/main
到此這篇關(guān)于基于WPF實(shí)現(xiàn)PDF的顯示與轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)WPF PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# WinForm程序設(shè)計(jì)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了C# WinForm程序設(shè)計(jì)簡(jiǎn)單計(jì)算器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
C#調(diào)用FFmpeg操作音視頻的實(shí)現(xiàn)示例
本文主要介紹了C#調(diào)用FFmpeg操作音視頻的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
在C# WPF下自定義滾動(dòng)條ScrollViewer樣式的操作
這篇文章主要介紹了在C# WPF下自定義滾動(dòng)條ScrollViewer樣式的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
C#?wpf實(shí)現(xiàn)截屏框熱鍵截屏的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#?wpf實(shí)現(xiàn)截屏框熱鍵截屏的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以學(xué)習(xí)一下2023-09-09
C# DataTable中Compute方法用法集錦(數(shù)值/字符串/運(yùn)算符/表等操作)
這篇文章主要介紹了C# DataTable中Compute方法用法,總結(jié)分析了DataTable中Compute方法常見的數(shù)值運(yùn)算操作、字符串操作、運(yùn)算符操作、表運(yùn)算等相關(guān)技巧,需要的朋友可以參考下2016-06-06

