Esempio n. 1
0
        public static void SpecifyPDFPermissions()
        {
            //ExStart: SpecifyPDFPermissions
            // Prepare an HTML code
            var code = @"<div>Hello World!!</div>";

            // Initialize the HTML document from the HTML code
            using (var document = new Aspose.Html.HTMLDocument(code, "."))
            {
                // Create the instance of Rendering Options
                var options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions();

                // Set the permissions to the file
                options.Encryption = new Aspose.Html.Rendering.Pdf.Encryption.PdfEncryptionInfo(
                    "user_pwd",
                    "owner_pwd",
                    Aspose.Html.Rendering.Pdf.Encryption.PdfPermissions.PrintDocument,
                    Aspose.Html.Rendering.Pdf.Encryption.PdfEncryptionAlgorithm.RC4_128);

                // Create the PDF Device and specify options and output file
                using (var device = new Aspose.Html.Rendering.Pdf.PdfDevice(options, "output.pdf"))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }
            }
            //ExEnd: SpecifyPDFPermissions
        }
Esempio n. 2
0
        public static void SpecifyRenderingOptions()
        {
            //ExStart: SpecifyRenderingOptions
            // Prepare an HTML code
            var code = @"<span>Hello World!!</span>";

            // Initialize the HTML document from the HTML code
            using (var document = new Aspose.Html.HTMLDocument(code, "."))
            {
                // Create the instance of PdfRenderingOptions and set a custom page-size
                var options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions()
                {
                    PageSetup =
                    {
                        AnyPage = new Aspose.Html.Drawing.Page(
                            new Aspose.Html.Drawing.Size(
                                Aspose.Html.Drawing.Length.FromInches(5),
                                Aspose.Html.Drawing.Length.FromInches(2)))
                    }
                };

                // Create the PDF Device and specify options and output file
                using (var device = new Aspose.Html.Rendering.Pdf.PdfDevice(options, "output.pdf"))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }
            }
            //ExEnd: SpecifyRenderingOptions
        }
Esempio n. 3
0
        public static void SpecifyLeftRightPageSize()
        {
            //ExStart: SpecifyLeftRightPageSize
            // Prepare an HTML code
            var code = @"<style>div { page-break-after: always; }</style>
            <div>First Page</div>
            <div>Second Page</div>
            <div>Third Page</div>
            <div>Fourth Page</div>";

            // Initialize the HTML document from the HTML code
            using (var document = new Aspose.Html.HTMLDocument(code, "."))
            {
                // Create the instance of Rendering Options and set a custom page-size
                var options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions();
                options.PageSetup.SetLeftRightPage(
                    new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(400, 200)),
                    new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(400, 100))
                    );

                // Create the PDF Device and specify options and output file
                using (var device = new Aspose.Html.Rendering.Pdf.PdfDevice(options, "output.pdf"))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }
            }
            //ExEnd: SpecifyLeftRightPageSize
        }
Esempio n. 4
0
        public static void AdjustPageSizeToContent()
        {
            //ExStart: AdjustPageSizeToContent
            // Prepare an HTML code
            var code = @"<style>
                div { page-break-after: always; }
            </style>
            <div style='border: 1px solid red; width: 400px'>First Page</div>
            <div style='border: 1px solid red; width: 600px'>Second Page</div>
            ";

            // Initialize the HTML document from the HTML code
            using (var document = new Aspose.Html.HTMLDocument(code, "."))
            {
                // Create the instance of Rendering Options and set a custom page-size
                var options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions();
                options.PageSetup.AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(500, 200));

                // Enable auto-adjusting for the page size
                options.PageSetup.AdjustToWidestPage = true;

                // Create the PDF Device and specify options and output file
                using (var device = new Aspose.Html.Rendering.Pdf.PdfDevice(options, "output.pdf"))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }
            }
            //ExEnd: AdjustPageSizeToContent
        }
Esempio n. 5
0
 ///<Summary>
 /// ConvertHtmlToPdf to convert html file to pdf
 ///</Summary>
 public Response ConvertHtmlToPdf(string[] fileNames, string folderName)
 {
     return(ProcessTask_(fileNames, (inFiles, outPath, zipOutFolder) =>
     {
         Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions();
         if (Opts.HasCustomParameter("ownerPassword") || Opts.HasCustomParameter("userPassword"))
         {
             var userPw = Opts.GetCustomParameter("userPassword");
             var ownerPw = Opts.GetCustomParameter("ownerPassword");
             if (!(string.IsNullOrEmpty(userPw) && string.IsNullOrEmpty(ownerPw)))
             {
                 pdf_options.Encryption = new Aspose.Html.Rendering.Pdf.Encryption.PdfEncryptionInfo(
                     userPw,
                     ownerPw,
                     (Aspose.Html.Rendering.Pdf.Encryption.PdfPermissions) 0xF3C,
                     Aspose.Html.Rendering.Pdf.Encryption.PdfEncryptionAlgorithm.RC4_128
                     );
             }
         }
         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.PDF, customParams);
         helper.Export(inFiles, outPath, pdf_options);
     }));
 }
Esempio n. 6
0
        public static void SpecifyBackgroundColor()
        {
            //ExStart: SpecifyBackgroundColor
            // Prepare an HTML code and save it to the file.
            var code = @"<p>Hello World!!</p>";

            System.IO.File.WriteAllText("document.html", code);

            // Create an instance of HTML document
            using (var document = new Aspose.Html.HTMLDocument("document.html"))
            {
                // Initialize options with 'cyan' as a background-color
                var options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions()
                {
                    BackgroundColor = System.Drawing.Color.Cyan
                };

                // Create an instance of PDF device
                using (var device = new Aspose.Html.Rendering.Pdf.PdfDevice(options, "output.pdf"))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }
            }
            //ExEnd: SpecifyBackgroundColor
        }
Esempio n. 7
0
        public static void SpecifyResolution()
        {
            //ExStart: SpecifyResolution
            // Prepare an HTML code and save it to the file.
            var code = @"
                <style>
                p
                { 
                    background: blue; 
                }
                @media(min-resolution: 300dpi)
                {
                    p 
                    { 
                        /* high resolution screen color */
                        background: green
                    }
                }
                </style>
                <p>Hello World!!</p>";

            System.IO.File.WriteAllText("document.html", code);

            // Create an instance of HTML document
            using (var document = new Aspose.Html.HTMLDocument("document.html"))
            {
                // Create options for low-resolution screens
                var options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions()
                {
                    HorizontalResolution = 50,
                    VerticalResolution   = 50
                };

                // Create an instance of PDF device
                using (var device = new Aspose.Html.Rendering.Pdf.PdfDevice(options, "output_resolution_50.pdf"))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }


                // Create options for high-resolution screens
                options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions()
                {
                    HorizontalResolution = 300,
                    VerticalResolution   = 300
                };

                // Create an instance of PDF device
                using (var device = new Aspose.Html.Rendering.Pdf.PdfDevice(options, "output_resolution_300.pdf"))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }
            }
            //ExEnd: SpecifyResolution
        }
 ///<Summary>
 /// ConvertHtmlToPdf to convert html file to pdf
 ///</Summary>
 public Response ConvertHtmlToPdf(string fileName, string folderName)
 {
     return(ProcessTask(fileName, folderName, ".pdf", false, false, delegate(string inFilePath, string outPath, string zipOutFolder)
     {
         Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions();
         SourceFormat srcFormat = ExportHelper.GetSourceFormatByFileName(fileName);
         ExportHelper helper = ExportHelper.GetHelper(srcFormat, ExportFormat.PDF);
         helper.Export(inFilePath, outPath, pdf_options);
     }));
 }
Esempio n. 9
0
        private static byte[] GetPdf(string html)
        {
            var stream = new MemoryStream();

            Aspose.Html.Drawing.Page page = new Aspose.Html.Drawing.Page();
            page.Size = new Aspose.Html.Drawing.Size(1200, 800);
            Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions();
            pdf_options.PageSetup.AnyPage = page;
            using (Aspose.Html.Rendering.Pdf.PdfDevice pdf_device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, stream))
                using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
                    using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(html, ""))
                    {
                        renderer.Render(pdf_device, html_document);
                        return(stream.ToArray());
                    }
        }
