Beispiel #1
0
        private static void MakeImageTiff(object sender, Aspose.Html.Dom.Events.Event e)

        {
            Console.WriteLine("<MakeImage>");
            if (sender is HTMLDocument doc)
            {
                if (doc.ReadyState.Equals("complete"))
                {
                    Console.WriteLine(doc.ReadyState);
                    var imgOptions = new ImageRenderingOptions();
                    imgOptions.Format      = ImageFormat.Tiff;
                    imgOptions.Compression = ImageRenderingOptions.Comression.LZW;
                    imgOptions.PageSetup.AnyPage.Margin = new Aspose.Html.Drawing.Margin(Unit.FromCentimeters(1));
                    imgOptions.PageSetup.AnyPage.Size   = new Aspose.Html.Drawing.Size(Unit.FromCentimeters(14.8), Unit.FromCentimeters(21));
                    using (var imgDevice = new ImageDevice(imgOptions, "Aspose_HTML.Tiff"))
                        using (var renderer = new HtmlRenderer())
                        {
                            // Render the output using HtmlRenderer
                            renderer.Render(imgDevice, doc);
                        }
                    IsFinished.Set();
                }
                else
                {
                    Console.WriteLine(doc.ReadyState);
                }
            }
            Console.WriteLine("</MakeImage>");
        }
        private static void SetImageOptionsRunMethodAndRevert(Graphics g,
                                                              ImageRenderingOptions newImageRenderingOptions, Action methodToRun)
        {
            var originalRenderingOptions = GetImageRenderingOptions(g);

            SetImageRenderingOptions(g, newImageRenderingOptions);
            methodToRun?.Invoke();
            SetImageRenderingOptions(g, originalRenderingOptions);
        }
 private static void SetImageRenderingOptions(Graphics g, ImageRenderingOptions imageRenderingOptions)
 {
     g.InterpolationMode  = imageRenderingOptions.InterpolationMode;
     g.SmoothingMode      = imageRenderingOptions.SmoothingMode;
     g.PixelOffsetMode    = imageRenderingOptions.PixelOffsetMode;
     g.CompositingQuality = imageRenderingOptions.CompositingQuality;
     g.TextRenderingHint  = imageRenderingOptions.TextRenderingHint;
     g.CompositingMode    = imageRenderingOptions.CompositingMode;
 }
