1

I want to set the Title of Web page which is returning the PDF file stream as:

public ActionResult PrintInvoice(long ID)
{
    var data = db.Documents.Where(x => x.InvoiceNumber == ID);
      ReportDocument rd = new ReportDocument();
      rd.Load(Server.MapPath("~/Reports/InvoiceDocument.rpt"));

    Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
    stream.Seek(0, SeekOrigin.Begin);
    return new FileStreamResult(stream, "application/pdf");               //For Showing PDF in Browser itself
}

and on this Page I want to set the Title.

How can I set the Title on this Page.

Currently the Title on the Page looks like as shown below in Image::

enter image description here

Rahul
  • 2,309
  • 6
  • 33
  • 60
  • With @cubitouch answer (which is correct) you will not get the title to change and I don't think there is a way to do it. What you will get is that the file will open in the browser and when you save it it will get the filename you specify in the third argument of the `File()` call. – Tallmaris Jan 28 '14 at 10:24
  • 2
    Here is an article explaining again that what you want to achieve cannot be done: http://forums.asp.net/t/1707949.aspx. The workaround is explained at the bottom: to use an `IFRAME` where to render the pdf into. – Tallmaris Jan 28 '14 at 10:27
  • 1
    Browsers (at least Chrome and Firefox) set title of the page from title metadata of pdf document. http://www.w3.org/TR/WCAG20-TECHS/PDF18.html – Valyok26 Mar 29 '16 at 09:05

2 Answers2

0

Have a look at HTTP Headers, like in that thread.

Try something like :

Response.AppendHeader("Content-Disposition", "inline; filename=your page title");

Also have a look at this thread which recommend :

return File(stream, "application/pdf", "your page title");

Keep in mind that this kind of data can be executed differently from different browsers.

Community
  • 1
  • 1
cubitouch
  • 1,929
  • 15
  • 28
  • But its responding the file to Download. and I just want to show the Title on webpage . – Rahul Jan 28 '14 at 10:03
  • `Content-Disposition` should have `inline;` specified as well: http://stackoverflow.com/questions/3724278/asp-net-mvc-how-can-i-get-the-browser-to-open-and-display-a-pdf-instead-of-disp – Tallmaris Jan 28 '14 at 10:11
  • @cubitouch There isn't any Effect on the Title. – Rahul Jan 28 '14 at 10:18
  • @RahulRJ What exactly is the current title of the page ? – cubitouch Jan 28 '14 at 10:20
  • @cubitouch I have edited My Post,In that I have attached the Image of Title I am getting on Page. – Rahul Jan 28 '14 at 10:21
0

This is what I ended up doing in my situation.

The controller code below consists of two actions. The first action returns a model I can use for setting the page title (this could just be a string depending on your use case). The second action is for getting the file contents. In my case I was storing the file contents in a database so I use an id to fetch the document.

The second action also sets the response headers so that the file name shows up properly when they try to download the file.

    public IActionResult PreviewDocument(int id)
    {
        Document document = _legislationFolderService.GetDocument(id);

        if (document == null)
            return NotFound($"Could not find document with id of {id}");

        return View(document);
    }

    public IActionResult PreviewDocumentContents(int id)
    {
        DocumentContents documentContents = _legislationFolderService.GetDocumentContents(id);

        if (documentContents == null)
            return NotFound($"Could not find contents for document with id of {id}");

        Response.Headers.Add("Content-Disposition", $"inline; filename={documentContents.Document.Name}.pdf");
        return new FileStreamResult(new MemoryStream(documentContents.Contents), "application/pdf");
    }

In the view below (PreviewDocument.cshtml) I used an iframe to fill the page and link to the PreviewDocumentContents action. I didn't want the layout included from my main template so I set that to null and put up a basic html structure for the page, where I set the title right in the html.

@model EFloorFiles.Service.Models.Document

@{
    Layout = null;
    ViewBag.Title = Model.Name;
}

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewData["Title"] - E-Floor Files</title>
    <style type="text/css">
        body, html {
            width: 100%;
            height: 100%;
            overflow: hidden;
            margin: 0;
        }

        iframe {
            width: 100%;
            height: 100%;
            border: none;
        }
    </style>
</head>
<body>
    <iframe src="@Url.Action("PreviewDocumentContents", new { id = Model.Id })"></iframe>
</body>
</html>
Frank Hoffman
  • 887
  • 1
  • 11
  • 16