• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

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:
 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();

Open in new window


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(); 

Open in new window

0
Camillia
Asked:
Camillia
  • 3
  • 2
1 Solution
 
käµfm³d 👽Commented:
Use a MemoryStream instead of a FileStream.

MemoryStream ms = new MemoryStream();
PdfWriter wri = PdfWriter.GetInstance(doc, ms);

Open in new window


...and then write the bytes instead of a file:

Response.BinaryWrite(ms.ToArray());

Open in new window

0
 
themrrobertCommented:
he gave a perfect answer damn i was too late, nice one :)
0
 
CamilliaAuthor Commented:
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);
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CamilliaAuthor Commented:
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(); 

Open in new window

0
 
käµfm³d 👽Commented:
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.
0
 
CamilliaAuthor Commented:
Did it like this.
    Response.ContentType = "application/pdf";
               
                 Response.AddHeader("Content-Disposition", "attachment");
                Response.BinaryWrite(ms.ToArray());
             
               Response.End();
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now