在 C# 中将 PDF 导出为 JPG

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8782942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:24:27  来源:igfitidea点击:

Export PDF to JPG(s) in C#

c#.netimagepdfpdfsharp

提问by Jason

I need to save a one page pdf document as an image for a thumbnail on a website.

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

I've been messing around with PDFSharp and have had no luck.

我一直在搞弄 PDFSharp 并且没有运气。

I have tried this: http://www.pdfsharp.net/wiki/ExportImages-sample.ashx?AspxAutoDetectCookieSupport=1but all it does is extract the embedded images in the PDF file which is not the desired result.

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

Ideas on how to do this? Anyone know a good library that can handle this?

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

Edit: Please let me know why this is such a bad question. If anyone has a good solution to this it would be a great resource for many other people. Especially since google searches come up empty.

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

回答by Jason

Take a look at Ghostscript. You can render PDF to images with it.

看看 Ghostscript。您可以使用它将 PDF 渲染为图像。

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

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

回答by plinth

(disclaimer: I work for Atalasoft and wrote a lot of the PDF technology) If you use the PdfDecoder in AtalasoftdotImage, this is straight forward:

(免责声明:Atalasoft我的工作,写了很多的PDF技术的)如果你使用的PdfDecoder AtalasoftdotImage,这是直截了当:

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);
        }
    }
}

回答by AffineMesh

ABCpdf exports PDF documents to JPEG with C#. See: http://www.websupergoo.com/helppdfnet/source/4-examples/19-rendering.htm

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

回答by CompanyDroneFromSector7G

I got this from somewhere on the web - can't remember exactly where but it works for me!
I just made it into a nice function.

It uses the GhostScript APIs (GSdll32.dll)

Examples of the imageFormat parameter are "jpeg", "tiff32nc", etc.

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

它使用 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;
    }

回答by Lilith River

Ghostscript is currently the de-facto standard for rendering PDFs. It's a bit tricky to wrap, even using GhostScriptSharp.

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

Jason Morse wrote a great C# wrapper for rendering PDFsas a plugin to the open-source imageresizing.net library.

Jason Morse 编写了一个很棒的 C# 包装器,用于将 PDF 渲染为开源imageresizing.net 库的插件。

If it's an asp.net application, the library allows on-the-fly rendering, so you can just add a querystring to get the jpeg/png version:

如果它是一个 asp.net 应用程序,该库允许即时渲染,因此您只需添加一个查询字符串即可获取 jpeg/png 版本:

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

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

You can also use the managed API instead (in any application type - not asp.net specific)

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

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

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

The PdfRenderer plugin is GPL licensed, just like Ghostscript.

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