您可以使用iTextSharp库和System.Drawing.Imaging命名空间来将PDF文件转换为图像。以下是一个简单的示例:

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing.Imaging;

string pdfFile = "example.pdf";
Document doc = new Document();
PdfReader reader = new PdfReader(pdfFile);
int pageCount = reader.NumberOfPages;

for (int i = 1; i <= pageCount; i++)
{
    string outputFile = $"page{i}.png";
    using (FileStream fs = new FileStream(outputFile, FileMode.Create))
    {
        PdfDictionary page = reader.GetPageN(i);
        Rectangle cropBox = reader.GetCropBox(i);
        int rotation = page.GetAsNumber(PdfName.ROTATE)?.IntValue ?? 0;
        using (Bitmap bmp = new Bitmap((int)cropBox.Width, (int)cropBox.Height))
        {
            bmp.SetResolution(72, 72);
            using (Graphics gr = Graphics.FromImage(bmp))
            {
                gr.RotateTransform(-rotation);
                gr.TranslateTransform(-cropBox.Left, -cropBox.Top);
                PdfRenderHelper.RenderPage(page, gr);
                bmp.Save(fs, ImageFormat.Png);
            }
        }
    }
}

reader.Close();

此代码将每个PDF页面转换为PNG图像并将其保存在一个单独的文件中。您可以根据需要更改输出文件的名称和格式。请注意,此示例使用默认页面大小和方向,如果您需要处理不同大小或方向的页面,则需要进行修改

C# 将pdf多页文件转换为图片

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

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