生成pdf缩略图(开放源码/免费)

时间:2022-12-30 00:31:05

Looking at other posts for this could not find an adequate solution that for my needs. Trying to just get the first page of a pdf document as a thumbnail. This is to be run as a server application so would not want to write out a pdf document to file to then call a third application that reads the pdf to generate the image on disk.

在其他的帖子中找不到合适的解决方案来满足我的需要。尝试将pdf文档的第一页作为缩略图。这将作为一个服务器应用程序运行,因此不希望将pdf文档写入文件,然后调用读取pdf的第三个应用程序来生成磁盘上的映像。

doc = new PDFdocument("some.pdf");
page = doc.page(1);
Image image = page.image;

Thanks.

谢谢。

5 个解决方案

#1


26  

Matthew Ephraim released an open source wrapper for Ghostscript that sounds like it does what you want and is in C#.

Matthew以法莲为Ghostscript发布了一个开源的包装器,听起来就像你想要的一样。

Link to Source Code: https://github.com/mephraim/ghostscriptsharp

链接到源代码:https://github.com/mephraim/ghostscriptsharp。

Link to Blog Posting: 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/。

You can make a simple call to the GeneratePageThumb method to generate a thumbnail (or use GeneratePageThumbs with a start and end page number to generate thumbnails for multiple seperate pages, with each page being a seperate output file), default file format is jpeg but you can change it, and many other options, by using the alternate GenerateOutput method call and specify options such as file format, page size, etc...

你可以做一个简单的调用GeneratePageThumb方法生成缩略图(或使用GeneratePageThumbs开始和结束页码为多个独立的页面生成缩略图,每个页面作为一个独立的输出文件),默认文件格式jpeg但是你可以改变它,和许多其他选项,通过使用替代GenerateOutput方法调用并指定选项,例如文件格式,页面大小,等等。

#2


19  

I think that Windows API Code pack for Microsoft .NET framework might do the trick easiest. What it can is to generate the same thumbnail that Windows Explorer does (and that is first page), and you can chose several sizes, they go up to 1024x1024, so it should be enough. It is quite simple, just create ShellObject.FromParsingName(filepath) and find its Thumbnail subclass.

我认为微软。net框架的Windows API代码包可能是最简单的方法。它所能做的就是生成与Windows资源管理器相同的缩略图(这是第一页),并且您可以选择不同的大小,它们会增加到1024x1024,所以应该足够了。它非常简单,只需创建ShellObject.FromParsingName(filepath)并找到它的缩略图子类。

The problem might be what your server is. This works on Windows 7, Windows Vista and I guess Windows Server 2008. Also, Windows Explorer must be able to show thumbnails on that machine. The easiest way to insure that is to install Adobe Reader. If all of this is not a problem, I think that this is the most elegant way.

问题可能是服务器的问题。这适用于Windows 7、Windows Vista和Windows Server 2008。此外,Windows资源管理器必须能够在机器上显示缩略图。最简单的方法是安装adobereader。如果所有这些都不是问题,我认为这是最优雅的方式。

UPDATE: Adobe Reader has dropped support for thumbnails in the recent versions so its legacy versions must be used.

更新:adobereader在最近的版本中放弃了对缩略图的支持,因此必须使用其遗留版本。

UPDATE2: According to comment from Roberto, you can still use latest version of Adobe Reader if you turn on thumbnails option in Edit - Preferences - General.

UPDATE2:根据Roberto的评论,如果你在编辑首选项中打开缩略图选项,你仍然可以使用最新版本的adobereader。

#3


1  

Download PDFLibNet and use the following code

下载PDFLibNet并使用以下代码

public void ConvertPDFtoJPG(string filename, String dirOut)
{
    PDFLibNet.PDFWrapper _pdfDoc = new PDFLibNet.PDFWrapper();
    _pdfDoc.LoadPDF(filename);

    for (int i = 0; i < _pdfDoc.PageCount; i++)
    {

        Image img = RenderPage(_pdfDoc, i);

        img.Save(Path.Combine(dirOut, string.Format("{0}{1}.jpg", i,DateTime.Now.ToString("mmss"))));

    }
    _pdfDoc.Dispose();
    return;
}
public  Image RenderPage(PDFLibNet.PDFWrapper doc, int page)
{
    doc.CurrentPage = page + 1;
    doc.CurrentX = 0;
    doc.CurrentY = 0;

    doc.RenderPage(IntPtr.Zero);

        // create an image to draw the page into
        var buffer = new Bitmap(doc.PageWidth, doc.PageHeight);
        doc.ClientBounds = new Rectangle(0, 0, doc.PageWidth, doc.PageHeight);
        using (var g = Graphics.FromImage(buffer))
        {
            var hdc = g.GetHdc();
            try
            {
                doc.DrawPageHDC(hdc);
            }
            finally
            {
                g.ReleaseHdc();
            }
        }
        return buffer;

}

#4


0  

I used to do this kind of stuff with imagemagick (Convert) long ago. There is a .Net Wrapper for that, maybe it's worth checking out : http://imagemagick.codeplex.com/releases/view/30302

我很久以前就用imagemagick(转换)做过这种事情。有一个。net包装,也许值得一试:http://imagemagick.codeplex.com/releases/view/30302

#5


0  

http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx

http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx

This works very well. The only dependencies are GhostScript's gsdll32.dll (you need to download GhostScript separately to get this, but there is no need to have GhostScript installed in your production environment), and PDFSharp.dll which is included in the project.

这是很好。惟一的依赖项是GhostScript的gsdll32。dll(您需要单独下载GhostScript来获得这个,但是不需要在您的生产环境中安装GhostScript)和PDFSharp。包含在项目中的dll。

#1


26  

Matthew Ephraim released an open source wrapper for Ghostscript that sounds like it does what you want and is in C#.

Matthew以法莲为Ghostscript发布了一个开源的包装器,听起来就像你想要的一样。

Link to Source Code: https://github.com/mephraim/ghostscriptsharp

链接到源代码:https://github.com/mephraim/ghostscriptsharp。

Link to Blog Posting: 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/。

You can make a simple call to the GeneratePageThumb method to generate a thumbnail (or use GeneratePageThumbs with a start and end page number to generate thumbnails for multiple seperate pages, with each page being a seperate output file), default file format is jpeg but you can change it, and many other options, by using the alternate GenerateOutput method call and specify options such as file format, page size, etc...

你可以做一个简单的调用GeneratePageThumb方法生成缩略图(或使用GeneratePageThumbs开始和结束页码为多个独立的页面生成缩略图,每个页面作为一个独立的输出文件),默认文件格式jpeg但是你可以改变它,和许多其他选项,通过使用替代GenerateOutput方法调用并指定选项,例如文件格式,页面大小,等等。

#2


19  

I think that Windows API Code pack for Microsoft .NET framework might do the trick easiest. What it can is to generate the same thumbnail that Windows Explorer does (and that is first page), and you can chose several sizes, they go up to 1024x1024, so it should be enough. It is quite simple, just create ShellObject.FromParsingName(filepath) and find its Thumbnail subclass.

我认为微软。net框架的Windows API代码包可能是最简单的方法。它所能做的就是生成与Windows资源管理器相同的缩略图(这是第一页),并且您可以选择不同的大小,它们会增加到1024x1024,所以应该足够了。它非常简单,只需创建ShellObject.FromParsingName(filepath)并找到它的缩略图子类。

The problem might be what your server is. This works on Windows 7, Windows Vista and I guess Windows Server 2008. Also, Windows Explorer must be able to show thumbnails on that machine. The easiest way to insure that is to install Adobe Reader. If all of this is not a problem, I think that this is the most elegant way.

问题可能是服务器的问题。这适用于Windows 7、Windows Vista和Windows Server 2008。此外,Windows资源管理器必须能够在机器上显示缩略图。最简单的方法是安装adobereader。如果所有这些都不是问题,我认为这是最优雅的方式。

UPDATE: Adobe Reader has dropped support for thumbnails in the recent versions so its legacy versions must be used.

更新:adobereader在最近的版本中放弃了对缩略图的支持,因此必须使用其遗留版本。

UPDATE2: According to comment from Roberto, you can still use latest version of Adobe Reader if you turn on thumbnails option in Edit - Preferences - General.

UPDATE2:根据Roberto的评论,如果你在编辑首选项中打开缩略图选项,你仍然可以使用最新版本的adobereader。

#3


1  

Download PDFLibNet and use the following code

下载PDFLibNet并使用以下代码

public void ConvertPDFtoJPG(string filename, String dirOut)
{
    PDFLibNet.PDFWrapper _pdfDoc = new PDFLibNet.PDFWrapper();
    _pdfDoc.LoadPDF(filename);

    for (int i = 0; i < _pdfDoc.PageCount; i++)
    {

        Image img = RenderPage(_pdfDoc, i);

        img.Save(Path.Combine(dirOut, string.Format("{0}{1}.jpg", i,DateTime.Now.ToString("mmss"))));

    }
    _pdfDoc.Dispose();
    return;
}
public  Image RenderPage(PDFLibNet.PDFWrapper doc, int page)
{
    doc.CurrentPage = page + 1;
    doc.CurrentX = 0;
    doc.CurrentY = 0;

    doc.RenderPage(IntPtr.Zero);

        // create an image to draw the page into
        var buffer = new Bitmap(doc.PageWidth, doc.PageHeight);
        doc.ClientBounds = new Rectangle(0, 0, doc.PageWidth, doc.PageHeight);
        using (var g = Graphics.FromImage(buffer))
        {
            var hdc = g.GetHdc();
            try
            {
                doc.DrawPageHDC(hdc);
            }
            finally
            {
                g.ReleaseHdc();
            }
        }
        return buffer;

}

#4


0  

I used to do this kind of stuff with imagemagick (Convert) long ago. There is a .Net Wrapper for that, maybe it's worth checking out : http://imagemagick.codeplex.com/releases/view/30302

我很久以前就用imagemagick(转换)做过这种事情。有一个。net包装,也许值得一试:http://imagemagick.codeplex.com/releases/view/30302

#5


0  

http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx

http://www.codeproject.com/KB/cs/GhostScriptUseWithCSharp.aspx

This works very well. The only dependencies are GhostScript's gsdll32.dll (you need to download GhostScript separately to get this, but there is no need to have GhostScript installed in your production environment), and PDFSharp.dll which is included in the project.

这是很好。惟一的依赖项是GhostScript的gsdll32。dll(您需要单独下载GhostScript来获得这个,但是不需要在您的生产环境中安装GhostScript)和PDFSharp。包含在项目中的dll。