Javascript ing Umbraco.Core.Models; 使用Umbraco.Core.Services; 使用Umbraco.Core.Services.Implement; 命名空间PdfPreview { 公共类PdfPreviewComponent:IComponent { 私有只读IContentTypeBaseServiceProvider\u contentTypeBaseServiceProvider; //我们需要注入IContentTypeBaseServiceProvider。它由设置图像值的助手使用。 公共PdfPreviewComponent(IContentTypeBaseServiceProvider contentTypeBaseServiceProvider) { _contentTypeBaseServiceProvider=contentTypeBaseServiceProvider; } 公共无效初始化() { //将处理程序附加到事件 MediaService.Saving+=MediaService\u Saving; } 私有void MediaService_保存(Umbraco.Core.Services.IMediaService发件人,Umbraco.Core.Events.SaveEventArgs e) { foreach(e.SavedEntities中的IMedia项) { if(item.ContentType.Alias.Equals(Umbraco.Web.PublishedModels.File.ModelTypeAlias)) { var filePropertyValue=(字符串)item.GetValue(“umbracoFile”); if(filePropertyValue!=null&!string.IsNullOrEmpty(filePropertyValue)) { var isPdf=filePropertyValue.EndsWith(“.pdf”,StringComparison.InvariantCultureIgnoreCase); 如果(isPdf) { //Ghostscript.NET需要知道Ghostscripts DLL在哪里 var binpath=AppDomain.CurrentDomain.RelativeSearchPath; 字符串gsDllPath=Path.Combine(binpath,Environment.Is64BitProcess?@“gsdll64.dll”:@“gsdll32.dll”); var version=new GhostscriptVersionInfo(新版本(0,0,0),gsDllPath,string.Empty,GhostscriptLicense.GPL); 使用(var rasterizer=new Ghostscript.NET.rasterizer.GhostscriptRasterizer()) { //我们得到上传的PDF文件在服务器中的实际位置 var location=System.Web.Hosting.HostingEnvironment.MapPath(filePropertyValue); //我们用光栅器打开它。 光栅化器。打开(位置、版本、真); //我们以图像的形式获得第一页。 var firstPageAsImage=rasterizer.GetPage(2002002001); 使用(var stream=new MemoryStream()) { //我们需要将图像转换为流。。。 保存(流,ImageFormat.Png); //…因为此帮助程序只接受流 SetValue(_contentTypeBaseServiceProvider,“缩略图”、“缩略图.png”、流); } } } } } } } 公共无效终止() { } } //我们使用Composer将组件添加到组件列表中。这里我们使用ComponentComposer帮助器基类来完成这项工作。 公共类PdfPreviewComposer:ComponentComposer{} }
注意:我们不需要在事件处理程序中保存Umbraco节点,因为所有这些都是在保存之前发生的。在此之后,文件的保存实际上将在Umbraco中进行Javascript ing Umbraco.Core.Models; 使用Umbraco.Core.Services; 使用Umbraco.Core.Services.Implement; 命名空间PdfPreview { 公共类PdfPreviewComponent:IComponent { 私有只读IContentTypeBaseServiceProvider\u contentTypeBaseServiceProvider; //我们需要注入IContentTypeBaseServiceProvider。它由设置图像值的助手使用。 公共PdfPreviewComponent(IContentTypeBaseServiceProvider contentTypeBaseServiceProvider) { _contentTypeBaseServiceProvider=contentTypeBaseServiceProvider; } 公共无效初始化() { //将处理程序附加到事件 MediaService.Saving+=MediaService\u Saving; } 私有void MediaService_保存(Umbraco.Core.Services.IMediaService发件人,Umbraco.Core.Events.SaveEventArgs e) { foreach(e.SavedEntities中的IMedia项) { if(item.ContentType.Alias.Equals(Umbraco.Web.PublishedModels.File.ModelTypeAlias)) { var filePropertyValue=(字符串)item.GetValue(“umbracoFile”); if(filePropertyValue!=null&!string.IsNullOrEmpty(filePropertyValue)) { var isPdf=filePropertyValue.EndsWith(“.pdf”,StringComparison.InvariantCultureIgnoreCase); 如果(isPdf) { //Ghostscript.NET需要知道Ghostscripts DLL在哪里 var binpath=AppDomain.CurrentDomain.RelativeSearchPath; 字符串gsDllPath=Path.Combine(binpath,Environment.Is64BitProcess?@“gsdll64.dll”:@“gsdll32.dll”); var version=new GhostscriptVersionInfo(新版本(0,0,0),gsDllPath,string.Empty,GhostscriptLicense.GPL); 使用(var rasterizer=new Ghostscript.NET.rasterizer.GhostscriptRasterizer()) { //我们得到上传的PDF文件在服务器中的实际位置 var location=System.Web.Hosting.HostingEnvironment.MapPath(filePropertyValue); //我们用光栅器打开它。 光栅化器。打开(位置、版本、真); //我们以图像的形式获得第一页。 var firstPageAsImage=rasterizer.GetPage(2002002001); 使用(var stream=new MemoryStream()) { //我们需要将图像转换为流。。。 保存(流,ImageFormat.Png); //…因为此帮助程序只接受流 SetValue(_contentTypeBaseServiceProvider,“缩略图”、“缩略图.png”、流); } } } } } } } 公共无效终止() { } } //我们使用Composer将组件添加到组件列表中。这里我们使用ComponentComposer帮助器基类来完成这项工作。 公共类PdfPreviewComposer:ComponentComposer{} },javascript,c#,asp.net,razor,umbraco,Javascript,C#,Asp.net,Razor,Umbraco,注意:我们不需要在事件处理程序中保存Umbraco节点,因为所有这些都是在保存之前发生的。在此之后,文件的保存实际上将在Umbraco中进行 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
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> { }
}