Filling multiple fields in PDF form using PDFBOX and Lock editing of pdf document after filling - pdfbox

I am new to PDF BOX, need to fill the information in PDF Form, which has sections and field names:
PDF form contains information of
NAME
and box to fill the information
Address
and box to fill the information
City
box to fill the information.
if i have the Name, Address and city information how to fill that into the pdf form using pdf box.
Have used the example from PDFbox, acroForm only fills sample fields how to get this filled with particular information at particular field.
once after filling it how to make it locked.
String formTemplate = "C:/FillFormField.pdf";
PDDocument pdfDocument = PDDocument.load(new File(formTemplate))
// get the document catalog
PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
// as there might not be an AcroForm entry a null check is necessary
if (acroForm != null)
{
// Retrieve an individual field and set its value.
PDTextField field = (PDTextField) acroForm.getField( "sampleField" );
field.setValue("Text Entry");
// If a field is nested within the form tree a fully qualified name
// might be provided to access the field.
field = (PDTextField) acroForm.getField( "fieldsContainer.nestedSampleField" );
field.setValue("Text Entry");
}
// Save and close the filled out form.
pdfDocument.save("C:/FillFormField.pdf");
Currently dont have errors above code is working to fill out some junk fields.
need to fill exactly the Name with box info with Name information
Address with box info with Address information

I am able to do it with PDF Debugger from PDFBOX.
Used CMD prompt to get the structure of the file
java -jar pdfbox-app-1.8.10.jar PDFDebugger yourfile.pdf
please get the above jar file from apache PDF box.
once you able to see the structure, find the fields name.

For my purpose of locking, i dont need to use the encryption method. so used Acroform Flatten PDF method.
// flatten() method saves the PDF read only
acroForm.flatten();

Related

Office Addin Custom Attributes on Range Text

