简体   繁体   中英

In iOS, how can you programmatically fill out a pdf form field?

I need to take an existing pdf file and programmatically fill in a list of form fields with text and then save the pdf without ever displaying it to the user.

For instance, if the pdf file contains fields named "LastName", and "FirstName" I would like to set the value of "FirstName" to "Louis" and then save the file.

I've been searching for a long time and can't find any guidance on even where to start since the iOS documentation (and most of the questions on here) seem geared towards displaying or creating pdf content instead of modifying it.

EDIT:
My main question is: Is it possible to open a pdf stream (I know how to do this) and copy each existing pdf dictionary item into a new pdf? I have not been able to find a way to write the dictionary items to a pdf.

I doubt that kind of functionality will ever be in the iOS frameworks. The reason most of the related info you can find "seem[s] geared towards displaying or creating pdf content instead of modifying it" is because that's what the vast majority of use cases will want or need for PDF functionality.

You'll need to find a 3rd party library that can open up PDFs, fill out the AcroForm fields, and then stamp out a PDF. I use iText on Java (there is also iTextSharp for C#) but I don't know of anything for Objective-C.

Once you find that library, you'll need to integrate it into your project. There are undoubtedly several related questions/answers here on SO for whatever version of the SDK you're using.

This would be easier to do with a HTML page. If you wish to use a HTML page instead of a .pdf form then thius is how you would go about doing it:

[field1 setText:@"Field 1 Text"];
[field2 setText:@"Field 2 Text"];

NSString *result;
result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"$('#field1').val('%@');", field1.text]];
result = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"$('#pfield2').val('%@');", field2.text]];
result = [webView stringByEvaluatingJavaScriptFromString:@"$('#submitbutton').click();"];

You would need to create two UILabels or UITextFields and call them "field2" and "field2" in your .h file. You can then find the ID of the field you need to replace eg #field1 and then put it where I have put "#field1" and again for the second field where I have put "#field2". There also needs to be a UIWebView with the page already loaded. This code is to be used after the UIWebView page has been loaded. Maybe do the following:

-(void)webViewDidFinishLoad:(UIWebView *)webView {

// Insert above code here

}

You probably need a full understanding of Javascript if you want to do this for the whole form, but this should get you started.

Hope that helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM