Within this article, we'll demonstrate how to create a PDF document including a table and extract its data using Java code. It's important to note that I used a third-party library called Spire.PDF forJava to complete the operations above.
DEPENDENCY
Before
coding, we need to add the Spire.PDF.jar file as a dependency in Java program.
The JAR file can be downloaded from this link. Or we can easily import the JAR
file by adding the following code to the pom.xml file in Java project.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.pdf</artifactId>
<version>4.10.2</version>
</dependency>
</dependencies>
Create a PDF Document and add a Table to it
Spire.PDF for Java supports creating a blank PDF document using PdfDocument class and add a table to a specified
page of PDF using PdfTable.draw(PdfPageBase.new Point2D.Float(x,y)) method. In the process of adding a table,
we can set its title, cell padding, and so on. Full code samples can be seen as below.
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.tables.*;
import java.awt.*;
import java.awt.geom.Point2D;
public class CreateTable {
public static void main(String[] args) {
//Create a PdfDocument instance
PdfDocument doc = new PdfDocument();
//Set margins
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.setTop(unitCvtr.convertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point));
margin.setBottom(margin.getTop());
margin.setLeft(unitCvtr.convertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point));
margin.setRight(margin.getLeft());
//Add one page
PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);
float y = 10;
//Draw title
PdfBrush brush1 = PdfBrushes.getBlack();
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial",Font.BOLD ,16));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.getCanvas().drawString("Country List", font1, brush1, page.getCanvas().getClientSize().getWidth() / 2, y, format1);
y = y + (float) font1.measureString("Country List", format1).getHeight();
y = y + 5;
//Data source to create table
String[] data = {"Name;Capital;Continent;Area;Population", "Argentina;Buenos Aires;South America;2777815;32300003", "Bolivia;La Paz;South America;1098575;7300000", "Brazil;Brasilia;South America;8511196;150400000", "Canada;Ottawa;North America;9976147;26500000", "Chile;Santiago;South America;756943;13200000", "Colombia;Bagota;South America;1138907;33000000", "Cuba;Havana;North America;114524;10600000", "Ecuador;Quito;South America;455502;10600000", "El Salvador;San Salvador;North America;20865;5300000", "Guyana;Georgetown;South America;214969;800000", "Jamaica;Kingston;North America;11424;2500000", "Mexico;Mexico City;North America;1967180;88600000", "Nicaragua;Managua;North America;139000;3900000", "Paraguay;Asuncion;South America;406576;4660000", "Peru;Lima;South America;1285215;21600000", "United States of America;Washington;North America;9363130;249200000", "Uruguay;Montevideo;South America;176140;3002000", "Venezuela;Caracas;South America;912047;19700000"};
String[][] dataSource = new String[data.length][];
for (int i = 0; i < data.length; i++) {
dataSource[i] = data[i].split("[;]", -1);
}
//Create a PdfTable instance and set data source
PdfTable table = new PdfTable();
table.getStyle().setCellPadding(2);
table.getStyle().setHeaderSource(PdfHeaderSource.Rows);
table.getStyle().setHeaderRowCount(1);
table.getStyle().setShowHeader(true);
table.setDataSource(dataSource);
//Draw table to the page
PdfLayoutResult result = table.draw(page, new Point2D.Float(0, y));
y = y + (float) result.getBounds().getHeight() + 5;
//Draw string below table
PdfBrush brush2 = PdfBrushes.getGray();
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 0,9));
page.getCanvas().drawString(String.format("* %1$s countries in the list.", data.length - 1), font2, brush2, 5, y);
//Save the file
doc.saveToFile("output/CreateTable.pdf");
}
}The following screenshot is to show the result after adding a table to PDF
Extract Table Data from PDFSpire.PDF for Java provides PdfTableExtractor.extractTable(int pageIndex) method to detect and extract tables
from PDF. The following are the steps to extract table data from PDF:
l Load a PDF sample document using PdfDocument class.
l Create a StringBuilder instance and a PdfTableExtractor instance.
l Loop through the pages in the PDF, extract tables from each page into a PdfTable array using PdfTableExtractor.extractTable(int
pageIndex) method.
l Loop through the rows and columns in each table, and then extract data from each table cell using PdfTable.getText(int
rowIndex, int columnIndex) method, finally append the data to the StringBuilder instance using StringBuilder.append() method.
l Write the data to a .txt document using Writer.write() method.
import com.spire.pdf.PdfDocument;
import com.spire.pdf.utilities.PdfTable;
import com.spire.pdf.utilities.PdfTableExtractor;
import java.io.FileWriter;
import java.io.IOException;
public class ExtractTable {
public static void main(String[] args) throws IOException {
//Load a sample PDF document
PdfDocument pdf = new PdfDocument("C:\\Users\\Test1\\Desktop\\CreateTable.pdf");
//Create a StringBuilder instance
StringBuilder builder = new StringBuilder();
//Create a PdfTableExtractor instance
PdfTableExtractor extractor = new PdfTableExtractor(pdf);
//Loop through the pages in the PDF
for (int pageIndex = 0; pageIndex < pdf.getPages().getCount(); pageIndex++) {
//Extract tables from the current page into a PdfTable array
PdfTable[] tableLists = extractor.extractTable(pageIndex);
//If any tables are found
if (tableLists != null && tableLists.length > 0) {
//Loop through the tables in the array
for (PdfTable table : tableLists) {
//Loop through the rows in the current table
for (int i = 0; i < table.getRowCount(); i++) {
//Loop through the columns in the current table
for (int j = 0; j < table.getColumnCount(); j++) {
//Extract data from the current table cell and append to the StringBuilder
String text = table.getText(i, j);
builder.append(text + " | ");
}
builder.append("\r\n");
}
}
}
}
//Write data into a .txt document
FileWriter fw = new FileWriter("output/ExtractTable.txt");
fw.write(builder.toString());
fw.flush();
fw.close();
}
}Below is a screenshot of the extracted table data