コード例 #1
0
    protected void GetPDF_Click(object sender, EventArgs e)
    {
        // 1. Save the current page state
        // 1.1. Create ID
        string id = Guid.NewGuid().ToString();

        // 1.2 Save the current state in the Application State.
        HouseDescription hd = GetPageState();

        Application[id] = hd;

        // 2. Create url
        string pageUrl           = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
        string pageUrlWithQuerry = string.Format("{0}?id={1}", pageUrl, id);

        // 3. Get PDF
        SautinSoft.PdfVision v = new SautinSoft.PdfVision();

        // Set "Edge mode" to support all modern CSS.
        SautinSoft.PdfVision.TrySetBrowserModeEdgeInRegistry();
        byte[] pdf = v.ConvertHtmlFileToPDFStream(pageUrlWithQuerry);

        // 4. Show PDF result
        if (pdf != null)
        {
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = "application/PDF";
            //Response.AddHeader("content-disposition", "attachment; filename=Result.pdf");
            Response.AddHeader("content-disposition", "inline; filename=Result.pdf");
            Response.BinaryWrite(pdf);
            Response.Flush();
            Response.End();
        }
    }
コード例 #2
0
        static void Main(string[] args)
        {
            // Convert folder of HTML files to single PDF file.
            SautinSoft.PdfVision v = new SautinSoft.PdfVision();

            // Set "Edge mode" to support all modern CSS.
            SautinSoft.PdfVision.TrySetBrowserModeEdgeInRegistry();

            v.PageStyle.PageOrientation.Portrait();
            v.PageStyle.PageSize.Letter();
            v.PageStyle.PageMarginLeft.Mm(20);
            v.PageStyle.PageMarginBottom.Mm(20);
            v.PageStyle.PageMarginTop.Mm(15);

            string   directoryWithHTMLs = Path.GetFullPath(@"..\..\");
            FileInfo singlePdf          = new FileInfo(@"Result.pdf");

            // Convert each HTML file from directory to a PDF file.
            string[]      htmlFiles    = Directory.GetFiles(directoryWithHTMLs, "*.htm*");
            List <byte[]> pdfInventory = new List <byte[]>();

            foreach (string htmlFile in htmlFiles)
            {
                pdfInventory.Add(v.ConvertHtmlFileToPDFStream(htmlFile));
            }

            // Merge all PDFs into a single PDF file.
            try
            {
                File.WriteAllBytes(singlePdf.FullName, v.MergePDFStreamArrayToPDFStream(pdfInventory));
                // Open the resulting PDF document in a default PDF Viewer.
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(singlePdf.FullName)
                {
                    UseShellExecute = true
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
コード例 #3
0
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBoxURl.Text == "")
        {
            Result.Text = "Please type any URL before start converting!";
            return;
        }

        SautinSoft.PdfVision v = new SautinSoft.PdfVision();

        // Set "Edge mode" to support all modern CSS.
        SautinSoft.PdfVision.TrySetBrowserModeEdgeInRegistry();

        byte[] pdfBytes = null;

        // Specify top and bottom page margins
        v.PageStyle.PageMarginTop.Mm(5f);
        v.PageStyle.PageMarginBottom.Mm(5f);

        // convert URL to pdf stream
        pdfBytes = v.ConvertHtmlFileToPDFStream(TextBoxURl.Text);

        // show PDF
        if (pdfBytes != null)
        {
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = "application/PDF";
            //Response.AddHeader("content-disposition", "attachment; filename=Result.pdf");
            Response.AddHeader("content-disposition", "inline; filename=Result.pdf");
            Response.BinaryWrite(pdfBytes);
            Response.Flush();
            Response.End();
        }
        else
        {
            Result.Text = "Converting failed!";
        }
    }