尝试使用c#asp.net后面的代码为PDF内的pdf位置分配超链接

问题描述 投票:0回答:1

尝试使用c#asp.net web表单为PDF中的pdf位置分配超链接。

这是我的C#代码分配给pdf位置的链接URL。

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();
    }
}

这段代码显示了一个像www.example.com/my.pdf这样的网址作为输出。

但我需要输出如下:click here to download pdf

我正在尝试下面的新代码来获取我需要的输出:

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);

但是我无法使用分配pdf的视图

formFieldMap["text_attchedfilertpin"]

我正在寻找你的帮助,谢谢你提前。

c# asp.net
1个回答
0
投票

为了将PDF链接识别为文件下载,您需要添加一个特殊的Content-Disposition: attachment; file=xxx.pdf HTTP标头(请参阅此代码示例 - code to download PDF file in C#)。

假设你想要一个链接http://www.example.com/plans/my123.pdf,点击它时会启动一个名为“my123”的培训计划的PDF文件下载。

您可以创建一个HTTP处理程序 - 一个实现PlanPDF的类IHttpHandler。在处理程序的代码中,您可以设置正确的Content-Type,Content-Disposition,Content-Length并传输PDF文件,如上面的链接所示。 See this article for a simple example of IHttpHandler

接下来,您需要配置URL重写,以便来到/plans/my123.pdf的请求被映射到您的处理程序PlanPDF。您可以在“Web.config”中执行此操作(有关示例,请参阅相同的代码项目文章)。

从请求URL路径解析计划名称,并使用它来确定要传输的培训计划文件。

© www.soinside.com 2019 - 2024. All rights reserved.