Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
3.29/5 (5 votes)
See more:
I have written the following code to create a pdf file and save the pdf file using ITextSharp.

using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO; 
Collapse | Copy Code
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "PDF Files|*.pdf";
dlg.FilterIndex = 0;
 
string fileName = string.Empty;
 
if (dlg.ShowDialog() == DialogResult.OK)
{
fileName = dlg.FileName;
           
Document myDocument = new Document(iTextSharp.text.PageSize.A4, 10, 10, 42, 35);
PdfWriter.GetInstance(myDocument, new FileStream(fileName, FileMode.Create));
myDocument.Open();
myDocument.Add(new Paragraph("ID:" + TextBox1.Text));pre>
myDocument.Close();
} 


I want to open the file from the memory instead of opening the file that I saved with an opendialog. How can I do that?

Thank you
Posted
Updated 28-Feb-12 19:21pm
v3

using (MemoryStream myMemoryStream = new MemoryStream())
           {
               Document myDocument = new Document();
               PdfWriter myPDFWriter = PdfWriter.GetInstance(myDocument, myMemoryStream);

               myDocument.Open();

               // Add to content to your PDF here...
               PdfPTable table = new PdfPTable(2);
               PdfPCell header = new PdfPCell(new Phrase("Your Heading"));
               header.Colspan = 2;
               header.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
               table.AddCell(header);
               table.AddCell("ID:" + Textbox1.Text);
               myDocument.Add(table);
               myDocument.Close();

               byte[] content = myMemoryStream.ToArray();

               // Write out PDF from memory stream.
               using (FileStream fs = File.Create("C:\\Test.pdf"))
               {
                   fs.Write(content, 0, (int)content.Length);
               }
           }


When you close your form you can delete the file

File.Delete(C:\\Test.pdf);
 
Share this answer
 
var file = Path.GetTempFileName();
            string filepath = Path.GetTempPath();
            string strFilename = Path.GetFileName(file);
            using (MemoryStream ms = new MemoryStream())
            {
                Document doc = new Document(); 
                //PdfWriter.GetInstance(doc, ms);
                PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Path.Combine(filepath, strFilename), FileMode.Create));
                doc.AddTitle("Document Title"); 
                doc.Open();
                doc.Add(new Paragraph("My paragraph. Bla Bla Test"));
                doc.Close(); 
            }
            axAcroPDF1.src = Path.Combine(filepath, strFilename);     
 
Share this answer
 
The solution is ok , worked for me. Thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900