ASP.NET - how to generated new pdf form then write and read using asp.net

Asked By Anto Bilson on 22-Sep-11 07:59 AM


hi,
 
I've downloaded pdf form [not pdf file],the pdf form contains some information's like[our basic details] also submit button in pdf form.Now i've enter corresponding details in the pdf form fields,then i click the submit button the corresponding values from pdf form will store to db.[only values are store no need to store fields because already created the fields in db, similarly no need to store pdf form in db] i need only the entered values are stored in db

pls suggest me to do this process

Thanks
smr replied to Anto Bilson on 22-Sep-11 08:03 AM
Hi

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

refer
http://weblogs.asp.net/sreejukg/archive/2011/03/07/create-pdf-document-using-itextsharp-in-asp-net-4-0-and-memorymappedfile.aspx
http://csharpdotnetfreak.blogspot.com/
dipa ahuja replied to Anto Bilson on 22-Sep-11 08:07 AM
Untitled document
This is the simple example
 
Try this way:
 
Download the dll of itextSharp.dll
 
http://sourceforge.net/projects/itextsharp/
and try this code
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
private void btnExport_Click(object sender, EventArgs e)
{
  Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
  PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("d:\\Test.pdf", FileMode.Create));
   doc.Open();//Open Document to write
 
   Paragraph paragraph = new Paragraph("data Exported From DataGridview!");
   doc.Add(paragraph);
  doc.Add(t1);
  doc.Close(); //Close document
  //
  MessageBox.Show("PDF Created!");
}
 
 
 
Reena Jain replied to Anto Bilson on 22-Sep-11 08:13 AM
Hi,

If you want to generate PDF files in .net, then you have to opt for the third party libraries which will make your life easier. There are many PDF helper libraries which come for free, out of which the iTextSharp.dll stands first and it is a very useful one, since I have used it in my project also. Also it is being claimed that this dll is developed using managed code. So, I don't see any obstacle in using this.

You can download iTextSharp http://sourceforge.net/projects/itextsharp/.

Even sourceforge.net offers a good tutorials to kick start the development. Access the tutorials http://itextsharp.sourceforge.net/tutorial/index.html.

You can also some of the user reviews in http://sourceforge.net/projects/itextsharp/reviews/.
by using iTextSharp you can create PDF easily like this

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
 
public partial class ShowPDF : System.Web.UI.Page
{
 
  protected void Page_Load(object sender, System.EventArgs e)
  {
  if (!isPostBack) {
    MyPDF();
  }
  }
  public ShowPDF()
  {
  Load += Page_Load;
  }
}
 
public void MyPDF()
{
  Document doc = new Document();
  PdfWriter.GetInstance(doc, new FileStream(Request.PhysicalApplicationPath + "\\1.pdf", FileMode.Create));
  doc.Open();
  doc.Add(new Paragraph("Reena PDF"));
  doc.Close();
  Response.Redirect("~/newpdf.pdf");
}
 
public void ShowTable()
{
  Document doc = new Document();
  PdfWriter.GetInstance(doc, new FileStream(Request.PhysicalApplicationPath + "\\2.pdf", FileMode.Create));
  doc.Open();
  Table table = new Table(3);
  table.BorderWidth = 1;
  table.BorderColor = new Color(0, 0, 255);
  table.Padding = 3;
  table.Spacing = 1;
  Cell cell = new Cell("header");
  cell.Header = true;
  cell.Colspan = 3;
  table.AddCell(cell);
  cell = new Cell("example cell with colspan 1 and rowspan 2");
  cell.Rowspan = 2;
  cell.BorderColor = new Color(255, 0, 0);
  table.AddCell(cell);
  table.AddCell("1.1");
  table.AddCell("2.1");
  table.AddCell("1.2");
  table.AddCell("2.2");
  table.AddCell("cell test1");
  cell = new Cell("big cell");
  cell.Rowspan = 2;
  cell.Colspan = 2;
  cell.HorizontalAlignment = Element.ALIGN_CENTER;
  cell.VerticalAlignment = Element.ALIGN_MIDDLE;
  cell.BackgroundColor = new Color(192, 192, 192);
  table.AddCell(cell);
  table.AddCell("cell test2");
  doc.Add(table);
  doc.Close();
  Response.Redirect("~/new1.pdf");
}

Hope this will help you
Anto Bilson replied to smr on 22-Sep-11 08:51 AM

hi Mr.Robin,

Thanks,
 good very nice,

but already i've mentioned that is not a pdf file ,that is pdf form

please refer the link

http://help.adobe.com/en_US/Acrobat/9.0/Samples/interactiveform_enabled.pdf

i need like this pdf form and get the values from pdf form when click the submit button, then store the datas to db