Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            // The path to the documents directory.
            string dataDir = Path.GetFullPath("../../../Data/");

            //Open an Excel file
            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(dataDir + "Book1.xls");

            //Get the first worksheet
            Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];

            //Apply different Image and Print options
            Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();

            //Set the Format
            options.SaveFormat = SaveFormat.XPS;

            //Render the sheet with respect to specified printing options
            Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, options);

            // Save
            sr.ToImage(0, dataDir + "out_printingxps.xps");

            //Export the whole workbook to XPS
            Aspose.Cells.Rendering.WorkbookRender wr = new Aspose.Cells.Rendering.WorkbookRender(workbook, options);
            wr.ToImage(dataDir + "out_whole_printingxps.xps");
        }
        public static void Run()
        {
            // ExStart:RenderWorksheetToGraphicContext
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            // Create workbook object from source file
            Workbook workbook = new Workbook(dataDir + "SampleBook.xlsx");

            // Access first worksheet
            Worksheet worksheet = workbook.Worksheets[0];

            // Create empty Bitmap
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(1100, 600);

            // Create Graphics Context
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
            g.Clear(System.Drawing.Color.Blue);

            // Set one page per sheet to true in image or print options
            Aspose.Cells.Rendering.ImageOrPrintOptions opts = new Aspose.Cells.Rendering.ImageOrPrintOptions();
            opts.OnePagePerSheet = true;

            // Render worksheet to graphics context
            Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(worksheet, opts);
            sr.ToImage(0, g, 0, 0);

            // Save the graphics context image in Png format
            bmp.Save(dataDir + "OutputImage_out.png", System.Drawing.Imaging.ImageFormat.Png);
            //ExEnd:RenderWorksheetToGraphicContext
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 打印DEMO
        /// Aspose.Cell 原理输出为Image, 打印Image
        /// </summary>
        public static void PrintDemo(string filePath)
        {
            Workbook workbook = new Workbook(filePath);

            //Get the worksheet to be printed
            Worksheet worksheet = workbook.Worksheets[0];

            PageSetup pageSetup = worksheet.PageSetup;

            pageSetup.Orientation = PageOrientationType.Landscape;
            //pageSetup.LeftMargin = 0;
            //pageSetup.RightMargin = 0.1;
            //pageSetup.BottomMargin = 0.3;
            //pageSetup.PrintArea = "A2:J29"; // 打印区域
            //Apply different Image / Print options.

            Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();

            //Set the Printing page property
            //options.PrintingPage = PrintingPageType.IgnoreStyle;
            //Render the worksheet

            Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(worksheet, options);

            System.Drawing.Image map = sr.ToImage(0); // 核心 -- 将 SheetRendar转为 Image 进行打印
                                                      // map.Save(@"D:\HoweDesktop\A{0}".FormatWith(DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss")));

            // send to printer 核心打印 Image
            System.Drawing.Printing.PrinterSettings printSettings = new System.Drawing.Printing.PrinterSettings();
            string strPrinterName = printSettings.PrinterName;

            sr.ToPrinter(strPrinterName);
        }
        public static void Run()
        {
            // ExStart:1
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            // Open an Excel file
            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(dataDir + "Book1.xls");

            // Get the first worksheet
            Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];

            // Apply different Image and Print options
            Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();

            // Set the Format
            options.SaveFormat = SaveFormat.XPS;

            // Render the sheet with respect to specified printing options
            Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, options);

            // Save
            sr.ToImage(0, dataDir + "out_printingxps.out.xps");

            // Export the whole workbook to XPS
            Aspose.Cells.Rendering.WorkbookRender wr = new Aspose.Cells.Rendering.WorkbookRender(workbook, options);
            wr.ToImage(dataDir + "out_whole_printingxps.out.xps");
            // ExEnd:1
        }
Ejemplo n.º 5
0
        public static void Run()
        {
            // ExStart:1
            //Source directory
            string sourceDir = RunExamples.Get_SourceDirectory();

            //Output directory
            string outputDir = RunExamples.Get_OutputDirectory();

            // Create workbook object from source file
            Workbook workbook = new Workbook(sourceDir + "sampleWorksheetToImageDesiredSize.xlsx");

            // Access first worksheet
            Worksheet worksheet = workbook.Worksheets[0];

            // Set image or print options we want one page per sheet. The image format is in png and desired dimensions are 400x400
            Aspose.Cells.Rendering.ImageOrPrintOptions opts = new Aspose.Cells.Rendering.ImageOrPrintOptions();
            opts.OnePagePerSheet = true;
            opts.ImageType       = Drawing.ImageType.Png;
            opts.SetDesiredSize(400, 400);

            // Render sheet into image
            Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(worksheet, opts);
            sr.ToImage(0, outputDir + "outputWorksheetToImageDesiredSize.png");
            // ExEnd:1

            Console.WriteLine("WorksheetToImageDesiredSize executed successfully.");
        }
Ejemplo n.º 6
0
        public static void Run()
        {
            //Source directory
            string sourceDir = RunExamples.Get_SourceDirectory();

            //Output directory
            string outputDir = RunExamples.Get_OutputDirectory();

            // Create workbook object from source file
            Workbook workbook = new Workbook(sourceDir + "sampleRenderWorksheetToGraphicContext.xlsx");

            // Access first worksheet
            Worksheet worksheet = workbook.Worksheets[0];

            // Create empty Bitmap
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(1100, 600);

            // Create Graphics Context
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
            g.Clear(System.Drawing.Color.Blue);

            // Set one page per sheet to true in image or print options
            Aspose.Cells.Rendering.ImageOrPrintOptions opts = new Aspose.Cells.Rendering.ImageOrPrintOptions();
            opts.OnePagePerSheet = true;

            // Render worksheet to graphics context
            Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(worksheet, opts);
            sr.ToImage(0, g, 0, 0);

            // Save the graphics context image in Png format
            bmp.Save(outputDir + "outputRenderWorksheetToGraphicContext.png", System.Drawing.Imaging.ImageFormat.Png);

            Console.WriteLine("RenderWorksheetToGraphicContext executed successfully.");
        }
Ejemplo n.º 7
0
        public static void Main(string[] args)
        {
            // The path to the documents directory.
            string dataDir = Aspose.Cells.Examples.Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            //Open an Excel file
            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(dataDir + "Book1.xls");

            //Get the first worksheet
            Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];

            //Apply different Image and Print options
            Aspose.Cells.Rendering.ImageOrPrintOptions options = new Aspose.Cells.Rendering.ImageOrPrintOptions();
            
            //Set the Format
            options.SaveFormat = SaveFormat.XPS;
            
            //Render the sheet with respect to specified printing options
            Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, options);
            
            // Save
            sr.ToImage(0, dataDir + "out_printingxps.xps");

            //Export the whole workbook to XPS
            Aspose.Cells.Rendering.WorkbookRender wr = new Aspose.Cells.Rendering.WorkbookRender(workbook, options);
            wr.ToImage(dataDir + "out_whole_printingxps.xps");
        }
        public static void Main()
        {
            // ExStart: 1
            // Load sample Excel file containing slicer.
            Workbook wb = new Workbook(sourceDir + "sampleRenderingSlicer.xlsx");

            // Access first worksheet.
            Worksheet ws = wb.Worksheets[0];

            // Set the print area because we want to render slicer only.
            ws.PageSetup.PrintArea = "B15:E25";

            // Specify image or print options, set one page per sheet and only area to true.
            Aspose.Cells.Rendering.ImageOrPrintOptions imgOpts = new Aspose.Cells.Rendering.ImageOrPrintOptions();
            imgOpts.HorizontalResolution = 200;
            imgOpts.VerticalResolution   = 200;
            imgOpts.ImageType            = Aspose.Cells.Drawing.ImageType.Png;
            imgOpts.OnePagePerSheet      = true;
            imgOpts.OnlyArea             = true;

            // Create sheet render object and render worksheet to image.
            Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(ws, imgOpts);
            sr.ToImage(0, outputDir + "outputRenderingSlicer.png");
            // ExEnd: 1

            Console.WriteLine("RenderingSlicer executed successfully.");
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 转换为图片
        /// </summary>
        /// <param name="filepath"></param>
        /// <param name="imageFolderName"></param>
        /// <param name="imageFolderRootPath"></param>
        public void ConvertToImage(string filepath, string imageFolderName, string imageFolderRootPath)
        {
            if (!Directory.Exists(imageFolderRootPath))
            {
                ///若路径不存在,创建路径
                Directory.CreateDirectory(imageFolderRootPath);
            }

            if (!Directory.Exists(imageFolderRootPath + "\\" + imageFolderName))
            {
                ///若路径不存在,创建路径
                Directory.CreateDirectory(imageFolderRootPath + "\\" + imageFolderName);
            }


            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(filepath);
            var count = 0;

            foreach (Aspose.Cells.Worksheet sheet in workbook.Worksheets)
            {
                if (sheet.IsVisible)
                {
                    //Define ImageOrPrintOptions
                    Aspose.Cells.Rendering.ImageOrPrintOptions imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions();
                    //Specify the image format
                    imgOptions.AllColumnsInOnePagePerSheet = true;
                    imgOptions.IsCellAutoFit = true;
                    imgOptions.OnlyArea      = false;
                    imgOptions.OnlyArea      = true;
                    imgOptions.ImageFormat   = System.Drawing.Imaging.ImageFormat.Png;
                    //Only one page for the whole sheet would be rendered
                    imgOptions.OnePagePerSheet = false;

                    //Render the sheet with respect to specified image/print options
                    Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions);
                    for (int i = 0; i < sr.PageCount; i++)
                    {
                        count++;
                        string imageFilePath = string.Format(@"{0}\{1}\{2}.png", imageFolderRootPath, imageFolderName, i);
                        sr.ToImage(i, imageFilePath);
                    }
                    //Render the image for the sheet

                    //System.Drawing.Bitmap bitmap = sr.ToImage(0,"D:\\1.jpg");

                    //Save the image file specifying its image format.
                    // bitmap.Save(@"d:\test\SheetImage.jpg");
                }
            }

            string jsFileName = string.Format(@"{0}\{1}\{2}.js", imageFolderRootPath, imageFolderName, "info");
            var    fileType   = (filepath.EndsWith(".xlsx")) ? "xlsx" : "xls";

            File.WriteAllText(jsFileName, "var imageInfo={PageCount:" + count + ",FileType:'" + fileType + "'}");
        }
