Create WebService to read PDF from folder and download using C# and VB.Net in ASP.Net

Last Reply on Feb 03, 2018 07:08 AM By dharmendr

Posted on Feb 01, 2018 11:19 PM

I am receving JSON  response  from the webservice and there is a pdf string that is to be converted  base64.How can I  bind  the pdf  to  gridview.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">

           <asp:TemplateField HeaderText = "" ItemStyle-Width="100">
            <asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
           <asp:BoundField ItemStyle-Width="150px" DataField="EMPLOYEE_NAME" HeaderText="Employee Name" />
           <asp:BoundField ItemStyle-Width="150px" DataField="FATHER_NAME" HeaderText="Father's Name " />
           <asp:BoundField ItemStyle-Width="150px" DataField="POSTED_DEPARTMENT" HeaderText="Posted Department" />
           <asp:BoundField ItemStyle-Width="150px" DataField="DATE_OF_BIRTH" HeaderText="Date Of Birth" />
            <asp:TemplateField HeaderText = "data" ItemStyle-Width="100" Visible="false">
            <asp:Label ID="lbldata" Text='<%#Eval("pdfString")%>' runat="server" ClientIDMode="static" />
        <asp:TemplateField HeaderText = "pdf" ItemStyle-Width="100">
             <a id='dwnldLnk'  style="display:none;" />
            <asp:TemplateField HeaderText = "pdf2" ItemStyle-Width="100">
             <a href="#" onclick="downloadPDF();" title='IPR.pdf'>Download</a>


if (item[i].key == "pdfString")
    emp.pdfString  = item[i].value;

 Here I am getting  pdf String

Posted on Feb 03, 2018 07:08 AM

Hi pvermacs,

Check this example. Now please take its reference and correct your code.

I have created a web service that return base64String.



public string GetData()
    string path = Server.MapPath("~/Files/Test.pdf");
    byte[] bytes = System.IO.File.ReadAllBytes(path);
    return Convert.ToBase64String(bytes);


Public Function GetData() As String
    Dim path As String = Server.MapPath("~/Files/Test.pdf")
    Dim bytes As Byte() = System.IO.File.ReadAllBytes(path)
    Return Convert.ToBase64String(bytes)
End Function

Acccessing the service in page.


<asp:LinkButton Text="Download" runat="server" OnClick="Download" />



protected void Download(object sender, EventArgs e)
    PDFService service = new PDFService();
    string base64String = service.GetData();
    byte[] bytes = Convert.FromBase64String(base64String);
    System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
    Response.Buffer = true;
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Test.pdf");


Protected Sub Download(ByVal sender As Object, ByVal e As EventArgs)
    Dim service As PDFService = New PDFService()
    Dim base64String As String = service.GetData()
    Dim bytes As Byte() = Convert.FromBase64String(base64String)
    Dim ms As IO.MemoryStream = New IO.MemoryStream(bytes)
    Response.Buffer = True
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=Test.pdf")
End Sub

On the Download button click pdf file will be downloaded.