0

Trying to fill name and address on each boxes using

 cb.SetTextMatrix(x, y);// x and y positions .
 cb.ShowText("data");

enter image description here

But fails to do so .

the problem

Midhul
  • 50
  • 1
  • 11
  • Bruno already gave a good answer, so here merely some hints concerning your question: you say that your code fails to fill in the fields. But how does it fail? And your code uses some `cb` variable. What does it contain? And have you applied some other method of it? Then you show a screenshot of the form but don't share it as pdf. Thus, we cannot seriously inspect it or try and reproduce the problem. So, if you want answers which more directly refer to your code and problem as is, you need to provide more information. – mkl Oct 31 '17 at 08:53
  • this is my problem =>if there is 16 small columns for Name field ?do i need to create 16 AcroFields object to fill the name ? I tried to create one Acrofield object for entire name but two characters are filled on the same columns for eg: when i tried to fill the name "ELIZABETH", EL are coming in the same column .Please check the problem image .thank you – Midhul Oct 31 '17 at 09:03
  • As @Bruno explained in his answer: *"Define the fields as **Comb** fields so that each box contains* only a single *glyph."*. – mkl Oct 31 '17 at 09:38

1 Answers1

1

The code you are using isn't entirely incorrect, but it has several flaws. For starters: you don't know the value of the x and y parameters, and that's kind of crucial if you want the text to be in the correct position.

Also: you are writing PDF syntax directly into the content stream. In your snippet, you forgot to create the text object (with cb.BeginText() and cb.EndText()). If you are new at PDF, you shouldn't try writing PDF syntax directly into the content stream unless you have a solid understanding of ISO-32000-1. Have you ever read ISO-32000-1? If not, then why are you using low-level operations? That doesn't make much sense, does it? There are helper classes such as ColumnText to add content at absolute positions.

Looking at the screen shot you shared, I see that some fields require "Comb" functionality. This functionality makes sure that each small box contains exactly one glyph (if you don't know what a glyph is, think of it as the visual representation of a character).

If you want to make it easy on yourself, you should test if the form is interactive first. Answer this question:

Does the form contain AcroFields?

If the answer is "Yes", fill out the form using the AcroFields object. You can find out which field names to use by following the instructions in the answer to this question: How do I enumerate all the fields in a PDF file in ITextSharp ?

If the answer is "No", open the file in Adobe Acrobat, and manually add fields. Define the fields as Comb fields so that each box contains each glyph. To get a nice-looking result, select a monospaced font such as Courier (using a proportional font will probably give you an uglier result). This operation adds AcroForm fields.

Once you have an interactive form with AcroFields (assuming you have defined them correctly), filling out the form is as easy as this in iText 5:

PdfReader reader = new PdfReader(template);
PdfStamper stamper = new PdfStamper(reader,
    new FileStream(newFile, FileMode.Create));
AcroFields form = stamper.AcroFields;         
form.SetField(key1, value1);      
form.SetField(key2, value2);      
form.SetField(key3, value3);
...
stamper.Close();

See How to create and fill out a PDF form

However, since you are new at all of this, I recommend that you use iText 7 as described in the jump-start tutorial:

PdfDocument pdf = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
IDictionary<String, PdfFormField> fields = form.GetFormFields();
PdfFormField toSet;
fields.TryGetValue(key1, out toSet);
toSet.SetValue(value1);
fields.TryGetValue(key2, out toSet);
toSet.SetValue(value2);
...
pdf.Close();

If you want to remove the interactivity after filling out the form, you need to flatten the fields. This is also documented on the official web site.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 1
    this is my problem =>if there is 16 small columns for Name field ?do i need to create 16 AcroFields object to fill the name ? I tried to create one Acrofield object for entire name but two characters are filled on the same columns for eg: when i tried to fill the name "ELIZABETH", EL are coming in the same column .Please check the problem image .thank you – Midhul Oct 31 '17 at 09:07
  • 1
    @Midhul As Bruno explained in his answer: *"Define the fields as **Comb** fields so that each box contains* only a single *glyph."*. – mkl Oct 31 '17 at 09:41
  • Please read the FAQ question [How to get the number of characters in a field?](https://developers.itextpdf.com/question/how-get-number-characters-field) In that example, there's a property `/MaxLen` with value 12 (in your case, it should be 16, because you have 16 blocks). The comb flag is set to true, which means that each character will take 1 / 12 of the available space (in your case 1 / 16). The characters will be evenly distributed, hence they should match the boxes. – Bruno Lowagie Oct 31 '17 at 09:49
  • thank you sir ,You Saved My time ."select a monospaced font such as Courier " this is the solution – Midhul Oct 31 '17 at 11:54