0

Trying to assigning hyperlink to pdf location inside PDF using c# asp.net web forms.

This is my C# code assigned link url to pdf location.

protected void FillPDF()
{
    Dictionary<string, string> formFieldMap;

    pdfPath = Path.Combine(Server.MapPath("../img/fullapplication_final.pdf"), ""); // need to take
    formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);
    string livepath = "http://www.example.com/";

    if (!string.IsNullOrEmpty(Request.QueryString["RegistrationId"].ToString() as string))
    {
        bo.Para1 = Request.QueryString["RegistrationId"].ToString();
        bo.Para2 = "3";

        DataTable dt = bl.Admin_Get_UserInformation(bo);

        formFieldMap["text_attchedfilertpin"] = livepath + "TrainingPlan/" + dt.Rows[0]["TrainingPlan"].ToString();
    }
}

This code is showing an url like www.example.com/my.pdf as its output.

But I need the output to be like this : click here to download pdf

I am trying below new code to get the output as I need it:

HyperLink DynLink = new HyperLink();
DynLink.ID = "DynLink1";
DynLink.Text = "click here to donwload pdf";
DynLink.NavigateUrl = livepath + "TrainingPlan/" + dt.Rows[0]["TrainingPlan"].ToString();

Page.Controls.Add(DynLink);

But I'm not able to assign view of pdf using

formFieldMap["text_attchedfilertpin"]

I am looking for your help thank you in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mazhar 124
  • 123
  • 1
  • 3
  • 16

1 Answers1

0

In order for the PDF link to be recognized as a file download, you need to add a special Content-Disposition: attachment; file=xxx.pdf HTTP header (see this for code example - code to download PDF file in C# ).

Let's say you want to have a link http://www.example.com/plans/my123.pdf that when clicked initiates a PDF file download of a training plan called "my123".

You can create an HTTP handler - a class PlanPDF that implements IHttpHandler. In the code of the handler you can set the right Content-Type, Content-Disposition, Content-Length and transmit the PDF file as in the link above. See this article for a simple example of IHttpHandler

Next you need to configure URL rewriting so that requests coming to /plans/my123.pdf get mapped to your handler PlanPDF. This you can do in your "Web.config" (see the same codeproject article for an example).

Parse the plan name from the request URL path, and use it to determine which training plan file to transmit.

battlmonstr
  • 5,841
  • 1
  • 23
  • 33
  • please write code as i need above will help more to me. @battlmonstr – mazhar 124 May 26 '18 at 15:42
  • Check this for code: https://www.codeproject.com/Articles/335968/Implementing-HTTPHandler-and-HTTPModule-in-ASP-NET (`class CspxHandler`). It is almost all you need. – battlmonstr May 26 '18 at 15:45
  • i need to view that link in this location formFieldMap["text_attchedfilertpin"]. @battlmonstr – mazhar 124 May 26 '18 at 15:47
  • please write some code text is getting confused to me, this trying since morning. @ battlmonstr – mazhar 124 May 26 '18 at 15:52
  • I have simplified the answer. Please read the codeproject link and ask more questions. The solution I explain should work fine with your PDF form. Please ask questions if you don't understand the solution. – battlmonstr May 26 '18 at 15:56
  • see i added as answer, please write some code, will helpful. @battlmonstr – mazhar 124 May 26 '18 at 15:57
  • A custom HTTP handler for PDF is all you need. There's plenty of HTTP handler examples around. Here's a description of what an HTTP handler is - https://stackoverflow.com/questions/391130/what-is-an-httphandler-in-asp-net – battlmonstr May 26 '18 at 16:01
  • till what time you are online, tomorrow i have client deadline if i face any problem i will message. @battlmonstr – mazhar 124 May 26 '18 at 16:06
  • Tell your client that it's Sunday and you go to church and relax. No deadlines on Sunday :) – battlmonstr May 26 '18 at 16:09