首页 文章

在C#中将PDF导出为JPG [关闭]

提问于
浏览
11

我需要将一页pdf文档保存为网站缩略图的图像 .

我一直在乱用PDFSharp并没有运气 .

我试过这个:http://www.pdfsharp.net/wiki/ExportImages-sample.ashx?AspxAutoDetectCookieSupport=1但它只是提取PDF文件中的嵌入图像,这不是理想的结果 .

关于如何做到这一点的想法?有人知道一个好的图书馆可以处理这个吗?

编辑:请让我知道为什么这是一个如此糟糕的问题 . 如果有人有一个很好的解决方案,这对许多其他人来说将是一个很好的资源 . 特别是因为谷歌搜索空了 .

5 回答

  • 5

    看看Ghostscript . 您可以使用它将PDF渲染到图像 .

    http://www.mattephraim.com/blog/2009/01/06/a-simple-c-wrapper-for-ghostscript/

  • 2

    Ghostscript目前是渲染PDF的事实上的标准 . 即使使用GhostScriptSharp,包装也有点棘手 .

    Jason Morse写了一个great C# wrapper for rendering PDFs作为开源imageresizing.net library的插件 .

    如果它是一个asp.net应用程序,库允许动态渲染,所以你只需添加一个查询字符串来获取jpeg / png版本:

    /pdfs/letter.pdf?format=jpg&page=2

    您也可以使用托管API(在任何应用程序类型中 - 不是特定于asp.net)

    ImageBuilder.Current.Build(“letter.pdf”,“dest.jpg”,new ResizeSettings(“format = jpg; page = 2”));

    PdfRenderer插件是GPL许可的,就像Ghostscript一样 .

  • 1

    ABCpdf使用C#将PDF文档导出为JPEG . 见:http://www.websupergoo.com/helppdfnet/source/4-examples/19-rendering.htm

  • 0

    (免责声明:我为Atalasoft工作并写了很多PDF技术)如果你在Atalasoft dotImage中使用PdfDecoder,这很简单:

    public void PdfToJpegThumb(Stream srcStream, int pageNo, int maxDimension, Stream dstStream)
    {
        PdfDecoder decoder = new PdfDecoder();
        decoder.Resolution = 96; // reduce default resolution to speed up rendering
        // render page
        using (AtalaImage pdfimage = decoder.read(srcStream, pageNo, null)) {
            Thumbnail tn = new Thumbnail(maxDimension, maxDimension);
            // make a thumbnail image
            using (AtalaImage tnImage = tn.Create(pdfImage)) {
                // save it
                tnImage.Save(dstStream, new JpegEncoder(), null);
            }
        }
    }
    
  • 5

    我从网上的某个地方得到了这个 - 不记得确切的位置,但它对我有用!
    我把它变成了一个很好的功能 .

    它使用GhostScript API(GSdll32.dll)

    imageFormat参数的示例是"jpeg","tiff32nc"等 .

    #region GhostScript API functions
        [DllImport("gsdll32.dll", EntryPoint = "gsapi_new_instance")]
        private static extern int CreateAPIInstance(out IntPtr pinstance,
                                                IntPtr caller_handle);
    
        [DllImport("gsdll32.dll", EntryPoint = "gsapi_init_with_args")]
        private static extern int InitAPI(IntPtr instance, int argc, IntPtr argv);
    
        [DllImport("gsdll32.dll", EntryPoint = "gsapi_exit")]
        private static extern int ExitAPI(IntPtr instance);
    
        [DllImport("gsdll32.dll", EntryPoint = "gsapi_delete_instance")]
        private static extern void DeleteAPIInstance(IntPtr instance);
        #endregion
    
        public bool CreateImage(string inputPath, string outputPath, string imageFormat, int firstPage, int lastPage, int width, int height)
        {
            bool result = false;
            try
            {
                string[] args = GetArgs(inputPath, outputPath, imageFormat, firstPage, lastPage, width, height);
                var argStrHandles = new GCHandle[args.Length];
                var argPtrs = new IntPtr[args.Length];
    
                // Create a handle for each of the arguments after 
                // they've been converted to an ANSI null terminated
                // string. Then store the pointers for each of the handles
                for (int i = 0; i < args.Length; i++)
                {
                    argStrHandles[i] = GCHandle.Alloc(StringToAnsi(args[i]), GCHandleType.Pinned);
                    argPtrs[i] = argStrHandles[i].AddrOfPinnedObject();
                }
    
                // Get a new handle for the array of argument pointers
                var argPtrsHandle = GCHandle.Alloc(argPtrs, GCHandleType.Pinned);
    
                // Get a pointer to an instance of the GhostScript API 
                // and run the API with the current arguments
                IntPtr gsInstancePtr;
                CreateAPIInstance(out gsInstancePtr, IntPtr.Zero);
                InitAPI(gsInstancePtr, args.Length, argPtrsHandle.AddrOfPinnedObject());
    
                // Cleanup arguments in memory
                for (int i = 0; i < argStrHandles.Length; i++)
                    argStrHandles[i].Free();
    
                argPtrsHandle.Free();
    
                // Clear API
                ExitAPI(gsInstancePtr);
                DeleteAPIInstance(gsInstancePtr);
    
                result = true;
            }
            catch(Exception e)
            {
                throw e;
            }
            return result;
        }
    

相关问题