【问题标题】:How Can I Convert Pdf File to Text如何将 Pdf 文件转换为文本
【发布时间】:2019-11-07 14:28:41
【问题描述】:

我想从 android 的文件管理器中选择一个 pdf 文件并将其转换为文本,以便文本到语音可以读取它。我正在关注来自 android 开发者网站的文档;但是,此示例用于打开文本文件。我正在使用 PdfReader 类/库来打开文件并转换为文本。但我不知道如何将它与 Uri 集成。 这是我需要使用 PdfReader 从 pdf 转换为文本的代码

PdfReader pdfReader = new PdfReader(file.getPath());
stringParser = PdfTextExtractor.getTextFromPage(pdfReader, 1).trim();
pdfReader.close();

我正在使用意图调用文件管理器,以便用户可以选择一个 pdf 文件

fab.setOnClickListener(new View.OnClickListener() {
@Override
   public void onClick(View view) {
      intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
      intent.setType("*/*");
      startActivityForResult(intent, READ_REQUEST_CODE);
   }
});

然后我正在获取 uri 并打开文件

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
        if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            if(resultData != null) {
                Uri uri = resultData.getData();
                Toast.makeText(MainActivity.this, filePath , Toast.LENGTH_LONG).show();
                readPdfFile(uri);
            }
        }
    }

    private String readTextFromUri(Uri uri) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        try (InputStream inputStream =
                     getContentResolver().openInputStream(uri);
             BufferedReader reader = new BufferedReader(
                     new InputStreamReader(Objects.requireNonNull(inputStream)))) {
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
        }
        return stringBuilder.toString();
    }

【问题讨论】:

  • readPdfFile(uri); ???您没有发布 readPdfFIle() 的代码。你也没有告诉你在哪里以及如何调用发布的函数。例如,您没有告诉您使用 PdfReader 的方式。
  • @blackpos 阅读帖子中的所有信息。 pdfreader 代码进入 readTextFromUri 中,该方法正在解析文本文件。我需要修改以便它解析一个 pdf 文件

标签: android file-manager pdf-reader


【解决方案1】:
public class SyncPdfTextExtractor {
  // TODO: When you have your own Premium account credentials, put them down here:
  private static final String CLIENT_ID = "FREE_TRIAL_ACCOUNT";
  private static final String CLIENT_SECRET = "PUBLIC_SECRET";
  private static final String ENDPOINT = "https://api.whatsmate.net/v1/pdf/extract?url=";

  /**
   * Entry Point
   */
  public static void main(String[] args) throws Exception {
    // TODO: Specify the URL of your small PDF document (less than 1MB and 10 pages)
    // To extract text from bigger PDf document, you need to use the async method.
    String url = "https://www.harvesthousepublishers.com/data/files/excerpts/9780736948487_exc.pdf";
    SyncPdfTextExtractor.extractText(url);
  }

  /**
   * Extracts the text from an online PDF document.
   */
  public static void extractText(String pdfUrl) throws Exception {
    URL url = new URL(ENDPOINT + pdfUrl);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("GET");
    conn.setRequestProperty("X-WM-CLIENT-ID", CLIENT_ID);
    conn.setRequestProperty("X-WM-CLIENT-SECRET", CLIENT_SECRET);

    int statusCode = conn.getResponseCode();
    System.out.println("Status Code: " + statusCode);
    InputStream is = null;
    if (statusCode == 200) {
        is = conn.getInputStream();
        System.out.println("PDF text is shown below");
        System.out.println("=======================");
    } else {
        is = conn.getErrorStream();
        System.err.println("Something is wrong:");
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
    String output;
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }
    conn.disconnect();
  }

}
------------------------------------

Copying above code follow below Steps-

Specify the URL of your online PDF document on line 20.
Replace the Client ID and Secret on lines 10 and 11 if you have your own credentials.

【讨论】:

    【解决方案2】:

    使用这个
    摇篮:-

    implementation 'com.itextpdf:itextg:5.5.10'
    
    try {
          String parsedText="";
          PdfReader reader = new PdfReader(yourPdfPath);
          int n = reader.getNumberOfPages();
          for (int i = 0; i <n ; i++) {
               parsedText   = parsedText+PdfTextExtractor.getTextFromPage(reader, i+1).trim()+"\n"; //Extracting the content from the different pages
          }
          System.out.println(parsedText);
          reader.close();
       } catch (Exception e) {
          System.out.println(e);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-27
      • 1970-01-01
      • 2019-06-03
      • 2010-12-23
      • 2011-11-26
      • 2012-02-07
      相关资源
      最近更新 更多