HTML JPG PDF XML DOCX
  Product Family
PDF

Convert PDF to TXT in Java SDK

Convert PDF documents to TXT format with Aspose.PDF Cloud Java SDK

Get Started

How to Convert PDF to TXT Using SDK for Java

In order to convert PDF to TXT, we’ll use Aspose.PDF Cloud Java SDK This Cloud SDK allows you to easily build cloud-based PDF creator, editor & converter apps in Java language for various cloud platforms. Open Repository package manager, search for Aspose.PDF Cloud and install. You may also use the following command from the Package Manager Console for install it using Maven.

Add Aspose Cloud repository to your application pom.xml

Add Aspose Cloud repository


    <repositories>
        <repository>
            <id>aspose-cloud</id>
            <name>Aspose Cloud Repository</name>
            <url>https://releases.aspose.cloud/java/repo/</url>
        </repository>
    </repositories>

To install the API client library to your local Maven repository, simply execute:

Installation from Github


    mvn clean install

Steps to Convert PDF to TXT via Java SDK

Aspose.PDF Cloud developers can easily load & convert PDF to TXT files in just a few lines of code.

  1. Create a new Configuration object with your Application Secret and Key
  2. Create an object to connect to the Cloud API
  3. Upload your document file
  4. Perform the conversion
  5. Download the result
 

This sample code shows PDF to TXT Cloud Java SDK Conversion


    package com.aspose.asposecloudpdfusecases.conversions;

import java.io.File;
import java.io.PrintWriter;
import java.nio.file.Path;
import com.aspose.asposecloudpdf.api.PdfApi;
import com.aspose.asposecloudpdf.model.TextRect;
import com.aspose.asposecloudpdf.model.TextRectsResponse;

public class ConvertPdfToTxt {
    public static void Convert() {
        String REMOTE_FOLDER   = "Your_Temp_Pdf_Cloud";
	    String LOCAL_FOLDER    = "c:\\Samples";
        String PDF_FILE_NAME  = "sample.pdf";
	    String TXT_OUTPUT = "convert_pdf_txt_output.txt";
    
        try {
            PdfApi pdfApi = new PdfApi(API_KEY, API_SECRET);

            // upload local PDF file to remote storage
            File file = new File(Path.of(LOCAL_FOLDER, PDF_FILE_NAME).toString());
            String srcPath = Path.of(REMOTE_FOLDER, PDF_FILE_NAME).toString();
            pdfApi.uploadFile(srcPath, file, null);
            System.out.println(String.format("Files %s successfully uploaded!", PDF_FILE_NAME));

           // convert PDF to TXT
            TextRectsResponse response = pdfApi.getText(PDF_FILE_NAME, 0., 0., 0., 0., null, null, null, REMOTE_FOLDER, null);
    
            if (response.getCode() != 200)
                System.err.println("Error: unexpected error when converting PDF to TXT! '" + response.getStatus() + "'");
            else {
                // Save all text in a UTF-8 encoded file
                PrintWriter pw = new PrintWriter(Path.of(LOCAL_FOLDER, TXT_OUTPUT).toString());
                for (TextRect txRect : response.getTextOccurrences().getList()) {
                    pw.write(txRect.getText());
                }
                pw.close();
                System.out.println("Successfully converted PDF document to TXT ! '" + TXT_OUTPUT + "'");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
  • Other Supported Conversions

    You can also convert many other file formats

    BMP TO PDF (Bitmap Image)
    EMF TO PDF (Enhanced Metafile Format)
    EPUB TO PDF (E-book Format)
    GIF TO PDF (Graphical Interchange Format)
    HTML-TO-PDF (Hyper Text Markup Language)
    JPEG TO PDF (JPEG Image)
    MD TO PDF (Markdown)
    PCL TO PDF (Printer Command Language)
    PDF TO BMP (Bitmap Image)
    PDF TO DOCX (Office 2007+ Words Document)
    PDF TO EMF (Enhanced Metafile Format)
    PDF TO EPUB (E-book Format)
    PDF TO GIF (Graphical Interchange Forma)
    PDF TO HTML (Hyper Text Markup Language)
    PDF TO JPEG (JPEG Image)
    PDF TO PDF/A (Portable Document Format/A)
    PDF TO PNG (Portable Network Graphics)
    PDF TO PPTX (Open XML presentation Format)
    PDF TO SVG (Scalable Vector Graphics)
    PDF TO TEX (LaTeX Output Text)
    PDF TO TIFF (Tagged Image Format)
    PDF TO TXT (Text Document)
    PDF TO XLSX (OOXML Excel File)
    PDF TO XPS (XML Paper Specifications)
    PDF TO PPTX (Open XML presentation Format)
    PNG TO PDF (Portable Network Graphics)
    SVG TO PDF (Scalable Vector Graphics)
    TEX TO PDF (LaTeX Output Text)
    TEXT TO PDF (Text Document)
    TIFF TO PDF (Tagged Image Format)
    XPS TO PDF (XML Paper Specifications)