public ActionResult GetResourceForHtml(GetResourceForHtmlParameters parameters) { try { if (!string.IsNullOrEmpty(parameters.ResourceName) && parameters.ResourceName.IndexOf("/", StringComparison.Ordinal) >= 0) { parameters.ResourceName = parameters.ResourceName.Replace("/", ""); } var resource = new HtmlResource(parameters.ResourceName) { //ResourceName = parameters.ResourceName, //ResourceType = Utils.GetResourceType(parameters.ResourceName), DocumentPageNumber = parameters.PageNumber }; var stream = _htmlHandler.GetResource(parameters.DocumentPath, resource); if (stream == null || stream.Length == 0) { return(new HttpStatusCodeResult((int)HttpStatusCode.Gone)); } return(File(stream, Utils.GetMimeType(parameters.ResourceName))); } catch (Exception e) { return(this.JsonOrJsonP(new FailedResponse { Reason = e.Message }, null)); } }
/// <summary> /// Get document html representation /// </summary> public static void Get_Html_With_Resources() { Console.WriteLine("***** {0} *****", "Get document html representation"); /* ********************* SAMPLE ********************* */ // Setup GroupDocs.Viewer config ViewerConfig config = new ViewerConfig(); config.StoragePath = @"C:\storage"; // Create html handler ViewerHtmlHandler htmlHandler = new ViewerHtmlHandler(config); string guid = "word.doc"; List<PageHtml> pages = htmlHandler.GetPages(guid); foreach (PageHtml page in pages) { Console.WriteLine("Page number: {0}", page.PageNumber); Console.WriteLine("Resources count: {0}", page.HtmlResources.Count); Console.WriteLine("Html content: {0}", page.HtmlContent); // Html resources descriptions foreach (HtmlResource resource in page.HtmlResources) { Console.WriteLine(resource.ResourceName, resource.ResourceType); // Get html page resource stream Stream resourceStream = htmlHandler.GetResource(guid, resource); } } }
protected void Page_Load(object sender, EventArgs e) { _htmlHandler = (ViewerHtmlHandler)Session["htmlHandler"]; GetResourceForHtmlParameters parameters = new GetResourceForHtmlParameters(); parameters.DocumentPath = GetValueFromQueryString("documentPath"); parameters.ResourceName = GetValueFromQueryString("resourceName"); parameters.PageNumber = int.Parse(GetValueFromQueryString("pageNumber") != String.Empty ? GetValueFromQueryString("pageNumber") : "0"); if (!string.IsNullOrEmpty(parameters.ResourceName) && parameters.ResourceName.IndexOf("/", StringComparison.Ordinal) >= 0) { parameters.ResourceName = parameters.ResourceName.Replace("/", ""); } try { var resource = new HtmlResource { ResourceName = parameters.ResourceName, ResourceType = Utils.GetResourceType(parameters.ResourceName), DocumentPageNumber = parameters.PageNumber }; var stream = _htmlHandler.GetResource(parameters.DocumentPath, resource); if (stream == null || stream.Length == 0) { Response.StatusCode = ((int)HttpStatusCode.Gone); Response.End(); } else { byte[] Bytes = new byte[stream.Length]; stream.Read(Bytes, 0, Bytes.Length); //string contentDispositionString = "attachment; filename=\"" + parameters.ResourceName + "\""; string contentDispositionString = new ContentDisposition { FileName = parameters.ResourceName, Inline = true }.ToString(); HttpContext.Current.Response.ContentType = Utils.GetImageMimeTypeFromFilename(parameters.ResourceName); HttpContext.Current.Response.AddHeader("Content-Disposition", contentDispositionString); HttpContext.Current.Response.AddHeader("Content-Length", stream.Length.ToString()); HttpContext.Current.Response.OutputStream.Write(Bytes, 0, Bytes.Length); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } } catch { } }
public ActionResult GetResourceForHtml(GetResourceForHtmlParameters parameters) { if (!string.IsNullOrEmpty(parameters.ResourceName) && parameters.ResourceName.IndexOf("/", StringComparison.Ordinal) >= 0) { parameters.ResourceName = parameters.ResourceName.Replace("/", ""); } var resource = new HtmlResource { ResourceName = parameters.ResourceName, ResourceType = Utils.GetResourceType(parameters.ResourceName), DocumentPageNumber = parameters.PageNumber }; var stream = _htmlHandler.GetResource(parameters.DocumentPath, resource); if (stream == null || stream.Length == 0) { return(new HttpStatusCodeResult((int)HttpStatusCode.Gone)); } return(File(stream, Utils.GetImageMimeTypeFromFilename(parameters.ResourceName))); }
public HttpResponseMessage Get(string file, string attachment, int page, string resource) { var attachmentPath = "cache\\" + Path.GetFileNameWithoutExtension(file) + Path.GetExtension(file).Replace(".", "_") + "\\attachments\\" + attachment; ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler(); DocumentInfoContainer info = handler.GetDocumentInfo(file); List <int> pageNumberstoRender = new List <int>(); pageNumberstoRender.Add(page); HtmlOptions o = new HtmlOptions(); int pageNumber = page; o.PageNumbersToRender = pageNumberstoRender; o.PageNumber = page; o.CountPagesToRender = 1; List <PageHtml> pages = handler.GetPages(attachmentPath, o); List <HtmlResource> htmlResources = pages.Where(x => x.PageNumber == page).Select(x => x.HtmlResources).FirstOrDefault(); var fileResource = htmlResources.Where(x => x.ResourceName == resource).FirstOrDefault(); string type = ""; if (fileResource != null) { switch (fileResource.ResourceType) { case HtmlResourceType.Font: type = "application/font-woff"; break; case HtmlResourceType.Style: type = "text/css"; break; case HtmlResourceType.Image: type = "image/jpeg"; break; case HtmlResourceType.Graphics: type = "image/svg+xml"; break; } Stream stream = handler.GetResource(attachmentPath, fileResource); var result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue(type); return(result); } return(null); }
public HttpResponseMessage Get(string file, int page, string resource) { ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler(); List <int> pageNumberstoRender = new List <int>(); pageNumberstoRender.Add(page); HtmlOptions o = new HtmlOptions(); int pageNumber = page; o.PageNumbersToRender = pageNumberstoRender; o.PageNumber = page; o.CountPagesToRender = 1; List <PageHtml> list = Utils.LoadPageHtmlList(handler, file, o); List <HtmlResource> htmlResources = list.Where(x => x.PageNumber == page).Select(x => x.HtmlResources).FirstOrDefault(); var fileResource = htmlResources.Where(x => x.ResourceName == resource).FirstOrDefault(); string type = ""; if (fileResource != null) { switch (fileResource.ResourceType) { case HtmlResourceType.Font: type = "application/font-woff"; break; case HtmlResourceType.Style: type = "text/css"; break; case HtmlResourceType.Image: type = "image/jpeg"; break; case HtmlResourceType.Graphics: type = "image/svg+xml"; break; } Stream stream = handler.GetResource(file, fileResource); var result = new HttpResponseMessage(HttpStatusCode.OK); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue(type); return(result); } return(null); }
public ActionResult Index(string file, int page, string resource) { ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler(); List <int> pageNumberstoRender = new List <int>(); pageNumberstoRender.Add(page); HtmlOptions o = new HtmlOptions(); int pageNumber = page; o.PageNumbersToRender = pageNumberstoRender; o.PageNumber = page; o.CountPagesToRender = 1; List <PageHtml> list = Utils.LoadPageHtmlList(handler, file, o); List <HtmlResource> htmlResources = list.Where(x => x.PageNumber == page).Select(x => x.HtmlResources).FirstOrDefault(); var fileResource = htmlResources.Where(x => x.ResourceName == resource).FirstOrDefault(); string type = ""; if (fileResource != null) { switch (fileResource.ResourceType) { case HtmlResourceType.Font: type = "application/font-woff"; break; case HtmlResourceType.Style: type = "text/css"; break; case HtmlResourceType.Image: type = "image/jpeg"; break; case HtmlResourceType.Graphics: type = "image/svg+xml"; break; } Stream stream = handler.GetResource(file, fileResource); return(new FileStreamResult(stream, type)); } return(null); }
/// <summary> /// Get attached file's html representation /// </summary> /// <param name="DocumentName">Input document name</param> public static void GetEmailAttachmentHTMLRepresentation(String DocumentName) { try { //ExStart:GetEmailAttachmentHTMLRepresentation // Setup GroupDocs.Viewer config ViewerConfig config = Utilities.GetConfigurations(); // Setup html conversion options HtmlOptions htmlOptions = new HtmlOptions(); htmlOptions.IsResourcesEmbedded = false; // Init viewer html handler ViewerHtmlHandler handler = new ViewerHtmlHandler(config); DocumentInfoContainer info = handler.GetDocumentInfo(DocumentName); // Iterate over the attachments collection foreach (AttachmentBase attachment in info.Attachments) { Console.WriteLine("Attach name: {0}, size: {1}", attachment.Name, attachment.FileType); // Get attachment document html representation List <PageHtml> pages = handler.GetPages(attachment, htmlOptions); foreach (PageHtml page in pages) { Console.WriteLine(" Page: {0}, size: {1}", page.PageNumber, page.HtmlContent.Length); foreach (HtmlResource htmlResource in page.HtmlResources) { Stream resourceStream = handler.GetResource(attachment, htmlResource); Console.WriteLine(" Resource: {0}, size: {1}", htmlResource.ResourceName, resourceStream.Length); } } } //ExEnd:GetEmailAttachmentHTMLRepresentation } catch (System.Exception exp) { Console.WriteLine(exp.Message); } }
public ActionResult Index(string file, string attachment, int page, string resource) { var attachmentPath = "cache\\" + Path.GetFileNameWithoutExtension(file) + Path.GetExtension(file).Replace(".", "_") + "\\attachments\\" + attachment; ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler(); DocumentInfoContainer info = handler.GetDocumentInfo(file); List <int> pageNumberstoRender = new List <int>(); pageNumberstoRender.Add(page); HtmlOptions o = new HtmlOptions(); int pageNumber = page; o.PageNumbersToRender = pageNumberstoRender; o.PageNumber = page; o.CountPagesToRender = 1; List <PageHtml> pages = handler.GetPages(attachmentPath, o); List <HtmlResource> htmlResources = pages.Where(x => x.PageNumber == page).Select(x => x.HtmlResources).FirstOrDefault(); var fileResource = htmlResources.Where(x => x.ResourceName == resource).FirstOrDefault(); string type = ""; if (fileResource != null) { switch ((int)fileResource.ResourceType) { case 2: type = "application/font-woff"; break; case 3: type = "text/css"; break; case 1: type = "image/jpeg"; break; } Stream stream = handler.GetResource(attachmentPath, fileResource); return(new FileStreamResult(stream, type)); } return(null); }
private static List <PageHtml> GetHtmlPages(string filePath, HtmlOptions htmlOptions, out List <string> cssList) { var htmlHandler = (ViewerHtmlHandler)HttpContext.Current.Session["htmlHandler"]; var htmlPages = htmlHandler.GetPages(filePath, htmlOptions); cssList = new List <string>(); foreach (var page in htmlPages) { var indexOfBodyOpenTag = page.HtmlContent.IndexOf("<body>", StringComparison.InvariantCultureIgnoreCase); if (indexOfBodyOpenTag > 0) { page.HtmlContent = page.HtmlContent.Substring(indexOfBodyOpenTag + "<body>".Length); } var indexOfBodyCloseTag = page.HtmlContent.IndexOf("</body>", StringComparison.InvariantCultureIgnoreCase); if (indexOfBodyCloseTag > 0) { page.HtmlContent = page.HtmlContent.Substring(0, indexOfBodyCloseTag); } foreach (var resource in page.HtmlResources.Where(_ => _.ResourceType == HtmlResourceType.Style)) { var cssStream = _htmlHandler.GetResource(filePath, resource); var text = new StreamReader(cssStream).ReadToEnd(); var needResave = false; if (text.IndexOf("url(\"", StringComparison.Ordinal) >= 0 && text.IndexOf("url(\"/_layouts/15/GroupDocs-Viewer-SharePoint/GetResourceForHtml.aspx?documentPath=", StringComparison.Ordinal) < 0) { needResave = true; text = text.Replace("url(\"", string.Format("url(\"/_layouts/15/GroupDocs-Viewer-SharePoint/GetResourceForHtml.aspx?documentPath={0}&pageNumber={1}&resourceName=", filePath, page.PageNumber)); } if (text.IndexOf("url('", StringComparison.Ordinal) >= 0 && text.IndexOf("url('/_layouts/15/GroupDocs-Viewer-SharePoint/GetResourceForHtml.aspx?documentPath=", StringComparison.Ordinal) < 0) { needResave = true; text = text.Replace("url('", string.Format( "url('/_layouts/15/GroupDocs-Viewer-SharePoint/GetResourceForHtml.aspx?documentPath={0}&pageNumber={1}&resourceName=", filePath, page.PageNumber)); } cssList.Add(text); if (needResave) { var fullPath = Path.Combine(_tempPath, filePath, "html", "resources", string.Format("page{0}", page.PageNumber), resource.ResourceName); System.IO.File.WriteAllText(fullPath, text); } } List <string> cssClasses = Utils.GetCssClasses(page.HtmlContent); foreach (var cssClass in cssClasses) { var newCssClass = string.Format("page-{0}-{1}", page.PageNumber, cssClass); page.HtmlContent = page.HtmlContent.Replace(cssClass, newCssClass); for (int i = 0; i < cssList.Count; i++) { cssList[i] = cssList[i].Replace(cssClass, newCssClass); } } } return(htmlPages); }