Many developers building document viewing solutions often have to load and edit PDF form field data. For programmers who use the LEADTOOLS PDF SDK or the LEADTOOLS Document Viewer SDK this is an easy task! With minimal code, developers leveraging LEADTOOLS can easily provide users an interface to load and enter data into PDF form fields and then save the PDF.
The code snippets below show how to load, edit, and save PDF form fields.
Load PDF Form Fields
function selectAndLoadFile() {
//creates an input element on the Import Document button to upload files
//into the document editor
var input = document.createElement('input');
input.type = 'file';
input.style.display = 'none';
input.accept = '.doc, .docx, .pdf, .rtf, .txt';
input.onchange = function (e) {
var files = input.files;
if (!files || !files.length)
return;
var file = files[0];
document.body.style.cursor = 'wait';
//Create the document loading options
var loadDocumentOptions = new lt.Document.LoadDocumentOptions();
loadDocumentOptions.loadFormFieldsMode = 1;
//load the document from a file using the options
lt.Document.DocumentFactory.loadFromFile(file, loadDocumentOptions)
.done((doc) => {
//set the new document in the document viewer
documentViewer.setDocument(doc);
//check to see if the document has form fields
if (doc.formFields.hasFormFields) {
console.log("This document has PDF Form Fields. Click the 'Display Field Data in the Console' to see more information")
}
else {
console.log("This document does not have PDF Form Fields")
}
});
//return the cursor to default
document.body.style.cursor = 'default';
};
input.click();
}
Export and Save PDF Form Fields
function exportPDF() {
//gets the document in the document viewer
var doc = documentViewer.document;
//prepares the document for saving
documentViewer.prepareToSave();
//saves the document to the cache
lt.Document.DocumentFactory.saveToCache(doc)
.done(function () {
//once the doc is saved to the cache, creates the printing option
var options = new lt.Document.Viewer.PrintDocumentOptions();
options.showAnnotations = true;
options.usePdfPrinting = true;
//gets the iframe HTMLElement that will "house" the printable pdf
options.parent = document.getElementById('parent');
//prints and opens the browser printer for saving/printing
documentViewer.print(options);
})
}
For more information on the LEADTOOLS Document Viewer SDK, check out our full tutorial to Load, Edit, and Export PDF Form Field Compatible PDF Files.
Free Evaluation, Free Technical Support, Free Demos, & More!
Download our FREE 60-day evaluation to test all of our features and actually program with LEADTOOLS before a purchase is even made. Gain access to our extensive documentation, sample source code, demos, and tutorials.
Our support team is here to help! Contact our support team for free technical support via live chat or email.
For pricing or licensing questions, contact our sales team via email or call us at 704-332-5532.