Extrayendo texto sin formato de archivo PDF

Extraer texto de todas las páginas de un documento PDF

Extraer texto de un documento PDF es una necesidad común. En este ejemplo, verás cómo Aspose.PDF for Java permite extraer texto de todas las páginas de un documento PDF. Para extraer texto de todas las páginas del PDF:

  1. Crear un objeto de la TextAbsorber clase.
  2. Abrir el PDF usando Document clase y llamar al Aceptar método del Páginas colección.
  3. El TextAbsorber La clase absorbe el texto del documento y lo devuelve en la propiedad Text.

El siguiente fragmento de código le muestra cómo extraer texto de todas las páginas del documento PDF.

public static void ExtractFromAllPages() {
        // The path to the documents directory.

        String filePath = _dataDir + "ExtractTextAll.pdf";

        // Open document
        Document pdfDocument = new com.aspose.pdf.Document(filePath);

        // Create TextAbsorber object to extract text
        TextAbsorber textAbsorber = new com.aspose.pdf.TextAbsorber();

        // Accept the absorber for all the pages
        pdfDocument.getPages().accept(textAbsorber);

        // Get the extracted text
        String extractedText = textAbsorber.getText();
        try {
            java.io.FileWriter writer = new java.io.FileWriter(_dataDir + "extracted-text.txt", true);
            // Write a line of text to the file
            writer.write(extractedText);
            // Close the stream
            writer.close();
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }

    }

Extraer texto resaltado de documento PDF

En varios escenarios de extracción de texto de un documento PDF, puede surgir la necesidad de extraer solo el texto resaltado del documento PDF. Para implementar la funcionalidad, hemos añadido los métodos TextMarkupAnnotation.GetMarkedText() y TextMarkupAnnotation.GetMarkedTextFragments() en la API. Puede extraer el texto resaltado del documento PDF filtrando TextMarkupAnnotation y utilizando los métodos mencionados. El siguiente fragmento de código muestra cómo puede extraer el texto resaltado del documento PDF.

public static void ExtractHighlightedText() {
        Document doc = new Document(_dataDir + "ExtractHighlightedText.pdf");
        // Loop through all the annotations
        for (Annotation annotation : doc.getPages().get_Item(1).getAnnotations()) {
            // Filter TextMarkupAnnotation
            if (annotation.getAnnotationType() == AnnotationType.Highlight) {
                HighlightAnnotation highlightedAnnotation = (HighlightAnnotation) annotation;
                // Retrieve highlighted text fragments
                TextFragmentCollection collection = highlightedAnnotation.getMarkedTextFragments();
                for (TextFragment tf : collection) {
                    // Display highlighted text
                    System.out.println(tf.getText());
                }
            }
        }
    }

Acceder a fragmentos de texto y elementos de segmento desde XML

A veces necesitamos acceder a elementos TextFragement o TextSegment al procesar documentos PDF generados a partir de XML. Aspose.PDF for Android via Java proporciona acceso a dichos elementos por nombre. El fragmento de código a continuación muestra cómo usar esta funcionalidad.

public static void AccessTextFragmentAndSegmentElements() { String inXml = “40014.xml”; Document doc = new Document(); doc.bindXml(_dataDir + inXml);

    TextSegment segment = (TextSegment) doc.getObjectById("boldHtml");
    segment = (TextSegment) doc.getObjectById("strongHtml");

    System.out.println(segment.getText());
    
}