Skip to content

Instantly share code, notes, and snippets.

@JoelGeraci-Datalogics
Created September 1, 2015 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoelGeraci-Datalogics/e6cbd3dc06263315a9b9 to your computer and use it in GitHub Desktop.
Save JoelGeraci-Datalogics/e6cbd3dc06263315a9b9 to your computer and use it in GitHub Desktop.
This sample will populate a PDF form based on data in a MySQL database. One PDF per record will be created.
/*
* Copyright Datalogics, Inc. 2015
*/
package pdfjt.cookbook.forms;
import java.io.InputStream;
import java.net.URL;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.HashMap;
import pdfjt.util.SampleFileServices;
import com.adobe.fontengine.font.Font;
import com.adobe.internal.io.ByteReader;
import com.adobe.internal.io.ByteWriter;
import com.adobe.internal.io.InputStreamByteReader;
import com.adobe.pdfjt.pdf.document.PDFDocument;
import com.adobe.pdfjt.pdf.document.PDFOpenOptions;
import com.adobe.pdfjt.pdf.document.PDFSaveFullOptions;
import com.adobe.pdfjt.pdf.graphics.font.PDFFont;
import com.adobe.pdfjt.pdf.interactive.forms.PDFFieldList;
import com.adobe.pdfjt.pdf.interactive.forms.PDFFieldNode;
import com.adobe.pdfjt.services.ap.AppearanceService;
import com.adobe.pdfjt.services.ap.spi.APContext;
import com.adobe.pdfjt.services.ap.spi.APResources;
import com.mysql.jdbc.Statement;
/**
* This sample will populate a PDF form based on data in a MySQL database. One
* PDF per record will be created. There are 9 records.
*/
public class FillFormsFromSQL {
private static final String inputPDF = "http://dev.datalogics.com/cookbook/forms/BlankInputFormSimple.pdf";
private static final String outputDir = "cookbook/Forms/output/";
private static final String databaseConnection = "jdbc:mysql://dev.datalogics.com:3306/datalogi_northwind";
static public void main(String[] args) throws Exception {
/*
* Check if the JDBC Driver is available first.
*/
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("No MySQL JDBC Driver");
e.printStackTrace();
return;
}
/*
* Try to connect to the database before we even get the PDF file.
*/
Connection connection = DriverManager.getConnection(databaseConnection, "datalogi_demo", "4ObLvP1NXo6U9gCxmU");
if (connection != null) {
/*
* Get the PDF File
*/
InputStream fis = new URL(inputPDF).openStream();
ByteReader byteReader = new InputStreamByteReader(fis);
PDFDocument pdfDocument = PDFDocument.newInstance(byteReader, PDFOpenOptions.newInstance());
/*
* Get the field list from the interactive form object
*/
PDFFieldList fieldList = pdfDocument.getInteractiveForm().getChildren();
/*
* Get the resources necessary to generate the field appearances
* after we add the data.
*/
APResources apResources = new APResources(pdfDocument.getCosDocument().getOptions().getFontSet(),
pdfDocument.getCosDocument().getOptions().getDocLocale(),
new HashMap<Font, PDFFont>());
APContext apContext = new APContext(apResources, true, null);
/*
* Make sure the output directory is created so we can save the new
* PDF files.
*/
SampleFileServices.createDir(outputDir);
/*
* Get all the records in the Employees database
*/
Statement statement = (Statement) connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM Employees;");
/*
* Get the number of columns (fields) in the result set.
*/
ResultSetMetaData metaData = resultSet.getMetaData();
int numColumns = metaData.getColumnCount();
/*
* Iterate through the result set. If a field name matches a column
* name, populate the field with the data.
*/
while (resultSet.next()) {
for (int i = 1; i < numColumns + 1; i++) {
String fieldName = metaData.getColumnLabel(i);
PDFFieldNode pdfField = fieldList.getFieldNamed(fieldName);
if (pdfField != null) {
pdfField.setStringValue(resultSet.getString(fieldName));
}
}
/*
* Generate appearances for the new field values.
*/
AppearanceService.generateAppearances(pdfDocument, apContext, null);
/*
* Write out the new file.
*/
String outputFileName = "NewForm_" + resultSet.getString("EmployeeID") + ".pdf";
ByteWriter outputFile = SampleFileServices.getRAFByteWriter(outputDir + outputFileName);
pdfDocument.save(outputFile, PDFSaveFullOptions.newInstance());
System.out.println("Created: " + outputFileName);
}
connection.close();
} else {
System.out.println("Couldn't connect to table.");
return;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment