Search in sources :

Example 6 with DocumentUnderstandingServiceClient

use of com.google.cloud.documentai.v1beta2.DocumentUnderstandingServiceClient in project java-document-ai by googleapis.

the class ParseTableBeta method parseTable.

public static void parseTable(String projectId, String location, String inputGcsUri) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (DocumentUnderstandingServiceClient client = DocumentUnderstandingServiceClient.create()) {
        // Configure the request for processing the PDF
        String parent = String.format("projects/%s/locations/%s", projectId, location);
        TableBoundHint tableBoundHints = TableBoundHint.newBuilder().setBoundingBox(// Each vertice coordinate must be a number between 0 and 1
        BoundingPoly.newBuilder().addNormalizedVertices(NormalizedVertex.newBuilder().setX(0).setX(0).build()).addNormalizedVertices(NormalizedVertex.newBuilder().setX(1).setX(0).build()).addNormalizedVertices(NormalizedVertex.newBuilder().setX(1).setX(1).build()).addNormalizedVertices(NormalizedVertex.newBuilder().setX(0).setX(1).build()).build()).setPageNumber(1).build();
        TableExtractionParams params = TableExtractionParams.newBuilder().setEnabled(true).addTableBoundHints(tableBoundHints).build();
        GcsSource uri = GcsSource.newBuilder().setUri(inputGcsUri).build();
        // mime_type can be application/pdf, image/tiff,
        // and image/gif, or application/json
        InputConfig config = InputConfig.newBuilder().setGcsSource(uri).setMimeType("application/pdf").build();
        ProcessDocumentRequest request = ProcessDocumentRequest.newBuilder().setParent(parent).setTableExtractionParams(params).setInputConfig(config).build();
        // Recognizes text entities in the PDF document
        Document response = client.processDocument(request);
        // Get all of the document text as one big string
        String text = response.getText();
        // Get the first table in the document
        if (response.getPagesCount() > 0) {
            Document.Page page1 = response.getPages(0);
            if (page1.getTablesCount() > 0) {
                Document.Page.Table table = page1.getTables(0);
                System.out.println("Results from first table processed:");
                List<Document.Page.DetectedLanguage> detectedLangs = page1.getDetectedLanguagesList();
                String langCode = detectedLangs.size() > 0 ? detectedLangs.get(0).getLanguageCode() : "NOT_FOUND";
                System.out.printf("First detected language: : %s", langCode);
                Document.Page.Table.TableRow headerRow = table.getHeaderRows(0);
                System.out.println("Header row:");
                for (Document.Page.Table.TableCell tableCell : headerRow.getCellsList()) {
                    if (tableCell.getLayout().getTextAnchor().getTextSegmentsList() != null) {
                        // Extract shards from the text field
                        // First shard in document doesn't have startIndex property
                        System.out.printf("\t%s", getText(tableCell.getLayout(), text));
                    }
                }
            }
        }
    }
}
Also used : DocumentUnderstandingServiceClient(com.google.cloud.documentai.v1beta2.DocumentUnderstandingServiceClient) GcsSource(com.google.cloud.documentai.v1beta2.GcsSource) Document(com.google.cloud.documentai.v1beta2.Document) TableBoundHint(com.google.cloud.documentai.v1beta2.TableBoundHint) TableExtractionParams(com.google.cloud.documentai.v1beta2.TableExtractionParams) InputConfig(com.google.cloud.documentai.v1beta2.InputConfig) ProcessDocumentRequest(com.google.cloud.documentai.v1beta2.ProcessDocumentRequest)

Example 7 with DocumentUnderstandingServiceClient

use of com.google.cloud.documentai.v1beta2.DocumentUnderstandingServiceClient in project java-document-ai by googleapis.

the class QuickStart method quickStart.

public static void quickStart(String projectId, String location, String inputGcsUri) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (DocumentUnderstandingServiceClient client = DocumentUnderstandingServiceClient.create()) {
        // Configure the request for processing a single document
        String parent = String.format("projects/%s/locations/%s", projectId, location);
        GcsSource uri = GcsSource.newBuilder().setUri(inputGcsUri).build();
        // mime_type can be application/pdf, image/tiff,
        // and image/gif, or application/json
        InputConfig config = InputConfig.newBuilder().setGcsSource(uri).setMimeType("application/pdf").build();
        ProcessDocumentRequest request = ProcessDocumentRequest.newBuilder().setParent(parent).setInputConfig(config).build();
        // Recognizes text entities in the PDF document
        Document response = client.processDocument(request);
        // Get all of the document text as one big string
        String text = response.getText();
        // Process the output
        for (Document.Entity entity : response.getEntitiesList()) {
            System.out.printf("Entity text: %s\n", getText(entity, text));
            System.out.printf("Entity type: %s\n", entity.getType());
            System.out.printf("Entity mention text: %s\n", entity.getMentionText());
        }
    }
}
Also used : DocumentUnderstandingServiceClient(com.google.cloud.documentai.v1beta2.DocumentUnderstandingServiceClient) GcsSource(com.google.cloud.documentai.v1beta2.GcsSource) InputConfig(com.google.cloud.documentai.v1beta2.InputConfig) Document(com.google.cloud.documentai.v1beta2.Document) ProcessDocumentRequest(com.google.cloud.documentai.v1beta2.ProcessDocumentRequest)

Aggregations

Document (com.google.cloud.documentai.v1beta2.Document)7 DocumentUnderstandingServiceClient (com.google.cloud.documentai.v1beta2.DocumentUnderstandingServiceClient)7 GcsSource (com.google.cloud.documentai.v1beta2.GcsSource)7 InputConfig (com.google.cloud.documentai.v1beta2.InputConfig)7 ProcessDocumentRequest (com.google.cloud.documentai.v1beta2.ProcessDocumentRequest)7 Page (com.google.api.gax.paging.Page)2 BatchProcessDocumentsRequest (com.google.cloud.documentai.v1beta2.BatchProcessDocumentsRequest)2 BatchProcessDocumentsResponse (com.google.cloud.documentai.v1beta2.BatchProcessDocumentsResponse)2 FormExtractionParams (com.google.cloud.documentai.v1beta2.FormExtractionParams)2 GcsDestination (com.google.cloud.documentai.v1beta2.GcsDestination)2 KeyValuePairHint (com.google.cloud.documentai.v1beta2.KeyValuePairHint)2 OperationMetadata (com.google.cloud.documentai.v1beta2.OperationMetadata)2 OutputConfig (com.google.cloud.documentai.v1beta2.OutputConfig)2 TableBoundHint (com.google.cloud.documentai.v1beta2.TableBoundHint)2 TableExtractionParams (com.google.cloud.documentai.v1beta2.TableExtractionParams)2 Blob (com.google.cloud.storage.Blob)2 Bucket (com.google.cloud.storage.Bucket)2 Storage (com.google.cloud.storage.Storage)2 File (java.io.File)2 FileReader (java.io.FileReader)2