Ejemplo n.º 10
0
        private static void XlsToImgMothed(string filename, Workbook book, Aspose.Cells.Rendering.ImageOrPrintOptions imgOptions, int i)
        {
            //获取一张工作表
            Worksheet sheet = book.Worksheets[i];

            //创建一个纸张底色渲染对象
            Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions);
            for (int j = 0; j < sr.PageCount; j++)
            {
                string imgpath = imageDirectoryPath + "/" + filename + "_" + i + "_" + j + "_.jpeg";
                if (!File.Exists(imgpath))
                {
                    sr.ToImage(j, imgpath);
                }
            }
        }
Ejemplo n.º 11
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);
        }
Ejemplo n.º 12
0
        public static void Run()
        {
            // ExStart:WorksheetToImageDesiredSize
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            // Create workbook object from source file
            Workbook workbook = new Workbook(dataDir + "SampleBook.xlsx");

            // Access first worksheet
            Worksheet worksheet = workbook.Worksheets[0];

            // Set image or print options we want one page per sheet. The image format is in png and desired dimensions are 400x400
            Aspose.Cells.Rendering.ImageOrPrintOptions opts = new Aspose.Cells.Rendering.ImageOrPrintOptions();
            opts.OnePagePerSheet = true;
            opts.ImageFormat     = System.Drawing.Imaging.ImageFormat.Png;
            opts.SetDesiredSize(400, 400);

            // Render sheet into image
            Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(worksheet, opts);
            sr.ToImage(0, dataDir + "ImageWithDesiredSize_out.png");
            // ExEnd:WorksheetToImageDesiredSize
        }
        public static void Run()
        {
            // ExStart:WorksheetToImageDesiredSize
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            // Create workbook object from source file
            Workbook workbook = new Workbook(dataDir + "SampleBook.xlsx");

            // Access first worksheet
            Worksheet worksheet = workbook.Worksheets[0];

            // Set image or print options we want one page per sheet. The image format is in png and desired dimensions are 400x400
            Aspose.Cells.Rendering.ImageOrPrintOptions opts = new Aspose.Cells.Rendering.ImageOrPrintOptions();
            opts.OnePagePerSheet = true;
            opts.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
            opts.SetDesiredSize(400, 400);

            // Render sheet into image
            Aspose.Cells.Rendering.SheetRender sr = new Aspose.Cells.Rendering.SheetRender(worksheet, opts);
            sr.ToImage(0, dataDir + "ImageWithDesiredSize_out.png");
            // ExEnd:WorksheetToImageDesiredSize
        }