Is is possible to add validation to PDF form fields via iTextSharp.
I currently generate a PDF document, fill editable form fields with values from my database and present the document to the user in a webpage.
PdfReader pdfReader = new PdfReader(template);
PdfStamper pdfStamper = new PdfStamper(pdfReader, writeStream);
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.SetField("field1", myobj.field1value);
...
pdfStamper.Close();
I'd like to be able to dynamically add validation e.g. numeric field min-max values, or custom JavaScript to the field in this document rendering process. I know this is possible when designing the form in Acrobat, but I can't find any methods/fields for accessing these validation fields through iTextsharp.
When looking for answers, please consult the documentation: "iText in Action".
Your question is answered by an example:
Java: http://itextpdf.com/examples/iia.php?id=238
C#: http://kuujinbo.info/iTextInAction2Ed/index.aspx?ch=Chapter13&ex=AddJavaScriptToForm
In this example, some custom JavaScript including a validate() method is added to a field/button. This is the JavaScript: http://examples.itextpdf.com/resources/js/extra.js
The JS added as Additional Action (AA) or as action to a Submit button is identical to the JS one would write for HTML.
Related
I'm using iTextSharp to fill my form fields on my template with values.
Some of this fields should be flattened so that the user can't edit it.
The rest of the fields should be filled by the user and my program can read out the data and insert it into the database.
My Solution works fine with Abobe Reader DC, but I have problems with Adobe Reader X. An update of the version on the clients isn't possible.
So I enabled usage rights in the PDF template. The problem is that with iTextSharp I have to enable append on the PdfStamper. If this is enabled, it's not possible to flatten a part of the form because of the Adobe signature on the document.
Now I have following idea:
I split my template into two documents. The first document is filled using my code and Formflattening is true.
Here's the Code:
MemoryStream outstream = new MemoryStream();
Document document = new Document();
PdfSmartCopy writer = new PdfSmartCopy(document, outstream);
document.Open();
//newFileStream is the flattend Form
pdfReader = new PdfReader(newFileStream.ToArray());
writer.AddDocument(pdfReader);
PdfReader reader = new PdfReader(Properties.Resources.PdfForm);
writer.AddDocument(reader);
reader.Close();
writer.Close();
document.Close();
return outstream.ToArray();
Now I merge the document with my second template and save it then I have the document that I want. The first page is 'readonly' and the second page has formfields.
My problem is now that the user can fill the document (checkboxes and textboxes) but if the user saves the document the filled checkboxes disappear. Hence, if the user sends me back the document there are no checkboxes in it.
I don't understand why only checkboxes disappear.
I have a PDF template with form fields where I used iText 7.1.0 to replace some placeholder text inside different form fields which is working fine:
PdfReader reader = new PdfReader(pdf_template);
PdfDocument pdf = new PdfDocument(reader, new PdfWriter(pdf_output));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
form.GetField("Vertragsnummer").SetValue("12345");
form.FlattenFields();
pdf.Close();
reader.Close();
Now I have a form field for which option is set to allow rich text format as there are some parts with bold font etc..
Inside the form field standard text there are again placeholders which I want to replace.
Currently I've got the following code:
PdfFormField tf_maintext = form.GetField("maintext");
tf_maintext.SetRichText(tf_maintext.GetRichText());
Now I need to replace the placeholders but I didn't find any working solution for C#.
For normal form fields I can use
PdfFormField kdnr = form.GetField("Kundennummer");
kdnr.SetValue(kdnr.GetValueAsString().Replace("7777777777", "1111111111"));
the normal Replace function to achieve this.
But I need a solution to replace text in the rich text format field to ensure that the formatting is still there afterwards.
How can I do this?
SO i have a created a PDF template form with textbox which are editable fields. I am able to generate the pre-populated PDF with values from my database into the template through MVC 4.0 Application. Which works fine. Now i want to add a image from a folder into the PDF which will distinguished one form with another form. The image will depend on the user in-put. Image will go at the bottom of the PDF. I don't see any image-box or image container as a filed option. Only one i can see are text-box,checkbox,radio,list box etc but nothing like a iimage holder.
Dose any one know how to add image dynamically into PDF?
You can find the answer to your question in the official documentation, more specifically in chapter 8. In section 8.2.3, entitled "Pushbuttons", I explain that we usually use buttons as placeholders for images, because buttons can have an icon.
The ReplaceIcon example shows how you can replace the icon of an exising button. As you are using C#, you may want to take a look at ReplaceIcon.cs:
PdfReader reader = new PdfReader(aPdf);
using (MemoryStream ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, ms)) {
AcroFields form = stamper.AcroFields;
PushbuttonField ad = form.GetNewPushbuttonFromField("button_name");
ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
ad.ProportionalIcon = true;
ad.Image = Image.GetInstance(yourImage);
form.ReplacePushbuttonField("button_name", ad.Field);
}
}
// ms will contain your PDF in memory
}
reader.Close();
Note that the line ad.ProportionalIcon = true; will scale your image so that it fits the button.
I want to create a fillable PDF using Open Office or something which is open source. With Open Office I created the fillable PDF and it works fine with Foxit Reader; we can also save it. Now the problem is I have an image in the PDF which should also be fillable, like the other fields. User should be able to put his/her image in the image box and save it.
Later I will be reading the PDF using iTextSharp to retrieve the field's values and save it in the database. Other than the image everything works fine. I tried to create the image box with Open Office but when I open it in the PDF reader I cannot change the picture and moreover how will I read the image using iTextSharp and display it on the picture box so that user can save all the data in the database in future?
I tried with this but its showing null value
string pdfTemplate = #"c:\Temp\PDF\Untitled 1.pdf";
var reader = new PdfReader(pdfTemplate);
var output = new MemoryStream();
var stamper = new PdfStamper(reader, output);
//textBox2.Text = stamper.AcroFields.GetField("f1_09(0)");
Bitmap bimg = new Bitmap(stamper.AcroFields.GetField("ImageControl"));
System.Drawing.Image tempimg = bimg;
pictureBox1.Image = tempimg;
stamper.FormFlattening = true;
stamper.Close();
reader.Close();
It is not possible to 'put an image into a fillable form'. The PDF specification only supports fillable text, not images...
Here is a list of the general types for interactive AcroForm elements which are available according to the PDF-1.7 ISO specification:
Button fields represent interactive controls on the screen that the user can manipulate with the mouse. They include pushbuttons, check boxes, and radio buttons.
Text fields are boxes or spaces in which the user can enter text from the keyboard.
Choice fields contain several text items, at most one of which may be selected as the field value. They include scrollable list boxes and combo boxes.
Signature fields represent digital signatures and optional data for authenticating the name of the signer and the document’s contents.
As you can see, there are no images in this list...
I have an ASP.NET User control, .ascx and I need to generate a PDF with the contents of the user control. Is this possible, if so can someone point me in the right direction?
Thanks
There are many third-party PDF creation tools out there. One we've used at my work that works well is PdfSharp
As far as outputting the user control's as text, this is what you need to do:
Page page = new Page();
HtmlForm form = new HtmlForm();
[USERCONTROL] uc = (ReportHeaderInfoUC)page.LoadControl("~/UserControls/[NAME OF YOUR USER CONTROL].ascx");
page.Controls.Add(form);
form.Controls.Add(uc);
//call the function on the user control that populates the control and pass in any data needed (if any)
uc.Populate(data);
StringWriter stringWriter = new StringWriter();
this.Server.Execute(page, stringWriter, false);
What you are basically doing here is creating a new WebForm, adding the user control to it and having the server render the output to a stream, which is basically what ASP.NET does for a regular page request.
To write the contents of the outputted user control to a file use:
File.WriteAllText([FILE PATH], stringWriter.ToString());
As long as the user control is only text based, it should not be difficult to output as a pdf with any pdf creation library out there (search for generating pdf, or pdf creation). If your usercontrols have more complex html tags then you basically need to buy a Html 2 Pdf library. I have not been able to find an acceptable free library that supports html to pdf.
You can also build up tables in PDF, but it would be very difficult to convert from html tables to pdf tables generically without buying a library to do that in my experience. You could also code the building of the PDF tables separately from the usercontrol, but again very difficult to combine that function into it.