Export PDF files to Java

I have a table that retrieves data from an SQL database. I am trying to find the easiest way to export this table, as it is, to a PDF file. Nothing unusual, just a title and a table with its contents. I searched here, and also checked external packages (dokmoz, etc.), but I didn’t decide. I am new to Java and I am looking for the easiest way to export a table to pdf.

Trying to answer possible questions, this is how I fill out the table:

try { result = DBConnection.getTableContent("customers", attributes, where, null, null); DefaultTableModel model = (DefaultTableModel) searchTable.getModel(); model.setRowCount(0); for (int i = 0; i < result.size(); i++) { model.addRow(result.get(i).toArray()); } } 

thanks

+4
source share
4 answers

You can use iText PDF Api. It is quite easy to use. You just need to load the jar, import the class, and you're good to go. Check out this tutorial on how to use classes.

+7
source

I have an example code:

 public static void createSamplePDF(String header[], String body[][]) throws Exception{ Document documento = new Document(); //Create new File File file = new File("D:/newFileName.pdf"); file.createNewFile(); FileOutputStream fop = new FileOutputStream(file); PdfWriter.getInstance(documento, fop); documento.open(); //Fonts Font fontHeader = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); Font fontBody = new Font(Font.FontFamily.COURIER, 12, Font.NORMAL); //Table for header PdfPTable cabetabla = new PdfPTable(header.length); for (int j = 0; j < header.length; j++) { Phrase frase = new Phrase(header[j], fontHeader); PdfPCell cell = new PdfPCell(frase); cell.setBackgroundColor(new BaseColor(Color.lightGray.getRGB())); cabetabla.addCell(cell); } documento.add(cabetabla); //Tabla for body PdfPTable tabla = new PdfPTable(header.length); for (int i = 0; i < body.length; i++) { for (int j = 0; j < body[i].length; j++) { tabla.addCell(new Phrase(body[i][j], fontBody)); } } documento.add(tabla); documento.close(); fop.flush(); fop.close(); } 

just call:

 createSamplePDF(new String[]{"col1", "col2"}, new String[][]{{"rw11", "rw12"},{"rw21", "rw22"}}); 
+5
source

Use any java pdf generation library. Maybe ms access database can do this for you, but JDBC does not provide such a function, and it should not.

0
source

You can link to this link where the data table is exported to pdf or exel using HtmlWorker

How to export table displayed in jsp to pdf in java strust2

0
source

Source: https://habr.com/ru/post/1482006/


All Articles