iTextSharp generate PDF and show it on the browser directly
Solution 1
The problem solved with the code below:
HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim pdfDoc As New Document()
PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream)
pdfDoc.Open()
'WRITE PDF <<<<<<
pdfDoc.Add(New Paragraph("My first PDF"))
'END WRITE PDF >>>>>
pdfDoc.Close()
HttpContext.Current.Response.Write(pdfDoc)
HttpContext.Current.Response.End()
Hope help! :)
Solution 2
You need to set Content Type
of the Response object
and add the binary
form of the pdf
in the header
private void ReadPdfFile()
{
string path = @"C:\Somefile.pdf";
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length",buffer.Length.ToString());
Response.BinaryWrite(buffer);
}
}
(or) you can use System.IO.MemoryStream
to read and display:
Here you can find that way of doing it
Open Generated pdf file through code directly without saving it onto the disk
mrjimoy_05
I am a Objective-C, Swift, Java, ABAP, VB.Net, Visual C#, PHP developer utilizing PostgreSQL, MongoDB, SQL Server, MySQL, DB2, Oracle :)
Updated on November 23, 2020Comments
-
mrjimoy_05 over 3 years
How to open the PDF file after created using iTextSharp on ASP.Net? I don't want to save it on the server, but directly when the new PDF is generated, it shows on the browser. Is it possible to do that?
Here is the example for what I mean: Click here . But on this example, the file directly downloaded.
How can I do that?
Dim doc1 = New Document() 'use a variable to let my code fit across the page... Dim path As String = Server.MapPath("PDFs") PdfWriter.GetInstance(doc1, New FileStream(path & "/Doc1.pdf", FileMode.Create)) doc1.Open() doc1.Add(New Paragraph("My first PDF")) doc1.Close()
The code above do save the PDF to the server.
Thank you very much in advance! :)
-
mrjimoy_05 about 12 yearsSo it means I still should have to save the PDF to the server and then read it?
-
Sivajith about 11 years@ mrjimoy.how to open it in new tab...currently open in same tab so that the web page is replaced and cannot go bak...