0

I have the following code that after a PDF is generated submitting a query to SQL will display in browser. But I am looking is to display the file in a PDF viewer in the page and not using the browser reader. The viewer is inside a DIV. You can see the example shown in the image attached, which I run as test to show the file from the local system and not streaming. This function Response.BinaryWrite(bytes); sends the generated PDF to the browser viewer. How can I do for it to display in the viewer inside the DIV instead?

string reportLabel = lbReports.Text;

document.Add(table);
document.Close();
byte[] bytes = memoryStream.ToArray();

Response.Clear();
Response.AddHeader("Content-Disposition", "inline");

Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);

This is the code that should show the generated PDF file in the DIV:

string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"698px\" height=\"450px\">";
embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
embed += "</object>";
ltEmbed.Text = string.Format(embed, ("blabla.pdf")); /*it shows this PDF as local file just for test*/
memoryStream.Close();
this.Context.ApplicationInstance.CompleteRequest();

Image here: sample

The idea is to have the file generated temporarily and the user will decide if saving it or not. I have tried other options, but none seems to work, especially if it is inside a DIV. In other words, I am looking to display the dynamic PDF file using ltEmbed.Text = string.Format(embed, ...);

Mr. Munoz
  • 69
  • 1
  • 3
  • 13

3 Answers3

0

We have used in our project. Try & let me know.

DESIGN

 <div id="pdfPopup" style="display: none;">
    <a href="Javascript:void(0);" id="cl" onclick="document.getElementById('pdfPopup').style.display ='none';"
        class="closebtn2" style="z-index: 1111111;">
        <img src="images/btnClose.png" alt="" />
    </a>

    <div class="overlay" style="visibility: visible !important;">
    </div>

    <div id="popUpContainer" style="border-radius: 8px; -webkit-border-radius: 8px; -moz-border-radius: 8px;
        background-color: #fff; width: 1000px; height: 600px; top: 20px; left: 50%; margin-left: -500px;
        overflow: auto; position: absolute; z-index: 1111111;">
        <object data="" type="application/pdf" width="100%" height="600px">
        </object>
    </div>

 </div>

JS

 function DisplayPDF() 
 {
        var PDFServerPath = document.getElementById('<%=hdnPdfPath.ClientID %>').value;
        var x = document.getElementsByTagName('object')[0];
        x.setAttribute("data", PDFServerPath);
        document.getElementById('pdfPopup').style.display = "block";
  }

Code

 ScriptManager.RegisterStartupScript(Me, Me.GetType(), "DisplayPDF", "DisplayPDF();", True)
Rajan
  • 303
  • 1
  • 12
0

I had an oppurtunity to do the same kind of task, i did in MVC

my controller Method code looks like this

        byte[] fileContent = Content;
        string base64 = Convert.ToBase64String(fileContent);
        Response.Headers.Remove("Content-Disposition");
        Response.Buffer = true;
        Response.ContentType = "application/pdf";
        Response.Headers.Add("Content-Disposition", "inline; filename=MyPdfFile.pdf");
        return File(fileContent, "applicaton/pdf"); 

And my javascript is

        var iframe = document.createElement('iframe');
        iframe.frameBorder = 0;
        iframe.width = "890px";
        iframe.height = "550px";
        iframe.id = "frameForMyPdf";
        iframe.setAttribute("src", "/Home/GetPDFContent?PdfId=101");
        document.getElementById("divPdfPreviewPopup").appendChild(iframe);
Sameer549
  • 31
  • 4
0

After long research, I was suggested some workaround, so here I share this link with the solution, that to me it works perfect.

How to stream a generated PDF file to a reader inside a DIV and not the browser

Mr. Munoz
  • 69
  • 1
  • 3
  • 13