Beispiel #4
0
        protected static IDevice GetOutputDevice(ConversionApplicationOptions options, string filePath)
        {
            T ApplyPageSizeIfAny <T>(T renderingOptions)
                where T : RenderingOptions
            {
                if (options.PageSize != null)
                {
                    renderingOptions.PageSetup.AnyPage.Size = options.PageSize;
                }
                return(renderingOptions);
            }

            if (FileFormat.PDF == options.OutputFormat)
            {
                var pdfOptions = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions();
                if (!string.IsNullOrEmpty(options.UserPassword) && !string.IsNullOrEmpty(options.OwnerPassword))
                {
                    const PdfPermissions allPermissions = PdfPermissions.PrintDocument | PdfPermissions.ModifyContent |
                                                          PdfPermissions.ExtractContent | PdfPermissions.ModifyTextAnnotations |
                                                          PdfPermissions.FillForm | PdfPermissions.ExtractContentWithDisabilities |
                                                          PdfPermissions.AssembleDocument | PdfPermissions.PrintingQuality;

                    pdfOptions.Encryption = new Aspose.Html.Rendering.Pdf.Encryption.PdfEncryptionInfo(
                        options.UserPassword,
                        options.OwnerPassword,
                        allPermissions,
                        PdfEncryptionAlgorithm.RC4_128);
                }
                pdfOptions.BackgroundColor = options.BackgroundColor;
                return(new PdfDevice(pdfOptions, filePath));
            }

            if (FileFormat.XPS == options.OutputFormat)
            {
                var xpsOptions = new XpsRenderingOptions();
                xpsOptions = ApplyPageSizeIfAny(xpsOptions);
                return(new XpsDevice(ApplyPageSizeIfAny(xpsOptions), filePath));
            }


            if (FileFormat.JPEG == options.OutputFormat ||
                FileFormat.PNG == options.OutputFormat ||
                FileFormat.BMP == options.OutputFormat ||
                FileFormat.TIFF == options.OutputFormat ||
                FileFormat.GIF == options.OutputFormat)
            {
                var imageOptions = new ImageRenderingOptions(options.OutputFormat.ToImageFormat());
                imageOptions.BackgroundColor = options.BackgroundColor;
                imageOptions = ApplyPageSizeIfAny(imageOptions);
                return(new ImageDevice(imageOptions, filePath));
            }


            throw new ArgumentException("The output format is not supported.", "OutputFormat");
        }
 ///<Summary>
 /// ConvertHtmlToTiff to convert html file to tiff
 ///</Summary>
 public Response ConvertHtmlToTiff(string fileName, string folderName)
 {
     return(ProcessTask(fileName, folderName, ".tiff", false, false, delegate(string inFilePath, string outPath, string zipOutFolder)
     {
         ImageRenderingOptions img_options = new ImageRenderingOptions();
         img_options.Format = ImageFormat.Tiff;
         SourceFormat srcFormat = ExportHelper.GetSourceFormatByFileName(fileName);
         ExportHelper helper = ExportHelper.GetHelper(srcFormat, ExportFormat.TIFF);
         helper.Export(inFilePath, outPath, img_options);
     }));
 }
        public MemoryStream GenerateImage(string htmlPath)
        {
            var html = new HTMLDocument(htmlPath);

            using (var renderer = new HtmlRenderer())
            {
                ImageRenderingOptions options = new ImageRenderingOptions();
                options.Format = ImageFormat.Jpeg;
                MemoryStream imageStream = new MemoryStream();
                using (ImageDevice ImageDevice = new ImageDevice(options, imageStream))
                {
                    renderer.Render(ImageDevice, html);
                    return(imageStream);
                }
            }
        }
        public MemoryStream GenerateImageFromWord(Stream template, string parragraf)
        {
            Document        doc     = new Document(template);
            DocumentBuilder builder = new DocumentBuilder(doc);

            builder.Font.Border.Color     = Color.Green;
            builder.Font.Border.LineWidth = 2.5;
            builder.Font.Border.LineStyle = LineStyle.DashDotStroker;
            builder.Writeln(parragraf);
            ImageRenderingOptions options = new ImageRenderingOptions();

            options.Format = ImageFormat.Jpeg;
            MemoryStream imageStream = new MemoryStream();

            using (ImageDevice ImageDevice = new ImageDevice(options, imageStream))
            {
                doc.Save(imageStream, SaveFormat.Jpeg);
                return(imageStream);
            }
        }
        ///<Summary>
        /// ConvertHtmlToImages to convert html file to images
        ///</Summary>
        public Response ConvertHtmlToImages(string fileName, string folderName, string outputType)
        {
            if (outputType.Equals("bmp") || outputType.Equals("jpg") ||
                outputType.Equals("png") || outputType.Equals("gif"))
            {
                ImageFormat  format    = ImageFormat.Bmp;
                ExportFormat expFormat = ExportFormat.BMP;

                if (outputType.Equals("jpg"))
                {
                    format    = ImageFormat.Jpeg;
                    expFormat = ExportFormat.JPEG;
                }
                else if (outputType.Equals("png"))
                {
                    format    = ImageFormat.Png;
                    expFormat = ExportFormat.PNG;
                }
                else if (outputType.Equals("gif"))
                {
                    format    = ImageFormat.Gif;
                    expFormat = ExportFormat.GIF;
                }

                return(ProcessTask(fileName, folderName, "." + outputType, true, false, delegate(string inFilePath, string outPath, string zipOutFolder)
                {
                    ImageRenderingOptions img_options = new ImageRenderingOptions();
                    img_options.Format = format;
                    SourceFormat srcFormat = ExportHelper.GetSourceFormatByFileName(fileName);
                    ExportHelper helper = ExportHelper.GetHelper(srcFormat, expFormat);
                    helper.Export(inFilePath, outPath, img_options);
                }));
            }

            return(new Response
            {
                FileName = null,
                Status = "Output type not found",
                StatusCode = 500
            });
        }
