在开发过程中遇到的:pdf文件中包含文字、表格、图片等,需要将图片取出来存入本地,如何操作呢?
在面向百度编程的过程中,发现大部分东西要么要钱,要么就存在各种bug等,所以.....推荐使用iTextSharp 操作pdf
以下就是整个操作过程:
/// <summary> /// 编写提取图片的方法,代码如下: /// </summary> /// <param name="pdfFile"></param> public static void ExtractImage(string pdfFile) { PdfReader pdfReader = new PdfReader(pdfFile); for (int pageNumber = 1; pageNumber <= pdfReader.NumberOfPages; pageNumber++) { PdfReader pdf = new PdfReader(pdfFile); PdfDictionary pg = pdf.GetPageN(pageNumber); PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES)); PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT)); try { foreach (PdfName name in xobj.Keys) { PdfObject obj = xobj.Get(name); if (obj.IsIndirect()) { PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj); string width = tg.Get(PdfName.WIDTH).ToString(); string height = tg.Get(PdfName.HEIGHT).ToString(); //bug 出现位置:未将对象引用.... ImageRenderInfo imgRI = ImageRenderInfo.CreateForXObject(new GraphicsState(), (PRIndirectReference)obj, tg); RenderImage(imgRI); } } } catch (Exception ex) { Console.WriteLine(ex); continue; } } }
调用过程:
public ActionResult Index() { string path = @"C:UsersAdministratorDesktop20190415164629.pdf"; Pdftoimg.ExtractImage(path); return View(); }
特别注意:
1.必须确保path 的路径指定的地方有pdf文件
2.在NuGet中拉取的最新的包可能用不了:建议使用稳定版本中的稳定版本
如果是在控制台应用程序中:则可以使用PdfiumViewer
public class PdfiumViewerTool { /// <summary> /// 将PDF转换为图片 /// </summary> /// <param name="pdfPath">pdf文件位置</param> /// <param name="pageNumber">pdf文件张数</param> /// <param name="size">pdf文件尺寸</param> /// <param name="outputPath">输出图片位置与名称</param> public void RenderPage(string pdfPath, int pageNumber, System.Drawing.Size size, string outputPath, int dpi = 300) { using (var document = PdfiumViewer.PdfDocument.Load(pdfPath)) using (var stream = new FileStream(outputPath, FileMode.Create)) using (var image = GetPageImage(pageNumber, size, document, dpi)) { image.Save(stream, ImageFormat.Jpeg); } } private static Image GetPageImage(int pageNumber, Size size, PdfiumViewer.PdfDocument document, int dpi) { return document.Render(pageNumber - 1, size.Width, size.Height, dpi, dpi, PdfRenderFlags.Annotations); } }
调用:
class Program { static void Main(string[] args) { string path = @"C:/Users/Administrator/Desktop/20190415164629.pdf"; var pdf = PdfiumViewer.PdfDocument.Load(path); var pdfpage = pdf.PageCount; var pagesizes = pdf.PageSizes; PdfiumViewerTool p2p = new PdfiumViewerTool(); 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; p2p.RenderPage(path, i, size, @"C:UsersAdministratorDesktop" + i + @".jpg"); } } }
一般会出现这个问题:
解决办法:找到程序属性,更改平台目标即可
以上内容如有侵权:请联系作者删除