Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7705347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:50:42+00:00 2026-05-31T23:50:42+00:00

Please help. I am obviously no expert but using suggestions from this site, I

  • 0

Please help.
I am obviously no expert but using suggestions from this site, I think I am really close to doing the following

Be able to open a dynamically generated PDF in
a) a new Tab
b) an iframe

Hopefully, I just need a couple of lines of the correct syntax and I will be there.

I am dynamically generating the PDF in a controller using itextSharp

CONTROLLER

public FileStreamResult GetPdf()
  {
    ...
    return new FileStreamResult(Response.OutputStream, "application/pdf"){FileDownloadName = "download.pdf"};
  }

VIEW

<input id="btnNewTab" type="button" value="New Tab" />
<input id="btnIframe" type="button" value="Iframe" />

<div id="pdfDiv"></div>

<script type="text/javascript">
  $(function () {

    $("#btnIframe").click(function () {
      $.get('/PDFTest/GetPdf', function (pdf) {
        alert(pdf.length);  // Works as expected
        // What do I need to put here to get the pdf to appear in a iframe
      });
    });

    $("#btnNewTab").click(function () {
      // asks me if I want to Open or Save the PDF.
      // If I choose Open, the PDF opens in a completely new Window.
      // Instead of the dialog, I just want the PDF to open in a new Tab
      // I am probably going about this in completely the wrong way.
      var HTML = "<iframe src='/PDFTest/GetPdf' style='width: 100%; height: 600px' ></iframe>";
      $('#pdfDiv').html(HTML);
    });

  });
</script>

In response to your suggestion Darin, I changed the Controller to:

public FileStreamResult GetPdf(someInfo from View)
  {
    ...
      Response.ContentType = "application/pdf";
      Response.AddHeader("Content-Disposition", "inline;test.pdf"); 
      Response.Buffer = true;
      Response.Clear();
      Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
      Response.OutputStream.Flush();
      Response.End();

      return new FileStreamResult(Response.OutputStream, "application/pdf");
  }

Having done that, your suggestions worked fine but I realise that I did not explain my intentions clearly enough. I have therefore changed the VIEW to reflect what i am trying to do.

input id="btnNewTab" type="button" value="New Tab" />
<input id="btnIframe" type="button" value="Iframe" />
<iframe id="iframe"></iframe>
<div id="pdfDiv">
</div>
<script type="text/javascript">
  $(function () {

    $("#btnIframe").click(function () {

      $.ajax({
        url: '/PDFTest/GetPdf',
        type: "GET",
        data: json,  // This line will not be a problem
        dataType: "json",
        contentType: "application/pdf", // This line might be a problem
        success: function (pdf) {
          // What to I need to need to do to display the PDF in the above iframe
          $("#iframe").attr("src", pdf); // HTTP Error 400 - Bad Request.
        }
      });
    });

    $("#btnNewTab").click(function () {
      $.ajax({
        url: '/PDFTest/GetPdf',
        type: "GET",
        data: json,  // This line will not be a problem
        dataType: "json",
        contentType: "application/pdf", // This line might be a problem
        success: function (pdf) {
          // What to I need to need to do to disply the PDF in a new window
        }
      });
    });

  });
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:50:43+00:00Added an answer on May 31, 2026 at 11:50 pm

    Action:

    public ActionResult GetPdf()
    {
        byte[] pdf = ...
        Response.AppendHeader("Content-Disposition", "inline;test.pdf");
        return File(pdf, "application/pdf");
    }
    

    To open in new Tab/Window:

    @Html.ActionLink("view pdf", "getpdf", "somecontroller", null, new { target = "_blank" })
    

    To open in an iframe your code looks fine. Just make sure to set the Content-Disposition header to inline.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think i am doing everything correctly.....but obviously i am missing something, my while
Please help! I'm really at my wits' end. My program is a little personal
Please help! I couldn't figure it out how to map the following situation: I
Please help me convert this line to C#. objManagementBaseObject.SetPropertyValue(hDefKey, CType(&H & Hex(RegistryHive.LocalMachine), Long)) Related
Please help me with using the DrScheme built-in function filter. create a function hello
I have this quick question regarding cost estimation using Function Points. We are doing
I got this example from one StackOverflow question that was asked but I couldn't
This has been asked several times, i know, but help me understand something. You
Can someone please help me to understand what Am I doing wrong ? I
Please help! Background info I have a WPF application which accesses a SQL Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.