Initial commit
This commit is contained in:
9
SIG.Web/AppCode/SigControls/PDFViewer/PdfClientAction.cs
Normal file
9
SIG.Web/AppCode/SigControls/PDFViewer/PdfClientAction.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public sealed class PdfClientAction
|
||||
{
|
||||
public string Action { get; set; }
|
||||
public string Message { get; set; }
|
||||
public string Url { get; set; }
|
||||
}
|
||||
}
|
||||
10
SIG.Web/AppCode/SigControls/PDFViewer/PdfDocumentComposer.cs
Normal file
10
SIG.Web/AppCode/SigControls/PDFViewer/PdfDocumentComposer.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public sealed class PdfDocumentComposer
|
||||
{
|
||||
public byte[] Compose(byte[] originalPdf)
|
||||
{
|
||||
return originalPdf ?? new byte[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public sealed class PdfSignatureAsset
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public byte[] Bytes { get; set; }
|
||||
public string ContentType { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public sealed class PdfSignatureComposer
|
||||
{
|
||||
public byte[] ApplySignatures(byte[] originalPdf, IEnumerable<PdfSignaturePlacement> placements)
|
||||
{
|
||||
// Placeholder. Keep signature composition behind this class.
|
||||
return originalPdf ?? new byte[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public sealed class PdfSignaturePlacement
|
||||
{
|
||||
public int Page { get; set; }
|
||||
public float X { get; set; }
|
||||
public float Y { get; set; }
|
||||
public float Width { get; set; }
|
||||
public float Height { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public sealed class PdfSignatureSavingEventArgs : EventArgs
|
||||
{
|
||||
public PdfSignatureAsset Signature { get; }
|
||||
public bool Cancel { get; set; }
|
||||
|
||||
public PdfSignatureSavingEventArgs(PdfSignatureAsset signature)
|
||||
{
|
||||
Signature = signature;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public sealed class PdfSignedDocumentEventArgs : EventArgs
|
||||
{
|
||||
public byte[] Bytes { get; }
|
||||
public string FileName { get; }
|
||||
|
||||
public PdfSignedDocumentEventArgs(byte[] bytes, string fileName)
|
||||
{
|
||||
Bytes = bytes;
|
||||
FileName = fileName;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
SIG.Web/AppCode/SigControls/PDFViewer/PdfSourceMode.cs
Normal file
11
SIG.Web/AppCode/SigControls/PDFViewer/PdfSourceMode.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public enum PdfSourceMode
|
||||
{
|
||||
None,
|
||||
Bytes,
|
||||
FilePath,
|
||||
Url,
|
||||
TempStorage
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public sealed class PdfStoredSignature
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string DataUrl { get; set; }
|
||||
}
|
||||
}
|
||||
29
SIG.Web/AppCode/SigControls/PDFViewer/PdfTempStorage.cs
Normal file
29
SIG.Web/AppCode/SigControls/PDFViewer/PdfTempStorage.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public static class PdfTempStorage
|
||||
{
|
||||
private const string Folder = "~/App_Data/PdfTemp";
|
||||
|
||||
public static string Save(byte[] bytes, string extension = ".pdf")
|
||||
{
|
||||
if (bytes == null) throw new ArgumentNullException(nameof(bytes));
|
||||
var dir = HttpContext.Current.Server.MapPath(Folder);
|
||||
Directory.CreateDirectory(dir);
|
||||
var fileName = Guid.NewGuid().ToString("N") + extension;
|
||||
var path = Path.Combine(dir, fileName);
|
||||
File.WriteAllBytes(path, bytes);
|
||||
return path;
|
||||
}
|
||||
|
||||
public static byte[] Read(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path) || !File.Exists(path))
|
||||
return null;
|
||||
return File.ReadAllBytes(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public sealed class PdfUploadedDocument
|
||||
{
|
||||
public string FileName { get; set; }
|
||||
public byte[] Bytes { get; set; }
|
||||
}
|
||||
}
|
||||
24
SIG.Web/AppCode/SigControls/PDFViewer/PdfViewerHandler.cs
Normal file
24
SIG.Web/AppCode/SigControls/PDFViewer/PdfViewerHandler.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Web;
|
||||
using System.Web.SessionState;
|
||||
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public sealed class PdfViewerHandler : IHttpHandler, IRequiresSessionState
|
||||
{
|
||||
public bool IsReusable => false;
|
||||
|
||||
public void ProcessRequest(HttpContext context)
|
||||
{
|
||||
var path = context.Request.QueryString["path"];
|
||||
var bytes = PdfTempStorage.Read(path);
|
||||
if (bytes == null)
|
||||
{
|
||||
context.Response.StatusCode = 404;
|
||||
return;
|
||||
}
|
||||
|
||||
context.Response.ContentType = "application/pdf";
|
||||
context.Response.BinaryWrite(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
15
SIG.Web/AppCode/SigControls/PDFViewer/PdfViewerState.cs
Normal file
15
SIG.Web/AppCode/SigControls/PDFViewer/PdfViewerState.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
[Serializable]
|
||||
public sealed class PdfViewerState
|
||||
{
|
||||
public string DocumentId { get; set; }
|
||||
public PdfSourceMode SourceMode { get; set; }
|
||||
public string FileName { get; set; }
|
||||
public string TempPath { get; set; }
|
||||
public List<PdfSignaturePlacement> Signatures { get; set; } = new List<PdfSignaturePlacement>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Web.Script.Serialization;
|
||||
using System.Web.SessionState;
|
||||
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
public sealed class PdfViewerUploadHandler : IHttpHandler, IRequiresSessionState
|
||||
{
|
||||
public bool IsReusable => false;
|
||||
|
||||
public void ProcessRequest(HttpContext context)
|
||||
{
|
||||
if (context.Request.Files.Count == 0)
|
||||
{
|
||||
WriteJson(context, new { success = false, message = "No file received." });
|
||||
return;
|
||||
}
|
||||
|
||||
var file = context.Request.Files[0];
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
file.InputStream.CopyTo(ms);
|
||||
var path = PdfTempStorage.Save(ms.ToArray());
|
||||
WriteJson(context, new { success = true, path, fileName = file.FileName });
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteJson(HttpContext context, object payload)
|
||||
{
|
||||
context.Response.ContentType = "application/json";
|
||||
context.Response.Write(new JavaScriptSerializer().Serialize(payload));
|
||||
}
|
||||
}
|
||||
}
|
||||
91
SIG.Web/AppCode/SigControls/PDFViewer/SigPdfViewer.cs
Normal file
91
SIG.Web/AppCode/SigControls/PDFViewer/SigPdfViewer.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Net;
|
||||
using System.Web.UI;
|
||||
using System.Web.UI.WebControls;
|
||||
|
||||
namespace SIG.Web.AppCode.SigControls.PDFViewer
|
||||
{
|
||||
[ToolboxData("<{0}:SigPdfViewer runat=server></{0}:SigPdfViewer>")]
|
||||
public class SigPdfViewer : CompositeControl, INamingContainer
|
||||
{
|
||||
private HiddenField _path;
|
||||
|
||||
[Category("SIG")]
|
||||
public string DocumentPath
|
||||
{
|
||||
get { EnsureChildControls(); return _path.Value; }
|
||||
set { EnsureChildControls(); _path.Value = value; }
|
||||
}
|
||||
|
||||
[Category("SIG")]
|
||||
public bool ShowSignaturePanel
|
||||
{
|
||||
get { return ViewState["ShowSignaturePanel"] as bool? ?? false; }
|
||||
set { ViewState["ShowSignaturePanel"] = value; }
|
||||
}
|
||||
|
||||
public void LoadFromBytes(byte[] pdfBytes, string fileName = "document.pdf")
|
||||
{
|
||||
DocumentPath = PdfTempStorage.Save(pdfBytes);
|
||||
}
|
||||
|
||||
public void LoadFromServerPath(string physicalPath)
|
||||
{
|
||||
DocumentPath = physicalPath;
|
||||
}
|
||||
|
||||
public void LoadFromUrl(string url)
|
||||
{
|
||||
using (var client = new WebClient())
|
||||
{
|
||||
LoadFromBytes(client.DownloadData(url));
|
||||
}
|
||||
}
|
||||
|
||||
protected override void CreateChildControls()
|
||||
{
|
||||
Controls.Clear();
|
||||
_path = new HiddenField { ID = "HdnPdfPath" };
|
||||
Controls.Add(_path);
|
||||
}
|
||||
|
||||
protected override void Render(HtmlTextWriter writer)
|
||||
{
|
||||
EnsureChildControls();
|
||||
writer.AddAttribute(HtmlTextWriterAttribute.Class, "sig-pdf-control");
|
||||
writer.RenderBeginTag(HtmlTextWriterTag.Div);
|
||||
_path.RenderControl(writer);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(DocumentPath))
|
||||
{
|
||||
writer.AddAttribute(HtmlTextWriterAttribute.Class, "alert alert-info");
|
||||
writer.RenderBeginTag(HtmlTextWriterTag.Div);
|
||||
writer.WriteEncodedText("No PDF loaded.");
|
||||
writer.RenderEndTag();
|
||||
}
|
||||
else
|
||||
{
|
||||
var url = "SigPdfViewerHandler.ashx?path=" + Page.Server.UrlEncode(DocumentPath);
|
||||
writer.AddAttribute(HtmlTextWriterAttribute.Class, "sig-pdf-toolbar");
|
||||
writer.RenderBeginTag(HtmlTextWriterTag.Div);
|
||||
writer.AddAttribute(HtmlTextWriterAttribute.Href, url);
|
||||
writer.AddAttribute(HtmlTextWriterAttribute.Target, "_blank");
|
||||
writer.AddAttribute(HtmlTextWriterAttribute.Class, "btn btn-default");
|
||||
writer.RenderBeginTag(HtmlTextWriterTag.A);
|
||||
writer.Write("Open PDF");
|
||||
writer.RenderEndTag();
|
||||
writer.RenderEndTag();
|
||||
|
||||
writer.AddAttribute(HtmlTextWriterAttribute.Src, url);
|
||||
writer.AddAttribute(HtmlTextWriterAttribute.Class, "sig-pdf-frame");
|
||||
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");
|
||||
writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "700px");
|
||||
writer.RenderBeginTag(HtmlTextWriterTag.Iframe);
|
||||
writer.RenderEndTag();
|
||||
}
|
||||
|
||||
writer.RenderEndTag();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user