700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > C# pdf转成图片(可转成jpg png等格式)

C# pdf转成图片(可转成jpg png等格式)

时间:2024-01-26 09:04:19

相关推荐

C# pdf转成图片(可转成jpg png等格式)

源码下载处

一.工具界面(支持4.0平台,建议大家用4.0,不会报错)

二.需要的dll文件(NuGet直接下载)【如果没找到这两个文件,我放在我的资源大家免费下,如果不是免费的大家给我说我改成不要积分就可下载的】

ImageResizer.Plugins.PdfiumRenderer.Pdfium.Dll

PdfiumViewer.Dll

三.代码

using PdfiumViewer;using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Drawing.Imaging;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace PDFtoJPG{public partial class Form1 : Form{public Form1(){InitializeComponent();}/// <summary>/// PDF的textbox后面对应的按钮/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void butPDF_Click(object sender, EventArgs e){OpenFileDialog dialog = new OpenFileDialog();if (dialog.ShowDialog() == DialogResult.OK){//FileName:显示文件路径+名字this.txtPDF.Text = dialog.FileName;}}/// <summary>/// 图片的textbox后面对应的按钮/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void butTUPIAN_Click(object sender, EventArgs e){#region 测试SafeFileName,不用关//OpenFileDialog dialog = new OpenFileDialog();//if (dialog.ShowDialog() == DialogResult.OK)//{// //SafeFileName:只显示文件名字// this.txtJPG.SelectedText = dialog.SafeFileName;//}#endregionSystem.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();if (folder.ShowDialog() == DialogResult.OK){//SelectedPath:获取文件夹绝对路径,显示在textbox里面this.txtJPG.Text = folder.SelectedPath;}}/// <summary>/// PDF转图片格式的button按钮/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void butPDFtoTUPIAN_Click(object sender, EventArgs e){string strpdfPath = txtPDF.Text.ToString();var pdf = PdfiumViewer.PdfDocument.Load(strpdfPath);var pdfpage = pdf.PageCount;var pagesizes = pdf.PageSizes;for (int i = 1; i <= pdfpage; i++){Size size = new Size();size.Height = (int)pagesizes[(i - 1)].Height;size.Width = (int)pagesizes[(i - 1)].Width;//可以把".jpg"写成其他形式RenderPage(strpdfPath, i, size, txtJPG.Text.ToString() + "\\"+ txtName.Text.ToString() + @".jpg");}MessageBox.Show("转换成功!");}public void RenderPage(string pdfPath, int pageNumber, System.Drawing.Size size, string outputPath, int dpi = 300){using (var document = PdfiumViewer.PdfDocument.Load(pdfPath))using (var stream = new FileStream(outputPath, FileMode.Create))using (var image = GetPageImage(pageNumber, size, document, dpi)){image.Save(stream, ImageFormat.Jpeg);}}/// <summary>/// /// </summary>/// <param name="pageNumber">pdf文件张数</param>/// <param name="size">pdf文件尺寸</param>/// <param name="document">pdf文件位置</param>/// <param name="dpi"></param>/// <returns></returns>private static System.Drawing.Image GetPageImage(int pageNumber, Size size, PdfiumViewer.PdfDocument document, int dpi){return document.Render(pageNumber - 1, size.Width, size.Height, dpi, dpi, PdfRenderFlags.Annotations);}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。