Recently, I've been working on a web application project where I needed to display PDF files from a local server folder. Here are the steps I followed:
1. Download the PDF file from a link.
2. Save the downloaded PDF file to a local folder. If you're working on a web application, it's necessary to store it in the server folder.
3. Display the PDF files from the local folder.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Click To Download Pdf" OnClick="Button1_Click" />
<br />
<br />
<iframe runat="server" id="iframepdf" height="600" width="800" src=""> </iframe>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string filesave = Server.MapPath("~/DownloaddFiles/mypdffile.pdf");
string pdfurl = "https://www.quickpickdeal.com/files/SamplePdf1_12mb_6pages.pdf";
DownLoadpdfFromUrl(pdfurl,filesave);
iframepdf.Src = "~/DownloaddFiles/mypdffile.pdf";
}
public void DownLoadpdfFromUrl(string url, string savepath)
{
using (var client = new System.Net.WebClient())
{
client.DownloadFile(url, savepath);
}
}
}
This code snippet allows the user to download a PDF file from a URL, save it locally, and then display it on the web page using an iframe.