I would like to know if it's possible to add additional attributes to simple Range text or Paragraph text from my Word Add-in. For simplicity see code below where I can populate the document with text but additionally i would like to store additional but behind the scenes info along with that text. Ultimately I want these read these custom attributes using the Open Xml SDK when these documents go through a processing stage.
private void AddAttributedContent(string documentContent)
{
var doc = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
var range = doc.Range();
range.Font.Size = 12;
range.Font.Name = "Calibri";
range.Underline = Word.WdUnderline.wdUnderlineSingle;
range.Text = documentContent;
// range.AddOpenXmlProperty("MyProp", "MyValue");
var para = doc.Paragraphs.Add();
para.Range.Text = documentContent;
//para.AddCustomProperty("MyProp", "MyVal");
}
Edit:
Ideally our property would sit inside of the resulting RunProprties :
No, you can't do that. You could probably do it with a content control or a text box though.
For a ContentControl, you could probably use Tag (if this is like sources, it probably has to be unique, though the description seems to imply it's meant for the purpose you need) or Title.
The Tag property is different from the Title property in that a tag is never displayed while a user edits a document. Instead, developers can use it to store a value for programmatic manipulation while the document is opened.
For a text box (a Shape), you could use AlternativeText or Title.
Note that altering Title (in both cases) or AlternativeText will probably affect the way the document is displayed if you save it as HTML.

ABC PDF version 6.0 Form Fields Read Only

We have a VB.NET program that is using Supergoo's ABCPDF version 6.1.1.2. Our program takes standard XML strings and places values in a corresponding PDF form field on the template PDF.
Problem:
We have over 3000 PDF files that have all been "tagged" with form fields. There could be up to 50 form fields on the template PDFs for a total of roughly 150,000 form fields in use potentially. We have noticed that some of these form fields have their form field common properties set to hidden by mistake. (see screenshot)
The issue is that the PDF coming back after the string values have been added are not showing up. Duh right? Fix the form field property and call it done. However, there is no way to know how many other of the other 150,000 form fields have been improperly tagged like this.
Does anyone know if I can adjust the PDF generation program to forcefully ignore that form field common property? Here is a sample of the vb.net code I am hoping to slightly alter...
Dim theDoc As Doc = New Doc
theDoc.Form.Fields("SampleFieldName").?????? 'can we set something here to ignore the hidden property?
According to the docs at
http://www.websupergoo.com/helppdfnet/source/6-abcpdf.objects/field/2-properties/page.htm
The .Page property of a Field object will tell you the page the field is on. Since Page is a class, if the result 'Is Nothing' then you know that the field is not visible since it doesn't appears on any page in the PDF document.
Please note that there are a few caveats when using fields that are not hidden but not actually visible when rendered (being too small, being spread on two pages, etc). If you need need to handle that you may be interested in http://www.websupergoo.com/helppdfnet/source/6-abcpdf.objects/field/2-properties/rect.htm depending on your use cases.
For the ABCPDF v6 software, I have discovered through Mihai's suggestion that it is possible. I have coded this C# example in the hopes that it helps someone down the road...
static void SetFillableFieldsToWriteableExample(string origFileLocation, string newFileLocation)
{
Doc theDoc = new Doc();
theDoc.Read(origFileLocation);
var theFields = theDoc.Form.GetFieldNames();
foreach (string theField in theFields)
{
Field theFieldInstance = theDoc.Form[theField];
theDoc.SetInfo(theFieldInstance.ID, "/F", "4");
}
theDoc.Save(newFileLocation);
}
I have tested this and it works when all fields are text fields on the PDF. Not sure on the other field types.
This code should not be used in a production environment as written here. There is no guarantee that the origFileLocation or newFileLocation references a PDF and no error handling among other issues. This is for demonstration purposes only.

Filling pdf fields with Chinese characters garbled

I am trying to fill a pdf field with Chinese characters from an fdf or xfdf.
So far I have tried, pdftk, mcpdf, pdfbox and fpdm.
They can all get the characters into the field, but they don't display. When I click on the field to edit, the characters show as expected, but when I click out of the field again they disappear. If I input English they are displayed incorrectly, eg "hello" becomes "IFMMP".
This has all lead me to suspect it's an issue with fonts/character maps, I have tried embedding the full font into the pdf and it made no difference. I have installed the fonts on the machine to no avail.
If I edit the pdf and fill the field in Acrobat it accepts the Chinese characters without a problem and I can view the pdf in a reader. I have tried using pdftk from the command line on the same Windows machine and I am having the same problem.
I need this to work in a Linux environment, and preferably in python or through a command-line script, but really at this point I'd just like to see it work at all! I have attached the sample pdf, fdf, xfdf and the output it is creating, any help would be greatly appreciated as I've run out of ideas. I have been using the command:
"pdftk test_form.pdf fill_form test.xfdf output output.pdf verbose"
https://drive.google.com/folderview?id=0B6ExNaWGFzvnfnJHSC1ZdXhSU2RQVENjYW56UkZyYWJMdWhZTkpQYkZBcUs0Tjhjb0NITVE&usp=sharing
When a form field is filled the fields value is populated and (optional) a visual appearance for the form field is generated reflecting the newly set value. So the reason that you are seeing the value when you click into the form field is that the fields value will be displayed but as long as the field is not activated the fields appearance is used.
If you tried setting the value with PDFBox 1.8 you might try using PDFBox 2.0 as this now has unicode support and the appearance generation is redone.
You also need to ensure that the font you are using in the form is available on the system you are filling your form with. Otherwise with PDFBox 2.0 you might get an error message similar to
Warning: Using fallback font 'TimesNewRomanPSMT' for 'MingLiU'
Exception in thread "main" java.lang.IllegalArgumentException: No glyph for U+5185 in font MingLiU
Which is as MingLiU is not available on the system it has been replaced by TimesNewRomanPSMT which doesn't have the character needed.
As another solution you can also direct the Adobe Reader to calculate the appearance for you when the form is opened using
PDAcroForm form = doc.getDocumentCatalog().getAcroForm();
form.setNeedAppearances(true);
again using PDFBox 2.0
I've created a little sample using PDFBox 2 but creating a form from scratch to test if it can handle the Chinese text
// create a new PDF document
PDDocument doc = new PDDocument();
PDPage page = new PDPage();
// add a new AcroForm and add that to the document
PDAcroForm form = new PDAcroForm(doc);
doc.getDocumentCatalog().setAcroForm(form);
// Add and set the resources and default appearance at the form level
PDFont font = PDType0Font.load(doc, new File("/Library/Fonts/Arial Unicode.ttf"));
PDResources res = new PDResources();
COSName fontName = res.add(font);
form.setDefaultResources(res);
String da = "/" + fontName.getName() + " 12 Tf 0 g";
form.setDefaultAppearance(da);
// add a page to the document
doc.addPage(page);
// add a form field to the form
PDTextField textBox = new PDTextField(form);
textBox.setPartialName("Chinese");
form.getFields().add(textBox);
// specify the annotation associated with the field
// and add it to the page
PDAnnotationWidget widget = textBox.getWidget();
PDRectangle rect = new PDRectangle(100f,300f,120f,350f);
widget.setRectangle(rect);
page.getAnnotations().add(widget);
// set the field value
textBox.setValue("木兰辞");
doc.save("ChineseOut.pdf");
which works fine. I also tested with the font you are using unfortunately this had an error as MingLiU is a TrueType collection which PDFBox can not handle at that point in time.

Automatic calcul fields don't work with iText

I have an interactive pdf form that contains totals field, those fields fulfilling automatically when other fields are filled by users, and it works very well manually. but when I fill it programmatically using iText with java, the normal one fills very well but those with the automatic calculation does not work and shows zero.
static String book = "myForm.pdf";
static String bookstamp = "myFormTest.pdf";
PdfReader reader = new PdfReader (book);
PdfStamper stamper = new PdfStamper (reader, new FileOutputStream (String.format (bookstamp)));
stamper.getAcroFields ();
stamper.setField ("ca50", "15000");
stamper.getAcroFields () ;
stamper.setField ("ca30", "150");
stamper.close ();
Automatic calculation of field values in PDFs happens by means of Javascript in the PDF. In case of certain events (e.g. value of a field changed or focus of a field lost) this Javascript is executed and sets the field value to the newly calculated one.
iText, on the other hand, does not run any Javascript. In iText only those parts of the PDF are changed which you explicitly change, side effects are not desired.
Thus, to show appropriately re-calculated fields upon opening your PDF after editing it with iText, you should also trigger the re-calculation of the fields upon opening the PDF.
If you provide the PDF file itself, you can be shown how to do that.
PS: You can trigger re-calculation when the document is opened by adding
stamper.addJavaScript("this.calculateNow();");
reader.removeUsageRights();
before
stamper.close ();
Unfortunately this will disable the Reader-Enabling of the document.

How to get fillable PDF to prompt for required fields when user saves?

I've created a fillable PDF form using Acrobat 10. I see where you can set form fields to be required, but all that does is outline the fields in red.
How can I get a prompt asking users to enter data in the required fields when they hit save?
You need to use Adobe-JavaScript if you want to achieve this.
Read this page in the Adobe documentation for more details:
Enforcing Required Fields
From that page, the code you have to include in your PDF file will look like this:
f = getField(event.target.name)
if (f.value.length == 0)
{
f.setFocus()
//Optional Message - Comment out the next line to remove
app.alert("This field is required. Please enter a value.")
}
More details about the pop-up alert can be found here:
https://acrobatusers.com/tutorials/popup_windows_part1