我希望找到一种将Excel、PowerPoint、PDF和Word转换为图像的方法。我想知道是否有人使用过Aspose套件,并知道是否可以使用Aspose.PDF套件完成所有操作,还是需要同时获取Aspose.slides和Aspose.word?
我希望找到一种将Excel、PowerPoint、PDF和Word转换为图像的方法。我想知道是否有人使用过Aspose套件,并知道是否可以使用Aspose.PDF套件完成所有操作,还是需要同时获取Aspose.slides和Aspose.word?
您需要使用Aspose.Slides、Aspose.Words、Aspose.Cells和Aspose.Pdf,或者您可以使用Web Api。
可以在不使用任何第三方组件的情况下将办公文档转换为图像。以下代码将把Excel工作表中的特定范围转换为图像。
重要提示:不要忘记使用[STAThread]属性标记方法。
使用:
using xls = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Windows.Forms;
转化代码:
[STAThread]
static void Main(string[] args)
{
string fileNameToProcess = @"D:\Book2.xlsx";
//Start Excel and create a new document.
xls.Application oExcel = new xls.Application();
xls.Workbook wb = null;
try
{
wb = oExcel.Workbooks.Open(
fileNameToProcess.ToString(), false, false, Type.Missing, "", "", true, xls.XlPlatform.xlWindows, "", false, false, 0, false, true, 0);
//wb.RefreshAll();
xls.Sheets sheets = wb.Worksheets as xls.Sheets;
xls.Worksheet sheet = sheets[1];
//Following is used to find range with data
string startRange = "A1";
string endRange = "P25";
xls.Range range = sheet.get_Range(startRange, endRange);
range.CopyPicture(xls.XlPictureAppearance.xlScreen, xls.XlCopyPictureFormat.xlBitmap);
System.Drawing.Image imgRange = GetImageFromClipboard();
imgRange.Save(@"d:\ExcelSheetImage.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
wb.Save();
Console.Write("Specified range converted to image successfully. Press Enter to continue.");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
//throw;
}
finally
{
wb.Close();
oExcel.Quit();
oExcel = null;
}
Console.ReadLine();
}
public static System.Drawing.Image GetImageFromClipboard()
{
System.Drawing.Image returnImage = null;
// This doesn't work
//if (Clipboard.ContainsImage())
//{
// returnImage = Clipboard.GetImage();
//}
//return returnImage;
// This works
System.Windows.Forms.IDataObject d = Clipboard.GetDataObject();
if (d.GetDataPresent(DataFormats.Bitmap))
{
returnImage = Clipboard.GetImage();
}
return returnImage;
}
我叫Nayyer,目前在Aspose担任开发者推广员。
我知道现在已经是2019年了,但如果有人仍然想知道,你可以使用Aspose.Words来完成这个任务。
以下值(以及更多)在SaveFormat枚举中可用:
Tiff = 100,
//
// Summary:
// Renders a page of the document and saves it as a PNG file.
Png = 101,
//
// Summary:
// Renders a page of the document and saves it as a BMP file.
Bmp = 102,
//
// Summary:
// Renders a page of the document and saves it as a JPEG file.
Jpeg = 104,
//
// Summary:
// Renders a page of the document and saves it as a GIF file.
Gif = 105
你可以这样使用它们:
var fileContents = asposeDocument.GenerateDocument(Aspose.Words.SaveFormat.Png);