使用Aspose在.NET中将Excel、PowerPoint、PDF和Word转换为图像

4

我希望找到一种将Excel、PowerPoint、PDF和Word转换为图像的方法。我想知道是否有人使用过Aspose套件,并知道是否可以使用Aspose.PDF套件完成所有操作,还是需要同时获取Aspose.slides和Aspose.word?


为什么不直接在Aspose论坛上发布这个问题呢? - user1805246
我给支持团队发了电子邮件,但有时在这里发帖会得到更快的回复。自从我了解了Web API后,这种方式运作得很好 :) - Art F
4个回答

3

您需要使用Aspose.Slides、Aspose.Words、Aspose.Cells和Aspose.Pdf,或者您可以使用Web Api


1
感谢提供 Web API 的链接,这样就不需要额外的 Aspose 功能,而且更加经济实惠,因为我的项目并不需要那些功能。 - Art F

2

可以在不使用任何第三方组件的情况下将办公文档转换为图像。以下代码将把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;
    }

我会检查这个的,我花了几天时间研究使用Interop的方法,但是没有成功。我之前问过这个问题,所以我已经编写了所有代码,但只是演示版本,还没有购买,所以现在改变还不算太晚。 - Art F

1

我叫Nayyer,目前在Aspose担任开发者推广员。

  • Aspose.Words提供了创建/操作MS Word文档的功能,同时还提供将Word文档转换为图像格式的功能。您可以查看以下链接以获取更多有关将MS Word文档转换为图像的详细信息(只需从SaveFormat枚举中选择Jpeg、Png、Bmp即可)。
  • 同样,您可以使用Aspose.Cells将Excel工作表转换为图像格式。将工作表转换为图像
  • Aspose.Slides可用于将PowerPoint演示文稿转换为图像格式。创建幻灯片缩略图图像
  • Aspose.Pdf支持将PDF页面转换为图像格式的功能。有关更多详细信息,请访问将所有PDF页面转换为JPEG格式

PowerPoint教程已经迁移到http://www.aspose.com/docs/display/slidesnet/Creating+Slides+Thumbnail+Image。 - Vitani

0

我知道现在已经是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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接