You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by "Wojack, Robert (USTP)" <Ro...@usdoj.gov> on 2015/12/29 21:45:59 UTC

How to trim whitespace from options in a combobox on an existing PDF form?

We have a client that provides us with interactive PDF forms for our app to populate fields with user data.

The problem is the client sometimes provides us forms with typos and is not responsive about fixing them. In this case, we are given a form with a combo box of options, and some of the options accidentally have trailing or leading whitespace. So when we attempt to select an option using the setValue(String optionValue) method, it fails to match (i.e., "Maine" fails to match the "Maine " option on the form).

As a workaround, we can add the extraneous whitespace to induce a match for these occasional goofs, but of course that is extremely hacky. Is there a way to trim all of the whitespace from all of the option values in an existing PDF before attempting to set the value?

We are using PDFBox 1.2.1, but we are open to upgrading to a newer version.

Thanks

Bob Wojack


Re: How to trim whitespace from options in a combobox on an existing PDF form?

Posted by Gilad Denneboom <gi...@gmail.com>.
Glad to hear you were able to solve it!

On Thu, Dec 31, 2015 at 5:44 PM, Wojack, Robert (USTP) <
Robert.Wojack@usdoj.gov> wrote:

> Nevermind. I found that I can just pass the hard-coded string "Opt".
>
> Thanks for your help, Gilad. It was exactly what I needed.
>
>
> Bob Wojack
> SRA International, Inc.
> Onsite Contractor for the Department of Justice
> Senior Tech Lead
> US Trustee Program
> Onsite Email Address:     Robert.Wojack@usdoj.gov
> Onsite Phone Number:    TBD
> Cell#:                     (301)910-9975
>
>
> -----Original Message-----
> From: Wojack, Robert (USTP) [mailto:Robert.Wojack@usdoj.gov]
> Sent: Thursday, December 31, 2015 11:05 AM
> To: users@pdfbox.apache.org
> Subject: RE: How to trim whitespace from options in a combobox on an
> existing PDF form?
>
> Hi Gilad,
>
> Thanks for your prompt response. It seems as though the const,
> COSName.OPT, is not available in PDFBox 1.2, so I assume that we would
> still need to upgrade to PDFBox 1.8 even to roll our own getOptions(),
> correct?
>
> Bob Wojack
> SRA International, Inc.
> Onsite Contractor for the Department of Justice Senior Tech Lead US
> Trustee Program Onsite Email Address:     Robert.Wojack@usdoj.gov Onsite
> Phone Number:    TBD
> Cell#:                     (301)910-9975
>
> -----Original Message-----
> From: Gilad Denneboom [mailto:gilad.denneboom@gmail.com]
> Sent: Tuesday, December 29, 2015 4:56 PM
> To: users@pdfbox.apache.org
> Subject: Re: How to trim whitespace from options in a combobox on an
> existing PDF form?
>
> You're using a very old version of PDFBox. In later versions a method was
> added to the PDChoiceField class called getOptions. You can use it to
> return a COSArray with the options for that field, and then iterate through
> them (trimming them as you do) and comparing them to your string. You can
> also write your own method to write new options for your field, based on
> the code of getOptions, which is basically just this line:
>
> return (COSArray) getDictionary().getDictionaryObject(COSName.OPT);
>
>
> On Tue, Dec 29, 2015 at 9:45 PM, Wojack, Robert (USTP) <
> Robert.Wojack@usdoj.gov> wrote:
>
> > We have a client that provides us with interactive PDF forms for our
> > app to populate fields with user data.
> >
> > The problem is the client sometimes provides us forms with typos and
> > is not responsive about fixing them. In this case, we are given a form
> > with a combo box of options, and some of the options accidentally have
> > trailing or leading whitespace. So when we attempt to select an option
> > using the setValue(String optionValue) method, it fails to match
> > (i.e., "Maine" fails to match the "Maine " option on the form).
> >
> > As a workaround, we can add the extraneous whitespace to induce a
> > match for these occasional goofs, but of course that is extremely
> > hacky. Is there a way to trim all of the whitespace from all of the
> > option values in an existing PDF before attempting to set the value?
> >
> > We are using PDFBox 1.2.1, but we are open to upgrading to a newer
> version.
> >
> > Thanks
> >
> > Bob Wojack
> >
> >
>  B KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKCB
>   [  X  ܚX K  K[XZ[
>   \ \  ][  X  ܚX P
>  \ X  K ܙ B  ܈ Y  ] [ۘ[    [X[     K[XZ[
>   \ \  Z [
>  \ X  K ܙ B
>

RE: How to trim whitespace from options in a combobox on an existing PDF form?

Posted by "Wojack, Robert (USTP)" <Ro...@usdoj.gov>.
Nevermind. I found that I can just pass the hard-coded string "Opt". 

Thanks for your help, Gilad. It was exactly what I needed.


Bob Wojack
SRA International, Inc.
Onsite Contractor for the Department of Justice
Senior Tech Lead
US Trustee Program
Onsite Email Address:     Robert.Wojack@usdoj.gov
Onsite Phone Number:    TBD
Cell#:			   (301)910-9975


-----Original Message-----
From: Wojack, Robert (USTP) [mailto:Robert.Wojack@usdoj.gov] 
Sent: Thursday, December 31, 2015 11:05 AM
To: users@pdfbox.apache.org
Subject: RE: How to trim whitespace from options in a combobox on an existing PDF form?

Hi Gilad,

Thanks for your prompt response. It seems as though the const, COSName.OPT, is not available in PDFBox 1.2, so I assume that we would still need to upgrade to PDFBox 1.8 even to roll our own getOptions(), correct?

Bob Wojack
SRA International, Inc.
Onsite Contractor for the Department of Justice Senior Tech Lead US Trustee Program Onsite Email Address:     Robert.Wojack@usdoj.gov Onsite Phone Number:    TBD
Cell#:			   (301)910-9975

-----Original Message-----
From: Gilad Denneboom [mailto:gilad.denneboom@gmail.com]
Sent: Tuesday, December 29, 2015 4:56 PM
To: users@pdfbox.apache.org
Subject: Re: How to trim whitespace from options in a combobox on an existing PDF form?

You're using a very old version of PDFBox. In later versions a method was added to the PDChoiceField class called getOptions. You can use it to return a COSArray with the options for that field, and then iterate through them (trimming them as you do) and comparing them to your string. You can also write your own method to write new options for your field, based on the code of getOptions, which is basically just this line:

return (COSArray) getDictionary().getDictionaryObject(COSName.OPT);


On Tue, Dec 29, 2015 at 9:45 PM, Wojack, Robert (USTP) < Robert.Wojack@usdoj.gov> wrote:

> We have a client that provides us with interactive PDF forms for our 
> app to populate fields with user data.
>
> The problem is the client sometimes provides us forms with typos and 
> is not responsive about fixing them. In this case, we are given a form 
> with a combo box of options, and some of the options accidentally have 
> trailing or leading whitespace. So when we attempt to select an option 
> using the setValue(String optionValue) method, it fails to match 
> (i.e., "Maine" fails to match the "Maine " option on the form).
>
> As a workaround, we can add the extraneous whitespace to induce a 
> match for these occasional goofs, but of course that is extremely 
> hacky. Is there a way to trim all of the whitespace from all of the 
> option values in an existing PDF before attempting to set the value?
>
> We are using PDFBox 1.2.1, but we are open to upgrading to a newer version.
>
> Thanks
>
> Bob Wojack
>
>
B KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKCB  [  X  ܚX KK[XZ[
 \ \  ][  X  ܚX P   
 \X K ܙ B  ܈Y][ۘ[  [X[  K[XZ[
 \ \  Z[   
 \X K ܙ B 

RE: How to trim whitespace from options in a combobox on an existing PDF form?

Posted by "Wojack, Robert (USTP)" <Ro...@usdoj.gov>.
Hi Gilad,

Thanks for your prompt response. It seems as though the const, COSName.OPT, is not available in PDFBox 1.2, so I assume that we would still need to upgrade to PDFBox 1.8 even to roll our own getOptions(), correct?

Bob Wojack
SRA International, Inc.
Onsite Contractor for the Department of Justice
Senior Tech Lead
US Trustee Program
Onsite Email Address:     Robert.Wojack@usdoj.gov
Onsite Phone Number:    TBD
Cell#:			   (301)910-9975

-----Original Message-----
From: Gilad Denneboom [mailto:gilad.denneboom@gmail.com] 
Sent: Tuesday, December 29, 2015 4:56 PM
To: users@pdfbox.apache.org
Subject: Re: How to trim whitespace from options in a combobox on an existing PDF form?

You're using a very old version of PDFBox. In later versions a method was added to the PDChoiceField class called getOptions. You can use it to return a COSArray with the options for that field, and then iterate through them (trimming them as you do) and comparing them to your string. You can also write your own method to write new options for your field, based on the code of getOptions, which is basically just this line:

return (COSArray) getDictionary().getDictionaryObject(COSName.OPT);


On Tue, Dec 29, 2015 at 9:45 PM, Wojack, Robert (USTP) < Robert.Wojack@usdoj.gov> wrote:

> We have a client that provides us with interactive PDF forms for our 
> app to populate fields with user data.
>
> The problem is the client sometimes provides us forms with typos and 
> is not responsive about fixing them. In this case, we are given a form 
> with a combo box of options, and some of the options accidentally have 
> trailing or leading whitespace. So when we attempt to select an option 
> using the setValue(String optionValue) method, it fails to match 
> (i.e., "Maine" fails to match the "Maine " option on the form).
>
> As a workaround, we can add the extraneous whitespace to induce a 
> match for these occasional goofs, but of course that is extremely 
> hacky. Is there a way to trim all of the whitespace from all of the 
> option values in an existing PDF before attempting to set the value?
>
> We are using PDFBox 1.2.1, but we are open to upgrading to a newer version.
>
> Thanks
>
> Bob Wojack
>
>

Re: How to trim whitespace from options in a combobox on an existing PDF form?

Posted by Gilad Denneboom <gi...@gmail.com>.
You're using a very old version of PDFBox. In later versions a method was
added to the PDChoiceField class called getOptions. You can use it to
return a COSArray with the options for that field, and then iterate through
them (trimming them as you do) and comparing them to your string. You can
also write your own method to write new options for your field, based on
the code of getOptions, which is basically just this line:

return (COSArray) getDictionary().getDictionaryObject(COSName.OPT);


On Tue, Dec 29, 2015 at 9:45 PM, Wojack, Robert (USTP) <
Robert.Wojack@usdoj.gov> wrote:

> We have a client that provides us with interactive PDF forms for our app
> to populate fields with user data.
>
> The problem is the client sometimes provides us forms with typos and is
> not responsive about fixing them. In this case, we are given a form with a
> combo box of options, and some of the options accidentally have trailing or
> leading whitespace. So when we attempt to select an option using the
> setValue(String optionValue) method, it fails to match (i.e., "Maine" fails
> to match the "Maine " option on the form).
>
> As a workaround, we can add the extraneous whitespace to induce a match
> for these occasional goofs, but of course that is extremely hacky. Is there
> a way to trim all of the whitespace from all of the option values in an
> existing PDF before attempting to set the value?
>
> We are using PDFBox 1.2.1, but we are open to upgrading to a newer version.
>
> Thanks
>
> Bob Wojack
>
>