c# web PDF 转图片总结

分享是一种美德, 感谢网上分享代码的朋友

项目中需要将 PDF 转成图片,在网上找了很多方法,这里对几种方法做个总结

1. spire.pdf

这个是我正在用的, 需要库 Spire.License.dll 和 Spire.Pdf.dll, 这个库网上有不带水印的破解版,但是破解库在web上加载会报错,只能用正版带水印。 转换目前看起来还行,800多页能正常生成,缺陷是有水印,不过我以找到去掉水印的方法。

非常感谢这位朋友分享的去水印方法:https://blog.csdn.net/weixin_44296167/article/details/100011329?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.channel_param

去掉水印方法: 

在调用转换方法前,先修改License中的注册码 , 这个方法完全有效

 SpireHelper SH = new SpireHelper();
 SH.ActivateMemoryPatching();

// 转换图片
 PDFConvertPic pdf = new PDFConvertPic();
 int picNum = pdf.PDFtoImg(pdfFileName, DPI, bigName, smallName);

修改注册码

public static class SpireHelper
    {
        public static void ActivateMemoryPatching()
        {
            Assembly[] arr = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assembly in arr)
            {
                if (assembly.FullName.StartsWith("Spire.License"))
                    ActivateForAssembly(assembly);
            }
            AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(ActivateOnLoad);
        }
 
        private static void ActivateOnLoad(object sender, AssemblyLoadEventArgs e)
        {
            string Name = e.LoadedAssembly.FullName;
            if (Name.IndexOf("Spire.License") != -1)
                ActivateForAssembly(e.LoadedAssembly);
        }
 
        private static void ActivateForAssembly(Assembly assembly)
        {
            bool isFound = false;
 
            Type[] arrType = null;
            try
            {
                arrType = assembly.GetTypes();
            }
            catch (ReflectionTypeLoadException err)
            {
                arrType = err.Types;
            }
 
            MethodInfo miLicensed = typeof(SpireHelper).GetMethod("InvokeMe", BindingFlags.NonPublic | BindingFlags.Static);
            MethodInfo miEvaluation = null;
 
            foreach (Type type in arrType)
            {
                if (isFound) break;
 
                if (type == null) continue;
 
                MethodInfo[] arrMInfo = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Static);
 
                foreach (MethodInfo info in arrMInfo)
                {
                    if (isFound) break;
 
                    try
                    {
                        string strMethod = info.ToString();
                        if ((strMethod.StartsWith("Byte[]")) && (strMethod.IndexOf("(System.String)") > 0))
                        {
                            miEvaluation = info;
                            MemoryPatching(miEvaluation, miLicensed);
                            isFound = true;
                            break;
                        }
                    }
                    catch
                    {
                        throw new InvalidOperationException("MemoryPatching for " + assembly.FullName + " failed !");
                    }
                }
            }
 
            string LData = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiIHN0YW5kYWxvbmU9InllcyI/Pgo8TGljZW5zZSBLZXk9ImNyOERaN1hKMkR5MUs2UUJBTkRPSVRLdlpjTzZkelVod2lsSHBnVlluQ3k0cXlHV2V6TFZubFJGeFAxNU1mSWZnUmdNWm1XaEdOQWRFNFRqZWZnQ1ovbFR2b1BkSXRIbDZXdDVBNWk1TVhFbnFkQnVPMUthRnovRFFzYUdWTGhzdjlySG1ybnRxSElFRGxJeGRxYUpNcGtLb0Frd1A3d1N6T01KMVkrbUNmVTVVRmV6REwvTjd1enJ4M0Y0d2I1SGErd0E2VFQ5VFJ3SzAzejlFS01aRmwzU1lSL3o0YVU3TE0wZFNYWTlqU0ZKZ2dqZlZzRFVLaUJyVm5td1ljaXVyOUVrYmw5Q3RaWTAzdG1yZm01QlplKzZnaHRFTm4wb2gzMzh0WlJleWpjcjc0QWs3MWhnWWtuTE9CQzE1VllmalhzcXVBVW13MlI2TWNWMlBPT2JyY1RSYlhBZ3pvUWJPeWQ4U2JFWmN3aE43NktQd1dzUVFTMUowdGlZSFVLeE9tMnQ0ZkJWMGhQVmhhOUI4Y0swNHFKUVp0MDBaMWNKRGEwd2I4VWx6RWs5QkhVVzJlbk9mVDE0UnlIQ2krWUdlbVBLY2RDUXJoMXpyWVRGN0ltb0x4N3h1NGV2RFRZc2xzV0JrbFFJb3g4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HVjdD4KICAgIDwvUHJvZHVjdHM+CiAgICA8SXNzdWVyPgogICAgICAgIDxOYW1lPklzc3VlcjwvTmFtZT4KICAgICAgICA8RW1haWw+aXNzdWVyQGlzc3Vlci5jb208L0VtYWlsPgogICAgICAgIDxVcmw+aHR0cDovL3d3dy5pc3N1ZXIuY29tPC9Vcmw+CiAgICA8L0lzc3Vlcj4KPC9MaWNlbnNlPg==";
 
            //Spire.License.LicenseProvider.SetLicenseKey(LData);
            Type type2 = assembly.GetType("Spire.License.LicenseProvider");
            MethodInfo mi = type2.GetMethod("SetLicenseKey", new Type[] { typeof(String) });
            mi.Invoke(Activator.CreateInstance(type2, null), new String[] { LData });
        }
 
 
        private static byte[] InvokeMe(String string_0)
        {
            return Convert.FromBase64String("PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiIHN0YW5kYWxvbmU9InllcyI/Pgo8TGljZW5zZSBLZXk9IngiIFZlcnNpb249IjkuOSI+CiAgICA8VHlwZT5SdW50aW1lPC9UeXBlPgogICAgPFVzZXJuYW1lPlVzZXJOYW1lPC9Vc2VybmFtZT4KICAgIDxFbWFpbD5lTWFpbEBob3N0LmNvbTwvRW1haWw+CiAgICA8T3JnYW5pemF0aW9uPk9yZ2FuaXphdGlvbjwvT3JnYW5pemF0aW9uPgogICAgPExpY2Vuc2VkRGF0ZT4yMDE2LTAxLTAxVDEyOjAwOjAwWjwvTGljZW5zZWREYXRlPgogICAgPEV4cGlyZWREYXRlPjIwOTktMTItMzFUMTI6MDA6MDBaPC9FeHBpcmVkRGF0ZT4KICAgIDxQcm9kdWN0cz4KICAgICAgICA8UHJvZHVjdD4KICAgICAgICAgICAgPE5hbWU+U3BpcmUuT2ZmaWNlIFBsYXRpbnVtPC9OYW1lPgogICAgICAgICAgICA8VmVyc2lvbj45Ljk5PC9WZXJzaW9uPgogICAgICAgICAgICA8U3Vic2NyaXB0aW9uPgogICAgICAgICAgICAgICAgPE51bWJlck9mUGVybWl0dGVkRGV2ZWxvcGVyPjk5OTk5PC9OdW1iZXJPZlBlcm1pdHRlZERldmVsb3Blcj4KICAgICAgICAgICAgICAgIDxOdW1iZXJPZlBlcm1pdHRlZFNpdGU+OTk5OTk8L051bWJlck9mUGVybWl0dGVkU2l0ZT4KICAgICAgICAgICAgPC9TdWJzY3JpcHRpb24+CiAgICAgICAgPC9Qcm9kdWN0PgogICAgPC9Qcm9kdWN0cz4KICAgIDxJc3N1ZXI+CiAgICAgICAgPE5hbWU+SXNzdWVyPC9OYW1lPgogICAgICAgIDxFbWFpbD5pc3N1ZXJAaXNzdWVyLmNvbTwvRW1haWw+CiAgICAgICAgPFVybD5odHRwOi8vd3d3Lmlzc3Vlci5jb208L1VybD4KICAgIDwvSXNzdWVyPgo8L0xpY2Vuc2U+Cg==");
        }
 
        private static unsafe void MemoryPatching(MethodBase miEvaluation, MethodBase miLicensed)
        {
            IntPtr IntPtrEval = GetMemoryAddress(miEvaluation);
            IntPtr IntPtrLicensed = GetMemoryAddress(miLicensed);
 
            if (IntPtr.Size == 8)
                *((long*)IntPtrEval.ToPointer()) = *((long*)IntPtrLicensed.ToPointer());
            else
                *((int*)IntPtrEval.ToPointer()) = *((int*)IntPtrLicensed.ToPointer());
        }
 
        private static unsafe IntPtr GetMemoryAddress(MethodBase mb)
        {
            RuntimeHelpers.PrepareMethod(mb.MethodHandle);
 
            if ((Environment.Version.Major >= 4) || ((Environment.Version.Major == 2) && (Environment.Version.MinorRevision >= 3053)))
            {
                return new IntPtr(((int*)mb.MethodHandle.Value.ToPointer() + 2));
            }
 
            UInt64* location = (UInt64*)(mb.MethodHandle.Value.ToPointer());
            int index = (int)(((*location) >> 32) & 0xFF);
            if (IntPtr.Size == 8)
            {
                ulong* classStart = (ulong*)mb.DeclaringType.TypeHandle.Value.ToPointer();
                ulong* address = classStart + index + 10;
                return new IntPtr(address);
            }
            else
            {
                uint* classStart = (uint*)mb.DeclaringType.TypeHandle.Value.ToPointer();
                uint* address = classStart + index + 10;
                return new IntPtr(address);
            }
        }
    }

 

2. O2S.Components.PDFRender4NET

这个库转换会乱码, 特别是目录,没法用,不知道是不是库的问题(我换了两个不同时间的库都一样)

3. pdfiumviewer

这个库在web上无法使用, 总是提示库无法加载,winform上使用一切正常

这个需要两个库  PdfiumViewer 和 ImageResizer.Plugins.PdfiumRenderer.Pdfium.Dll ,可用 vs 软件中 : 工具 -》Nuget 包管理器 -》管理解决方案的管理器 Nuget 包管理器 -》 浏览 中搜索安装。

 具体使用看这个链接: http://www.dfcsoft.com/View.aspx?id=b6edeafd43b74fc78bb99d52b70d006a

4. acrobat 

这个报的希望最大,但是很失望, 无论桌面和web 都要安装 Adobe Acrobat Professional 这个软件,使用时候添加引用:Microsoft.CSharp.dll,Acrobat.dll 和Microsoft.VisualBasic.dll,以及 system.wendows.forms ,装完最好重启下电脑。 这个的坑就在于它是将pdf的内容添加到了剪贴板 , 而且web使用剪贴板就要启动一个单线程。 我在使用时因为剪贴板被同时使用生成图片会丢失,而且在web上使用是有多线程同时转换的问题,所以放弃这个方法。不过转换速度确实快,毕竟是正版。

说明:

Clipboard 类只能在设置为单线程单元 (STA) 模式的线程中使用

// 单线程
ThreadStart ts = delegate { GrabImage(bigPicName, picDPI, smallPicName); };
Thread g_AutoScanGrabImage = new Thread(ts);
g_AutoScanGrabImage.SetApartmentState(ApartmentState.STA); // 这句加上就可以了
g_AutoScanGrabImage.Start();

 

你可能感兴趣的:(c#,c#)