Method for extracting and analyzing table in PDF (Portable Document Format)

By converting and aggregating instructions in PDF files, calculating nested relationships and parsing object types, successfully extracting and parsing table information in PDFs, the problem that computers find it difficult to understand PDF tables is solved, and high-accuracy table extraction and analysis is achieved.

CN119759355AActive Publication Date: 2025-04-04HANGZHOU JIAYAN XIAOAN INFORMATION TECHNOLOGY CO LTD
View PDF 9 Cites 0 Cited by

Patent Information

Application Number
CN202411683420.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-22
Publication Date
2025-04-04
Estimated Expiration
2044-11-22

AI Technical Summary

Technical Problem

The lack of structural information in PDF documents makes it difficult for computers to understand and analyze the tabular information, resulting in the inability to directly extract and parse the tabular data.

Method used

By reading the PDF file, converting instructions into basic elements, performing multiple rounds of aggregation operations, calculating nested relationships and parsing object types, determining table objects, and performing table parsing to get all cells.

Benefits of technology

It realizes the extraction and parsing of table information in PDF files with high accuracy, and structures information that computers cannot understand into understandable data structures.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119759355A_ABST
    Figure CN119759355A_ABST
Patent Text Reader

Abstract

The invention discloses a method for extracting and analyzing a table in a PDF (Portable Document Format), which comprises the following steps of: reading a PDF file, and reading an instruction in the PDF into a memory; converting contents in the instruction into basic elements; performing multiple rounds of polymerization operation on the basic element to obtain a polymerized object; calculating a nesting relationship for the aggregated objects, analyzing the types of the aggregated objects, and determining objects of which the types are tables; and performing one-time table analysis on the object of which the type is a table to obtain all table cells of the table. According to the invention, high-accuracy PDF table extraction and analysis are realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of document processing, and in particular to a method for extracting and parsing a table in a PDF. Background Art

[0002] Concept 1 What is PDF

[0003] Portable Document Format (PDF) is a file format that presents documents in a way that is independent of applications, hardware, and operating systems. Each PDF file contains a complete description of a flat document with a fixed layout, including text, glyphs, graphics, and other information that needs to be displayed. In addition to flat text and graphics, PDF files may also include interactive elements such as logical structure elements, annotations, forms, layers, and rich media (including video content). The PDF specification also provides the encryption functions, digital signatures, document attachments, and metadata required to enable these functions.

[0004] PDF was originally just a text page preview format, mainly for the purpose of facilitating electronic printing. But at that time, e-reading was on the rise: on the one hand, consumers began to shift their attention from paper reading to e-reading, and they needed a convenient and reliable document reading tool. And it is unacceptable in reality if an e-book can be deleted by any user during the transmission process. On the other hand, there is an urgent need for enterprises: the same document can still display the same visual content on different machines and different operating systems after transmission. Until today, we have found that the same Word document has different display effects on Windows operating system and macOS. However, PDF documents can do it.

[0005] To sum up, the core concept of PDF is that once a document is created, its text format and visual effects are completely fixed, and its visual presentation is exactly the same regardless of the application, hardware, or operating system.

[0006] Concept 2: The Principle of PDF

[0007] The way PDF achieves cross-platform consistency is closely related to the format in which its data is stored. Before parsing its data structure, we need to explain several basic concepts: glyph, font, and code point.

[0008] A glyph is a vector graphics object with a specific shape and bounding box. Figure 1 Shown is a glyph.

[0009] A font is a set of characters and glyphs. A font includes font size, font bold, font family, etc. Take font family as an example. We are usually familiar with the Chinese font family "Songti" and the English font family "Times New Roman". The same character has different glyphs in different fonts. For example, the same character "g" has a glyph "g" in "Times New Roman" and a glyph "g" in "Arial".

[0010] Code points are stored in digital form to represent a specific glyph, which is mainly used to let the computer know which glyph to render. For example, the code point corresponding to the glyph "g" is 0067.

[0011] The text content of a PDF file is actually a series of program instructions. Each program instruction tells the PDF browser to draw a specific glyph or line at a certain location. Although the visual style we see is directly displayed after we open a PDF file with a PDF browser, the computer actually completes the "translation" process from program instructions to visual content.

[0012] like Figure 2 As shown in the figure, the first program instruction means: "At the position (503,688), use white and 8.5 font size to draw a glyph m..." Through these instructions, the PDF browsing tool renders glyphs one by one, green, blue, and black lines on the screen, so that we finally see the visual style shown in the left picture above. Therefore, no matter in any application, operating system, or printing device, the visual style we see is exactly the same.

[0013] Concept three: vector, vector cross product

[0014] In mathematics, a vector (also called a Euclidean vector or a geometric vector) refers to a quantity with both magnitude and direction. It can be visualized as a line segment with an arrow. The direction of the arrow represents the direction of the vector; the length of the line segment represents the magnitude of the vector.

[0015] Suppose there is a line segment with its starting point a(x1,y1) and its end point b(x2,y2), and its vector is: (x2-x1,y2-y1).

[0016] Vector cross product: Assume that vector a is (x1, y1) and vector b is (x2, y2), the result of the cross product is: x1*y2-x2*y1. The geometric meaning is that c is the area of ​​a parallelogram that is perpendicular to the plane where a and b are located, with |b|·sinθ as the height and |a| as the base.

