【发布时间】:2021-03-21 01:02:40
【问题描述】:
我目前使用的是 Umbraco 版本:8.5.3,并且我使用媒体选择器来选择 pdf 文件,它会在网站上显示链接。
我现在想要的是获取 pdf 的第一页并将其作为图像显示在我的网站上。
任何帮助将不胜感激。
谢谢。
【问题讨论】:
标签: javascript c# asp.net razor umbraco
我目前使用的是 Umbraco 版本:8.5.3,并且我使用媒体选择器来选择 pdf 文件,它会在网站上显示链接。
我现在想要的是获取 pdf 的第一页并将其作为图像显示在我的网站上。
任何帮助将不胜感激。
谢谢。
【问题讨论】:
标签: javascript c# asp.net razor umbraco
这不是一件容易的事,但最好的方法是在上传 PDF 时生成图像。
您可以在文件媒体类型中创建一个新属性来保存缩略图。为此使用上传字段:
注意:如果您将 Thumbnail 属性放在第一个属性,它将显示在媒体部分:)
要创建预览,您需要将侦听器连接到 MediaService 的 Saving 事件。您需要为此使用组件。
在以下示例中,我使用Ghoscript.NET wrapper 从 PDF 创建图像。您还需要使用Ghostscript 库。要获取库,您需要安装程序并从安装它的文件夹中获取 dll。看起来 9.26 以上的版本出现了一些错误。此示例已使用 9.26 版本进行测试。
using Ghostscript.NET;
using System;
using System.Drawing.Imaging;
using System.IO;
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Implement;
namespace PdfPreview
{
public class PdfPreviewComponent : IComponent
{
private readonly IContentTypeBaseServiceProvider _contentTypeBaseServiceProvider;
// we need to inject the IContentTypeBaseServiceProvider. It's used by the helper that set the image value.
public PdfPreviewComponent(IContentTypeBaseServiceProvider contentTypeBaseServiceProvider)
{
_contentTypeBaseServiceProvider = contentTypeBaseServiceProvider;
}
public void Initialize()
{
// attach the handler to the event
MediaService.Saving += MediaService_Saving;
}
private void MediaService_Saving(Umbraco.Core.Services.IMediaService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IMedia> e)
{
foreach (IMedia item in e.SavedEntities)
{
if (item.ContentType.Alias.Equals(Umbraco.Web.PublishedModels.File.ModelTypeAlias))
{
var filePropertyValue = (string)item.GetValue("umbracoFile");
if (filePropertyValue != null && !string.IsNullOrEmpty(filePropertyValue))
{
var isPdf = filePropertyValue.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase);
if (isPdf)
{
// Ghostscript.NET needs to know where the Ghostscripts dlls are
var binpath = AppDomain.CurrentDomain.RelativeSearchPath;
string gsDllPath = Path.Combine(binpath, Environment.Is64BitProcess ? @"gsdll64.dll" : @"gsdll32.dll");
var version = new GhostscriptVersionInfo(new Version(0, 0, 0), gsDllPath, string.Empty, GhostscriptLicense.GPL);
using (var rasterizer = new Ghostscript.NET.Rasterizer.GhostscriptRasterizer())
{
// we get the actual location in the server of our PDF being upload
var location = System.Web.Hosting.HostingEnvironment.MapPath(filePropertyValue);
// and we open it with the rasterizer.
rasterizer.Open(location, version, true);
// we get the first page as an Image.
var firstPageAsImage = rasterizer.GetPage(200, 200, 1);
using (var stream = new MemoryStream())
{
// we need to transform the Image to Stream...
firstPageAsImage.Save(stream, ImageFormat.Png);
//...because this helper only accepts a Stream
item.SetValue(_contentTypeBaseServiceProvider, "thumbnail", "thumbnail.png", stream);
}
}
}
}
}
}
}
public void Terminate()
{
}
}
// we use a Composer to add our Component to the list of Components. Here we use the ComponentComposer helper base class to do it.
public class PdfPreviewComposer : ComponentComposer<PdfPreviewComponent> { }
}
注意:我们不需要在事件处理程序中保存 Umbraco 节点,因为所有这些都在保存之前发生。在此之后,文件的保存实际上会发生在 Umbraco 中。
【讨论】: