推荐使用ImageMagick
再使用这个之前使用过Spire.PDF结果是收费版,有水印。
缺点:需要安装ghostscript.exe
,只支持.Net Framework
废话不多说,代码如下
public static List<string> ConvertPdfToImage(string pdfFilePath)
{
List<string> outputPaths = new List<string>();
MagickReadSettings settings = new MagickReadSettings();
settings.Density = new Density(300, 300); //设置质量
using (MagickImageCollection images = new MagickImageCollection())
{
images.Read(pdfFilePath, settings);
for (int i = 0; i < images.Count; i++)
{
MagickImage image = (MagickImage)images[i];
image.Format = MagickFormat.Png;
string imagePath = System.Windows.Forms.Application.StartupPath + $"\\Images\\Import{DateTime.Now.ToString("HH:mm:ss").Replace(":","")}({i}).png";
image.Write(imagePath);
outputPaths.Add(imagePath);
}
}
return outputPaths;
}