使用 Ghostscript 从 PDF 创建缩略图






3.50/5 (8投票s)
上传 PDF 文件,将文件名保存到数据库,将 PDF 保存到文件夹,创建 PDF 的缩略图并将其保存到文件夹,并将图像保存到数据库
引言
在本文中,我将解释如何上传 PDF 文件,将文件名保存到数据库并将 PDF 文件保存到文件夹。 此外,创建缩略图并将其保存到文件夹和数据库。 与使用 Acrobat 栅格化方法相比,这是一种非常简单的方法。 它只有几行代码并且易于理解。
背景
我使用 Ghostscript 通过选择页码将 PDF 文件栅格化为图像。 在这种情况下,我们只需要第一页的页码来创建其缩略图。
下载 Ghostscript
首先,您需要从 这里 下载 Ghostscript DLL,然后单击下载按钮。 这包含 DLL 文件和示例文件。 将 DLL 复制到您的项目文件夹中,并在 Visual Studio 中添加对 Ghostscript.NET DLL 的引用。
Using the Code
您可以使用任何标准的上传控件执行 PDF 上传。 这里,我使用的是 Telerik 异步上传控件。
首先,从控制器获取文件名
if (RadAsyncUploadFile.UploadedFiles.Count > 0)
{
UploadedFile attachment = RadAsyncUploadFile.UploadedFiles[0];
filename = attachment.FileName;
}
然后,通过常规 SQL 查询将此文件名保存到数据库。
要将上传的 PDF 文件保存到文件夹
var pdffilename = filename; //or any name you prefer.
var thumbName = somename + "_thumb"; //usually filename _thumb, here I use some name because
// here filename will have the .pdf extension. so you need to perform
//string function to remove extension or use Path.GetFileNameWithoutExtension(path)
string pathfolder = (System.Configuration.ConfigurationManager.AppSettings["D:\ProjectName\"]);
UploadedFile attachment = RadAsyncUploadFile.UploadedFiles[0];
attachment.SaveAs(pathfolder + pdffilename, true);
现在,创建一个名为 Filemanagement
的新类文件,并在代码背后创建该类的对象
FileManagement _fileManagement = new FileManagement();
var id = sql query to get the id from the table where we saved the filename;
string filepath = pathfolder + pdffilename;
_fileManagement.CreateThumbnail(filepath, thumbName, currMac.ArtworkId);
现在在类文件中,添加以下代码
using System;
using System.Linq;
using Project.DataAccess.Interfaces;
using System.IO;
using System.Drawing;
using Ghostscript.NET.Rasterizer;
using Project.Entities.ProjectEntity;
namespace Project.DataAccess
{
public interface ISample
{
void Start(string filePath, string thumbName, long id);
}
public class RasterizerCropSample : ISample
{
readonly ProjectEntity_context = new ProjectEntity();
public void Start(string filePath, string thumbName, long artworkId)
{
int desired_x_dpi = 10;
int desired_y_dpi = 10;
string inputPdfPath = filePath;
string outputPath = @"D:\Project\Thumbnails\";
using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer())
{
rasterizer.CustomSwitches.Add("-dUseCropBox");
rasterizer.CustomSwitches.Add("-c");
rasterizer.CustomSwitches.Add("[/CropBox [24 72 559 794] /PAGES pdfmark");
rasterizer.CustomSwitches.Add("-f");
rasterizer.Open(inputPdfPath);
var pageNumber = 1;
// string pageFilePath = Path.Combine(outputPath, thumbName + ".png");
Image img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber);
//to save in folder
img.Save(pageFilePath, ImageFormat.Png);
//to save in database
byte[] byteImage = ImageToByteArray(img);
var getArtwork =
(from m in _context.tArtworkFiles where m.ArtworkId == idselect m).FirstOrDefault();
if (getArtwork != null)
{
getArtwork.Thumbnail = byteImage;
_context.SaveChanges();
}
}
}
public byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
}
public class FileManagement : IFileManager
{
readonly ProjectEntity_context = new ProjectEntity();
[STAThread]
public void CreateThumbnail(string filepath, string thumbName, long id)
{
RasterizerCropSample thumbnailCreate = new RasterizerCropSample();
thumbnailCreate.Start(filepath, thumbName, id);
}
这里,我们包含了 ghostscript 以通过选择第一页并创建图像来栅格化 PDF。
然后我们将图像保存到文件夹。
我们将栅格化后的图像转换为 ByteArray
,然后将其保存到 datatype
为 Image
的数据库列中。
希望这有帮助!
如有任何疑问,请留下评论。
关注点
我尝试使用 Acrobat 方法,该方法非常复杂并且使用了太多的代码行。
但是,我使用 Ghostscript,这使得整个过程简单易于编码。
历史
- 2017/02/28:初始发布