Commit 505a666a authored by 黄奎's avatar 黄奎

新增文件生成图片

parent 90c15357
......@@ -13,4 +13,25 @@
<PackageReference Include="NPOI" Version="2.5.1" />
</ItemGroup>
<ItemGroup>
<Reference Include="Aspose.Pdf">
<HintPath>..\lib\Aspose.Pdf.dll</HintPath>
</Reference>
<Reference Include="Aspose.Words">
<HintPath>..\lib\Aspose.Words.dll</HintPath>
</Reference>
<Reference Include="Spire.License">
<HintPath>..\lib\Spire.License.dll</HintPath>
</Reference>
<Reference Include="Spire.Pdf">
<HintPath>..\lib\Spire.Pdf.dll</HintPath>
</Reference>
<Reference Include="Spire.Presentation">
<HintPath>..\lib\Spire.Presentation.dll</HintPath>
</Reference>
<Reference Include="Spire.XLS">
<HintPath>..\lib\Spire.XLS.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Edu.Common.Plugin
{
/// <summary>
/// 图片转换
/// </summary>
public class ImageHelper
{
/// <summary>
/// 生成图片
/// </summary>
/// <param name="sourceFile">源文件地址</param>
/// <param name="targetFile">目标文件路径</param>
/// <returns></returns>
public static List<string> CreateImage(string sourceFile, string targetFile)
{
IImageHelper imageHelper = null;
//获取文件扩展名
string extension = Path.GetExtension(sourceFile).ToLower();
switch (extension)
{
case ".doc":
imageHelper = new WordToImage();
break;
case ".docx":
imageHelper = new WordToImage();
break;
case ".ppt":
imageHelper = new PptToImage();
break;
case ".pptx":
imageHelper = new PptToImage();
break;
case ".pdf":
imageHelper = new PdfToImage();
break;
case ".xls":
imageHelper = new ExcelToImage();
break;
case ".xlsx":
imageHelper = new ExcelToImage();
break;
}
return imageHelper.CreateImage(sourceFile, targetFile);
}
}
/// <summary>
/// 生成图片接口
/// </summary>
interface IImageHelper
{
/// <summary>
/// 生成图片方法
/// </summary>
/// <param name="sourceFile">源文件地址</param>
/// <param name="targetFile">目标文件路径</param>
/// <returns></returns>
List<string> CreateImage(string sourceFile, string targetFile);
}
/// <summary>
/// Excel转图片
/// </summary>
class ExcelToImage : IImageHelper
{
/// <summary>
/// 生成图片地址
/// </summary>
/// <param name="sourceFile">源文件地址</param>
/// <param name="targetFile">目标文件路径</param>
/// <returns></returns>
public List<string> CreateImage(string sourceFile, string targetFile)
{
List<string> list = new List<string>();
try
{
Spire.Xls.Workbook xls = new Spire.Xls.Workbook();
xls.LoadFromFile(sourceFile, Spire.Xls.ExcelVersion.Version97to2003);
string tempPdf = targetFile + "xls_temp_" + DateTime.Now.Ticks + ".pdf";
xls.SaveToFile(tempPdf, Spire.Xls.FileFormat.PDF);
list = new PdfToImage().CreateImage(tempPdf, targetFile);
File.Delete(tempPdf);
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "WordToImage");
}
return list;
}
}
/// <summary>
/// Word转图片
/// </summary>
class WordToImage : IImageHelper
{
/// <summary>
/// 生成图片地址
/// </summary>
/// <param name="sourceFile">源文件地址</param>
/// <param name="targetFile">目标文件路径</param>
/// <returns></returns>
public List<string> CreateImage(string sourceFile, string targetFile)
{
List<string> list = new List<string>();
try
{
Aspose.Words.Document doc = new Aspose.Words.Document(sourceFile);
Aspose.Words.Saving.ImageSaveOptions iso = new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Png)
{
Resolution = 128,
PrettyFormat = true,
UseAntiAliasing = true
};
for (int i = 0; i < doc.PageCount; i++)
{
iso.PageIndex = i;
string newFilePath = targetFile + "doc_" + i + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";
list.Add(newFilePath);
doc.Save(newFilePath, iso);
}
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "WordToImage");
}
return list;
}
}
/// <summary>
/// Pdf转图片
/// </summary>
class PdfToImage : IImageHelper
{
/// <summary>
/// 生成图片地址
/// </summary>
/// <param name="sourceFile">源文件地址</param>
/// <param name="targetFile">目标文件路径</param>>
/// <returns></returns>
public List<string> CreateImage(string sourceFile, string targetFile)
{
List<string> list = new List<string>();
string license = AppContext.BaseDirectory + @"ExtPlug\Aspose.Total.lic";
try
{
Aspose.Pdf.License license2 = new Aspose.Pdf.License();
license2.SetLicense(license);
Aspose.Pdf.Document document = new Aspose.Pdf.Document(sourceFile);
//默认质量为100,设置质量的好坏与处理速度不成正比,甚至是设置的质量越低反而花的时间越长,怀疑处理过程是先生成高质量的再压缩
var device = new Aspose.Pdf.Devices.PngDevice();
//遍历每一页转为PNG
for (var i = 1; i <= document.Pages.Count; i++)
{
string filePathOutPut = targetFile + "pdf_" + i + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png";
FileStream fs = new FileStream(filePathOutPut, FileMode.OpenOrCreate);
try
{
device.Process(document.Pages[i], fs);
fs.Close();
list.Add(filePathOutPut);
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "PdfToImage1");
fs.Close();
}
}
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "PdfToImage");
}
return list;
}
}
/// <summary>
/// ppt转图片
/// </summary>
class PptToImage : IImageHelper
{
/// <summary>
/// 生成图片地址
/// </summary>
/// <param name="sourceFile">源文件地址</param>
/// <param name="targetFile">目标文件路径</param>
/// <returns></returns>
public List<string> CreateImage(string sourceFile, string targetFile)
{
List<string> list = new List<string>();
try
{
Spire.Presentation.Presentation ppt = new Spire.Presentation.Presentation();
ppt.LoadFromFile(sourceFile);
//遍历幻灯片
for (int i = 0; i < ppt.Slides.Count; i++)
{
string newFilePath = string.Format(targetFile + "ppt_" + i + "_" + @"{0}.png", DateTime.Now.ToString("yyyyMMddHHmmssfff"));
Image image = ppt.Slides[i].SaveAsImage();//读取emf格式图片
image.Save(newFilePath, ImageFormat.Png);//把emf格式的图片转换成Png格式
list.Add(newFilePath);
}
ppt.Dispose();
}
catch (Exception ex)
{
Common.Plugin.LogHelper.Write(ex, "PptToImage");
}
return list;
}
}
}
\ No newline at end of file
......@@ -13,7 +13,13 @@
"Microsoft.Extensions.Configuration": "3.1.8",
"Microsoft.Extensions.Configuration.Json": "3.1.8",
"NPOI": "2.5.1",
"Newtonsoft.Json": "12.0.3"
"Newtonsoft.Json": "12.0.3",
"Aspose.Pdf": "8.6.0.0",
"Aspose.Words": "17.7.0.0",
"Spire.License": "1.3.6.40",
"Spire.Pdf": "3.9.130.14040",
"Spire.Presentation": "2.8.13.14040",
"Spire.XLS": "7.12.23.14040"
},
"runtime": {
"Edu.Common.dll": {}
......@@ -874,7 +880,55 @@
"System.Runtime": "4.1.0"
}
},
"System.Threading.Tasks.Extensions/4.5.4": {}
"System.Threading.Tasks.Extensions/4.5.4": {},
"Aspose.Pdf/8.6.0.0": {
"runtime": {
"Aspose.Pdf.dll": {
"assemblyVersion": "8.6.0.0",
"fileVersion": "8.6.0.0"
}
}
},
"Aspose.Words/17.7.0.0": {
"runtime": {
"Aspose.Words.dll": {
"assemblyVersion": "17.7.0.0",
"fileVersion": "17.7.0.0"
}
}
},
"Spire.License/1.3.6.40": {
"runtime": {
"Spire.License.dll": {
"assemblyVersion": "1.3.6.40",
"fileVersion": "1.3.6.40"
}
}
},
"Spire.Pdf/3.9.130.14040": {
"runtime": {
"Spire.Pdf.dll": {
"assemblyVersion": "3.9.130.14040",
"fileVersion": "3.9.130.14040"
}
}
},
"Spire.Presentation/2.8.13.14040": {
"runtime": {
"Spire.Presentation.dll": {
"assemblyVersion": "2.8.13.14040",
"fileVersion": "2.8.13.14040"
}
}
},
"Spire.XLS/7.12.23.14040": {
"runtime": {
"Spire.XLS.dll": {
"assemblyVersion": "7.12.23.14040",
"fileVersion": "7.12.23.14040"
}
}
}
}
},
"libraries": {
......@@ -1477,6 +1531,36 @@
"sha512": "sha512-zteT+G8xuGu6mS+mzDzYXbzS7rd3K6Fjb9RiZlYlJPam2/hU7JCBZBVEcywNuR+oZ1ncTvc/cq0faRr3P01OVg==",
"path": "system.threading.tasks.extensions/4.5.4",
"hashPath": "system.threading.tasks.extensions.4.5.4.nupkg.sha512"
},
"Aspose.Pdf/8.6.0.0": {
"type": "reference",
"serviceable": false,
"sha512": ""
},
"Aspose.Words/17.7.0.0": {
"type": "reference",
"serviceable": false,
"sha512": ""
},
"Spire.License/1.3.6.40": {
"type": "reference",
"serviceable": false,
"sha512": ""
},
"Spire.Pdf/3.9.130.14040": {
"type": "reference",
"serviceable": false,
"sha512": ""
},
"Spire.Presentation/2.8.13.14040": {
"type": "reference",
"serviceable": false,
"sha512": ""
},
"Spire.XLS/7.12.23.14040": {
"type": "reference",
"serviceable": false,
"sha512": ""
}
}
}
\ No newline at end of file
......@@ -80,7 +80,13 @@ namespace Edu.WebApi.Controllers.Course
query.Group_Id = base.UserInfo.Group_Id;
query.School_Id = base.UserInfo.School_Id;
var cateIds= base.ParmJObj.GetStringValue("cateIds");
var list = categoryModule.GetChildCategoryStringModule(cateIds);
string targetFile = @"F:/Test/temp/";
//ImageHelper.CreateImage("F:/Test/1.docx", targetFile);
//ImageHelper.CreateImage("F:/Test/2.pdf", targetFile);
//ImageHelper.CreateImage("F:/Test/3.ppt", targetFile);
ImageHelper.CreateImage("F:/Test/4.xls", targetFile);
return ApiResult.Success(data: list);
}
......@@ -107,8 +113,8 @@ namespace Edu.WebApi.Controllers.Course
{
var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Course_Category_ViewModel>(RequestParm.Msg.ToString());
extModel.CreateTime = DateTime.Now;
extModel.CreateBy = base.UserInfo.Id;
extModel.UpdateBy = base.UserInfo.Id;
extModel.CreateBy = UserInfo.Id;
extModel.UpdateBy = UserInfo.Id;
extModel.UpdateTime = DateTime.Now;
extModel.Group_Id = this.UserInfo.Group_Id;
extModel.School_Id = this.UserInfo.School_Id;
......
......@@ -28,5 +28,10 @@
<ProjectReference Include="..\Edu.Module.User\Edu.Module.User.csproj" />
<ProjectReference Include="..\Edu.Repository\Edu.Repository.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="ExtPlug\Aspose.Total.lic">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
<?xml version="1.0" encoding="utf-8" ?>
<License>
<Data>
<LicensedTo>Aspose Scotland Team</LicensedTo>
<EmailTo>billy.lundie@aspose.com</EmailTo>
<LicenseType>Developer OEM</LicenseType>
<LicenseNote>Limited to 1 developer, unlimited physical locations</LicenseNote>
<OrderID>140408052324</OrderID>
<UserID>94236</UserID>
<OEM>This is a redistributable license</OEM>
<Products>
<Product>Aspose.Total for .NET</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SerialNumber>9a59547c-41f0-428b-ba72-7c4368f151d7</SerialNumber>
<SubscriptionExpiry>20151231</SubscriptionExpiry>
<LicenseVersion>3.0</LicenseVersion>
<LicenseInstructions>http://www.aspose.com/corporate/purchase/license-instructions.aspx</LicenseInstructions>
</Data>
<Signature>FO3PHsblgDt8F59sMT1l1amyi9qk2V6E8dQkIP7LdTJSxDibNEFu1zOinQbqFfKv/ruttvcxoROkc1tUe0DtO6cP1Zf6J0VemgSY8i/LZECTGszRqJVQRZ0MoVnBhuPAJk5eli7fhVcF8hWd3E4XQ3LzfmJCuaj2NEteRi5Hrfg=</Signature>
</License>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8" ?>
<License>
<Data>
<LicensedTo>Aspose Scotland Team</LicensedTo>
<EmailTo>billy.lundie@aspose.com</EmailTo>
<LicenseType>Developer OEM</LicenseType>
<LicenseNote>Limited to 1 developer, unlimited physical locations</LicenseNote>
<OrderID>140408052324</OrderID>
<UserID>94236</UserID>
<OEM>This is a redistributable license</OEM>
<Products>
<Product>Aspose.Total for .NET</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SerialNumber>9a59547c-41f0-428b-ba72-7c4368f151d7</SerialNumber>
<SubscriptionExpiry>20151231</SubscriptionExpiry>
<LicenseVersion>3.0</LicenseVersion>
<LicenseInstructions>http://www.aspose.com/corporate/purchase/license-instructions.aspx</LicenseInstructions>
</Data>
<Signature>FO3PHsblgDt8F59sMT1l1amyi9qk2V6E8dQkIP7LdTJSxDibNEFu1zOinQbqFfKv/ruttvcxoROkc1tUe0DtO6cP1Zf6J0VemgSY8i/LZECTGszRqJVQRZ0MoVnBhuPAJk5eli7fhVcF8hWd3E4XQ3LzfmJCuaj2NEteRi5Hrfg=</Signature>
</License>
\ No newline at end of file
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment