You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Rakshit Ashtekar <ar...@tibco.com> on 2015/08/18 00:49:50 UTC

Supporting Unicode characters to fill in an existing PDF with Acroforms

Hello,

I am using PDFBox 2.0 dev snapshot to support Unicode characters. I have
few questions:

I want to write an existing PDF file which have few PDTextField with
Unicode characters.

I am using PDType0Font to load an external .ttf file
(Arial-Unicode-Regular.ttf) and setting the new font object to the
PDResources. Code snippet below:

PDType0Font font = PDType0Font.load(pdfDocument, new
File("Arial-Unicode-Regular.ttf"));
// Add and set the resources and default appearance at the form level
PDResources res = new PDResources();
COSName fontName = res.add(font);
acroForm.setDefaultResources(res);
String da = "/" + fontName.getName() + " 12 Tf 0 g";
acroForm.setDefaultAppearance(da);

But I am getting below exception
java.io.IOException: Could not find font: /CourierNewPSMT
at
org.apache.pdfbox.pdmodel.interactive.form.PDAppearanceString.getFont(PDAppearanceString.java:126)

I saw a similar ticket - https://issues.apache.org/jira/browse/PDFBOX-2848

*My questions:*
1. Does PDFBox 2.0 supports Unicode characters values to be set on an
existing PDF file which has PDTextField using setValue(String value) of the
PDTextField? I saw an example where Unicode characters are supported but
that PDF is created from scratch and it did not have AcroForms.

2. Will the .ttf file currently in sources of /
pdfbox/src/main/resources/org/apache/pdfbox/resources/ttf/ be packaged with
the final PDFBox jar? or the developers have to load it externally?

Appreciate your help.

Thanks,
Rakshit

Re: Supporting Unicode characters to fill in an existing PDF with Acroforms

Posted by John Hewson <jo...@jahewson.com>.
> On 18 Aug 2015, at 18:24, Rakshit Ashtekar <ar...@tibco.com> wrote:
> 
> Thanks a lot Mauran.
> 
> After I set the default appearance on the text field on which I need to set
> the Unicode, it worked! I also had to set the setNeedAppearances to true()
> to make it work.

Note that setNeedAppearances causes appearances to *not* be generated. It's a flag which tells the program which reads the PDF that it needs to generate the appearances itself. This may or may not be what you want.

> 
>> 2. Will the .ttf file currently in sources of /
>>> pdfbox/src/main/resources/org/apache/pdfbox/resources/ttf/ be packaged
>> with
>>> the final PDFBox jar? or the developers have to load it externally?
>> Yes,
> 
> 
> ​A
> clarification question, when you said
> ​ yes for the above question, did you mean that the .TTF file will be
> packaged along with the final PDFBox jar file?

No, don't use resources which are internal to PDFBox in your app because we might remove them in the future. You must ship your own TTF files.

> Also, is there a way through which multiple .ttf files can be loaded and
> used since a single .ttf file will not support all Unicode
> characters/multiple languages that we need to support?

No, when filling a field you can only use one font. This is a limitation of PDF. Either use one of the built-in fonts such as Helvetica or choose a font with a large Unicode repertoire, e.g. Arial Unicode MS.

It's possible that some programs may replace the font if the end user tries to type an unsupported character. You might want to test that out.

-- John

> Thanks in advance,
> Rakshit
> 
> On Tue, Aug 18, 2015 at 1:11 AM, Maruan Sahyoun <sa...@fileaffairs.de>
> wrote:
> 
>> Hi,
>> 
>>> Am 18.08.2015 um 00:49 schrieb Rakshit Ashtekar <ar...@tibco.com>:
>>> 
>>> Hello,
>>> 
>>> I am using PDFBox 2.0 dev snapshot to support Unicode characters. I have
>>> few questions:
>>> 
>>> I want to write an existing PDF file which have few PDTextField with
>>> Unicode characters.
>>> 
>>> I am using PDType0Font to load an external .ttf file
>>> (Arial-Unicode-Regular.ttf) and setting the new font object to the
>>> PDResources. Code snippet below:
>>> 
>>> PDType0Font font = PDType0Font.load(pdfDocument, new
>>> File("Arial-Unicode-Regular.ttf"));
>>> // Add and set the resources and default appearance at the form level
>>> PDResources res = new PDResources();
>>> COSName fontName = res.add(font);
>>> acroForm.setDefaultResources(res);
>>> String da = "/" + fontName.getName() + " 12 Tf 0 g";
>>> acroForm.setDefaultAppearance(da);
>>> 
>>> But I am getting below exception
>>> java.io.IOException: Could not find font: /CourierNewPSMT
>>> at
>> org.apache.pdfbox.pdmodel.interactive.form.PDAppearanceString.getFont(PDAppearanceString.java:126)
>> 
>> the reason is very likely that the field you are trying to fill has a
>> default appearance where the font is set to CourierNewPSMT. You need to
>> also set the default appearance for the field to use the loaded font.
>> 
>> If you could upload your file to a public location I can take a closer
>> look.
>> 
>>> 
>>> I saw a similar ticket -
>> https://issues.apache.org/jira/browse/PDFBOX-2848
>>> 
>>> *My questions:*
>>> 1. Does PDFBox 2.0 supports Unicode characters values to be set on an
>>> existing PDF file which has PDTextField using setValue(String value) of
>> the
>>> PDTextField? I saw an example where Unicode characters are supported but
>>> that PDF is created from scratch and it did not have AcroForms.
>> 
>> Unicode for field values is supported as of PDFBox 2.0
>> 
>>> 
>>> 2. Will the .ttf file currently in sources of /
>>> pdfbox/src/main/resources/org/apache/pdfbox/resources/ttf/ be packaged
>> with
>>> the final PDFBox jar? or the developers have to load it externally?
>> 
>> Yes,
>> 
>> BR
>> Maruan
>> 
>>> 
>>> Appreciate your help.
>>> 
>>> Thanks,
>>> Rakshit
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
>> For additional commands, e-mail: users-help@pdfbox.apache.org
>> 
>> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Supporting Unicode characters to fill in an existing PDF with Acroforms

Posted by Rakshit Ashtekar <ar...@tibco.com>.
Thanks a lot Mauran.

After I set the default appearance on the text field on which I need to set
the Unicode, it worked! I also had to set the setNeedAppearances to true()
to make it work.

> 2. Will the .ttf file currently in sources of /
> > pdfbox/src/main/resources/org/apache/pdfbox/resources/ttf/ be packaged
> with
> > the final PDFBox jar? or the developers have to load it externally?
> Yes,


​A
 clarification question, when you said
​ yes for the above question, did you mean that the .TTF file will be
packaged along with the final PDFBox jar file?

Also, is there a way through which multiple .ttf files can be loaded and
used since a single .ttf file will not support all Unicode
characters/multiple languages that we need to support?

Thanks in advance,
Rakshit

On Tue, Aug 18, 2015 at 1:11 AM, Maruan Sahyoun <sa...@fileaffairs.de>
wrote:

> Hi,
>
> > Am 18.08.2015 um 00:49 schrieb Rakshit Ashtekar <ar...@tibco.com>:
> >
> > Hello,
> >
> > I am using PDFBox 2.0 dev snapshot to support Unicode characters. I have
> > few questions:
> >
> > I want to write an existing PDF file which have few PDTextField with
> > Unicode characters.
> >
> > I am using PDType0Font to load an external .ttf file
> > (Arial-Unicode-Regular.ttf) and setting the new font object to the
> > PDResources. Code snippet below:
> >
> > PDType0Font font = PDType0Font.load(pdfDocument, new
> > File("Arial-Unicode-Regular.ttf"));
> > // Add and set the resources and default appearance at the form level
> > PDResources res = new PDResources();
> > COSName fontName = res.add(font);
> > acroForm.setDefaultResources(res);
> > String da = "/" + fontName.getName() + " 12 Tf 0 g";
> > acroForm.setDefaultAppearance(da);
> >
> > But I am getting below exception
> > java.io.IOException: Could not find font: /CourierNewPSMT
> > at
> >
> org.apache.pdfbox.pdmodel.interactive.form.PDAppearanceString.getFont(PDAppearanceString.java:126)
>
> the reason is very likely that the field you are trying to fill has a
> default appearance where the font is set to CourierNewPSMT. You need to
> also set the default appearance for the field to use the loaded font.
>
> If you could upload your file to a public location I can take a closer
> look.
>
> >
> > I saw a similar ticket -
> https://issues.apache.org/jira/browse/PDFBOX-2848
> >
> > *My questions:*
> > 1. Does PDFBox 2.0 supports Unicode characters values to be set on an
> > existing PDF file which has PDTextField using setValue(String value) of
> the
> > PDTextField? I saw an example where Unicode characters are supported but
> > that PDF is created from scratch and it did not have AcroForms.
>
> Unicode for field values is supported as of PDFBox 2.0
>
> >
> > 2. Will the .ttf file currently in sources of /
> > pdfbox/src/main/resources/org/apache/pdfbox/resources/ttf/ be packaged
> with
> > the final PDFBox jar? or the developers have to load it externally?
>
> Yes,
>
> BR
> Maruan
>
> >
> > Appreciate your help.
> >
> > Thanks,
> > Rakshit
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

Re: Supporting Unicode characters to fill in an existing PDF with Acroforms

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi,

> Am 18.08.2015 um 00:49 schrieb Rakshit Ashtekar <ar...@tibco.com>:
> 
> Hello,
> 
> I am using PDFBox 2.0 dev snapshot to support Unicode characters. I have
> few questions:
> 
> I want to write an existing PDF file which have few PDTextField with
> Unicode characters.
> 
> I am using PDType0Font to load an external .ttf file
> (Arial-Unicode-Regular.ttf) and setting the new font object to the
> PDResources. Code snippet below:
> 
> PDType0Font font = PDType0Font.load(pdfDocument, new
> File("Arial-Unicode-Regular.ttf"));
> // Add and set the resources and default appearance at the form level
> PDResources res = new PDResources();
> COSName fontName = res.add(font);
> acroForm.setDefaultResources(res);
> String da = "/" + fontName.getName() + " 12 Tf 0 g";
> acroForm.setDefaultAppearance(da);
> 
> But I am getting below exception
> java.io.IOException: Could not find font: /CourierNewPSMT
> at
> org.apache.pdfbox.pdmodel.interactive.form.PDAppearanceString.getFont(PDAppearanceString.java:126)

the reason is very likely that the field you are trying to fill has a default appearance where the font is set to CourierNewPSMT. You need to also set the default appearance for the field to use the loaded font.

If you could upload your file to a public location I can take a closer look.

> 
> I saw a similar ticket - https://issues.apache.org/jira/browse/PDFBOX-2848
> 
> *My questions:*
> 1. Does PDFBox 2.0 supports Unicode characters values to be set on an
> existing PDF file which has PDTextField using setValue(String value) of the
> PDTextField? I saw an example where Unicode characters are supported but
> that PDF is created from scratch and it did not have AcroForms.

Unicode for field values is supported as of PDFBox 2.0

> 
> 2. Will the .ttf file currently in sources of /
> pdfbox/src/main/resources/org/apache/pdfbox/resources/ttf/ be packaged with
> the final PDFBox jar? or the developers have to load it externally?

Yes,

BR
Maruan

> 
> Appreciate your help.
> 
> Thanks,
> Rakshit


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org