将pdf文件转换成图片文件
Posted skywalker037
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将pdf文件转换成图片文件相关的知识,希望对你有一定的参考价值。
将pdf文件转换成图片文件
概要
现在手头的项目有一个需求是要从微信卡包里面拉取发票下来,把它的PDF文档保存下来,并且转换成图片保存在本地,我以前也使用过像Components.PDFRender4NET.dll这些PDF插件,可是这些都测试了一下,或多或少有一些地方不是很满意,转换出来的发票图片有点模糊。最后同事推荐我使用Acrobat,结果真是让我惊喜。作为Adobe官方出品产品,其转换效率要比其他的DLL要快多了,而且应该更可靠一些。所以迫不及待的想和大家分享一下我的使用经验。
使用该插件基本要求:
工程中添加COM引用:Adobe Acrobat 9.0 Type Library(必须装了Adobe Acrobat9.0才会有)。
思路:
1、需要用到的COM对象:
1)CAcroPDDoc:Acrobat文档对象。
2)CAcroPDPage:页对象。
3)CAcroRect:用来描述页中一个矩形区域的对象。
4)CAcroPoint:实际上代表的是Size。
2、转换过程:
1)打开文档。
2)取出每一页。
3)获取每一页的大小,生成一个表示该页的矩形区域。
4)将当前页的指定区域编码成图片,并且复制到剪贴板中。
5)将剪贴板中的图片取出,保存为图片文件。
转换函数代码:
1 public static class PdfToImag 2 { 3 private class Info 4 { 5 public CAcroPDPage Page; 6 7 public CAcroRect Rect; 8 9 public double Resolution; 10 11 public Bitmap Bitmap = null; 12 } 13 14 [CompilerGenerated] 15 private static class Wo__0 16 { 17 public static CallSite<Func<CallSite, object, CAcroPDPage>> Wp__0; 18 19 public static CallSite<Func<CallSite, object, CAcroPoint>> Wp__1; 20 } 21 /// <summary> 22 /// 23 /// </summary> 24 /// <param name="filePath" pdf路径含扩展名></param> 25 /// <param name="imagPath" 图片存放路径含扩展名></param> 26 /// <param name="resolution"></param> 27 /// <returns></returns> 28 public static bool ConvertPDF2Image(string filePath,string imagPath, double resolution = 6.0) 29 { 30 CAcroPDDoc cAcroPDDoc = null; 31 CAcroPDPage cAcroPDPage = null; 32 CAcroRect cAcroRect = null; 33 CAcroPoint cAcroPoint = null; 34 Image result; 35 try 36 { 37 cAcroPDDoc = (AcroPDDoc)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("FF76CB60-2E68-101B-B02E-04021C009402"))); 38 bool flag = !cAcroPDDoc.Open(filePath); 39 if (flag) 40 { 41 throw new FileNotFoundException(); 42 } 43 bool flag2 = resolution <= 0.0; 44 if (flag2) 45 { 46 resolution = 1.0; 47 } 48 if (PdfToImag.Wo__0.Wp__0 == null) 49 { 50 PdfToImag.Wo__0.Wp__0 = CallSite<Func<CallSite, object, CAcroPDPage>>.Create(Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(CAcroPDPage), typeof(PdfToImag))); 51 } 52 cAcroPDPage = PdfToImag.Wo__0.Wp__0.Target(PdfToImag.Wo__0.Wp__0, cAcroPDDoc.AcquirePage(0)); 53 if (PdfToImag.Wo__0.Wp__1 == null) 54 { 55 PdfToImag.Wo__0.Wp__1 = CallSite<Func<CallSite, object, CAcroPoint>>.Create(Binder.Convert(CSharpBinderFlags.ConvertExplicit, typeof(CAcroPoint), typeof(PdfToImag))); 56 } 57 cAcroPoint = PdfToImag.Wo__0.Wp__1.Target(PdfToImag.Wo__0.Wp__1, cAcroPDPage.GetSize()); 58 cAcroRect = (AcroRect)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("6D12C400-4E34-101B-9CA8-9240CE2738AE"))); 59 int num = (int)((double)cAcroPoint.x * resolution); 60 int num2 = (int)((double)cAcroPoint.y * resolution); 61 cAcroRect.Left = 0; 62 cAcroRect.right = (short)num; 63 cAcroRect.Top = 0; 64 cAcroRect.bottom = (short)num2; 65 result = PdfToImag.GetclipboardData(cAcroPDPage, cAcroRect, resolution); 66 result.Save(imagPath, System.Drawing.Imaging.ImageFormat.Jpeg); 67 } 68 catch (Exception ex) 69 { 70 return false; 71 } 72 finally 73 { 74 bool flag3 = cAcroPDDoc != null; 75 if (flag3) 76 { 77 if (cAcroPDDoc != null) 78 { 79 cAcroPDDoc.Close(); 80 } 81 } 82 bool flag4 = cAcroPDPage != null; 83 if (flag4) 84 { 85 Marshal.ReleaseComObject(cAcroPDPage); 86 } 87 bool flag5 = cAcroRect != null; 88 if (flag5) 89 { 90 Marshal.ReleaseComObject(cAcroRect); 91 } 92 bool flag6 = cAcroPDDoc != null; 93 if (flag6) 94 { 95 Marshal.ReleaseComObject(cAcroPDDoc); 96 } 97 bool flag7 = cAcroPoint != null; 98 if (flag7) 99 { 100 Marshal.ReleaseComObject(cAcroPoint); 101 } 102 } 103 return true; 104 } 105 106 private static Image GetclipboardData(CAcroPDPage page, CAcroRect pdfRect, double resolution) 107 { 108 PdfToImag.Info info = new PdfToImag.Info 109 { 110 Page = page, 111 Rect = pdfRect, 112 Resolution = resolution 113 }; 114 Thread thread = new Thread(new ParameterizedThreadStart(PdfToImag.CopyToClipboard)); 115 thread.TrySetApartmentState(ApartmentState.STA); 116 thread.Start(info); 117 thread.Join(); 118 return info.Bitmap; 119 } 120 121 [STAThread] 122 private static void CopyToClipboard(object arge) 123 { 124 PdfToImag.Info info = arge as PdfToImag.Info; 125 CAcroPDPage page = info.Page; 126 CAcroRect rect = info.Rect; 127 double resolution = info.Resolution; 128 page.CopyToClipboard(rect, 0, 0, (short)(100.0 * resolution)); 129 IDataObject dataObject = Clipboard.GetDataObject(); 130 bool dataPresent = dataObject.GetDataPresent(DataFormats.Bitmap); 131 if (dataPresent) 132 { 133 Bitmap bitmap = (Bitmap)dataObject.GetData(DataFormats.Bitmap); 134 info.Bitmap = bitmap; 135 } 136 } 137 }
以上是关于将pdf文件转换成图片文件的主要内容,如果未能解决你的问题,请参考以下文章