[0017] Vector dot product: Assuming vector a is (x1, y1) and vector b is (x2, y2), the dot product result is: x1*x2+y1*y2.

[0018] The magnitude of a vector: the length of the vector.

[0019] COSINE similarity: Cosine similarity is a method to measure the similarity between two vectors. It measures their similarity by calculating the cosine value of the angle between the two vectors. The value range of cosine similarity is [-1,1], where 1 means that the two vectors are exactly the same, -1 means that the two vectors are completely opposite, and 0 means that the two vectors are orthogonal, that is, there is no correlation. The calculation method is the dot product of the two vectors / (the modulus of vector a * the modulus of vector b).

[0020] Concept Four Convex Hull

[0021] Convex Hull is a concept in computational geometry (graphics).

[0022] In a real vector space V, for a given set X, the intersection S of all convex sets containing X is called the convex hull of X. The convex hull of X can be constructed by the convex combination of all points (X1,...Xn) in X.

[0023] In two-dimensional Euclidean space, the convex hull can be imagined as a rubber band that just encloses all the points.

[0024] In loose terms, given a set of points on a two-dimensional plane, the convex hull is a convex polygon formed by connecting the outermost points, which can contain all the points in the point set.

[0025] The concept of extreme points in the convex hull: please refer to Figure 3 , use a two-dimensional coordinate system to describe the convex hull. The convex hull pole is the point with the smallest x and the smallest y, that is Figure 3 The location of the red dot in the middle.

[0026] Concept 5 Depth-first search (dfs)

[0027] Depth-first search is a type of graph algorithm, abbreviated as DFS, which stands for Depth First Search. In short, the process is to search every possible branch path until it cannot go any deeper, and each node can only be visited once.

[0028] Please refer to Figure 4 , Figure 4It is an undirected graph. If we initiate a depth-first search from point A (the following access order is not unique, the second point can be B, C, or D), we may get the following access process: A->B->E (no way! Backtrack to A)->C->F->H->G->D (no way, finally backtrack to A, A has no unvisited adjacent nodes, and this search ends).

[0029] Concept 6 PDF Questions

[0030] PDF documents only store program instructions for drawing visual content (including only glyphs, lines, color blocks, and pictures). In other words, it only records the position, color, size, and other information of characters, lines, and other objects on each page, without including any structural information. Therefore, through the content stored in the PDF file, the computer cannot directly know structural information such as "where is a table", "whether this paragraph spans columns or pages", and "which content is the third section of the article". However, the powerful visual system and cognitive ability of humans can combine and abstract independent characters, lines, color blocks, and other objects in the document, so as to fully understand the rich and complete structural information contained in the document.

[0031] The rich information in PDF documents contains important utilization value. Taking the financial field as an example, if an investor wants to analyze and compare the operating conditions of some listed companies this year, he will read the annual reports disclosed by these companies. For example, learn what the company's main business and products are; analyze the balance sheet, income statement, cash flow statement, etc. in the annual report; find out which companies are its suppliers... There are so many listed companies, and an annual report may be about 300 pages. If such a large workload can be replaced by machines, it will save a lot of time and cost. The most important basis for understanding and analyzing the knowledge contained in PDF documents by machines is to obtain rich and complete panoramic structural information in the documents. How to structure the information in PDF files that cannot be understood by computers into understandable data structures is a technical problem that technicians in this field need to solve.

[0032] The Chinese invention application with application number 202010922836.2 discloses "A method and related device for extracting table information from PDF", and its technical solution includes: performing text parsing on the PDF file to obtain text and text position information; performing closed contour recognition processing on the picture corresponding to the PDF file through an image recognition algorithm to obtain a rectangular contour array; and performing structured processing on the text according to the rectangular contour array and the text position information to obtain table information. Summary of the invention

[0033] The purpose of the present invention is to provide a method for extracting and parsing a table in a PDF file, so as to accurately structure the information in the PDF file that cannot be understood by a computer into an understandable data structure.

[0034] In order to solve the above technical problems, the present invention provides a method for extracting and parsing a table in a PDF, the method comprising:

[0035] Read the PDF file and read the instructions in the PDF into the memory;

[0036] Convert the contents of the instruction into basic elements;

[0037] Perform multiple rounds of aggregation operations on basic elements to obtain aggregated objects;

[0038] Calculate the nested relationship of the aggregated objects, parse the type of the aggregated objects, and determine the objects whose type is a table;

[0039] Perform a table parsing on the table type object to get all the cells in the table.

[0040] As a preferred solution, the method of reading a PDF file and reading instructions in the PDF into a memory includes:

[0041] Use the Apache open source library PDFBox to read files and get PDF instructions.

[0042] As a preferred solution, the method of converting the content in the instruction into the most basic elements includes:

[0043] According to PDF's standard self-developed framework, the instructions are parsed and initially converted into basic elements that can be operated.

[0044] As a preferred solution, the basic elements include at least one of the following:

[0045] Text, rectangle, line segment.

[0046] As a preferred solution, a method of performing multiple rounds of aggregation operations on basic elements to obtain aggregated objects includes:

[0047] For the text, first aggregate the texts with the same height according to the y-axis value of the coordinate;

[0048] The duplicated line segments and rectangles are deduplicated using the preset line segment deduplication algorithm;

[0049] The collinear line segments are aggregated once using a preset collinearity judgment algorithm;

[0050] The intersecting line segments are aggregated again through the preset intersection judgment algorithm;

[0051] Aggregate connected rectangles and line segments into an object region.

[0052] As a preferred solution, the line segment deduplication algorithm includes:

[0053] The line segments are compared with each other in terms of their starting and ending coordinates. If the difference is less than the set threshold, they are considered to be the same line segment.

[0054] The collinearity determination algorithm comprises:

[0055] The two line segments to be judged are regarded as two vectors, line segment 1 is vector ab, and line segment 2 is vector cd. When the areas of triangle abc and triangle abd formed by vector ab and vector cd are both close to 0, it can be determined that ab and cd are collinear.

[0056] The intersection judgment algorithm includes:

[0057] Consider line segment 1 as vector ab and line segment 2 as vector cd. First, determine whether the projections of the two vectors on the x-axis and y-axis overlap, that is, determine whether the ranges covered by the starting points and end points of the two vectors on the x-axis overlap. The same is true for the y-axis. If there is overlap, determine the result of the cross product of vector ab with vectors ac and ad. If the signs of the cross product results are opposite, it can be determined that ac and ad are on both sides of ab, that is, the two line segments have an intersection.

[0058] The methods for aggregating connected rectangles and line segments into an object region include:

[0059] Suppose there is a set, each element of which is a set of line segments that have been judged to be connected;

[0060] If there is a new line segment, traverse this collection to find out whether there is a line segment in the collection that is connected to the current line segment;

[0061] If the new line segment is not connected to any connected line segment group in the set, the new line segment is added to the set as a separate connected line segment group C;

[0062] If the new line segment is only connected to one connected line segment group, add the current line segment to the connected line segment group;

[0063] If the new line segment is connected to multiple connected line segment groups, the multiple connected line segment groups are first aggregated into one connected line segment group, and then the current line segment is placed in this connected line segment group.

[0064] As a preferred solution, the method of calculating the nested relationship of the aggregated objects, parsing the types of the aggregated objects, determining the object of the table type, and placing the object in a preset position includes:

[0065] Calculate the nesting relationship between regions in pairs to get a two-dimensional array, and then perform a depth-first search on this array to get the nesting relationship between all regions;

[0066] Perform a type judgment on the region, specifically:

[0067] Take out the endpoints of all line segments in the region, form a set, and then find the convex hull of this set;

[0068] The obtained convex hull is described by shape description algorithm and approximate rectangle algorithm. If it is an approximate rectangle and the entire convex hull is completely filled with regular rectangles, it is considered to be a table, otherwise it is considered to be a chart.

[0069] As a preferred solution, the shape description algorithm method includes:

[0070] A1: Define three pointers p1, p2, and p3. In the initial state, p1 is the location of the convex hull pole, p2 is the next point in the counterclockwise direction of p1, and p3 is the next point in the counterclockwise direction of p2.

[0071] A2: Calculate the cosine similarity of vectors p1p2 and p2p3;

[0072] A3: If the cosine similarity does not reach the preset threshold, point p2 to p3, and p3 to the next point in the counterclockwise direction of p3, and repeat step A2;

[0073] A4: If the cosine similarity is less than the first preset threshold, it is considered that a 90-degree corner is found, and p1 is pointed to p2, p2 is pointed to p3, and p3 is pointed to the next point in the counterclockwise direction, and step A2 is repeated;

[0074] A5: Repeat step A2 until the entire convex hull is traversed. Record the number of times p1 changes during the traversal process. The number of times p1 changes is the number of 90-degree angles of the convex hull. When the number of 90-degree angles of the convex hull is 4, the region is determined to be a rectangle.

[0075] The approximate rectangle algorithm includes:

[0076] B1: Take one side of the convex hull as the base, set the endpoints of the side as A and B, determine the two points with the largest and smallest projection values ​​in the AB direction among all the points, and then determine the point farthest from AB. We get three points and a line, that is, a rectangle.

[0077] B2: Traverse each edge of the convex hull and finally find the minimum rectangle;

[0078] B3: Obtain a minimum rectangle, and then calculate the difference between the area of ​​the minimum rectangle and the area of ​​the convex hull. If the difference between the area of ​​the minimum rectangle and the area of ​​the convex hull is less than a second preset threshold, the convex hull is approximately a rectangle.

[0079] As a preferred solution, a table is parsed for an object of table type, and a method for obtaining all cells of the table includes:

[0080] C1: Calculate the intersection points of all line segments in the object region and obtain a set of intersection points;

[0081] C2: Divide the horizontal line segment through the intersection points calculated in the previous step to obtain several horizontal line segments;

[0082] C3: Divide the vertical line segment through the intersection points calculated in the first step to obtain several vertical line segments;

[0083] C4: Traverse the horizontal line segments from left to right and from top to bottom to find the cell;

[0084] C5: Repeat step C4 until all horizontal line segments are traversed, thus obtaining all cells in the table.

[0085] As a preferred solution, in step C4, the method of traversing horizontal line segments from left to right and from top to bottom to find cells includes:

[0086] C41: Set the current horizontal line segment as the top border of the cell, and find the right border of the cell in the vertical line segment;

[0087] C42: If the right border is not found, then search for a horizontal line segment adjacent to the current line segment in the horizontal line segments, aggregate the current line segment and the adjacent line segment together and repeat step C41;

[0088] C43: If the right border is found, then search for the bottom border in the horizontal line segment;

[0089] C44: If the bottom border is not found, then search for a horizontal line segment adjacent to the current line segment in the vertical line segments, aggregate the current line segment and the adjacent line segment together and repeat step C43;

[0090] C45: If the bottom border is found, search for the left border in the vertical line segment;

[0091] C46: If the left border is found, determine whether the found left border can form a closed rectangle with the top border. If so, it is determined that a complete cell has been found.

[0092] Compared with the prior art, the present invention has the following beneficial effects:

[0093] The present invention reads the PDF file, reads the instructions in the PDF into the memory, converts the contents in the instructions into basic elements, performs multiple rounds of aggregation operations on the basic elements to obtain aggregated objects, calculates the nested relationship of the aggregated objects, and parses the types of the aggregated objects, determines the objects whose type is a table, performs a table analysis on the objects whose type is a table, obtains all the cells of the table, and realizes PDF table extraction and analysis with high accuracy. BRIEF DESCRIPTION OF THE DRAWINGS

[0094] Figure 1 A schematic diagram of a glyph provided for background technology;

[0095] Figure 2 A PDF instruction diagram provided for background technology;

[0096] Figure 3 Convex hull extreme point diagram provided for background technology;

[0097] Figure 4 Undirected graph provided for background technology;

[0098] Figure 5 A flowchart of a method for extracting and parsing a table in a PDF provided in this embodiment

[0099] Figure 6 A schematic diagram of the application of the approximate rectangle algorithm provided in this embodiment;

[0100] Figure 7 The initial state table diagram provided for this embodiment;

[0101] Figure 8 The table diagram provided in this embodiment after segmentation according to the intersection point;

[0102] Fig. 9 A schematic diagram of the sequence of searching cells provided for this embodiment. DETAILED DESCRIPTION

[0103] The accompanying drawings are only used for illustrative purposes and are not to be construed as limiting the present invention;

[0104] It should be clear that the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the embodiments of the present application.

[0105] The terms used in the embodiments of the present application are only for the purpose of describing specific embodiments, and are not intended to limit the embodiments of the present application. The singular forms of "a", "said" and "the" used in the embodiments of the present application and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more associated listed items.

[0106] When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present application. On the contrary, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the attached claims. In the description of the present application, it should be understood that the terms "first", "second", "third", etc. are only used to distinguish similar objects, and do not have to be used to describe a specific order or sequence, nor can they be understood as indicating or implying relative importance. For those of ordinary skill in the art, the specific meanings of the above terms in the present application can be understood according to the specific circumstances.

[0107] In addition, in the description of this application, unless otherwise specified, "multiple" means two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. The present invention is further described below in conjunction with the accompanying drawings and embodiments.

[0108] The present invention is further described below in conjunction with the accompanying drawings and embodiments.

[0109] Example 1

[0110] Please refer to Figure 5 This embodiment provides a method for extracting and parsing a table in a PDF, the method comprising:

[0111] S1: Read the PDF file and read the instructions in the PDF into the memory;

[0112] In a specific embodiment, the method of reading a PDF file and reading instructions in the PDF into a memory includes:

[0113] Use the Apache open source library PDFBox to read files and get PDF instructions.

[0114] S2: Convert the contents of the instruction into basic elements;

[0115] In a specific embodiment, the method of converting the content in the instruction into the most basic elements includes:

[0116] According to PDF's standard self-developed framework, the instructions are parsed and initially converted into basic elements that can be operated.

[0117] S3: Perform multiple rounds of aggregation operations on basic elements to obtain aggregated objects;

[0118] In a specific embodiment, the basic elements include at least one of the following:

[0119] Text, rectangle, line segment.

[0120] In a specific embodiment, a method of performing multiple rounds of aggregation operations on basic elements to obtain aggregated objects includes:

[0121] For the text, first aggregate the texts with the same height according to the y-axis value of the coordinate;

[0122] The duplicated line segments and rectangles are deduplicated using the preset line segment deduplication algorithm;

[0123] The collinear line segments are aggregated once using a preset collinearity judgment algorithm;

[0124] The intersecting line segments are aggregated again through the preset intersection judgment algorithm;

[0125] Aggregate connected rectangles and line segments into an object region.

[0126] In a specific embodiment, the line segment deduplication algorithm includes:

[0127] The line segments are compared with each other in terms of their starting and ending coordinates. If the difference is less than the set threshold, they are considered to be the same line segment.

[0128] The collinearity determination algorithm comprises:

[0129] The two line segments to be judged are regarded as two vectors, line segment 1 is vector ab, and line segment 2 is vector cd. When the areas of triangle abc and triangle abd formed by vector ab and vector cd are both close to 0, it can be determined that ab and cd are collinear.

[0130] The intersection judgment algorithm includes:

[0131] Consider line segment 1 as vector ab and line segment 2 as vector cd. First, determine whether the projections of the two vectors on the x-axis and y-axis overlap, that is, determine whether the ranges covered by the starting points and end points of the two vectors on the x-axis overlap. The same is true for the y-axis. If there is overlap, determine the result of the cross product of vector ab with vectors ac and ad. If the signs of the cross product results are opposite, it can be determined that ac and ad are on both sides of ab, that is, the two line segments have an intersection.

[0132] The methods for aggregating connected rectangles and line segments into an object region include:

[0133] Suppose there is a set, each element of which is a set of line segments that have been judged to be connected;

[0134] If there is a new line segment, traverse this collection to find out whether there is a line segment in the collection that is connected to the current line segment;

[0135] If the new line segment is not connected to any connected line segment group in the set, the new line segment is added to the set as a separate connected line segment group C;

[0136] If the new line segment is only connected to one connected line segment group, add the current line segment to the connected line segment group;

[0137] If the new line segment is connected to multiple connected line segment groups, the multiple connected line segment groups are first aggregated into one connected line segment group, and then the current line segment is placed in this connected line segment group.

[0138] S4: Calculate the nested relationship of the aggregated objects, parse the types of the aggregated objects, and determine the objects whose types are tables;

[0139] In a specific embodiment, a method of calculating the nested relationship of aggregated objects, parsing the type of the aggregated objects, determining an object of table type, and placing the object in a preset position includes:

[0140] Calculate the nesting relationship between regions in pairs to get a two-dimensional array, and then perform a depth-first search on this array to get the nesting relationship between all regions;

[0141] Perform a type judgment on the region, specifically:

[0142] Take out the endpoints of all line segments in the region, form a set, and then find the convex hull of this set;

[0143] The obtained convex hull is described by shape description algorithm and approximate rectangle algorithm. If it is an approximate rectangle and the entire convex hull is completely filled with regular rectangles, it is considered to be a table, otherwise it is considered to be a chart.

[0144] In a specific embodiment, the method of the shape description algorithm includes:

[0145] A1: Define three pointers p1, p2, and p3. In the initial state, p1 is the location of the convex hull pole, p2 is the next point in the counterclockwise direction of p1, and p3 is the next point in the counterclockwise direction of p2.

[0146] A2: Calculate the cosine similarity of vectors p1p2 and p2p3;

[0147] A3: If the cosine similarity does not reach the preset threshold, point p2 to p3, and p3 to the next point in the counterclockwise direction of p3, and repeat step A2;

[0148] A4: If the cosine similarity is less than the first preset threshold, it is considered that a 90-degree corner is found, and p1 is pointed to p2, p2 is pointed to p3, and p3 is pointed to the next point in the counterclockwise direction, and step A2 is repeated;

[0149] A5: Repeat step A2 until the entire convex hull is traversed. Record the number of times p1 changes during the traversal process. The number of times p1 changes is the number of 90-degree angles of the convex hull. When the number of 90-degree angles of the convex hull is 4, the region is determined to be a rectangle.

[0150] Please refer to Figure 6 , the approximate rectangle algorithm includes:

[0151] B1: Take one side of the convex hull as the base, set the endpoints of the side as A and B, determine the two points with the largest and smallest projection values ​​in the AB direction among all the points, and then determine the point farthest from AB. We get three points and a line, that is, a rectangle.

[0152] B2: Traverse each edge of the convex hull and finally find the minimum rectangle;

[0153] B3: Obtain a minimum rectangle, and then calculate the difference between the area of ​​the minimum rectangle and the area of ​​the convex hull. If the difference between the area of ​​the minimum rectangle and the area of ​​the convex hull is less than a second preset threshold, the convex hull is approximately a rectangle.

[0154] S5: Perform a table parsing on the table type object to obtain all cells of the table;

[0155] In a specific embodiment, a method of performing table parsing on an object of table type to obtain all cells of the table includes:

[0156] C1: Calculate the intersection points of all line segments in the object region and obtain a set of intersection points;

[0157] C2: Divide the horizontal line segment through the intersection points calculated in the previous step to obtain several horizontal line segments;

[0158] C3: Divide the vertical line segment through the intersection points calculated in the first step to obtain several vertical line segments;

[0159] C4: Traverse the horizontal line segments from left to right and from top to bottom to find the cell;

[0160] C5: Repeat step C4 until all horizontal line segments are traversed, thus obtaining all cells in the table.

[0161] In a specific embodiment, in step C4, the method of traversing horizontal line segments from left to right and from top to bottom to find a cell includes:

[0162] C41: Set the current horizontal line segment as the top border of the cell, and find the right border of the cell in the vertical line segment;

[0163] C42: If the right border is not found, then search for a horizontal line segment adjacent to the current line segment in the horizontal line segments, aggregate the current line segment and the adjacent line segment together and repeat step C41;

[0164] C43: If the right border is found, then search for the bottom border in the horizontal line segment;

[0165] C44: If the bottom border is not found, then search for a horizontal line segment adjacent to the current line segment in the vertical line segments, aggregate the current line segment and the adjacent line segment together and repeat step C43;

[0166] C45: If the bottom border is found, search for the left border in the vertical line segment;

[0167] C46: If the left border is found, determine whether the found left border can form a closed rectangle with the top border. If so, it is determined that a complete cell has been found.

[0168] Example 2

[0169] Please refer to Figure 5 This embodiment can be regarded as an improved or extended embodiment based on Embodiment 1, specifically:

[0170] S1: Read the PDF file and read the instructions in the PDF into the memory;

[0171] In a specific embodiment, the method of reading a PDF file and reading instructions in the PDF into a memory includes:

[0172] Use the Apache open source library PDFBox to read files and get PDF instructions;

[0173] S2: Convert the contents of the instruction into basic elements;

[0174] In a specific embodiment, the method of converting the content in the instruction into the most basic elements includes:

[0175] According to PDF's standard self-developed framework, the instructions are parsed and initially converted into basic elements that can be operated.

[0176] S3: Perform multiple rounds of aggregation operations on basic elements to obtain aggregated objects;

[0177] In a specific embodiment, the basic elements include at least one of the following:

[0178] Text, rectangle, line segment.

[0179] In a specific embodiment, a method of performing multiple rounds of aggregation operations on basic elements to obtain aggregated objects includes:

[0180] For the text, first aggregate the texts with the same height according to the y-axis value of the coordinate;

[0181] The duplicated line segments and rectangles are deduplicated using the preset line segment deduplication algorithm;

[0182] The collinear line segments are aggregated once using a preset collinearity judgment algorithm;

[0183] The intersecting line segments are aggregated again through the preset intersection judgment algorithm;

[0184] Aggregate connected rectangles and line segments into an object region.

[0185] It should be noted that in PDF, the text in the same line visually is also composed of multiple text rendering instructions.

[0186] It should be noted that for visual effects, PDF will sometimes render the same objects in very close positions, but this will cause problems in structuring this data, so it is necessary to remove these "noises" to get a clean result.

[0187] In a specific embodiment, the line segment deduplication algorithm includes:

[0188] The line segments are compared with each other in terms of their starting and ending coordinates. If the difference is less than the set threshold, they are considered to be the same line segment.

[0189] The collinearity determination algorithm comprises:

[0190] The two line segments to be judged are regarded as two vectors, line segment 1 is vector ab, and line segment 2 is vector cd. When the areas of triangle abc and triangle abd formed by vector ab and vector cd are both close to 0, it can be determined that ab and cd are collinear.

[0191] The intersection judgment algorithm includes:

[0192] Consider line segment 1 as vector ab and line segment 2 as vector cd. First, determine whether the projections of the two vectors on the x-axis and y-axis overlap, that is, determine whether the ranges covered by the starting points and end points of the two vectors on the x-axis overlap. The same is true for the y-axis. If there is overlap, determine the result of the cross product of vector ab with vectors ac and ad. If the signs of the cross product results are opposite, it can be determined that ac and ad are on both sides of ab, that is, the two line segments have an intersection.

[0193] The methods for aggregating connected rectangles and line segments into an object region include:

[0194] Suppose there is a set, each element of which is a set of line segments that have been judged to be connected;

[0195] If there is a new line segment, traverse this collection to find out whether there is a line segment in the collection that is connected to the current line segment;

[0196] If the new line segment is not connected to any connected line segment group in the set, the new line segment is added to the set as a separate connected line segment group C;

[0197] If the new line segment is only connected to one connected line segment group, add the current line segment to the connected line segment group;

[0198] If the new line segment is connected to multiple connected line segment groups, the multiple connected line segment groups are first aggregated into one connected line segment group, and then the current line segment is placed in this connected line segment group.

[0199] S4: Calculate the nested relationship of the aggregated objects, parse the types of the aggregated objects, and determine the objects whose types are tables;

[0200] In a specific embodiment, a method of calculating the nested relationship of aggregated objects, parsing the type of the aggregated objects, determining an object of table type, and placing the object in a preset position includes:

[0201] Calculate the nesting relationship between regions in pairs to get a two-dimensional array, and then perform a depth-first search on this array to get the nesting relationship between all regions;

[0202] Perform a type judgment on the region, specifically:

[0203] Take out the endpoints of all line segments in the region, form a set, and then find the convex hull of this set;

[0204] The obtained convex hull is described by shape description algorithm and approximate rectangle algorithm. If it is an approximate rectangle and the entire convex hull is completely filled with regular rectangles, it is considered to be a table, otherwise it is considered to be a chart (such as a bar chart, pie chart, line chart, etc.).

[0205] In a specific embodiment, the method of the shape description algorithm includes:

[0206] A1: Define three pointers p1, p2, and p3. In the initial state, p1 is the location of the convex hull pole, p2 is the next point in the counterclockwise direction of p1, and p3 is the next point in the counterclockwise direction of p2.

[0207] A2: Calculate the cosine similarity of vectors p1p2 and p2p3;

[0208] A3: If the cosine similarity does not reach the preset threshold, point p2 to p3, and p3 to the next point in the counterclockwise direction of p3, and repeat step A2;

[0209] A4: If the cosine similarity is less than the first preset threshold, it is considered that a 90-degree corner is found, and p1 is pointed to p2, p2 is pointed to p3, and p3 is pointed to the next point in the counterclockwise direction, and step A2 is repeated;

[0210] A5: Repeat step A2 until the entire convex hull is traversed. Record the number of times p1 changes during the traversal process. The number of times p1 changes is the number of 90-degree angles of the convex hull. When the number of 90-degree angles of the convex hull is 4, the region is determined to be a rectangle.

[0211] Please refer to Figure 6 , the approximate rectangle algorithm includes:

[0212] B1: Take one side of the convex hull as the base, set the endpoints of the side as A and B, determine the two points with the largest and smallest projection values ​​in the AB direction among all the points, and then determine the point farthest from AB. We get three points and a line, that is, a rectangle.

