In this concise tutorial, you will learn how to convert PDF to CSV using Java. It involves all step-by-step details about how using Java PDF to CSV conversion can be performed instantly, without needing to install any PDF processing application like Adobe Acrobat, etc. This conversion can be useful in different use cases, for instance, processing numeric values from a PDF file to create charts or graphs in an Excel file.
Steps to Convert PDF to CSV using Java
- Include Aspose.PDF for Java library reference from the Maven repository to convert PDF to CSV
- Add the reference to Aspose.PDF namespace in your project to export PDF to CSV
- Create an instance of Document class to load the source PDF file
- Initialize ExcelSaveOptions object to set desired properties for output CSV file
- Convert PDF to CSV in Java by using the Save method and ExcelSaveOptions parameter
These basic steps elaborate on how to create PDF to CSV Java based applications. First of all, resolve the reference by configuring the dependency from the Maven repository. Then we need to proceed by loading the input PDF file and writing the output CSV file on the disk while setting different properties for the output file.
Code to Convert PDF to CSV using Java
import com.aspose.pdf.Document; | |
import com.aspose.pdf.ExcelSaveOptions; | |
import com.aspose.pdf.License; | |
public class ConvertPdfToCsvUsingJava { | |
public static void main(String[] args) throws Exception { // main method to convert a PDF document to CSV file format | |
// Instantiate the license to avoid trial limitations while converting the PDF to comma separated CSV file | |
License asposePdfLicenseCSV = new License(); | |
asposePdfLicenseCSV.setLicense("Aspose.pdf.lic"); | |
// Load PDF document for converting it to comma seaparated value file | |
Document convertPDFDocumentToCSV = new Document("input.pdf"); | |
// Initialize ExcelSaveOptions class object to set the format of the output file | |
ExcelSaveOptions csvSave = new ExcelSaveOptions(); | |
csvSave.setFormat(ExcelSaveOptions.ExcelFormat.CSV); | |
// Save the converted output file in CSV format | |
convertPDFDocumentToCSV.save("ConvertPDFToCSV.csv", csvSave); | |
System.out.println("Done"); | |
} | |
} |
In order to export PDF to CSV in Java, you can utilize the above code sample. It loads the input PDF file using the Document class and then we can set different preferences for the output CSV file using the ExcelSaveOptions class. For example, setting the file format or uniform column division, etc. before saving the output file on the disk or a stream object as per your requirements.
In this example, we have explored how to convert PDF to CSV using Java. If you are interested to convert a PDF file to Excel programmatically, please refer to the article on how to convert PDF to Excel in Java.