Esempio n. 10
0
        public static void Run()
        {
            // ExStart:HtmlToPdf
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_Data();

            String InputHtml = dataDir + "input.html";

            using (FileStream fs = File.Create(InputHtml))
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(
                        @"<style>
                    .st
                    {
                    color: green;
                    }
                    </style>
                    <div id=id1>Aspose.Html rendering Text in Black Color</div>
                    <div id=id2 class=''st''>Aspose.Html rendering Text in Green Color</div><div id=id3 class=''st'' style='color: blue;'>Aspose.Html rendering Text in Blue Color</div>
                    <div id=id3 class=''st'' style='color: red;'><font face='Arial'>Aspose.Html rendering Text in Red Color</font></div>");
                }

            // File name for resultant PDF file
            string Resultant_output = dataDir + "simple-any-page_out.pdf";

            // Create PdfRendering Options object
            Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions();
            // The PageSetup also provides different properties i.e. FirstPage, LastPage, LeftPage, RightPage and they are used to setup (PageSize, Margin) for every page.
            // In most cases, usage of setup any page is enough, but in some complicated cases, you may need to fine tune page settings. It can be done either by CSS styles or by using rendering options.
            // the size for drawing is in pixels
            pdf_options.PageSetup.AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(400, 100));
            // Instantiate PdfDevice object while passing PdfRenderingOptions and resultant file path as arguments
            using (Aspose.Html.Rendering.Pdf.PdfDevice pdf_device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, Resultant_output))
                // Create HtmlRenderer object
                using (Aspose.Html.Rendering.HtmlRenderer renderer = new Aspose.Html.Rendering.HtmlRenderer())
                    // Create HtmlDocument instance while passing path of already created HTML file
                    using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(InputHtml))
                    {
                        // Render the output using HtmlRenderer
                        renderer.Render(pdf_device, html_document);
                    }
            // ExEnd:HtmlToPdf
        }
Esempio n. 11
0
        public HtmlPage()
        {
            RenderToPDF    = false;
            OutputFileName = "aspose-html-demo.pdf";
            var height     = Aspose.Html.Drawing.Unit.FromMillimeters(210);
            var width      = Aspose.Html.Drawing.Unit.FromMillimeters(297);
            var pageSizeA4 = new Aspose.Html.Drawing.Size(width, height);
            var margins    = new Aspose.Html.Drawing.Margin(
                Aspose.Html.Drawing.Unit.FromMillimeters(15),  // left
                Aspose.Html.Drawing.Unit.FromMillimeters(10),  // top
                Aspose.Html.Drawing.Unit.FromMillimeters(15),  // right
                Aspose.Html.Drawing.Unit.FromMillimeters(20)); // bottom

            options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions
            {
                PageSetup =
                {
                    AnyPage = new Aspose.Html.Drawing.Page(pageSizeA4, margins)
                }
            };
        }
Esempio n. 12
0
        public static void Run()
        {
            // ExStart:AdjustPdfPageSize
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_Data();

            String SimpleStyledFilePath = dataDir + "FirstFile.html";

            using (FileStream fs = File.Create(SimpleStyledFilePath))
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.Write(
                        @"<style>
                    .st
                    {
                    color: green;
                    }
                    </style>
                    <div id=id1>Aspose.Html rendering Text in Black Color</div>
                    <div id=id2 class=''st''>Aspose.Html rendering Text in Green Color</div>
                    <div id=id3 class=''st'' style='color: blue;'>Aspose.Html rendering Text in Blue Color</div>
                    <div id=id3 class=''st'' style='color: red;'><font face='Arial'>Aspose.Html rendering Text in Red Color</font></div>
                    ");
                }

            string pdf_output;

            // Create HtmlRenderer object
            using (Aspose.Html.Rendering.HtmlRenderer pdf_renderer = new Aspose.Html.Rendering.HtmlRenderer())
                // Create HtmlDocument instnace while passing path of already created HTML file
                using (Aspose.Html.HTMLDocument html_document = new Aspose.Html.HTMLDocument(SimpleStyledFilePath))
                {
                    // Set the page size less than document min-width. The content in the resulting file will be cropped becuase of element with width: 200px
                    Aspose.Html.Rendering.Pdf.PdfRenderingOptions pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions
                    {
                        PageSetup =
                        {
                            AnyPage            = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)),
                            AdjustToWidestPage = false
                        },
                    };
                    pdf_output = dataDir + "not-adjusted-to-widest-page_out.pdf";
                    using (Aspose.Html.Rendering.Pdf.PdfDevice device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, pdf_output))
                    {
                        // Render the output
                        pdf_renderer.Render(device, html_document);
                    }

                    // Set the page size less than document min-width and enable AdjustToWidestPage option. The page size of the resulting file will be changed according to content width
                    pdf_options = new Aspose.Html.Rendering.Pdf.PdfRenderingOptions
                    {
                        PageSetup =
                        {
                            AnyPage            = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(100, 100)),
                            AdjustToWidestPage = true
                        },
                    };
                    pdf_output = dataDir + "adjusted-to-widest-page_out.pdf";
                    using (Aspose.Html.Rendering.Pdf.PdfDevice device = new Aspose.Html.Rendering.Pdf.PdfDevice(pdf_options, pdf_output))
                    {
                        // Render the output
                        pdf_renderer.Render(device, html_document);
                    }
                }
            // ExEnd:AdjustPdfPageSize
        }