A method for extracting and parsing tables in PDF

By reading the instructions from the PDF file and converting them into basic elements, performing multiple rounds of aggregation and nested relationship calculations, and combining depth-first search and shape description algorithms, the problem of the difficulty in structuring table information in PDF files is solved, achieving high-accuracy table extraction and parsing.

CN119759355BActive Publication Date: 2025-09-02HANGZHOU JIAYAN XIAOAN INFORMATION TECHNOLOGY CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

Existing technologies struggle to structure the structural information in PDF files into a computer-understandable data structure, especially for extracting and parsing table information.

Method used

By reading the instructions in the PDF file, converting them into basic elements, performing multiple rounds of aggregation operations, calculating nesting relationships, and determining the table object through depth-first search and shape description algorithms, the final parsing yields all the cells of the table.

Benefits of technology

It achieves high-accuracy PDF table extraction and parsing, and can structure the table information in PDF files into an understandable data structure.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119759355B_ABST
    Figure CN119759355B_ABST
Patent Text Reader

Abstract

The present invention discloses a method for extracting and parsing tables in PDFs. The method comprises: reading a PDF file and reading instructions from the PDF into memory; converting the contents of the instructions into basic elements; performing multiple rounds of aggregation operations on the basic elements to obtain aggregated objects; calculating the nesting relationships of the aggregated objects and parsing the types of the aggregated objects to determine objects of table type; and performing a table parsing operation on the table type to obtain all cells of the table. The present invention achieves high-accuracy PDF table extraction and parsing.
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 tables in PDF. Background Art

[0002] Concept 1: What is PDF

[0003] Portable Document Format (PDF) is a file format that presents documents in a manner 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 required for display. In addition to flat text and graphics, PDF files may also include logical structure elements, interactive elements such as annotations, forms, layers, rich media (including video content), three-dimensional objects using U3D or PRC, and a variety of other data content. The PDF specification also provides the encryption, digital signatures, document attachments, and metadata required to enable these features.

