public ActionResult GetFile(GetFileParameters parameters)
        {
            try
            {
                var displayName = string.IsNullOrEmpty(parameters.DisplayName)
              ? Path.GetFileName(parameters.Path)
              : Uri.EscapeDataString(parameters.DisplayName);

                Stream fileStream;
                if (parameters.GetPdf)
                {
                    displayName = Path.ChangeExtension(displayName, "pdf");

                    var options = new PdfFileOptions
                    {
                        Watermark = Utils.GetWatermark(parameters.WatermarkText, parameters.WatermarkColor, parameters.WatermarkPosition, parameters.WatermarkWidth, parameters.WatermarkOpacity),
                    };

                    if (parameters.IsPrintable)
                    {
                        options.Transformations |= Transformation.AddPrintAction;
                    }

                    if (parameters.SupportPageRotation)
                    {
                        options.Transformations |= Transformation.Rotate;
                    }

                    options.Transformations |= Transformation.Reorder;

                    var pdfFileResponse = _htmlHandler.GetPdfFile(parameters.Path, options);
                    fileStream = pdfFileResponse.Stream;
                }
                else
                {
                    var fileResponse = _htmlHandler.GetFile(parameters.Path);
                    fileStream = fileResponse.Stream;
                }

                //jquery.fileDownload uses this cookie to determine that a file download has completed successfully
                Response.SetCookie(new HttpCookie("jqueryFileDownloadJSForGD", "true")
                {
                    Path = "/"
                });

                fileStream.Position = 0;
                using (var ms = new MemoryStream())
                {
                    fileStream.CopyTo(ms);
                    return(File(ms.ToArray(), "application/octet-stream", displayName));
                }
            }
            catch (Exception e)
            {
                return(this.JsonOrJsonP(new FailedResponse {
                    Reason = e.Message
                }, null));
            }
        }
Beispiel #2
0
        public static String GetPdfWithPrintDialog(GetFileParameters parameters)
        {
            var displayName = string.IsNullOrEmpty(parameters.DisplayName) ?
                              Path.GetFileName(parameters.Path) : Uri.EscapeDataString(parameters.DisplayName);

            var pdfFileOptions = new PdfFileOptions
            {
                // AddPrintAction = parameters.IsPrintable,
                Transformations = Transformation.Rotate | Transformation.Reorder,
                Watermark       = GetWatermark(parameters),
            };

            if (parameters.IsPrintable)
            {
                pdfFileOptions.Transformations |= Transformation.AddPrintAction;
            }

            var response = _htmlHandler.GetPdfFile(parameters.Path, pdfFileOptions);

            string contentDispositionString = new ContentDisposition {
                FileName = displayName, Inline = true
            }.ToString();

            HttpContext.Current.Response.AddHeader("Content-Disposition", contentDispositionString);

            return("");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            var file = GetValueFromQueryString("file");
            ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler();

            Stream pdf = null;

            try
            {
                pdf = handler.GetPdfFile(file).Stream;
            }
            catch (Exception x)
            {
                throw x;
            }
            using (var ms = new MemoryStream())
            {
                pdf.CopyTo(ms);
                ms.WriteTo(HttpContext.Current.Response.OutputStream);
                Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFileNameWithoutExtension(file) + ".pdf");
                HttpContext.Current.Response.ContentType = "application/pdf";
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
        }
        public HttpResponseMessage Get(string file)
        {
            ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler();

            Stream pdf = null;

            try
            {
                pdf = handler.GetPdfFile(file).Stream;
            }
            catch (Exception x)
            {
                throw x;
            }
            using (var ms = new MemoryStream())
            {
                pdf.CopyTo(ms);
                var result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(ms.ToArray())
                };
                result.Content.Headers.ContentDisposition =
                    new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
                {
                    FileName = Path.GetFileNameWithoutExtension(file) + ".pdf"
                };
                result.Content.Headers.ContentType =
                    new MediaTypeHeaderValue("application/pdf");

                return(result);
            }
        }
Beispiel #5
0
        public ActionResult Get(string file, string watermarkText, int?watermarkColor, WatermarkPosition?watermarkPosition, int?watermarkWidth, byte watermarkOpacity, bool isdownload)
        {
            ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler();

            Stream pdf = null;

            try
            {
                PdfFileOptions o = new PdfFileOptions();
                if (watermarkText != "")
                {
                    o.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity);
                }
                pdf = handler.GetPdfFile(file, o).Stream;
            }
            catch (Exception x)
            {
                throw x;
            }

            if (isdownload)
            {
                Response.Headers.Add("content-disposition", "attachment; filename=" + Path.GetFileNameWithoutExtension(file) + ".pdf");
                return(new FileStreamResult(pdf, "application/octet-stream"));
            }
            else
            {
                return(new FileStreamResult(pdf, "application/pdf"));
            }
        }
        public ActionResult GetFile(GetFileParameters parameters)
        {
            var displayName = string.IsNullOrEmpty(parameters.DisplayName) ?
                              Path.GetFileName(parameters.Path) : Uri.EscapeDataString(parameters.DisplayName);

            Stream fileStream;

            if (parameters.GetPdf)
            {
                displayName = Path.ChangeExtension(displayName, "pdf");

                var getPdfFileRequest = new PdfFileOptions
                {
                    Guid            = parameters.Path,
                    AddPrintAction  = parameters.IsPrintable,
                    Transformations = Transformation.Rotate | Transformation.Reorder,
                    Watermark       = GetWatermark(parameters),
                };

                var pdfFileResponse = _htmlHandler.GetPdfFile(getPdfFileRequest);
                fileStream = pdfFileResponse.Stream;
            }
            else
            {
                var fileResponse = _htmlHandler.GetFile(parameters.Path);
                fileStream = fileResponse.Stream;
            }

            //jquery.fileDownload uses this cookie to determine that a file download has completed successfully
            Response.SetCookie(new HttpCookie("jqueryFileDownloadJSForGD", "true")
            {
                Path = "/"
            });

            return(File(GetBytes(fileStream), "application/octet-stream", displayName));
        }