[0213] B2: Traverse each edge of the convex hull and finally find the minimum rectangle;

[0214] B3: Obtain a minimum rectangle, and then calculate the difference between the area of ​​the minimum rectangle and the area of ​​the convex hull. If the difference between the area of ​​the minimum rectangle and the area of ​​the convex hull is less than a second preset threshold, the convex hull is approximately a rectangle.

[0215] S5: Perform a table parsing on the table type object to obtain all cells of the table;

[0216] In a specific embodiment, a method of performing table parsing on an object of table type to obtain all cells of the table includes:

[0217] C1: Calculate the intersection points of all line segments in the object region and obtain a set of intersection points;

[0218] C2: Divide the horizontal line segment through the intersection points calculated in the previous step to obtain several horizontal line segments;

[0219] C3: Divide the vertical line segment through the intersection points calculated in the first step to obtain several vertical line segments;

[0220] C4: Traverse the horizontal line segments from left to right and from top to bottom to find the cell;

[0221] C5: Repeat step C4 until all horizontal line segments are traversed, thus obtaining all cells in the table.

[0222] In a specific embodiment, in step C4, the method of traversing horizontal line segments from left to right and from top to bottom to find a cell includes:

[0223] C41: Set the current horizontal line segment as the top border of the cell, and find the right border of the cell in the vertical line segment;

[0224] C42: If the right border is not found, then search for a horizontal line segment adjacent to the current line segment in the horizontal line segments, aggregate the current line segment and the adjacent line segment together and repeat step C41;

[0225] C43: If the right border is found, then search for the bottom border in the horizontal line segment;

[0226] C44: If the bottom border is not found, then search for a horizontal line segment adjacent to the current line segment in the vertical line segments, aggregate the current line segment and the adjacent line segment together and repeat step C43;

[0227] C45: If the bottom border is found, search for the left border in the vertical line segment;

[0228] C46: If the left border is found, determine whether the found left border can form a closed rectangle with the top border. If so, it is determined that a complete cell has been found.

[0229] Specifically, please refer to Figure 7 , Figure 7 This is the initial state table diagram. Cut the initial state table according to the intersection point. The table after cutting is as follows Figure 8As shown, after cutting according to the intersection point, line segment AE will become line segment AB, line segment BC, line segment CD, line segment DE, and BK will become line segment BF, line segment FG, line segment GH, line segment HI, line segment IJ, and line segment JK.

[0230] The actual order of searching cells according to the algorithm is:

[0231] Please refer to Fig. 9 , the first traversal will follow Fig. 9 Find these edges in the order marked with blue numbers to form cells. The second time will be Fig. 9 Find the second cell in the order marked by the red numbers, and then repeat the same process many times until all edges are traversed. ( Fig. 9 In the middle, blue 2 and red 5 are on the same edge)

[0232] Obviously, the above embodiments of the present invention are merely examples for clearly illustrating the present invention, and are not intended to limit the embodiments of the present invention. For those skilled in the art, other different forms of changes or modifications can be made based on the above description. It is not necessary and impossible to list all the embodiments here. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the claims of the present invention.

Claims

1. A method for extracting and parsing a table in a PDF, characterized in that: The method comprises: Read the PDF file and read the instructions in the PDF into the memory; Convert the contents of the instruction into basic elements; Perform multiple rounds of aggregation operations on basic elements to obtain aggregated objects; Calculate the nested relationship of the aggregated objects, parse the type of the aggregated objects, and determine the objects whose type is a table; Perform a table parsing on the table type object to get all the cells in the table.

2. The method for extracting and parsing a table in a PDF according to claim 1, characterized in that: The methods for reading PDF files and reading instructions in PDF into memory include: Use the Apache open source library PDFBox to read files and get PDF instructions.

3. The method for extracting and parsing a table in a PDF according to claim 1, characterized in that: Methods for converting the contents of a directive into its most basic elements include: According to PDF's standard self-developed framework, the instructions are parsed and initially converted into basic elements that can be operated.

4. The method for extracting and parsing a table in a PDF according to claim 1, characterized in that: The basic elements include at least one of the following: Text, rectangle, line segment.

5. The method for extracting and parsing a table in a PDF according to claim 4, characterized in that: Methods for performing multiple rounds of aggregation operations on basic elements to obtain aggregated objects include: For the text, first aggregate the texts with the same height according to the y-axis value of the coordinate; The duplicated line segments and rectangles are deduplicated using the preset line segment deduplication algorithm; The collinear line segments are aggregated once using a preset collinearity judgment algorithm; The intersecting line segments are aggregated again through the preset intersection judgment algorithm; Aggregate connected rectangles and line segments into an object region.

