Esempio n. 1
0
        /// <summary>
        /// 生成缩略图,只生成第一张页面的缩略图
        /// </summary>
        /// <param name="fileBuffer">文件的字节数组</param>
        /// <param name="fileType">文件类型,目前支持:Word、Excel、PDF</param>
        public static byte[] ConvertToSnap(byte[] fileBuffer, string fileType)
        {
            var snapStream = new MemoryStream();

            if (fileType == "xls" || fileType == "xlsx")
            {
                var book       = new Aspose.Cells.Workbook(new MemoryStream(fileBuffer));
                var sheet      = book.Worksheets[0];
                var imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions
                {
                    OnePagePerSheet      = true,
                    VerticalResolution   = 400,
                    HorizontalResolution = 300,
                    ImageFormat          = System.Drawing.Imaging.ImageFormat.Jpeg
                };
                var sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions);
                sr.ToImage(0, snapStream);
            }
            else if (fileType == "doc" || fileType == "docx")
            {
                var doc        = new Aspose.Words.Document(new MemoryStream(fileBuffer));
                var imgOptions = new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Jpeg)
                {
                    Resolution = 400,
                };
                doc.Save(snapStream, imgOptions);
            }
            else if (fileType == "pdf")
            {
                //aspose有性能问题
                //var converter = new Aspose.Pdf.Facades.PdfConverter();
                //converter.BindPdf(new Aspose.Pdf.Document(new MemoryStream(fileBuffer)));
                //converter.DoConvert();
                //converter.GetNextImage(snapStream, new Aspose.Pdf.PageSize(200, 150), System.Drawing.Imaging.ImageFormat.Jpeg,30);

                //换用ghostscript
                //GhostscriptRasterizer rasterizer = new GhostscriptRasterizer();
                //rasterizer.Open(new MemoryStream(fileBuffer));
                //Image page = rasterizer.GetPage(96, 96, 1);
                //page.Save(snapStream, ImageFormat.Jpeg);

                PDFFile pdfFile = PDFFile.Open(new MemoryStream(fileBuffer));
                Bitmap  bitmap  = pdfFile.GetPageImage(0, 56 * (int)Definition.Two);
                bitmap.Save(snapStream, ImageFormat.Jpeg);
            }

            var result = snapStream.ToArray();

            snapStream.Close();
            return(result);
        }
Esempio n. 2
0
        private static void DocumentToImage()
        {
            Document doc           = new Document(@"C:\test.doc");
            var      inputFileName = @"C:\test.docx";

            Aspose.Cloud.Words.DocumentBuilder builder = new Aspose.Cloud.Words.DocumentBuilder();

            builder.insertWatermarkImage(fileName, watermarkImage, rotationAngle);

            Aspose.Words.Saving.ImageSaveOptions options = new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Jpeg);
            options.PageCount = 1;
            for (int pageIndex = 0; pageIndex < doc.PageCount; pageIndex++)
            {
                string outputFileName = @"C:\" + string.Format("DOCUMENT{0}_{1}.jpeg", "Test", pageIndex + 1);
                options.PageIndex = pageIndex;

                doc.Save(outputFileName, options);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 生成缩略图,只生成第一张页面的缩略图
        /// </summary>
        /// <param name="fileBuffer">文件的字节数组</param>
        /// <param name="fileType">文件类型,目前支持:Word、Excel、PDF</param>
        public static byte[] ConvertToSnap(byte[] fileBuffer, string fileType)
        {
            var snapStream = new MemoryStream();

            if (fileType == "xls" || fileType == "xlsx")
            {
                var book       = new Aspose.Cells.Workbook(new MemoryStream(fileBuffer));
                var sheet      = book.Worksheets[0];
                var imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions
                {
                    OnePagePerSheet      = true,
                    VerticalResolution   = 400,
                    HorizontalResolution = 300,
                    ImageFormat          = System.Drawing.Imaging.ImageFormat.Jpeg
                };
                var sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions);
                sr.ToImage(0, snapStream);
            }
            else if (fileType == "doc" || fileType == "docx")
            {
                var doc        = new Aspose.Words.Document(new MemoryStream(fileBuffer));
                var imgOptions = new Aspose.Words.Saving.ImageSaveOptions(Aspose.Words.SaveFormat.Jpeg)
                {
                    Resolution = 400,
                };
                doc.Save(snapStream, imgOptions);
            }
            else if (fileType == "pdf")
            {
                var converter = new Aspose.Pdf.Facades.PdfConverter();
                converter.BindPdf(new Aspose.Pdf.Document(new MemoryStream(fileBuffer)));
                converter.DoConvert();
                converter.GetNextImage(snapStream, new Aspose.Pdf.PageSize(200, 150), System.Drawing.Imaging.ImageFormat.Jpeg);
            }

            var result = snapStream.ToArray();

            snapStream.Close();
            return(result);
        }