免费建设网站赚钱,微信推广引流平台,网站购买广告位,用qq邮箱做网站实现思路#xff1a;
步骤一、根据PDF地址下载pdf文件保存为临时文件#xff0c;获得pdf文件的byte[]数组
/// 从指定的 URL 下载 PDF 文件public byte[] DownloadPdf(string url){try{using (WebClient client new WebClient()){return client.DownloadData(url);}}catc…实现思路
步骤一、根据PDF地址下载pdf文件保存为临时文件获得pdf文件的byte[]数组
/// 从指定的 URL 下载 PDF 文件public byte[] DownloadPdf(string url){try{using (WebClient client new WebClient()){return client.DownloadData(url);}}catch (Exception ex){Console.WriteLine($下载PDF异常: {ex.Message});return null;}}
步骤二、初始化pdfDocument类并加载PDF文档遍历pdf文件一页生成一个jpg图片并保存图片 string url infoEntity.ReportUrl; //pdf地址byte[] pdfBytes DownloadPdf(url);string tempPdfPath Path.GetTempFileName();File.WriteAllBytes(tempPdfPath, pdfBytes);// 初始化一个PdfDocument类实例并加载PDF文档
PdfDocument doc new PdfDocument();
doc.LoadFromFile(tempPdfPath);
// 遍历PDF每一页for (int i 0; i doc.Pages.Count; i){Report_Pic report_Pic new Report_Pic();// 将PDF页转换成Bitmap图形System.Drawing.Image bmp doc.SaveAsImage(i);string imgPath Application.StartupPath;// 将Bitmap图形保存为jpg格式的图片 string pngPath ${imgPath}\\img\\{infoEntity.ReportFileName.Replace(.pdf,)}_page_{i}.jpg;if (File.Exists(pngPath)){try{File.Delete(pngPath);}catch (Exception ex){Log.error($删除图片时出错: {ex.Message});}}bmp.Save(pngPath, System.Drawing.Imaging.ImageFormat.Jpeg);Log.info(PDF地址tempPdfPath 图片地址 pngPath);string imageBase64Str ConvertImageToBase64(bmp); //图片内容[Base64编码]
Log.info(图片Base64编码imageBase64Str);//删除图片
File.Delete(pngPath);
}
// 删除临时文件
File.Delete(tempPdfPath);
步骤三、将每张图片转换成Base64编码 // 将图片转换为 Base64 编码static string ConvertImageToBase64(Image image){using (MemoryStream ms new MemoryStream()){image.Save(ms, ImageFormat.Jpeg);byte[] imageBytes ms.ToArray();return Convert.ToBase64String(imageBytes);}} 备注
目标框架.Net Framework 4
引用库
using System.Net;
using System.Drawing; //版本 4.0.0.0
using System.Drawing.Imaging;
using Spire.Pdf; //版本8.6.1.0