Reputation: 969
I am using editable pdf files (created by Nitro PDF Software) in my application. These pdf files have a lot of editable fields (like textboxes) and one button (like submit).
Whenever a user opens that pdf file, enters the text, and clicks the submit button they get redirected to an aspx page.
How do I get all the static and dynamic values that are posted to this page, and create another pdf file with the entered data? And how do I save the created pdf file?
Upvotes: 7
Views: 35920
Reputation: 9
Download itextSharp dll file,use namespace
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text.pdf.draw;
Document document = new Document(PageSize.A4,43,43, 43, 43);
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"D:\abc\text.pdf", FileMode.Create));
PdfPCell cell = null;
PdfPTable table = null;
document.Open();
Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph para = new Paragraph();
table = new PdfPTable(1);
table.TotalWidth = 340f;
table.LockedWidth = true;
table.SpacingBefore = 20f;
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(PhraseCell(new Phrase("SCHEME INSTALLMENT RECEIPT ", FontFactory.GetFont("Arial", 12,1)), PdfPCell.ALIGN_CENTER));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 1;
cell.PaddingBottom = 10f;
table.AddCell(cell);
document.Add(table);
Phrase ph1 = new Phrase();
Paragraph mm = new Paragraph();
ph1.Add(new Chunk(Environment.NewLine));
ph1.Add(new Chunk("Name : " + name,FontFactory.GetFont("Arial", 10,1)));
ph1.Add(glue);
ph1.Add(new Chunk("Bill No. : " + BillNo, FontFactory.GetFont("Arial", 10,1)));
ph1.Add(new Chunk(Environment.NewLine));
ph1.Add(new Chunk("Address : " + address, FontFactory.GetFont("Arial", 10,1)));
ph1.Add(glue);
ph1.Add(new Chunk("Bill Date : " + billDate, FontFactory.GetFont("Arial", 10,1)));
ph1.Add(new Chunk(Environment.NewLine));
ph1.Add(new Chunk("Mobile No. : " + mobile, FontFactory.GetFont("Arial", 10,1)));
ph1.Add(glue);
ph1.Add(new Chunk("Scheme No. : " + orderNo, FontFactory.GetFont("Arial", 10,1)));
mm.Add(ph1);
para.Add(mm);
document.Add(para);
table = new PdfPTable(3);
table.TotalWidth = 340f;
table.LockedWidth = true;
table.SpacingBefore = 20f;
table.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(PhraseCell(new Phrase("HSN Code ", FontFactory.GetFont("Arial", 10,1)), PdfPCell.ALIGN_CENTER));
table.AddCell(PhraseCell(new Phrase("No of Installment", FontFactory.GetFont("Arial", 10,1)), PdfPCell.ALIGN_CENTER));
table.AddCell(PhraseCell(new Phrase("Installment Amount", FontFactory.GetFont("Arial", 10,1)), PdfPCell.ALIGN_CENTER));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 3;
cell.PaddingBottom = 10f;
table.AddCell(cell);
table.AddCell(PhraseCell(new Phrase("7113", FontFactory.GetFont("Arial", 10,1)), PdfPCell.ALIGN_CENTER));
table.AddCell(PhraseCell(new Phrase(paidNo, FontFactory.GetFont("Arial", 10,1)), PdfPCell.ALIGN_CENTER));
table.AddCell(PhraseCell(new Phrase(paidAmount, FontFactory.GetFont("Arial", 10,1)), PdfPCell.ALIGN_CENTER));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 3;
cell.PaddingBottom = 10f;
table.AddCell(cell);
PdfContentByte contentByte = writer.DirectContent;
contentByte.MoveTo(45.0, 627.0);
contentByte.LineTo(550.0, 627.0);
document.Add(table);
Paragraph para1 = new Paragraph();
Phrase ph2 = new Phrase();
Paragraph mm1 = new Paragraph();
string amountWord = ConvertNumbertoWords(Convert.ToInt64(paidAmount));
ph2.Add(new Chunk(Environment.NewLine));
ph2.Add(new Chunk(Environment.NewLine));
ph2.Add(new Chunk(Environment.NewLine));
ph2.Add(new Chunk(amountWord + " Only", FontFactory.GetFont("Arial", 10,1)));
ph2.Add(new Chunk(Environment.NewLine));
ph2.Add(new Chunk("By Cash", FontFactory.GetFont("Arial", 10,1)));
ph2.Add(new Chunk(Environment.NewLine));
ph2.Add(new Chunk(paidAmount, FontFactory.GetFont("Arial", 10,1)));
mm1.Add(ph2);
para1.Add(mm1);
document.Add(para1);
Paragraph para3 = new Paragraph();
Phrase ph3 = new Phrase();
Paragraph mm3 = new Paragraph();
ph3.Add(new Chunk(Environment.NewLine));
ph3.Add(new Chunk("Credit Card Charges :", FontFactory.GetFont("Arial", 10,1)));
ph3.Add(new Chunk(Environment.NewLine));
ph3.Add(new Chunk("Customer Sign.", FontFactory.GetFont("Arial", 10,1)));
ph3.Add(glue);
ph3.Add(new Chunk("For Example", FontFactory.GetFont("Arial", 10,1)));
mm3.Add(ph3);
para3.Add(mm3);
document.Add(para3);
document.Close();
Upvotes: 0
Reputation: 113
To generate PDF, use following code:
NameSpace:
Using System.IO;
Using.iTextSharp.text;
Using.iTextSharp.text.pdf;
On button click:
private void button1_Click(object sender, EventArgs e)
{
Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@"C:\Test.pdf", FileMode.Create));
doc.Open();
Paragraph paragraph = new Paragraph("Hi, \n This is xxx from yyy \n This is my pdf file");
doc.Add(paragraph);
doc.Close();
}
Upvotes: 1
Reputation: 15678
From Wikipedia, PDF Interactive elements there are two possibilities for integrating data and PDFs (there are also links to the specifications):
For compatibility issues I would go for AcroForms. In that case I would use XFDF, because it is XML and therefore easier to parse. I never used Nitro, but when you build a PDF form you usually provide a "Save" button and choose at action for this button "Send/Post form to server" with the data format XML which is just XFDF.
This works only when the PDF is viewed in the browser. So the typical use case is: have an empty PDF template on the server, before returning the PDF to the user mix your data into the PDF, the user enters data in the form (PDF is opened in the browser via a plugin or natively in Chrome), then the user presses the save buttons which posts a xml on the server. The next time the user asks for his PDF, you took the recent data and mix it again with the template.
So only two questions are open:
See the complete process here: http://itextpdf.com/book/chapter.php?id=9 . This example updates the PDF with the form dynamically at run time. Since iText is used there is no difference between Java and C#.
Be aware that previous versions of iText (Java up to 2.1.7 and C# up to 4.1.6) were distributed under the Mozilla Public License or the LGPL, while current versions are distributed under the Affero General Public License. Thats explains why the older versions are still used.
Upvotes: 9
Reputation: 10418
How do I get all the static and dynamic values that are posted to this page?
You can retreive them as you retrieve any other value from an html control, for example:
string MyVal = Request.Form["FieldName"];
and create another pdf file with the entered data?
Here you can use a PDF library of your choice ( iText.Net, Nitro PDF, Amyuni PDF Creator.Net ), load your PDF form, set the values to each field, flatten your file if needed, and save. The code for this part depends on the library being used, but they are usally well documented so you should be able to find sample code easily.
Upvotes: 2