iTextSharp Checkboxes disappear in PDF when saving - c#

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.

Related

Bold letters on iTextSharp [duplicate]

This question already has answers here:
Bold a single word within a sentence with iTextSharp
(3 answers)
Closed 7 years ago.
I am writing a simple program on CSharp. What it does, is that you write a text(in a richTextBox), and it saves it's content on a .pdf in a specific path. Now, i want the user to target a specific word/phrase, and by clicking a button, the specific word/phrase will go bold. Is this possible ?? And if yes, how ??
First of all check this out : http://www.mikesdotnetting.com/article/81/itextsharp-working-with-fonts
What basically you need to do is to call the SetFieldProperty function
You take your old pdf, edit it and out put a new PDF with a bolded textbox.
MemoryStream stream = new MemoryStream(); //Memory stream to with new pdf with changed bold text
PdfReader pdfReader = new PdfReader("file.pdf"); //The original PDF
PdfStamper stamper = new PdfStamper(pdfReader, stream); //A stamper to create the pdf
SetFieldProperty("fieldName","textfont",BaseFont.COURIER_BOLD,null); //Change textbox properties
SetField("fieldName","TEXT"); //Change field text
stamper.Close(); //Save and close
byte[] newFile = stream.ToArray(); //Here you have your new file with the bolded text
This code will give you a new file with the bolded text
I think you can input an html to itextsharp. This way you can control many styling attributes using html. for ex: This is bold text. can be input as :
This is <b>bold</b> text.
You can use Html to customize the styling and color of the text like this. Although I haven't tried using external stylesheets using itextsharp, but the inline styling works fine.

Add image in an existing PDF with itextsharp

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.

PDF - Text not showing when filling form

I'm facing a problem when filling a PDF form using iTextSharp, I'm using the following code to fill the PDF form:
PdfReader pdfReader = new PdfReader(Properties.Resources.ConfirmationFees);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(folderPath + "\\" +fileName, FileMode.Create));
AcroFields pdfFFields = pdfStamper.AcroFields;
pdfFFields.SetFieldProperty("Text1", "textsize", 10.0f, null);
pdfFFields.SetField("Text1", serialNumber.ToString("D6") + "№");
pdfStamper.FormFlattening = false;
// close the pdf
pdfStamper.Close();
When I open the PDF, I have to select the textField and go to Properties, select border color or fill color, and click on "No Color". or just simply add a character to the textField.
I've tried to set the border and background color of the textField to null, but without luck.
So, how can I solve this problem without doing the mentioned way?
How did you create your form? If with Open/Libre Office, then the forms are a little bit crappy. You may need to add this line:
pdfFFields.setGenerateAppearances(true);
In your specific C# snippet, that would be:
pdfFFields.GenerateAppearances = true;
See also:
AcroForm values missing after flattening
PdfCopy and form values with iText: form values not visible
If this doesn't solve your problem, you need to tell us which version of iTextSharp you're using. If it's older than 5.5.1, please upgrade.

iTextSharp PDF form field validation

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.

Fillable PDF using Open Office for Image

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...

Categories

Resources