文本提取器API GroupDocs.Parser for Java v19.11来袭!简化文档解析过程

为了改善API的工作效率并简化开发人员的使用,从头开始对GroupDocs.Parser的架构进行了改进。此次更新将会提供GroupDocs.Parser for Java 19.11改进和简化的API。

    GroupDocs.Parser for Java是文本,图像和元数据提取器API,用于构建支持解析原始,结构化和格式化文本的业务应用程序。它还允许检索支持格式的文件元数据。

GroupDocs.Parser for Java


    自GroupDocs.Parser for Java API进入市场,它就成为了功能强大的文档解析器API之一,它可以解析和读取常用格式的文字处理文档,电子表格,演示文稿,电子书,电子邮件,标记文档,注释 ,档案和数据库。不仅文本,您还可以从各种文档格式中提取图像和元数据属性,包括PDF,XLS,XLSX,CSV,DOC,DOCX,PPT,PPTX,MPP,EML,MSG,OST,PST,ONE等。

    其中,为了改善API的工作效率并简化开发人员的使用,从头开始对GroupDocs.Parser的架构进行了改进。此次更新将会提供GroupDocs.Parser for Java 19.11改进和简化的API。

    本次更新增加了许多新功能,下面将会介绍本次更新的内容:

  • 引入了Parser类以从任何受支持格式的文档中读取和提取数据。

  • 所有数据类型的数据提取过程已统一。

  • 产品架构从头进行了修改,以简化使用不同选项和类来处理数据的过程。

  • 获取文档信息和预览生成的过程已简化。

迁移

    由于产品已进行了重大更新,因此类,方法及其使用方式也已更改。但是,还尚未从包中删除旧版API,而是将其移至com.groupdocs.parser.legacy包中。升级到v19.11后,您只需在项目范围内将包从com.groupdocs.parser替换为com.groupdocs.parser.legacy。这就可以摆脱立即构建问题。 然后,您可逐步进行更新源代码,并使用新的公共API的类和方法。

    下面将会介绍GroupDocs.Parser for Java v19.11中使用新旧API提取数据的简要比较。

文本

旧版:

// Create an extractor factoryExtractorFactory factory = new ExtractorFactory();// Create a text extractortry (TextExtractor extractor = factory.createTextExtractor(filePath)) {    // Extract a text from the text extractor    String textLine = null;    do {        textLine = extractor.extractLine();        if (textLine != null) {            System.out.println(textLine);        }    }    while (textLine != null);}

新版:

// Create an instance of Parser classtry (Parser parser = new Parser(filePath)) {    // Extract a text to the reader    try (TextReader reader = parser.getText()) {        // Check if text extraction is supported        if (reader == null) {            System.out.println("Text extraction isn't supported.");            return;        }        // Extract a text from the reader        String textLine = null;        do {            textLine = reader.readLine();            if (textLine != null) {                System.out.println(textLine);            }        }        while (textLine != null);    }}

文本页

旧版:

// Create an extractor factoryExtractorFactory factory = new ExtractorFactory();// Create a text extractortry (TextExtractor extractor = factory.createTextExtractor(filePath)) {    // Check if the extractor supports pagination    IPageTextExtractor pte = extractor instanceof IPageTextExtractor            nbsp;(IPageTextExtractor) extractor            : null;    if (pte != null) {        // Extract the first page        System.out.println(pte.extractPage(0));    }}

新版:

// Create an instance of Parser classtry (Parser parser = new Parser(filePath)) {    // Extract the first page text to the reader    try (TextReader reader = parser.getText(0)) {        // Check if text extraction is supported        if (reader != null) {            // Extract a text from the reader            System.out.println(reader.readToEnd());        }    }}

搜索

旧版:

