Beispiel #1
0
        public static ViewDataDictionary ObtenhaViewDatas(ViewDataDictionary ViewData,
                                                          EnumStatusAcoes EnumAcao, int?id,
                                                          string nameAction, Boolean modelRegistrado, Boolean possuiID)
        {
            ViewData["ehModelInvalid"] = false;

            if (!possuiID && EnumAcao.Equals(EnumStatusAcoes.CREATE))
            {
                ViewData["Title"]    = "CREATE NEW";
                ViewData["acao"]     = EnumAcao;
                ViewData["acaoForm"] = nameAction;
            }
            else if (possuiID)
            {
                if (modelRegistrado)
                {
                    if (EnumAcao.Equals(EnumStatusAcoes.EDIT))
                    {
                        ViewData["Title"]    = "EDIT";
                        ViewData["acao"]     = EnumAcao;
                        ViewData["acaoForm"] = nameAction;
                    }
                    else if (EnumAcao.Equals(EnumStatusAcoes.DELETE))
                    {
                        ViewData["Title"]    = "DELETE";
                        ViewData["acao"]     = EnumAcao;
                        ViewData["acaoForm"] = nameAction;
                    }
                    else if (EnumAcao.Equals(EnumStatusAcoes.DETAILS))
                    {
                        ViewData["Title"]    = "DETAILS";
                        ViewData["acao"]     = EnumAcao;
                        ViewData["acaoForm"] = "#";
                    }
                    else
                    {
                        ViewData.Clear();
                    }
                }
            }
            else
            {
                ViewData.Clear();
            }

            return(ViewData);
        }
Beispiel #2
0
        public ActionResult ConvertAnotherViewToPdf(IFormCollection formCollection)
        {
            // set a session variable to be used in the the converted view
            ViewDataDictionary viewData = new ViewDataDictionary(ViewData);

            viewData.Clear();

            viewData["MySessionVariable"] = formCollection["textBoxAnotherSessionVariable"];

            // get the About view HTML code
            string htmlToConvert = RenderViewAsString("AnotherView", viewData);

            // the base URL to resolve relative images and css
            HttpRequest httpRequest = this.ControllerContext.HttpContext.Request;
            UriBuilder  uriBuilder  = new UriBuilder();

            uriBuilder.Scheme = httpRequest.Scheme;
            uriBuilder.Host   = httpRequest.Host.Host;
            if (httpRequest.Host.Port != null)
            {
                uriBuilder.Port = (int)httpRequest.Host.Port;
            }
            uriBuilder.Path  = httpRequest.PathBase.ToString() + httpRequest.Path.ToString();
            uriBuilder.Query = httpRequest.QueryString.ToString();

            String thisViewUrl = uriBuilder.Uri.AbsoluteUri;
            String baseUrl     = thisViewUrl.Substring(0, thisViewUrl.Length - "ConvertHtmlPreservingState/ConvertAnotherViewToPdf".Length);

            // instantiate the HiQPdf HTML to PDF converter
            HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

            // render the HTML code as PDF in memory
            byte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert, baseUrl);

            // send the PDF document to browser
            FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");

            fileResult.FileDownloadName = "AnotherViewToPdf.pdf";

            return(fileResult);
        }
Beispiel #3
0
        public ActionResult ConvertThisViewToPdf(IFormCollection formCollection)
        {
            // add the custom value to view data
            ViewDataDictionary viewData = new ViewDataDictionary(ViewData);

            viewData.Clear();

            viewData.Add("MyTextValue", formCollection["textBoxText"]);
            viewData.Add("MyDropDownListValue", formCollection["dropDownListValues"]);

            // get the HTML code of this view
            string htmlToConvert = RenderViewAsString("Index", viewData);

            // the base URL to resolve relative images and css
            HttpRequest httpRequest = this.ControllerContext.HttpContext.Request;
            UriBuilder  uriBuilder  = new UriBuilder();

            uriBuilder.Scheme = httpRequest.Scheme;
            uriBuilder.Host   = httpRequest.Host.Host;
            if (httpRequest.Host.Port != null)
            {
                uriBuilder.Port = (int)httpRequest.Host.Port;
            }
            uriBuilder.Path  = httpRequest.PathBase.ToString() + httpRequest.Path.ToString();
            uriBuilder.Query = httpRequest.QueryString.ToString();

            String thisViewUrl = uriBuilder.Uri.AbsoluteUri;
            String baseUrl     = thisViewUrl.Substring(0, thisViewUrl.Length - "ConvertHtmlPreservingState/ConvertThisViewToPdf".Length);

            // instantiate the HiQPdf HTML to PDF converter
            HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

            // render the HTML code as PDF in memory
            byte[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert, baseUrl);

            FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");

            fileResult.FileDownloadName = "ThisViewToPdf.pdf";

            return(fileResult);
        }
Beispiel #4
0
        public ActionResult ConvertCurrentPageToPdf(IFormCollection collection)
        {
            ViewDataDictionary viewData = new ViewDataDictionary(ViewData);

            viewData.Clear();

            // transmit the posted data to view
            viewData.Add("firstName", collection["firstNameTextBox"]);
            viewData.Add("lastName", collection["lastNameTextBox"]);
            viewData.Add("gender", collection["gender"]);
            viewData.Add("haveCar", collection["haveCarCheckBox"]);
            viewData.Add("carType", collection["carTypeDropDownList"]);
            viewData.Add("comments", collection["commentsTextBox"]);

            // The string writer where to render the HTML code of the view
            StringWriter stringWriter = new StringWriter();

            // Render the Index view in a HTML string
            ViewEngineResult viewResult  = m_viewEngine.FindView(ControllerContext, "Index", true);
            ViewContext      viewContext = new ViewContext(
                ControllerContext,
                viewResult.View,
                viewData,
                TempData,
                stringWriter,
                new HtmlHelperOptions()
                );
            Task renderTask = viewResult.View.RenderAsync(viewContext);

            renderTask.Wait();

            // Get the view HTML string
            string htmlToConvert = stringWriter.ToString();

            // Get the base URL

            HttpRequest request    = this.ControllerContext.HttpContext.Request;
            UriBuilder  uriBuilder = new UriBuilder();

            uriBuilder.Scheme = request.Scheme;
            uriBuilder.Host   = request.Host.Host;
            if (request.Host.Port != null)
            {
                uriBuilder.Port = (int)request.Host.Port;
            }
            uriBuilder.Path  = request.PathBase.ToString() + request.Path.ToString();
            uriBuilder.Query = request.QueryString.ToString();

            String currentPageUrl = uriBuilder.Uri.AbsoluteUri;
            String baseUrl        = currentPageUrl.Substring(0, currentPageUrl.Length - "Convert_Current_Page/ConvertCurrentPageToPdf".Length);

            // Create a HTML to PDF converter object with default settings
            HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

            // Set license key received after purchase to use the converter in licensed mode
            // Leave it not set to use the converter in demo mode
            htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";

            // Set an adddional delay in seconds to wait for JavaScript or AJAX calls after page load completed
            // Set this property to 0 if you don't need to wait for such asynchcronous operations to finish
            htmlToPdfConverter.ConversionDelay = 2;

            // Convert the HTML string to a PDF document in a memory buffer
            byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlToConvert, baseUrl);

            // Send the PDF file to browser
            FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");

            fileResult.FileDownloadName = "Convert_Current_Page.pdf";

            return(fileResult);
        }
Beispiel #5
0
 /// <summary>
 /// Remove everything from the dictionary.
 /// </summary>
 public void Clear()
 {
     _parameters.Clear();
 }