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


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

我一直在搞乱PDFSharp而且没有运气。

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

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

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


10108
2018-01-09 01:11


起源

您在PDFSharp中尝试了什么?这里有一个例子: pdfsharp.net/wiki/... - keyboardP
向我们展示你尝试过的东西。我们会帮助你。 - Amy
谢谢你的downvotes!除了您链接的示例之外,我没有尝试任何其他内容,它提取PDF中的图像而不是渲染PDF并将其输出到图像。这就是我要问的原因:我没有看到在iTextSharp或PDFSharp中做到这一点的方法。我用Google搜索了很多,然后空手而归。 - Jason
PDFsharp无法呈现PDF文件 - 这就是创建缩略图所需的内容。此信息可在常见问题解答中找到。你已经发现Ghostscript可以做到这一点。 - The sky is the limit
我没有投票,但我可以看到这是一个糟糕的问题,因为之前已经多次询问过SO.Just搜索“[pdf] [c#]缩略图”会得到10个结果。 - yms


答案:


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

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


5
2018-01-09 04:12



谢谢,我会试试看! - Jason
像冠军一样工作。我建议获取源代码。它更容易理解,它与博客上的示例有点不同。唯一的事情是你必须知道所需的宽度/高度。我想我会努力找出从哪里收集信息。 - Jason
除了它有效之外,我对这个解决方案并不感到高兴。如果有人有更好的解决方案,请告诉我们,我会给你勾选标记。 - Jason
在pdf上获取页面大小的解决方案是什么? - David Freire


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

杰森莫尔斯写了一篇 用于渲染PDF的很棒的C#包装器 作为开源的插件 imageresizing.net库

如果它是一个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一样。


5
2018-01-20 17:18



我今天要看看。 - Jason


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


2
2018-01-13 09:55



我将看看这个,看看它是否比使用Ghostscript更清洁(必须如此)。谢谢! - Jason


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

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

1
2018-01-10 15:43



如果不花费2000多美元,这将是非常整洁的。 = P - Jason


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

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

0
2018-01-19 14:02



它取自 mattephraim.com/blog/2009/01/06/...。此外,您缺少所有帮助方法,如'GetArgs'等 - Jason
谢谢你的纠正 - CompanyDroneFromSector7G