It's certainly possible to do it with a script, but I'm not sure if it's possible to do it directly from Excel. My guess would be that it's not.
A barcode field in a PDF can be either a Barcode-type field, or it can be a plain-text field with a barcode font (typically 3-of-9) associate with it.
Basically you need to use the addField method of the Doc object to add the form field, and then you set its properties to what you want (for example, textFont to set the font used by a text field).
I've been through the JS API and am somewhat familiar with addField. As you probably know, the field types that can be added via addField do not include the barcode field, although I'm sure there is a "good" reason it's not supported. I was just hoping it was buried somewhere else and I just wasn't finding it.
I'll play around with doing what I need via a text field, but it was be so much more elegant and efficient if the bar code field was supported by addField.
It's not documented, but you can add a Barcode field using the addField
method. Just specify the cFieldType parameter as "barcode"... The rest of
the parameters are the same.
If you are correct, and I'm sure you are, you have just made my life better. What a thing to leave undocumented Adobe.
Thank you again and have a great week!
Thanks again to Gilad D for pointing out the undocumented addField functionality.
When I manually check the properties on the new barcode field, none of the properties (symbology, etc.) have been set as they are when you add a barcode field manually. No defaults roll in when one does thie via script. The field is basically a dead, grey box on the page.
Does anyone know where these properties are documented so I can address them? The JS API has no information and guessing at them seems like a hopeless task.
Yeah, I was afraid of that... This is probably the reason why it's not documented, because it's not fully implemented (yet?).
So I'm probably back to using a text field and a 3-of-9 font, which might work for what I'm doing if I can find one that will install. Working on that.
The biggest problem with 2D barcode fields is they don't work with Reader unless the file is specially enabled with LiveCycle Reader Extensions with the corresponding usage right, so that would have been your next hurdle.
Good points. That would certainly be my next obstacle if I used 2D. All I'm trying to do is add an identifying barcode to the top of my documents so they can be automatically filed when they come back. Using text field and a Code39 font is far more simple and probably the way to go.
Can't wait to see what gets in the way during Phase II, building the piece that picks them up and puts them away. Off the shelf might be a option, but anyone can do that. Ha.
Thanks for responding.
Another thing to consider is to generate the barcode using another application and then insert it to the PDF as an image.
Great suggestion. An aquaintance actually emailed me that same suggestion late last night. Specifically, since my PDFs are generated on the fly from a set of MS Word templates, he suggested I create a static PDF with a barcode field on it and then copy that field from that static PDF after setting the correct value.
Not sure it'll let me do that, but I will give it a go and post back.
George's points cause me to reconsider (and use) a 1D solution, but I'd still like to figure this 2D thing out.
So I think I have a workable strategy. And one that I can waste away the rest of my week on if I'm not careful.
I created a simple PDF that contains 2 fields; a 2D barcode field and a text field. I added the text field because it seems the simplest way to populate the 2D barcode field is to tie it to a text field. I see no other way to "set" its value. I also set the barcode field to PDF417 symbology.
So then I have my code open this existing PDF, set the value of the text field to whatever I want and save the file, which in turn causes (I believe) the barcode field to resolve itself to the text field value. Simple enough and it works just fine.
Now I would like to copy (via js script) the barcode field noted above to a newly created PDF. I think I may also have to copy the associated text field to the new PDF (and hide it) so the barcode field won't lose its value. Seems like the long way around the barn, but maybe not if it results in getting this 2D barcode into my "created-on-the-fly" PDFs with the correct value.
I promise I searched the API (and the forums, etc.) before asking this but, can anyone help me with the syntax of copying (and maybe pasting) a field from one document to another? I'm not asking anyone to write my code, just get me started in the write (see what I did there?) direction.
All I can seem to find is related to copying field "values". I need to copy the entire "field". And to a specific location.
Thank you Gentlemen.
You can't directly copy a field using code. You can create a field (using
addField) and then copy the properties of the old field into the new one,
but you can't copy the field object itself.
Well, that's not good news since I doubt I'll be able to copy the barcode properties anymore than I can set them in the first place. And if I could set the field properties I wouldn't need to copy the field from somewhere else.
Back to square One-D.
Thanks for the clarification.
Weren't you going to use images instead of a barcode field?
Sigh! I didn't mean to post that without more editing. This is what I meant to say:
You'll just need to set up the button that you create with the appropriate properties, such as "Icon Only" layout, read-only, no background/border color, etc.
Yes, I think the image method may be the way to go. I just really wanted to figure this out.
What George suggested is interesting and reads somewhat like the "image" suggestion.
Will the PDF file that becomes the button icon need to travel with the PDF containing the icon? I'm guessing (and hoping) not.
And will the barcode field need to be the size of the entire page? I assume it will and I'll no doubt get an answer to both in the process of trying this out.
Once an image has been set as the icon of a button it becomes embedded in the document and you don't need to have the source file anymore.
No, it won't need to travel with the PDF containing the button, and you can make the button any size you want, though it will need to be large enough for the bar code to scan successfully.
Understood. The button size I get. I was curious about the size of the barcode field in the PDF I'm going to use the button icon so that there isn't a bunch of white space. I'll stop wondering and just go do it.
I don't mean to wear you guys out but, this button icon method is pretty slick.
I've got to mess around with the scaling to get it just, but this appears to be a really nice solution.
Back later with the results. I know you'll be waiting anxiously!
Yes, buttons provide a solution for all types of tricky problems. Some suggestions:
buttonFitBounds property to true
buttonScaleHow to proportionally so that the bar code Y/X ratio is maintained
buttonScaleWhen so that the bar code is always show with the maximum amount of text it will be encoding and the bar code X dimension is maintained.
George (or others):
I'm trying to fully understand this button icon method, but I'm getting conflicting results. If you will, please help clarify:
When I create the PDF document that will become the button icon, all I have on the "page" is a reasonably-sized PDF417 barcode field, about 1/2 inch x 4 inches, and I have it centered. When I identify this document as the button icon in another PDF and set the properties of that button as you suggested (or any other way for that matter) the image is indeed there on the button face, but is very small. I would have expected Acrobat to fill the button face with the identified icon PDF using the limits of that documents content, if that makes sense.
When I use another regular PDF document as the icon image that is exactly what happens. It puts all the content on the button face. Any idea why it would not do this for my barcode-holding PDF?
Should I set up the document holding the barcode as something other than 8.5 x 11, or should that even matter?
Just crop the page to the extent of the bar code field.
So I lost a few days, but finally found time to try and sort this out. I put the code below in the console and it all seems to work except the buttonSetIcon line, which is the last line. I know this isn't as elegant as it could be (yet), I'm just trying to get it to work. My intent is to make this a folder-level script in the end.
Can anyone tell me where I'm going wrong? Is it a privilege issue?
var bcCoords = ["324", "756", "540", "720"];
var iconFileName = "/C/_Holding Pen/Icon.pdf";
var t = this.addField("bcFormID", "button", 0, bcCoords);
t.display = display.visible;
t.buttonPosition = position.iconOnly;
t.buttonScaleHow = scaleHow.proportional;
t.buttonScaleWhen = scaleWhen.always;
t.buttonFitBounds = true;
var x = this.importIcon("myIcon", iconFileName, 0);
var f = this.getField("bcFormID");
var i = this.getIcon("myIcon")
var y = f.buttonSetIcon(i);
Thanks for any help you can provide.
First off, the rect property needs to be an array of numbers, not strings.
Wow, thikans. Rookie mistake. Oddly, it puts the button field in the right place at the right size.
It probably makes the conversion behind the scenes.
So what exactly is going wrong? Does it create the button? Is the Icon object created?
Also, from which context are you running this code?
So what is the return value of the importIcon call? It would be simpler to just use the buttonImportIcon field method rather than creating a document icon and trying to use it.
I'm presently running this in the console. As best as I can tell, all is well until the last line. When I print the icon object (if I'm writng that right) to the console I get an affirmative response, and when I comment out the last line I get a "true", which I also assumed to be confirmation that all the other lines executed successfully. So I will say "yes", the icon object gets created. And the button field is there as well with the properties set correctly. Just no icon.
The return value of the icon call is "0", No Error. And as I wrote above, I can "print" it to ther console and see an object. I agree that the buttonImportIcon method would be simpler, but I couldn't seem to get that to work in the wee hours of last night either. I'll give it another shot.
But, based on how I read the JS API, won't I be prohibited from executing this from a folder level script, or can I do so as long as make it a trusted function?
Yes, you can place it in a trusted function and it will work fine. The general problem with creating a document icon and then a button icon is it needlessly increases the file size since it means two separate icon are created in the document instead of just one. This probably isn't much of a concern for the type of icons you're using though as they should be fairly small.
I just tested your exact code (after correcting the array and specifying a different PDF), and it works just fine, at least in Acrobat Pro 9.5.5.
And Acrobat Pro 11.0.06
Thanks George. I'll try it with another PDF and see what happens on this end. And thanks for the heads-up on the duplicative icon.
Well, all I did was use a different PDF and it works for me too. Go figure. Now I just need to figure out what's wrong with the PDF holding the barcode image. Thanks again.
Probably time to wrap up this thread but, I flattened the original PDF holding the barcode and it works with that one too. George actually wrote than in his June 4, 3:16pm post and it just flew by me. Sorry.
All is well. Thanks again!