How to Change CSV to PDF in Java

Here is a quick tutorial to explain the process of how to change CSV to PDF in Java. The CSV file is loaded into a Workbook class object along with the delimiter character that is used in it. Then we convert it from CSV to PDF in Java by saving the Workbook object as PDF along with the configuration of grid lines to be displayed and other settings in the output PDF file.

Steps to Change CSV to PDF in Java

  1. Add Aspose.Cells using the Maven repository in the project to convert CSV to PDF
  2. Create an instance of TxtLoadOptions and set the separator used in the source CSV
  3. Create an instance of Workbook and load the source CSV using the above configuration
  4. Instantiate an instance of PdfSaveOptions to configure the grid lines in the output PDF
  5. Enable and set the gridline type to be displayed in the output PDF
  6. Save the workbook as PDF using the configured PdfSaveOptions object

In these steps, we load the source CSV file into a Workbook object and describe the delimiter character so that the CSV file data is read according to it. Then we define the properties of the output PDF file like whether to display the gridlines or not and what type of grid lines to be shown in the output PDF using the PdfSaveOptions class. Finally, we convert CSV to PDF in Java by saving the workbook as PDF and setting the configuration defined in the PdfSaveOptions class object.

Code to Transfer CSV to PDF in Java

import com.aspose.cells.GridlineType;
import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.TxtLoadOptions;
import com.aspose.cells.Workbook;
public class HowToChangeCSVToPDFInJava {
public static void main(String[] args) throws Exception { //main function for HowToChangeCSVToPDFInJava class to change CSV to PDF
// Load Aspose.Cells license to remove trial version text from the converted PDF
License PdfFromCsvLicense = new License();
PdfFromCsvLicense.setLicense("Aspose.Cells.lic");
// Create a TxtLoadOptions class object to manage the loading of CSV file
TxtLoadOptions txtLoadOptions = new TxtLoadOptions();
// Set a separator character, if the source CSV does not contain default character i.e comma
txtLoadOptions.setSeparator('#');
// Create a workbook object by loading the CSV file using the loading options set above
Workbook workbook = new Workbook("sample.csv", txtLoadOptions);
// Instantiate a PdfSaveOptions class object to set properties of the output PDF file
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
// Set the grid-line type to Hair while displaying data in the PDF file
pdfSaveOptions.setGridlineType(GridlineType.HAIR);
// Set print grid lines parameter true to display table grid
workbook.getWorksheets().get(0).getPageSetup().setPrintGridlines(true);
// Configure the table to display complete text in PDF by calling autoFitColumns() functions
workbook.getWorksheets().get(0).autoFitColumns();
// Save the PDF file using the above mentioned settings
workbook.save("ConvertedPdf.pdf", pdfSaveOptions);
}
}

You can see that we transfer CSV to PDF in Java by loading the CSV file into Workbook class object that can load all other types of files also supported by MS Excel. We can control the CSV loading process by defining the AutoFilter, AutoFitterOptions, TextQualifier, IgnoreNotPrinted, and a lot of other options available in TxtLoadOptions class object. Similarly, we can set DefaultFont, DisplayDocTitle, FontEncoding, OptimizationType, and many other configurations in the output PDF file using the PdfSaveOptions class object while saving the Workbook.

We have learned the operation of changing the CSV to PDF in Java with a few lines of code. If you are interested in knowing the process of converting Excel to CSV, have a look at the article on how to convert Excel to CSV in Java.

 English