Beispiel #9
0
        public static void Run()
        {
            // ExStart:1
            string dataDir = RunExamples.GetDataDir_Data();

            using (var document = new Aspose.Html.HTMLDocument("<style>p { color: green; }</style><p>my first paragraph</p>", @"c:\work\"))
            {
                // Initialize rendering optionss and set jpeg as output format
                var options = new ImageRenderingOptions(ImageFormat.Jpeg);

                // Set the size and margin property for all pages.
                options.PageSetup.AnyPage = new Page(new Size(500, 500), new Margin(50, 50, 50, 50));

                //  If the document has an element which size is bigger than predefined by user page size output pages will be will be adjusted.
                options.PageSetup.AdjustToWidestPage = true;

                using (ImageDevice device = new ImageDevice(options, dataDir + @"document_out.jpg"))
                {
                    document.RenderTo(device);
                }
            }
            // ExEnd:1
        }
Beispiel #10
0
        public void ConvertZIPtoJPGTest()
        {
            // Prepare path to a source zip file
            string documentPath = Path.Combine(DataDir, "test.zip");

            // Prepare path for converted file saving
            string savePath = Path.Combine(OutputDir, "zip-to-jpg.jpg");

            // Create an instance of ZipArchiveMessageHandler
            using var zip = new ZipArchiveMessageHandler(documentPath);

            // Create an instance of the Configuration class
            using var configuration = new Configuration();

            // Add ZipArchiveMessageHandler to the chain of existing message handlers
            configuration
            .GetService <INetworkService>()
            .MessageHandlers.Add(zip);

            // Initialize an HTML document with specified configuration
            using var document = new HTMLDocument("zip:///test.html", configuration);

            // Create an instance of Rendering Options
            var options = new ImageRenderingOptions()
            {
                Format = ImageFormat.Jpeg
            };

            // Create an instance of Image Device
            using var device = new ImageDevice(options, savePath);

            // Render ZIP to JPG
            document.RenderTo(device);

            Assert.True(File.Exists(Path.Combine(OutputDir, "zip-to-jpg_1.jpg")));
        }
Beispiel #11
0
 ///<Summary>
 /// ConvertHtmlToTiff to convert html file to tiff
 ///</Summary>
 public Response ConvertHtmlToTiff(string[] fileNames, string folderName)
 {
     return(ProcessTask_(fileNames, (inFiles, outPath, zipOutFolder) =>
     {
         ImageRenderingOptions img_options = new ImageRenderingOptions();
         img_options.Format = ImageFormat.Tiff;
         if (Opts.HasCustomParameter("pageSize"))
         {
             var sz = OptionHelper.getPageSizeByName(Opts.GetCustomParameter("pageSize"));
             if (sz != null)
             {
                 img_options.PageSetup.AnyPage.Size = sz;
             }
         }
         if (Opts.HasCustomParameter("bgColor"))
         {
             string bgColor = Opts.GetCustomParameter("bgColor");
             if (!string.IsNullOrEmpty(bgColor))
             {
                 long argb = long.Parse(bgColor, System.Globalization.NumberStyles.HexNumber);
                 img_options.BackgroundColor = System.Drawing.Color.FromArgb((int)(argb | 0xFF000000));
             }
         }
         Dictionary <string, string> customParams = null;
         if (Opts.HasCustomParameter("mdTheme"))
         {
             var csstheme = Opts.GetCustomParameter("mdTheme");
             customParams = new Dictionary <string, string> {
                 { "cssTheme", csstheme }
             };
         }
         SourceFormat srcFormat = ExportHelper.GetSourceFormatByFileName(Opts.FileName);
         ExportHelper helper = ExportHelper.GetHelper(srcFormat, ExportFormat.TIFF, customParams);
         helper.Export(inFiles, outPath, img_options);
     }));
 }
Beispiel #12
0
        public virtual void Export(string sourcePath, string outPath, RenderingOptions options = null)
        {
            switch (dstFormat_)
            {
            case ExportFormat.PDF:
                PdfRenderingOptions pdf_opts = options as PdfRenderingOptions;
                if (pdf_opts == null)
                {
                    pdf_opts = new PdfRenderingOptions();
                }

                using (PdfDevice device = new PdfDevice(pdf_opts, outPath))
                {
                    Render(sourcePath, device);
                }
                break;

            case ExportFormat.XPS:
                XpsRenderingOptions xps_opts = options as XpsRenderingOptions
                ;
                if (xps_opts == null)
                {
                    xps_opts = new XpsRenderingOptions();
                }

                using (XpsDevice device = new XpsDevice(xps_opts, outPath))
                {
                    Render(sourcePath, device);
                }
                break;

            case ExportFormat.MD:
                IDevice nullDev = null;
                Render(sourcePath, nullDev);
                break;

            case ExportFormat.MHTML:
                throw new NotImplementedException("Conversion to 'MHTML' isn't implemented yet.");

            //break;
            case ExportFormat.JPEG:
            case ExportFormat.PNG:
            case ExportFormat.BMP:
            case ExportFormat.TIFF:
            case ExportFormat.GIF:
                ImageRenderingOptions img_opts = options as ImageRenderingOptions;
                if (img_opts == null)
                {
                    img_opts = new ImageRenderingOptions();
                }
                switch (dstFormat_)
                {
                case ExportFormat.JPEG:
                    img_opts.Format = ImageFormat.Jpeg; break;

                case ExportFormat.PNG:
                    img_opts.Format = ImageFormat.Png; break;

                case ExportFormat.BMP:
                    img_opts.Format = ImageFormat.Bmp; break;

                case ExportFormat.TIFF:
                    img_opts.Format = ImageFormat.Tiff; break;

                case ExportFormat.GIF:
                    img_opts.Format = ImageFormat.Gif; break;
                }
                using (ImageDevice device = new ImageDevice(img_opts, outPath))
                {
                    Render(sourcePath, device);
                }

                break;
            }
        }
Beispiel #13
0
        ///<Summary>
        /// ConvertHtmlToImages to convert html file to images
        ///</Summary>
        public Response ConvertHtmlToImages(string[] fileNames, string folderName, string outputType)
        {
            if (outputType.Equals("bmp") ||
                outputType.Equals("jpg") || outputType.Equals("jpeg") ||
                outputType.Equals("png") || outputType.Equals("gif"))
            {
                ImageFormat  format    = ImageFormat.Bmp;
                ExportFormat expFormat = ExportFormat.BMP;

                if (outputType.Equals("jpg") || outputType.Equals("jpeg"))
                {
                    format    = ImageFormat.Jpeg;
                    expFormat = ExportFormat.JPEG;
                }
                else if (outputType.Equals("png"))
                {
                    format    = ImageFormat.Png;
                    expFormat = ExportFormat.PNG;
                }
                else if (outputType.Equals("gif"))
                {
                    format    = ImageFormat.Gif;
                    expFormat = ExportFormat.GIF;
                }

                return(ProcessTask_(fileNames, (inFiles, outPath, zipOutFolder) =>
                {
                    ImageRenderingOptions img_options = new ImageRenderingOptions();
                    img_options.Format = format;
                    if (Opts.HasCustomParameter("pageSize"))
                    {
                        var sz = OptionHelper.getPageSizeByName(Opts.GetCustomParameter("pageSize"));
                        if (sz != null)
                        {
                            img_options.PageSetup.AnyPage.Size = sz;
                        }
                    }
                    if (Opts.HasCustomParameter("bgColor"))
                    {
                        string bgColor = Opts.GetCustomParameter("bgColor");
                        if (!string.IsNullOrEmpty(bgColor))
                        {
                            int argb = int.Parse(bgColor, System.Globalization.NumberStyles.HexNumber);
                            img_options.BackgroundColor = System.Drawing.Color.FromArgb(argb);
                        }
                    }
                    Dictionary <string, string> customParams = null;
                    if (Opts.HasCustomParameter("mdTheme"))
                    {
                        var csstheme = Opts.GetCustomParameter("mdTheme");
                        customParams = new Dictionary <string, string> {
                            { "cssTheme", csstheme }
                        };
                    }
                    SourceFormat srcFormat = ExportHelper.GetSourceFormatByFileName(Opts.FileName);
                    ExportHelper helper = ExportHelper.GetHelper(srcFormat, expFormat, customParams);

                    helper.Export(inFiles, outPath, img_options);
                }));
            }

            return(new Response
            {
                FileName = null,
                Status = "Output type not found",
                StatusCode = 500
            });
        }