Hi gkoutsiv,
Please refer below sample.
HTML
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<asp:Button Text="Save" ID="btnSave" runat="server" OnClick="SavePDF" />
<hr />
<div id="pdf_container">
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf_viewer.min.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
var pdfjsLib = window['pdfjs-dist/build/pdf'];
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js';
var pdfDoc = null;
var scale = 1; //Set Scale for zooming PDF.
var resolution = 2; //Set Resolution to Adjust PDF clarity.
LoadPdfFromUrl('PDFs/Sample.pdf');
function LoadPdfFromUrl(url) {
//Read PDF from URL.
pdfjsLib.getDocument(url).promise.then(function (pdfDoc_) {
pdfDoc = pdfDoc_;
//Reference the Container DIV.
var pdf_container = document.getElementById("pdf_container");
pdf_container.style.display = "block";
//Loop and render all pages.
for (var i = 1; i <= pdfDoc.numPages; i++) {
RenderPage(pdf_container, i);
}
});
};
function RenderPage(pdf_container, num) {
pdfDoc.getPage(num).then(function (page) {
//Create Canvas element and append to the Container DIV.
var canvas = document.createElement('canvas');
canvas.id = 'pdf-' + num;
ctx = canvas.getContext('2d');
pdf_container.appendChild(canvas);
//Create and add empty DIV to add SPACE between pages.
var spacer = document.createElement("div");
spacer.style.height = "20px";
pdf_container.appendChild(spacer);
//Set the Canvas dimensions using ViewPort and Scale.
var viewport = page.getViewport({ scale: scale });
canvas.height = resolution * viewport.height;
canvas.width = resolution * viewport.width;
//Render the PDF page.
var renderContext = {
canvasContext: ctx,
viewport: viewport,
transform: [resolution, 0, 0, resolution, 0, 0]
};
page.render(renderContext);
});
};
</script>
Namespace
C#
using System.IO;
VB.Net
Imports System.IO
Code
C#
protected void SavePDF(object sender, EventArgs e)
{
string filePath = Server.MapPath("~/PDFs/sample.pdf");
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
string fileNew = string.Format("{0}{1}.pdf", this.txtName.Text, DateTime.Now.ToString("MM_dd_yyyy"));
File.WriteAllBytes(Path.Combine(Server.MapPath("~/PDFs/"), fileNew), File.ReadAllBytes(filePath));
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('PDF File saved.')", true);
}
VB.Net
Protected Sub SavePDF(ByVal sender As Object, ByVal e As EventArgs)
Dim filePath As String = Server.MapPath("~/PDFs/sample.pdf")
Dim fileNameWithoutExtension As String = Path.GetFileNameWithoutExtension(filePath)
Dim fileNew As String = String.Format("{0}{1}.pdf", Me.txtName.Text, DateTime.Now.ToString("MM_dd_yyyy"))
File.WriteAllBytes(Path.Combine(Server.MapPath("~/PDFs/"), fileNew), File.ReadAllBytes(filePath))
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('PDF File saved.')", True)
End Sub
Screenshot