/// <summary> /// 从pdf中导出图片 /// </summary> /// <param name="pdfFilePath"></param> /// <param name="outDir"></param> public static void DealPdf(string pdfFilePath, string outDir) { //加载PDF文档 PdfDocument doc = new PdfDocument(); doc.LoadFromFile(pdfFilePath); FileInfo file = new FileInfo(pdfFilePath); if (!file.Exists) { Console.WriteLine($"pdf文件不存在"); } string path = Path.Combine(outDir, file.Name.Replace(".pdf", "")); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } for (int i = 0; i < doc.Pages.Count; i++) { Console.WriteLine($"处理第{i+1}页面图片"); // 实例化一个Spire.Pdf.PdfPageBase对象 PdfPageBase page = doc.Pages[i]; var texts = page.FindAllText().Finds.Where(a => (a.Size.Height >= 9 && a.Size.Height <= 10)) .OrderBy(a => a.Bounds, new PointComparer()).ToList(); if (!texts.Any()) { texts = page.FindAllText().Finds.Where(a => (a.Size.Height >= 6 && a.Size.Height <= 7)) .OrderBy(a => a.Bounds, new PointComparer()).ToList(); } var images = page.ImagesInfo.Where(a => a.Bounds.Size.Width < 300) .OrderBy(a => a.Bounds, new PointComparer()).ToList(); for (int j = 0; j < texts.Count; j++) { try { images[j].Image.Save(Path.Combine(path, $"{i + 1}_{j + 1}_{texts[j].MatchText ?? texts[j].SearchText}.jpg")); } catch (Exception e) { } } //// 获取所有pages里面的图片 //Image[] images = page.ExtractImages(); //if (images != null && images.Length > 0) //{ // listImages.AddRange(images); //} } }