6. The method for extracting and parsing a table in a PDF according to claim 5, characterized in that: The line segment deduplication algorithm includes: The line segments are compared with each other in terms of their starting and ending coordinates. If the difference is less than the set threshold, they are considered to be the same line segment. The collinearity determination algorithm comprises: The two line segments to be judged are regarded as two vectors, line segment 1 is vector ab, and line segment 2 is vector cd. When the areas of triangle abc and triangle abd formed by vector ab and vector cd are both close to 0, it can be determined that ab and cd are collinear. The intersection judgment algorithm includes: Consider line segment 1 as vector ab and line segment 2 as vector cd. First, determine whether the projections of the two vectors on the x-axis and y-axis overlap, that is, determine whether the ranges covered by the starting points and end points of the two vectors on the x-axis overlap. The same is true for the y-axis. If there is overlap, determine the result of the cross product of vector ab with vectors ac and ad. If the signs of the cross product results are opposite, it can be determined that ac and ad are on both sides of ab, that is, the two line segments have an intersection. The methods for aggregating connected rectangles and line segments into an object region include: Suppose there is a set, each element of which is a set of line segments that have been judged to be connected; If there is a new line segment, traverse this collection to find out whether there is a line segment in the collection that is connected to the current line segment; If the new line segment is not connected to any connected line segment group in the set, the new line segment is added to the set as a separate connected line segment group C; If the new line segment is only connected to one connected line segment group, add the current line segment to the connected line segment group; If the new line segment is connected to multiple connected line segment groups, the multiple connected line segment groups are first aggregated into one connected line segment group, and then the current line segment is placed in this connected line segment group.

7. The method for extracting and parsing a table in a PDF according to claim 1, characterized in that: The method of calculating the nested relationship of the aggregated objects, parsing the type of the aggregated objects, determining the object of the table type, and placing the object in a preset position includes: Calculate the nesting relationship between regions in pairs to get a two-dimensional array, and then perform a depth-first search on this array to get the nesting relationship between all regions; Perform a type judgment on the region, specifically: Take out the endpoints of all line segments in the region, form a set, and then find the convex hull of this set; The obtained convex hull is described by shape description algorithm and approximate rectangle algorithm. If it is an approximate rectangle and the entire convex hull is completely filled with regular rectangles, it is considered to be a table, otherwise it is considered to be a chart.

8. The method for extracting and parsing a table in a PDF according to claim 7, characterized in that: The method of the shape description algorithm comprises: A1: Define three pointers p1, p2, and p3. In the initial state, p1 is the location of the convex hull pole, p2 is the next point in the counterclockwise direction of p1, and p3 is the next point in the counterclockwise direction of p2. A2: Calculate the cosine similarity of vectors p1p2 and p2p3; A3: If the cosine similarity does not reach the preset threshold, point p2 to p3, and p3 to the next point in the counterclockwise direction of p3, and repeat step A2; A4: If the cosine similarity is less than the first preset threshold, it is considered that a 90-degree corner is found, and p1 is pointed to p2, p2 is pointed to p3, and p3 is pointed to the next point in the counterclockwise direction, and step A2 is repeated; A5: Repeat step A2 until the entire convex hull is traversed. Record the number of times p1 changes during the traversal process. The number of times p1 changes is the number of 90-degree angles of the convex hull. When the number of 90-degree angles of the convex hull is 4, the region is determined to be a rectangle. The approximate rectangle algorithm includes: B1: Take one side of the convex hull as the base, set the endpoints of the side as A and B, determine the two points with the largest and smallest projection values ​​in the AB direction among all the points, and then determine the point farthest from AB. We get three points and a line, that is, a rectangle. B2: Traverse each edge of the convex hull and finally find the minimum rectangle; B3: Obtain a minimum rectangle, and then calculate the difference between the area of ​​the minimum rectangle and the area of ​​the convex hull. If the difference between the area of ​​the minimum rectangle and the area of ​​the convex hull is less than a second preset threshold, the convex hull is approximately a rectangle.

9. The method for extracting and parsing a table in a PDF according to claim 8, characterized in that: Perform a table parsing on an object of table type and get all the cells of the table: C1: Calculate the intersection points of all line segments in the object region and obtain a set of intersection points; C2: Divide the horizontal line segment through the intersection points calculated in the previous step to obtain several horizontal line segments; C3: Divide the vertical line segment through the intersection points calculated in the first step to obtain several vertical line segments; C4: Traverse the horizontal line segments from left to right and from top to bottom to find the cell; C5: Repeat step C4 until all horizontal line segments are traversed, thus obtaining all cells in the table.

10. The method for extracting and parsing a table in a PDF according to claim 9, characterized in that: In step C4, the method of traversing horizontal line segments from left to right and from top to bottom to find a cell includes: C41: Set the current horizontal line segment as the top border of the cell, and find the right border of the cell in the vertical line segment; C42: If the right border is not found, then search for a horizontal line segment adjacent to the current line segment in the horizontal line segments, aggregate the current line segment and the adjacent line segment together and repeat step C41; C43: If the right border is found, then search for the bottom border in the horizontal line segment; C44: If the bottom border is not found, then search for a horizontal line segment adjacent to the current line segment in the vertical line segments, aggregate the current line segment and the adjacent line segment together and repeat step C43; C45: If the bottom border is found, search for the left border in the vertical line segment; C46: If the left border is found, determine whether the found left border can form a closed rectangle with the top border. If so, it is determined that a complete cell has been found.

Citation Information

Patent Citations

  • Form processing method and device in document conversion

    CN101866335A

  • Table vector analysis method and apparatus for PDF document

    CN108446264A

  • PDF table information extraction method and related device

    CN112069991A

  • Nested table extraction method and device, and storage medium

    CN112668289A

  • PDF (Portable Document Format) document-oriented table data extraction method and device

    CN117454851A