pdf 转 png
https://gitee.com/GVIEW/PdfiumViewer.Core
NuGet
Install-Package PdfiumViewer.Core
Install-Package PdfiumViewer.Native.x86_64.v8-xfa
Install-Package PdfiumViewer.Native.x86.v8-xfa
调用:
List<string>listFile= pdfHelper.PdfToImagePdfViewer(ofd.FileName);
源码类 pdfHelper
public List<string> PdfToImagePdfViewer(string pdfFilePath)
{
List<string> result = new List<string>();
string fullFilePath = Path.GetFullPath(pdfFilePath);
PdfToPic(pdfFilePath, "D:\\", ImageFormat.Png, ref result);
return result;
}
/// <summary>
/// <param name="filePath">pdf文件路径</param>
/// <param name="outFilePath">picture输出文件路径</param>
/// <param name="image">文件格式:Jpeg (转换后文件格式,如:Jpeg,Png等)</param>
/// </summary>
public void PdfToPic(string filePath, string outFilePath, ImageFormat image, ref List<string> list)
{
var pdf = PdfDocument.Load(filePath);
var pdfpage = pdf.PageCount;
var pagesizes = pdf.PageSizes;
outFilePath = $"{outFilePath}\\{DateTime.Now.ToString("yyyyMMdd-HHmmss")}";
for (int i = 1; i <= pdfpage; i++)
{
Size size = new Size();
size.Height = (int)pagesizes[(i - 1)].Height;
size.Width = (int)pagesizes[(i - 1)].Width;
//可以把".jpg"写成其他形式 RenderPage(filePath, i, size, picPath);
string outFile = $"{outFilePath}({i}).{image}";
RenderPage(filePath, i, size, outFile, image);
list.Add(outFile);
}
}
/// <summary>
/// 渲染图片
/// </summary>
/// <param name="pdfPath"></param>
/// <param name="pageNumber"></param>
/// <param name="size"></param>
/// <param name="outputPath"></param>
/// <param name="mat"></param>
/// <param name="dpi"></param>
private void RenderPage(string pdfPath, int pageNumber, System.Drawing.Size size, string outputPath, ImageFormat mat, int dpi = 300)
{
using (var document = PdfDocument.Load(pdfPath))
using (var stream = new FileStream(outputPath, FileMode.Create))
using (var image = GetPageImage(pageNumber, size, document, dpi))
image.Save(stream, mat);
}
/// <summary>
/// 根据页数转换图片
/// </summary>
/// <param name="pageNumber"></param>
/// <param name="size"></param>
/// <param name="document"></param>
/// <param name="dpi"></param>
/// <returns></returns>
private static System.Drawing.Image GetPageImage(int pageNumber, Size size, PdfDocument document, int dpi)
{
return document.Render(pageNumber - 1, size.Width, size.Height, dpi, dpi, PdfRenderFlags.CorrectFromDpi);
}
public bool ImageToPdf(List<string> ImageList, string pdfFileName)
{
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 25, 25, 25, 25);
try
{
iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(pdfFileName, FileMode.Create, FileAccess.ReadWrite));
document.Open();
iTextSharp.text.Image image;
foreach (var item in ImageList)
{
if (String.IsNullOrEmpty(item)) break;
image = iTextSharp.text.Image.GetInstance(item);
if (image.Height > iTextSharp.text.PageSize.A4.Height - 25)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
}
else if (image.Width > iTextSharp.text.PageSize.A4.Width - 25)
{
image.ScaleToFit(iTextSharp.text.PageSize.A4.Width - 25, iTextSharp.text.PageSize.A4.Height - 25);
}
image.Alignment = iTextSharp.text.Image.ALIGN_MIDDLE;
document.NewPage();
document.Add(image);
}
document.Close();
return true;
}
catch (Exception ex)
{
Console.WriteLine("转换失败,原因:" + ex.Message);
}
return false;
}