Beispiel #7
0
        public ActionResult Get(string file)
        {
            ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler();

            Stream pdf = null;

            try
            {
                pdf = handler.GetPdfFile(file).Stream;
            }
            catch (Exception x)
            {
                throw x;
            }

            return(new FileStreamResult(pdf, "application/pdf"));
        }
        public HttpResponseMessage Get(string file, string watermarkText, int?watermarkColor, WatermarkPosition?watermarkPosition, int?watermarkWidth, byte watermarkOpacity, bool isdownload)
        {
            ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler();

            Stream pdf = null;

            try
            {
                PdfFileOptions o = new PdfFileOptions();
                if (watermarkText != "")
                {
                    o.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity);
                }
                pdf = handler.GetPdfFile(file, o).Stream;
            }
            catch (Exception x)
            {
                throw x;
            }
            using (var ms = new MemoryStream())
            {
                pdf.CopyTo(ms);
                var result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(ms.ToArray())
                };

                if (isdownload)
                {
                    result.Content.Headers.Add("content-disposition", "attachment; filename=\"" + Path.GetFileNameWithoutExtension(file) + ".pdf\"");
                    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                }
                else
                {
                    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
                }

                return(result);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            var file = GetValueFromQueryString("file");

            string            watermarkText     = GetValueFromQueryString("watermarkText");
            int?              watermarkColor    = Convert.ToInt32(GetValueFromQueryString("watermarkColor"));
            WatermarkPosition watermarkPosition = (WatermarkPosition)Enum.Parse(typeof(WatermarkPosition), GetValueFromQueryString("watermarkPosition"), true);
            string            widthFromQuery    = GetValueFromQueryString("watermarkWidth");
            int?              watermarkWidth    = GetValueFromQueryString("watermarkWidth") == "null" || GetValueFromQueryString("watermarkWidth") == "" ? null : (int?)Convert.ToInt32(GetValueFromQueryString("watermarkWidth"));
            byte              watermarkOpacity  = Convert.ToByte(GetValueFromQueryString("watermarkOpacity"));

            ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler();

            Stream pdf = null;

            try
            {
                PdfFileOptions o = new PdfFileOptions();
                if (watermarkText != "")
                {
                    o.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity);
                }
                pdf = handler.GetPdfFile(file, o).Stream;
            }
            catch (Exception x)
            {
                throw x;
            }
            using (var ms = new MemoryStream())
            {
                pdf.CopyTo(ms);
                ms.WriteTo(HttpContext.Current.Response.OutputStream);
                Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFileNameWithoutExtension(file) + ".pdf");
                HttpContext.Current.Response.ContentType = "application/pdf";
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _config = new ViewerConfig
            {
                StoragePath = _storagePath,
                TempPath    = _tempPath,
                UseCache    = true
            };

            _htmlHandler  = new ViewerHtmlHandler(_config);
            _imageHandler = new ViewerImageHandler(_config);

            var parameters = (GetFileParameters)Session["fileparams"];

            Session.RemoveAll();

            var displayName = string.IsNullOrEmpty(parameters.DisplayName) ?
                              Path.GetFileName(parameters.Path) : Uri.EscapeDataString(parameters.DisplayName);

            Stream fileStream;

            if (parameters.GetPdf || parameters.IsPrintable)
            {
                displayName = Path.ChangeExtension(displayName, "pdf");

                var getPdfFileRequest = new PdfFileOptions
                {
                    Guid            = parameters.Path,
                    AddPrintAction  = parameters.IsPrintable,
                    Transformations = Transformation.Rotate | Transformation.Reorder,
                    Watermark       = GetWatermark(parameters),
                };

                var pdfFileResponse = _htmlHandler.GetPdfFile(getPdfFileRequest);
                fileStream = pdfFileResponse.Stream;
            }
            else
            {
                var fileResponse = _htmlHandler.GetFile(parameters.Path);

                fileStream = fileResponse.Stream;
            }

            //jquery.fileDownload uses this cookie to determine that a file download has completed successfully
            HttpContext.Current.Response.SetCookie(new HttpCookie("jqueryFileDownloadJSForGD", "true")
            {
                Path = "/"
            });
            byte[] Bytes = new byte[fileStream.Length];
            fileStream.Read(Bytes, 0, Bytes.Length);
            string contentDispositionString = "attachment; filename=\"" + displayName + "\"";

            if (parameters.IsPrintable)
            {
                contentDispositionString = new ContentDisposition {
                    FileName = displayName, Inline = true
                }.ToString();
            }
            if (parameters.IsPrintable)
            {
                HttpContext.Current.Response.ContentType = "application/pdf";
            }
            else
            {
                HttpContext.Current.Response.ContentType = "application/octet-stream";
            }

            HttpContext.Current.Response.AddHeader("Content-Disposition", contentDispositionString);
            HttpContext.Current.Response.AddHeader("Content-Length", fileStream.Length.ToString());
            HttpContext.Current.Response.OutputStream.Write(Bytes, 0, Bytes.Length);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }