Populating form fields with Unicode data

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Populating form fields with Unicode data

Dennis Gurock
Hello,

I'm trying to use itextsharp to populate form fields of a PDF created
with Acrobat 8 Professional with Unicode characters. I use the
PdfStamper class and the AcroFields SetField method to assign string
values to the form fields. Everything works as expected, except that
Unicode characters aren't displayed (they are completely removed from
the assigned text). The characters aren't visible whether FormFlattening
is enabled or not. However, if FormFlattening is disabled and one of the
form fields in the PDF Reader gets the focus for text input, the
characters are displayed while editing the field.

This makes me believe that this might be a font issue, but I'm not sure
which font is used by itextsharp for FormFlattening and how to change
it. Changing the font of the fields in Acrobat didn't make any
difference. Any ideas on how to populate (and flatten) PDF form fields
with Unicode characters? Thanks!

Kind regards,
Dennis Gurock


------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
itextsharp-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions
Reply | Threaded
Open this post in threaded view
|

Re: Populating form fields with Unicode data

Peter Schulz

Dear Dennis,

This looks like a font issue, as you say.

If you (1) embed your font and (2) ensure your font does contain the
characters you want
to display, you should always get there.

To embed a font, create a font in your code, then apply it to the
desired field via the PDFStamper.AcroFields.SetFieldProperty method.
This method has a number of advanced parameters which you can set to
null, for most scenarios:

Sample code follows:

            PdfReader pdfReader = new PdfReader(pdfTemplate);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new
FileStream(@"e:\TestPDF.pdf",
                                                                             
FileMode.Create));
            AcroFields pdfFormFields = pdfStamper.AcroFields;

             // for Unicode, use BaseFont.IDENTITY_H :
             // note that you'll need to add another font if you require
bold characters in some fields

            BaseFont fieldFontRoman =
BaseFont.CreateFont(@"C:\Windows\Fonts\times.ttf",
                                    BaseFont.IDENTITY_H,
                                    BaseFont.EMBEDDED);

            // apply the font to each field, ensure it is embedded:
            pdfStamper.AcroFields.SetFieldProperty("Text1", "textfont",
fieldFontRoman, null);
           
            pdfFormFields.SetField("Text1", @"This is text I filled out
here, and it is automatically being resized.");

            pdfStamper.FormFlattening = false;
            // if false, PDF can still be edited

            pdfStamper.Close();
            pdfReader.Close();

You may need to set the text size, use the following code.

        float fSize = 10;
        pdfStamper.AcroFields.SetFieldProperty("Name", "textsize",
fSize, null);

If  you set fSize = 0, you should have automatic resizing per the PDF
specification.
However, you may encounter a problem with the resizing of the text in
the form fields, at least I did. It does not behave the same
way as if you were to set text size = 0 (automatic) in Acrobat.

If that is a problem for you, you can even calculate the necessary text
size if you need to - use the
PdfStamper.AcroFields.GetFieldPositions(fieldName) method.


Regards
Peter Schulz



Dennis Gurock wrote:

> Hello,
>
> I'm trying to use itextsharp to populate form fields of a PDF created
> with Acrobat 8 Professional with Unicode characters. I use the
> PdfStamper class and the AcroFields SetField method to assign string
> values to the form fields. Everything works as expected, except that
> Unicode characters aren't displayed (they are completely removed from
> the assigned text). The characters aren't visible whether FormFlattening
> is enabled or not. However, if FormFlattening is disabled and one of the
> form fields in the PDF Reader gets the focus for text input, the
> characters are displayed while editing the field.
>
> This makes me believe that this might be a font issue, but I'm not sure
> which font is used by itextsharp for FormFlattening and how to change
> it. Changing the font of the fields in Acrobat didn't make any
> difference. Any ideas on how to populate (and flatten) PDF form fields
> with Unicode characters? Thanks!
>
> Kind regards,
> Dennis Gurock
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by:
> SourcForge Community
> SourceForge wants to tell your story.
> http://p.sf.net/sfu/sf-spreadtheword
> _______________________________________________
> itextsharp-questions mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/itextsharp-questions
>  


------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
itextsharp-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions
Reply | Threaded
Open this post in threaded view
|

Re: Populating form fields with Unicode data

Dennis Gurock
Hello Peter,

> If you (1) embed your font and (2) ensure your font does contain the
> characters you want to display, you should always get there.

Thanks a lot for your detailed explanation, I really appreciate it. I
still couldn't get it working, even with standard fonts such as MS Arial
and with characters that are definitely part of this font (I also
checked that the font was really embedded into the resulting PDF).

I now use stamper.GetOverContent(1) and draw/place the text directly
over the page. This is a bit more work but still works well in my
situation. And more importantly, this also works with Unicode characters
and the embedded fonts.

Kind regards,
Dennis Gurock

------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
itextsharp-questions mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/itextsharp-questions
Reply | Threaded
Open this post in threaded view
|

Re: Populating form fields with Unicode data

RohanW
This post has NOT been accepted by the mailing list yet.
In reply to this post by Peter Schulz
Hi Peter,
Thanks for the detailed reply. It helped me clear some my iTextSharp code.
I too have a PDF file which i'm filling dynamically using the code which you suggested.
I have embeded the font which I am using and any text i'm assigning to text field is visible till i dont flatten the file.

If i set  pdfStamper.FormFlattening = true;  all the dynamic values disappear and only empty PDF is dispalyed.

Is there any other steps I need to impliment to get it working ?