在C#中将PDF导出为JPG

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

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

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

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

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

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

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

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

Jason Morse编写了一个很棒的C#包装器,用于将PDF作为插件呈现给开源的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一样。

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

(免责声明:我为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); } } } 

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

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