实现思路:
步骤一、根据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