We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

使用TextFragmentAbsorber正则匹配文本查询不到

pdf中文字含有内嵌字体,导致使用TextFragmentAbsorber匹配不到正确的文本,如何处理。f94a6f84-ecb5-4254-8d6a-d04e0846a377.pdf (222.1 KB)

文档中的手机号和邮箱都无法用正常的正则匹配到。

public static final String PHONE_REG = "(?:(?:1[-\\s]*[3456789][-\\s]*\\d{1}[-\\s]*\\d{1}[-\\s]*\\d{1}[-\\s]*\\d{1}[-\\s]*\\d{1}[-\\s]*\\d{1}[-\\s]*\\d{1}[-\\s]*\\d{1}[-\\s]*\\d{1})|(?:0[1-9]\\d{1,2}[-\\s]*\\d{7,8}))(?!\\d)";
public static void main(String[] args) throws Exception {
    byte[] source = FileUtils.readFileToByteArray(new File("/Users/Carol/Desktop/f94a6f84-ecb5-4254-8d6a-d04e0846a377.pdf"));
    if (!getLicense()) {
        throw new Exception("com.aspose.pdf lic ERROR!");
    }
    try (ByteArrayInputStream searchInputStream = new ByteArrayInputStream(source); ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        Document pdfDoc = new Document(searchInputStream);

        TextSearchOptions textSearchOptions = new TextSearchOptions(true);
        TextEditOptions textEditOptions = new TextEditOptions(0, TextEditOptions.LanguageTransformation.class);
        TextFragmentAbsorber phoneTextFragmentAbsorber = new TextFragmentAbsorber(
                PHONE_REG,
                textSearchOptions,
                textEditOptions);

        PageCollection pages = pdfDoc.getPages();
        Page page = pages.get_Item(1);
        page.accept(phoneTextFragmentAbsorber);
        logger.info("[开始搜索]");

        for (TextFragment textFragment : phoneTextFragmentAbsorber.getTextFragments()) {
            String text = textFragment.getText();
            logger.info("手机: " + text);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@liurupeng

您能否在最后尝试以下代码示例,让我们知道您的情况如何? 希望这对你有帮助。

Document pdfDocument = new Document(MyDir + "f94a6f84-ecb5-4254-8d6a-d04e0846a377.pdf");
 
	// Create TextAbsorber object to find all instances of the input search phrase
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("^(.+@.+|\\d{10})$"); 

// Set text search option to specify regular expression usage
TextSearchOptions textSearchOptions = new TextSearchOptions(true);
textFragmentAbsorber.setTextSearchOptions(textSearchOptions);

// Accept the absorber for first page of document
pdfDocument.getPages().accept(textFragmentAbsorber);

// Get the extracted text fragments into collection
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();

// Loop through the fragments
for (TextFragment textFragment : (Iterable<TextFragment>) textFragmentCollection) {
    System.out.println("Text :- " + textFragment.getText());
    System.out.println("Position :- " + textFragment.getPosition());
    System.out.println("XIndent :- " + textFragment.getPosition().getXIndent());
    System.out.println("YIndent :- " + textFragment.getPosition().getYIndent());
    System.out.println("Font - Name :- " + textFragment.getTextState().getFont().getFontName());
    System.out.println("Font - IsAccessible :- " + textFragment.getTextState().getFont().isAccessible());
    System.out.println("Font - IsEmbedded - " + textFragment.getTextState().getFont().isEmbedded());
    System.out.println("Font - IsSubset :- " + textFragment.getTextState().getFont().isSubset());
    System.out.println("Font Size :- " + textFragment.getTextState().getFontSize());
    System.out.println("Foreground Color :- " + textFragment.getTextState().getForegroundColor());
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at com.aspose.pdf.internal.l137.I6I.lif(Unknown Source)
at com.aspose.pdf.internal.l137.I6I.lif(Unknown Source)
at com.aspose.pdf.internal.l137.I6I.lif(Unknown Source)
at com.aspose.pdf.internal.l134.I07.lif(Unknown Source)
at com.aspose.pdf.internal.l134.I01.lif(Unknown Source)
at com.aspose.pdf.internal.l134.I01.lif(Unknown Source)
at com.aspose.pdf.internal.l134.I07.lI(Unknown Source)
at com.aspose.pdf.internal.l134.I07.l1f(Unknown Source)
at com.aspose.pdf.internal.l134.I17.lif(Unknown Source)
at com.aspose.pdf.internal.l134.I17.lIF(Unknown Source)
at com.aspose.pdf.internal.l134.I17.liF(Unknown Source)
at com.aspose.pdf.internal.l134.I17.l0I(Unknown Source)
at com.aspose.pdf.internal.l134.I11.lif(Unknown Source)
at com.aspose.pdf.internal.l134.I11.lif(Unknown Source)
at com.aspose.pdf.internal.l134.I11.lIf(Unknown Source)
at com.aspose.pdf.internal.l134.I11.<init>(Unknown Source)
at com.aspose.pdf.internal.l134.I11.<init>(Unknown Source)
at com.aspose.pdf.TextFragmentAbsorber.visit(Unknown Source)
at com.aspose.pdf.Page.accept(Unknown Source)
at com.aspose.pdf.PageCollection.accept(Unknown Source)

@liurupeng

如果您使用旧版本的 Aspose.PDF for Java,请升级到最新版本的 Aspose.PDF for Java 22.12.

Text :- 1234#56665789):7% % % 3;<=#>?@AB@CBDEFG586HIDJ%
Position :- ( 42.959952, 683.108 )
XIndent :- 42.959952
YIndent :- 683.108
Font - Name :- SimHei
Font - IsAccessible :- true
Font - IsEmbedded - true
Font - IsSubset :- true
Font Size :- 12.0
Foreground Color :- #262626

@liurupeng

您正在获得正确的输出。 如果您使用 Adobe 从 PDF 文件中复制或提取文本,您将获得相同的输出。

但是,我们已将此问题记录为 PDFJAVA-42360 在我们的问题跟踪系统中,以获得与 PDF 中显示的相同文本。 一旦有更新可用,我们会通知您。