您可以使用PdfSharpCore库将PDF转换为图像。以下是一个简单的示例代码:

  1. 首先,您需要在项目中安装PdfSharpCore库。您可以使用NuGet包管理器来安装它。在Visual Studio中,右键单击您的项目,然后选择'管理NuGet程序包'。在搜索框中输入'PdfSharpCore',然后安装它。

  2. 创建一个名为'PdfToImageConverter'的类,其中包含以下方法:

using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public static class PdfToImageConverter
{
    public static void Convert(string pdfFilePath, string outputFolderPath, int dpi)
    {
        using (var document = PdfReader.Open(pdfFilePath))
        {
            for (int i = 0; i < document.PageCount; i++)
            {
                var page = document.Pages[i];
                var image = page.Render(dpi, dpi, PdfRenderFlags.CorrectFromDpi);
                var fileName = Path.GetFileNameWithoutExtension(pdfFilePath) + "_" + (i + 1) + ".png";
                var outputFilePath = Path.Combine(outputFolderPath, fileName);
                image.Save(outputFilePath, ImageFormat.Png);
            }
        }
    }
}
  1. 在您的代码中调用上述'Convert'方法,如下所示:
PdfToImageConverter.Convert("path/to/your/pdf/file.pdf", "path/to/output/folder", 300);

其中,'path/to/your/pdf/file.pdf' 是您要转换的PDF文件的路径,'path/to/output/folder' 是您要保存生成的图像的文件夹的路径,300是图像的DPI。

这应该会将您的PDF转换为PNG格式的图像,并将它们保存在指定的输出文件夹中。您可以根据需要更改生成的图像的格式和DPI。

使用PdfSharpCore库将PDF文件转换为图片 - .NET Core 5.0教程

原文地址: https://www.cveoy.top/t/topic/okzT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录