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.
Related
I am trying to create a form field in a PDF where the user can insert an image file and save the document so that the image is persistent (in a new PDF document, as opposed to altering an existing document). I know this is possible, because I've seen it done in other PDFs, but I can't work out how it's supposed to be done in iText 7 for .NET/C#.
I found this on Google, which seems to at least provide the JavaScript and outline of a solution, but I don't know how to edit the "Layout" of an iText PdfButtonFormField object. I have also tried this answer from the iText website, but it's geared towards adding to an existing document, and I couldn't get it to work anyway (some more elusive System.NullReferenceException errors).
Using the idea of creating a button and replacing the image, so far I have tried:
PdfWriter writer = new PdfWriter("myfile.pdf");
PdfDocument document = new PdfDocument(writer);
PdfPage pdfPage = document.AddNewPage(PageSize.A4);
PdfCanvas canvas = new PdfCanvas(pdfPage);
PdfAcroForm form = canvas.GetForm();
PdfButtonFormField button = PdfFormField.CreateButton(document, new Rectangle(50, 50), 0);
button.SetAction(PdfAction.CreateJavaScript("event.target.buttonImportIcon();"));
form.AddField(button); // <-- Error on this line
document.Close();
writer.Close();
In the hope that the buttonImportIcon() would be enough to override the buttons appearance. But I get a System.NullReferenceException: 'Object reference not set to an instance of an object.' error at the indicated line (unfortunately it is no more specific than that), with a slightly unhelpful stacktrace:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at iText.Forms.PdfAcroForm.AddField(PdfFormField field, PdfPage page)
at iText.Forms.PdfAcroForm.AddField(PdfFormField field)
at ReplaceIcon.Main(String[] args) in ReplaceIcon.cs:line 65
I also tried replacing the CreateButton with CreatePushButton, as in:
PdfButtonFormField button = PdfFormField.CreatePushButton(document, new Rectangle(50, 50), "name", "caption");
Using which the code compiles, and I get a "Select Image" dialogue box when I click on the button in the PDF, but the button remains just a grey square with "caption" written on it, rather than being replaced by the selected image. But I suspect that a generic button is required so you can overwrite the layout (somehow).
If anyone knows how this is supposed to be done, either using this button approach or another way, I would greatly appreciate some pointers. As I said, I am specifically interested in creating these fields in a newly generated PDF document, using iText 7 in a C# program.
But I get a System.NullReferenceException: 'Object reference not set to an instance of an object.'
This is the bug in the Acroform#addField method. NPE is being thrown every time it gets a nameless field as a parameter.
To avoid it, just set the field name before adding to the form (field#setName).
Using which the code compiles, and I get a "Select Image" dialogue box when I click on the button in the PDF, but the button remains just a grey square with "caption" written on it, rather than being replaced by the selected image. But I suspect that a generic button is required so you can overwrite the layout (somehow).
the PdfFormField.CreateButton method does not give you any advantages here. That method in iText creates an empty PdfButtonFormField (appearance and behavior should be defined by the developer after a field creation).
On the other side, CreatePushButton does almost what you need.
The only thing must be adjusted is a layout. By default, the created push button has the "label only" layout.
public void Generate()
{
PdfWriter writer = new PdfWriter("myfile.pdf");
PdfDocument document = new PdfDocument(writer);
PdfAcroForm form = PdfAcroForm.GetAcroForm(document, true);
PdfButtonFormField button = PdfFormField.CreatePushButton(document, new Rectangle(20, 500, 50, 50), "btn",
"load");
button.SetAction(PdfAction.CreateJavaScript("event.target.buttonImportIcon();"));
//change the layout type.
PdfDictionary widget = (PdfDictionary) button.GetKids().Get(0).GetIndirectReference().GetRefersTo();
widget.GetAsDictionary(PdfName.MK).Put(PdfName.TP, new PdfNumber((int) PushButtonLayouts.ICON_ONLY));
form.AddField(button); // <-- Error on this line
document.Close();
}
enum PushButtonLayouts
{
LABEL_ONLY = 0, //No icon; caption only
ICON_ONLY = 1, //No caption; icon only
ICON_TOP_LABEL_BOTTOM = 2, // Caption below the icon
LABEL_TOP_ICON_BOTTOM = 3, // Caption above the icon
ICON_LEFT_LABEL_RIGHT = 4, //Caption to the right of the icon
LABEL_LEFT_ICON_RIGHT = 5, //Caption to the left of the icon
LABEL_OVER = 6 // Caption overlaid directly on the icon
}
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.
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.
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...