The below portion of the code using MemoryMappedFile object to create a test pdf document in memory and perform read/write operation on file. The CreateViewStream() object will give you a stream that can be used to read or write data to/from file. The code is very straight forward and I included comment so that you can understand the code.
using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("test1.pdf", 1000000))
{
// Create a new pdf document object using the constructor. The parameters passed are document size, left margin, right margin, top margin and bottom margin.
iTextSharp.text.Document d = new iTextSharp.text.Document(PageSize.A4, 72,72,172,72);
//get an instance of the memory mapped file to stream object so that user can write to this
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
// associate the document to the stream.
PdfWriter.GetInstance(d, stream);
//add an image as bg
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(Server.MapPath("Image/bg.png"));
jpg.Alignment = iTextSharp.text.Image.UNDERLYING;
jpg.SetAbsolutePosition(0, 0);
//this is the size of my background letter head image. the size is in points. this will fit to A4 size document.
jpg.ScaleToFit(595, 842);
d.Open();
d.Add(jpg);
d.Add(new Paragraph(String.Format("Dear {0} {1},", title, fullname)));
d.Add(new Paragraph("\n"));
d.Add(new Paragraph(msg));
d.Add(new Paragraph("\n"));
d.Add(new Paragraph(String.Format("Administrator")));
d.Close();
}
//read the data from the file and send it to the response stream
byte[] b;
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
BinaryReader rdr = new BinaryReader(stream);
b = new byte[mmf.CreateViewStream().Length];
rdr.Read(b, 0, (int)mmf.CreateViewStream().Length);
}
Response.Clear();
Response.ContentType = "Application/pdf";
Response.BinaryWrite(b);
Response.End();
}
Press ctrl + f5 to run the application. First I got the user list. Click on the generate pdf icon. The created looks as follows.
http://weblogs.asp.net/blogs/sreejukg/clip_image003_4AA1BF2F.png