[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 if an e-book could be deleted by any user during the transmission process, it would be unacceptable in reality. On the other hand, companies have an urgent need: the same document can still display the same visual content on different machines and different operating systems after being transmitted. To this day, we find that the same Word document displays differently on Windows and macOS. But PDF documents can do this.

[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 information such as font size, font bold, and font family. For example, we are 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 in "Times New Roman" but a glyph in "Arial."

[0010] A code point is a number that represents a specific glyph and is used to tell the computer which glyph to render. For example, the glyph "g" has a code point of 0067.

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

[0012] like Figure 2 As shown, the first program instruction means: "At position (503,688), draw an m glyph using white color and a font size of 8.5..." Through these instructions, the PDF viewer renders the glyphs on the screen as green, blue, and black lines, ultimately resulting in the visual style shown in the left image above. Therefore, regardless of the 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 known as a Euclidean vector or geometric vector) is a quantity that has both magnitude and direction. It can be visually represented as a line segment with an arrow. The direction of the arrow represents the direction of the vector, and the length of the line segment represents the magnitude of the vector.

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

[0016] Vector cross product: Assume vector a is (x1, y1) and vector b is (x2, y2). The result of the cross product is: x1*y2 - x2* y1, geometrically speaking, c is the area of ​​a parallelogram 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: Assume 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 measures the similarity between two vectors by calculating the cosine of the angle between them. Cosine similarity ranges from -1 to 1, where 1 indicates the two vectors are identical, -1 indicates they are completely opposite, and 0 indicates they are orthogonal, meaning they have no correlation. It is calculated as the dot product of the two vectors divided by (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 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 extreme point is the point with the smallest x and the smallest y, that is Figure 3 The location of the red dot.

[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 can't go any deeper, and each node can only be visited once.

[0028] Please refer to Figure 4 , Figure 4 It 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 out! Backtrack to A)->C->F->H->G->D (no way out, eventually backtrack to A, A has no unvisited adjacent nodes, and this search ends).

[0029] Concept 6 PDF Questions

[0030] A PDF document simply stores program instructions for drawing visual content (including only glyphs, lines, color blocks, and images). 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 a PDF file, a computer cannot directly determine structural information such as "where is a table," "whether this paragraph spans columns or pages," or "which content is the third section of the article." However, humans have a powerful visual system and cognitive ability that can combine and abstract independent characters, lines, color blocks, and other objects in a document, thereby fully understanding the rich and complete structural information contained in the document.

[0031] The rich information contained in PDF documents holds significant value. For example, in the financial sector, if an investor wants to analyze and compare the operating performance of several listed companies this year, they would likely read their annual reports. For example, they might want to learn about the company's core businesses and products; analyze the balance sheet, income statement, cash flow statement, and other information contained in the annual report; and identify its suppliers. Given the sheer number of listed companies, and the fact that an annual report can be around 300 pages long, replacing manual labor with machines would significantly save time and money. The most crucial foundation for understanding and analyzing the knowledge contained in PDF documents is obtaining the rich, comprehensive, and panoramic structural information within them. Restructuring the computer-incomprehensible information in PDF files into understandable data structures is a technical challenge facing those skilled in the art.

[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 image 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 tables in PDF files, so as to accurately structure the information in the PDF file that cannot be understood by the computer into a comprehensible data structure.

[0034] To solve the above technical problems, the present invention provides a method for extracting and parsing tables in PDF, wherein the method comprises:

[0035] Read the PDF file and read the instructions in the PDF into 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 nesting relationship of the aggregated objects, parse the type of the aggregated objects, and determine the objects whose type is table;

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

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

[0041] Use the Apache open source library PDFBox to read files and obtain 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 the standard self-developed framework of PDF, 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 text with the same height according to the y-axis value of the coordinate;

[0048] The duplicate 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 using 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 starting and ending coordinates of the line segments are compared with each other. If the difference is less than the set threshold, they are considered to be the same line segment.

[0054] The collinearity determination algorithm includes:

[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 and ending points of the two vectors on the x-axis overlap. The same applies to the y-axis. If there is overlap, then 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 opposite 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 where each element is a set of line segments that have been judged to be connected;

[0060] If there is a new line segment, traverse this set to find out whether there is a line segment in the set 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, first aggregate the multiple connected line segment groups into one connected line segment group, and then put the current line segment into this connected line segment group.

[0064] As a preferred solution, a method for calculating the nesting 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:

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

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

[0067] Take 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 the shape description algorithm and the 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. Initially, p1 is the location of the convex hull extreme point, p2 is the next point counterclockwise from p1, and p3 is the next point counterclockwise from p2.

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

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

[0073] A4: If the cosine similarity reaches the first preset threshold, it is considered that a 90-degree corner has been found. At the same time, p1 is pointed to p2, p2 to p3, and p3 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 in the convex hull. When the number of 90-degree angles in the convex hull reaches 4, determine that the region is a rectangle.

[0075] The approximate rectangle algorithm includes:

[0076] B1: Take one side of the convex hull as the base, let the endpoints of the side be 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-type object is parsed once to obtain all cells of the table, including:

[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 by 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 cells;

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

[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 search for the right border of the cell in the vertical line segment;

[0087] C42: If the right border is not found, search for a horizontal line segment adjacent to the current line segment among 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, search for the bottom border in the horizontal line segment;

[0089] C44: If the bottom border is not found, 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 a PDF file, reads the instructions in the PDF into memory, converts the content in the instructions into basic elements, performs multiple rounds of aggregation operations on the basic elements to obtain aggregated objects, calculates the nesting relationship of the aggregated objects, and parses the types of the aggregated objects to determine the objects of table type. The present invention performs a table parsing on the objects of table type to obtain all cells of the table, thereby achieving high-accuracy PDF table extraction and parsing. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0095] Figure 2 A schematic diagram of PDF instructions provided as background technology.

[0096] Figure 3 Schematic diagram of convex hull extreme points provided for background technology.

[0097] Figure 4 An undirected graph is provided for background technology.

[0098] Figure 5 This is a flow chart of a method for extracting and parsing tables in PDF provided in this embodiment.

[0099] Figure 6 This is a schematic diagram of the application of the approximate rectangle algorithm provided in this embodiment.

[0100] Figure 7 This is a table diagram of the initial state provided for this embodiment.

[0101] Figure 8 This is a table diagram provided in this embodiment after segmentation according to the intersection points.

[0102] Figure 9 A schematic diagram of the order of searching cells provided in this embodiment. DETAILED DESCRIPTION

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

[0104] It should be clear that the embodiments described are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments of the present application, all other embodiments obtained by ordinary technicians in this field without making 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 for the purpose of describing specific embodiments only and are not intended to limit the embodiments of the present application. The singular forms "a," "the," 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 otherwise. 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 accompanying 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 appended 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 are not necessarily 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, "plurality" refers to two or more. "And / or" describes the relationship between associated objects, indicating that three relationships can exist. For example, "A and / or B" can mean: A exists alone, A and B exist simultaneously, and B exists alone. The character " / " generally indicates that the associated objects are in an "or" relationship. The present invention is further described below with reference to the accompanying drawings and examples.

[0108] The present invention is further described below with reference to 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, a method for 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 obtain 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 the standard self-developed framework of PDF, the instructions are parsed and initially converted into basic elements that can be operated.

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

[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 text with the same height according to the y-axis value of the coordinate;

[0122] The duplicate 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 using 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 starting and ending coordinates of the line segments are compared with each other. If the difference is less than the set threshold, they are considered to be the same line segment.

[0128] The collinearity determination algorithm includes:

[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 and ending points of the two vectors on the x-axis overlap. The same applies to the y-axis. If there is overlap, then 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 opposite 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 where each element is a set of line segments that have been judged to be connected;

[0134] If there is a new line segment, traverse this set to find out whether there is a line segment in the set 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, first aggregate the multiple connected line segment groups into one connected line segment group, and then put the current line segment into this connected line segment group.

[0138] S4: Calculate the nesting relationship of the aggregated objects, and resolve the type of the aggregated objects to determine the objects whose type is a table;

[0139] In a specific embodiment, a method for calculating nested relationships of aggregated objects, parsing the types 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 pairwise to obtain a two-dimensional array, and then perform a depth-first search on this array to obtain the nesting relationship between all regions;

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

[0142] Take 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 the shape description algorithm and the 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 shape description algorithm method includes:

[0145] A1: Define three pointers p1, p2, and p3. Initially, p1 is the location of the convex hull extreme point, p2 is the next point counterclockwise from p1, and p3 is the next point counterclockwise from p2.

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

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

[0148] A4: If the cosine similarity reaches the first preset threshold, it is considered that a 90-degree corner has been found. At the same time, p1 is pointed to p2, p2 to p3, and p3 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 in the convex hull. When the number of 90-degree angles in the convex hull reaches 4, determine that the region is 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, let the endpoints of the side be 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 for performing table parsing on a table object 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 by 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 cells;

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

[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 search for the right border of the cell in the vertical line segment;

[0163] C42: If the right border is not found, search for a horizontal line segment adjacent to the current line segment among 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, search for the bottom border in the horizontal line segment;

[0165] C44: If the bottom border is not found, 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 improvement or extension of the embodiment 1, specifically:

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

[0171] In a specific embodiment, a method for 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 obtain 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 the standard self-developed framework of PDF, the instructions are parsed and initially converted into basic elements that can be operated.

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

[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 text with the same height according to the y-axis value of the coordinate;

[0181] The duplicate 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 using 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 on the same visual line is also composed of multiple text rendering instructions.

[0186] It should be noted that for visual effects, PDF sometimes renders 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 obtain clean results.

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

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

[0189] The collinearity determination algorithm includes:

[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 and ending points of the two vectors on the x-axis overlap. The same applies to the y-axis. If there is overlap, then 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 opposite 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 where each element is a set of line segments that have been judged to be connected;

[0195] If there is a new line segment, traverse this set to find out whether there is a line segment in the set 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, first aggregate the multiple connected line segment groups into one connected line segment group, and then put the current line segment into this connected line segment group.

[0199] S4: Calculate the nesting relationship of the aggregated objects, and resolve the type of the aggregated objects to determine the objects whose type is a table;

[0200] In a specific embodiment, a method for calculating nested relationships of aggregated objects, parsing the types 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 pairwise to obtain a two-dimensional array, and then perform a depth-first search on this array to obtain the nesting relationship between all regions;

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

[0203] Take 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 using a shape description algorithm and an approximate rectangle algorithm. If it is an approximate rectangle and the entire convex hull is completely filled with regular rectangles, it is considered a table; otherwise, it is considered a chart (such as a bar chart, pie chart, line chart, etc.).

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

[0206] A1: Define three pointers p1, p2, and p3. Initially, p1 is the location of the convex hull extreme point, p2 is the next point counterclockwise from p1, and p3 is the next point counterclockwise from p2.

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

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

[0209] A4: If the cosine similarity reaches the first preset threshold, it is considered that a 90-degree corner has been found. At the same time, p1 is pointed to p2, p2 to p3, and p3 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 in the convex hull. When the number of 90-degree angles in the convex hull reaches 4, determine that the region is 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, let the endpoints of the side be 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 for performing table parsing on a table object 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 by 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 cells;

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

[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 search for the right border of the cell in the vertical line segment;

[0224] C42: If the right border is not found, search for a horizontal line segment adjacent to the current line segment among 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, search for the bottom border in the horizontal line segment;

[0226] C44: If the bottom border is not found, 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 order in which the algorithm actually searches for cells is:

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

[0232] Obviously, the above embodiments of the present invention are merely examples for the purpose of clearly illustrating the present invention, and are not intended to limit the embodiments of the present invention. Those skilled in the art will appreciate that other variations or modifications can be made based on the above description. It is not necessary and impossible to enumerate all embodiments here. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention shall be included within the scope of protection of the claims of the present invention.

Claims

1. A method for extracting and parsing tables in PDF, characterized in that: The method comprises: Read the PDF file and read the instructions in the PDF into 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 nesting relationship of the aggregated objects, parse the type of the aggregated objects, and determine the objects whose type is table; Perform a table parsing on the table type object to obtain all the cells of the table; The method of calculating the nesting 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 pairwise to obtain a two-dimensional array, and then perform a depth-first search on this array to obtain the nesting relationship between all regions; Perform a type judgment on the region, specifically: Take 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 the shape description algorithm and the approximate rectangle algorithm. If it is an approximate rectangle and the entire convex hull is completely filled with regular rectangles, it is considered a table; otherwise, it is considered a chart. The method of the shape description algorithm includes: A1: Define three pointers p1, p2, and p3. Initially, p1 is the location of the convex hull extreme point, p2 is the next point counterclockwise from p1, and p3 is the next point counterclockwise from p2. A2: Calculate the cosine similarity between vectors p1p2 and p2p3; A3: If the cosine similarity does not reach the first preset threshold, point p2 to p3, and p3 to the next point counterclockwise from p3, and repeat step A2; A4: If the cosine similarity reaches the first preset threshold, it is considered that a 90-degree corner has been found. At the same time, p1 is pointed to p2, p2 to p3, and p3 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 in the convex hull. When the number of 90-degree angles in the convex hull reaches 4, determine that the region is a rectangle. The approximate rectangle algorithm includes: B1: Take one side of the convex hull as the base, let the endpoints of the side be 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.

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

3. The method for extracting and parsing a table in a PDF according to claim 1, wherein: Methods for converting the contents of a directive into its most basic elements include: According to the standard self-developed framework of PDF, 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, wherein: 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 duplicate 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 using 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 starting and ending coordinates of the line segments are compared with each other. If the difference is less than the set threshold, they are considered to be the same line segment. The collinearity determination algorithm includes: 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 and ending points of the two vectors on the x-axis overlap. The same applies to the y-axis. If there is overlap, then 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 opposite 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 where each element is a set of line segments that have been judged to be connected; If there is a new line segment, traverse this set to find out whether there is a line segment in the set 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, first aggregate the multiple connected line segment groups into one connected line segment group, and then put the current line segment into this connected line segment group.

7. The method for extracting and parsing a table in a PDF according to claim 1, wherein: Perform a table parsing on a table object to obtain all the cells in 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 by 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 cells; C5: Repeat step C4 until all horizontal line segments are traversed, and all cells in the table are obtained.

8. The method for extracting and parsing a table in a PDF according to claim 7, characterized in that: In step C4, the method of traversing the horizontal line segments from left to right and from top to bottom to find the cell includes: C41: Set the current horizontal line segment as the top border of the cell, and search for the right border of the cell in the vertical line segment; C42: If the right border is not found, search for a horizontal line segment adjacent to the current line segment among 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, search for the bottom border in the horizontal line segment; C44: If the bottom border is not found, 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

  • PDF table information extraction method and related device

    CN112069991A

  • Nested table extraction method and device, and storage medium

    CN112668289A

  • Method and system for identifying incomplete frame line table for analytic PDF (Portable Document Format)

    CN118351557A