Creating a program that extracts data from a PDF and fills out another PDF form involves a variety of libraries and tools. Here's a simple example using Python, the PyPDF2 library for reading PDFs, and the pdfrw library for writing to PDFs.
Please note that this is a simple program and might not work with complex PDFs (especially scanned or image-based PDFs). Also, this example assumes that the PDFs are simple and the data can be extracted in a linear manner.
Python Code:
import PyPDF2
from pdfrw import PdfReader, PdfWriter, IndirectPdfDict
# Open source PDF, and read it into a PyPDF2.PdfFileReader
pdf_file = open('source.pdf', 'rb')
read_pdf = PyPDF2.PdfFileReader(pdf_file)
number_of_pages = read_pdf.getNumPages()
page = read_pdf.getPage(0)
page_content = page.extract_text()
# Assume that we have a simple PDF and we have the data in string format
data = page_content.split("\n") # split by new line
# Load the pdf to be filled
template_pdf = PdfReader('template.pdf')
# Assume that we know the fields to be filled in the form
for page in template_pdf.pages:
for index, field in enumerate(page.Annots or ()):
if '/T' in field:
key = field['/T'][1:-1]
if key in data:
field.update(
pdfrw_annot=1,
V='{}'.format(data[key])
)
# Save the filled pdf
PdfWriter().write('output.pdf', template_pdf)
This is a very basic example. In real scenarios, you might have to handle various edge cases and complex PDF structures. You also need to handle the mapping of the extracted data to the fields in the form.
More advanced and accurate results can be achieved through machine learning algorithms and Optical Character Recognition (OCR) techniques, but these are outside the scope of this simple Python script.
Please replace 'source.pdf' and 'template.pdf' with the paths to your actual files. The 'output.pdf' will be the resulting filled form. Make sure to install the required libraries using pip:
pip install PyPDF2 pdfrw