Camillia
asked on
Combine these 2 pieces of code
I'm following these 2 examples and it works. However, I dont want to save the PDF file to c drive. I just want to open it and not save it on a drive. How can I remove it?
http://www.dotnetspark.com/kb/654-simple-way-to-create-pdf-document-using.aspx
http://stackoverflow.com/questions/922564/trying-to-open-up-pdf-after-creating-it-with-itextsharp-but-cant-get-it-to-wor
This is what I have;
First section that writes to C drive:
Second section that opens it.
http://www.dotnetspark.com/kb/654-simple-way-to-create-pdf-document-using.aspx
http://stackoverflow.com/questions/922564/trying-to-open-up-pdf-after-creating-it-with-itextsharp-but-cant-get-it-to-wor
This is what I have;
First section that writes to C drive:
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("c:\\Test11.pdf", FileMode.Create));
//Open Document to write
doc.Open();
//Write some content
Paragraph paragraph = new Paragraph("This is my first line using Paragraph.");
Phrase pharse = new Phrase("This is my second line using Pharse.");
Chunk chunk = new Chunk(" This is my third line using Chunk.");
// Now add the above created text using different class object to our pdf document
doc.Add(paragraph);
doc.Add(pharse);
doc.Add(chunk);
doc.Close();
Second section that opens it.
string remoteFileName = "Test11.pdf";
string filePath = "c:\\Test11.pdf";
if (!File.Exists(filePath))
throw new FileNotFoundException(
string.Format("Final PDF file '{0}' was not found on disk.",
filePath));
var fi = new FileInfo(filePath);
Response.Clear();
Response.AddHeader("Content-Disposition",
String.Format("attachment; filename=\"{0}\"",
remoteFileName));
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(fi.FullName);
Response.End();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
he gave a perfect answer damn i was too late, nice one :)
ASKER
So what happens to these line of code and that file name? remove them?
string remoteFileName = "Test11.pdf";
string filePath = "c:\\Test11.pdf";
if (!File.Exists(filePath))
throw new FileNotFoundException(
string.Format("Final PDF file '{0}' was not found on disk.",
filePath));
var fi = new FileInfo(filePath);
string remoteFileName = "Test11.pdf";
string filePath = "c:\\Test11.pdf";
if (!File.Exists(filePath))
throw new FileNotFoundException(
string.Format("Final PDF file '{0}' was not found on disk.",
filePath));
var fi = new FileInfo(filePath);
ASKER
Closed it too fast. I have this code now, but it opens in the same browser window ...instead of another window. I lose my website when it opens in the same browser window...how can i fix it?
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
MemoryStream ms = new MemoryStream();
PdfWriter wri = PdfWriter.GetInstance(doc, ms);
//Open Document to write
doc.Open();
//Write some content
Paragraph paragraph = new Paragraph("<h1>Patient Referral Letter - " + patientName + "</h1>");
Phrase pharse = new Phrase("This is my second line using Pharse.");
Chunk chunk = new Chunk(" This is my third line using Chunk.");
// Now add the above created text using different class object to our pdf document
doc.Add(paragraph);
doc.Add(pharse);
doc.Add(chunk);
doc.Close();
Response.BinaryWrite(ms.ToArray());
Response.End();
How does a user trigger this page? By clicking a button? By clicking a link? You cannot trigger a new browser window from the server; this has to be requested by the client. If you were using a link, then you could use the target attribute to specify a new window should open. A button I think would need Javascript code.
ASKER
Did it like this.
Response.ContentType = "application/pdf";
Response.AddHeader("Conten t-Disposit ion", "attachment");
Response.BinaryWrite(ms.To Array());
Response.End();
Response.ContentType = "application/pdf";
Response.AddHeader("Conten
Response.BinaryWrite(ms.To
Response.End();