You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.7 KiB
60 lines
1.7 KiB
using PdfiumViewer;
|
|
using System.Drawing.Imaging;
|
|
using System.Drawing;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.IO;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Shentun.Utilities
|
|
{
|
|
public class PdfiumConverter
|
|
{
|
|
public static List<string> ConvertPdfToImages(string pdfBase64)
|
|
{
|
|
List<string> base64Images = new List<string>();
|
|
//try
|
|
//{
|
|
|
|
if (pdfBase64.Contains("base64,"))
|
|
{
|
|
pdfBase64 = pdfBase64.Substring(pdfBase64.IndexOf(",") + 1);
|
|
}
|
|
// 将Base64字符串转换为字节数组
|
|
byte[] pdfBytes = Convert.FromBase64String(pdfBase64);
|
|
Stream pdfStream = new MemoryStream(pdfBytes);
|
|
|
|
using var document = PdfDocument.Load(pdfStream);
|
|
|
|
for (int i = 0; i < document.PageCount; i++)
|
|
{
|
|
// 将页面转换为图片
|
|
using (Image image = document.Render(i, 600, 600, true))
|
|
{
|
|
// 将图片转换为Base64字符串
|
|
string base64 = ImageToBase64(image, ImageFormat.Jpeg);
|
|
base64Images.Add(base64);
|
|
}
|
|
}
|
|
|
|
//}
|
|
//catch (Exception ex)
|
|
//{
|
|
|
|
//}
|
|
|
|
return base64Images;
|
|
}
|
|
|
|
private static string ImageToBase64(Image image, ImageFormat format)
|
|
{
|
|
using (MemoryStream ms = new MemoryStream())
|
|
{
|
|
image.Save(ms, format);
|
|
byte[] imageBytes = ms.ToArray();
|
|
return Convert.ToBase64String(imageBytes);
|
|
}
|
|
}
|
|
}
|
|
}
|