// Create an extractor factoryExtractorFactory factory = new ExtractorFactory();// Create a text extractortry (TextExtractor extractor = factory.createTextExtractor(filePath)) {    // Check if the extractor supports search    ISearchable se = extractor instanceof ISearchable            nbsp;(ISearchable) extractor            : null;    if (se != null) {        // Create a handler        ListSearchHandler handler = new ListSearchHandler();        // Search "keyword" in the document        se.search(new SearchOptions(null), handler, java.util.Arrays.asList(new String[]{"keyword"}));        // Print search results        for (SearchResult result : handler.getList()) {            System.out.println(String.format("at %d: %s", result.getIndex(), result.getFoundText()));        }    }}

新版:

// Create an instance of Parser classtry (Parser parser = new Parser(filePath)) {    // Search "keyword" in the document    Iterable list = parser.search("keyword");    // Check if search is supported    if (list == null) {        System.out.println("Search isn't supported.");        return;    }    // Print search results    for (SearchResult result : list) {        System.out.println(String.format("at %d: %s", result.getPosition(), result.getText()));    }}

文件类型检测

旧版:

// Detect and print file typeSystem.out.println(CompositeMediaTypeDetector.DEFAULT.detect(filePath));

新版:

// Create an instance of Parser classtry (Parser parser = new Parser(filePath)) {    // Detect and print file type    System.out.println(parser.getDocumentInfo().getFileType());}

元数据

旧版:

// Create an extractor factoryExtractorFactory factory = new ExtractorFactory();// Create a metadata extractorMetadataExtractor extractor = factory.createMetadataExtractor(filePath);// Extract metadataMetadataCollection metadata = extractor.extractMetadata(filePath);// Print metadatafor (String key : metadata.getKeys()) {    String value = metadata.get_Item(key);    System.out.println(String.format("%s = %s", key, value));}

新版:

// Create an instance of Parser classtry (Parser parser = new Parser(filePath)) {    // Extract metadata    Iterable metadata = parser.getMetadata();    // Check if metadata extraction is supported    if (metadata == null) {        System.out.println("Metadata extraction isn't supported.");        return;    }    // Print metadata    for (MetadataItem item : metadata) {        System.out.println(String.format("%s = %s", item.getName(), item.getValue()));    }}

结构体

旧版:

// Create an extractor factoryExtractorFactory factory = new ExtractorFactory();// Create a text extractortry (TextExtractor extractor = factory.createTextExtractor(filePath)) {    // Check if the extractor supports text structure extraction    IStructuredExtractor se = extractor instanceof IStructuredExtractor            nbsp;(IStructuredExtractor) extractor            : null;    if (se != null) {        // Create a handler        Handler handler = new Handler();        // Extract text structure        se.extractStructured(handler);        // Print hyperlinks        for (String link : handler.getLinks()) {            System.out.println(link);        }    }} // Handler for the hyperlink extractionclass Handler extends StructuredHandler {    private final java.util.List links;    public Handler() {        links = new java.util.ArrayList();    }    public java.util.List getLinks() {        return links;    }    // Override the method to catch hyperlinks    @Override    protected void onStartHyperlink(HyperlinkProperties properties) {        links.add(properties.getLink());    }}

新版:

// Create an instance of Parser classtry (Parser parser = new Parser(filePath)) {    // Extract text structure to the XML reader    Document document = parser.getStructure();    // Check if text structure extraction is supported    if (document == null) {        System.out.println("Text structure extraction isn't supported.");        return;    }    // Read XML document    readNode(document.getDocumentElement());} void readNode(Node node) {    NodeList nodes = node.getChildNodes();    for (int i = 0; i < nodes.getLength(); i++) {        Node n = nodes.item(i);        if (n.getNodeName().toLowerCase() == "hyperlink") {            Node a = n.getAttributes().getNamedItem("link");            if (a != null) {                System.out.println(a.getNodeValue());            }        }        if (n.hasChildNodes()) {            readNode(n);        }    }}

   在线文档查看器GroupDocs.Viewer也已更新至v19.11,该版本修复许多小问题,感兴趣的朋友可以点击查看更新新闻


如果您对想要购买正版授权GroupDocs.Parser,可以联系在线客服咨询相关问题。

 

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2019年11月19日
下一篇 2019年11月19日

相关推荐

发表回复

登录后才能评论