PDF转换成JPEG格式

 

使用NuGet程序包下载安装 O2S.Components.PDFRender4NET.dll

 

using O2S.Components.PDFRender4NET; //引用 O2S.Components.PDFRender4NET.dll 

 

/// <summary>
/// PDF转JPEG
/// </summary>
/// <param name="pdfInputPath">PDF路径</param>
/// <param name="imageOutputPath">JPEG保存路径</param>
/// <param name="imageName">JPEG文件名</param>
/// <param name="startPageNum">PDF开始页</param>
/// <param name="endPageNum">PDF结束页</param>
/// <param name="imageFormat">转换图片格式</param>
public static void PdfToJPEG(string pdfInputPath, string imageOutputPath, string imageName, int startPageNum, int endPageNum, ImageFormat imageFormat)
{
try
{


PDFFile pdfFile = PDFFile.Open(pdfInputPath);
if (!Directory.Exists(imageOutputPath))
{
Directory.CreateDirectory(imageOutputPath);
}
// validate pageNum
if (startPageNum <= 0)
{
startPageNum = 1;
}
if (endPageNum > pdfFile.PageCount)
{
endPageNum = pdfFile.PageCount;
}
if (startPageNum > endPageNum)
{
int tempPageNum = startPageNum;
startPageNum = endPageNum;
endPageNum = startPageNum;
}
// start to convert each page
if (endPageNum == 1)
{
// 引用 Drawing;
Bitmap pageImage = pdfFile.GetPageImage(1 - 1, 56 * 5);
pageImage.Save(imageOutputPath + imageName + "." + imageFormat.ToString(), imageFormat);
pageImage.Dispose();
}
else
{
for (int i = startPageNum; i <= endPageNum; i++)
{
Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * 5);
if (i == 1)
{
pageImage.Save(imageOutputPath + imageName + "." + imageFormat.ToString(), imageFormat);
}
else
{
int ii = i - 1;
pageImage.Save(imageOutputPath + imageName + "(" + ii.ToString() + ")" + "." + imageFormat.ToString(), imageFormat);
}
pageImage.Dispose();
}
}
pdfFile.Dispose();
}
catch (Exception)
{

throw;
}
}

posted @ 2024-06-15 15:42  KevinSteven  阅读(14)  评论(0编辑  收藏  举报