PDF-to-Word two-dimensional array table recognition method and device and readable storage medium
Through the two-dimensional array table recognition method, the infection algorithm and island algorithm are used to identify the table area and border information in PDF files, which solves the problem of insufficient speed and accuracy of table recognition in the existing technology, and realizes efficient table information construction and recognition .
Patent Information
- Application Number
- CN202411450005.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-17
- Publication Date
- 2025-01-10
AI Technical Summary
In the process of converting PDF files to Word files, the table recognition speed and accuracy are insufficient, and the correct table information cannot be effectively constructed.
The table recognition method of PDF to Word is adopted to form line segments by obtaining coordinate information in PDF, building a two-dimensional array, using the infection algorithm and island algorithm to determine the table area and border information, generate a closed graph, and finally construct the complete table information.
It improves the efficiency and accuracy of table recognition, combines the advantages of speed and accuracy, directly reads the PDF underlying operator information, and quickly locates table area and cell information.
Smart Images

Figure CN119294374A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of PDF analysis, and in particular, relates to a PDF to Word two-dimensional array table recognition method, device and readable storage medium. Background Art
[0002] In the translation industry, PDF files cannot be directly parsed and restored due to the special format of the files. Therefore, during the parsing process, PDF files need to be converted into Word files. During the conversion process, it is crucial to construct the correct table information based on the underlying operator information of the PDF. The existing table recognition technology usually uses OCR to recognize wireframes and then determines the table area by the intersection of the wireframes, resulting in poor speed and accuracy in table recognition. Summary of the invention
[0003] The purpose of the present invention is to provide a PDF to Word two-dimensional array table recognition method to solve the technical problems existing in the prior art.
[0004] In order to achieve the above object, the technical solution adopted by the present invention is as follows: A PDF to Word two-dimensional array table recognition method includes the following steps: Step S1: construct line segments: obtain the coordinate information in the PDF, and generate all line segment information according to the path formed by the coordinate information; Step S2: Two-dimensional array conversion: determine the size of the two-dimensional array according to the width and height of the PDF page: the height is used as the row and the width is used as the column. According to the line segment information obtained in step S1, determine the position of the line segment in the two-dimensional array, and set the value of the position of the area where the line segment is located to 1; Step S3: Constructing a table area using an infection algorithm: Step S3.1: Traversing the two-dimensional array, when encountering a region position whose value is equal to 1, starting from the current position, infect all adjacent region positions whose values are 1 using the infection algorithm; Step S3.2: Changing the value of the region position from 1 to 2; Step S3.3: Obtaining the overall infection range, and constructing a table area based on the infection range; Step S4: constructing cell information using an island algorithm: based on the table area constructed in step S3, traverse the table area, and when a value of a certain area position is equal to 0, start from the current position and use the island algorithm to obtain all islands in the table area, each island is each cell; Step S5: Obtain left and right borders and upper and lower borders information, and construct border information according to the left and right borders and upper and lower borders information: Step S6: Generate and verify closed area information: Use the cell information in step S4 and the border information in step S5 to form a closed figure; Step S7: Construct table information: After sorting the closed figures obtained in step S6 from top to bottom and from left to right, determine the continuity between the closed figures. If any edges are adjacent or overlapping, store them in the same set, and finally determine a complete table with a border.
[0005] In one implementation, the method for obtaining the coordinate information in the PDF in step S1 is as follows: two coordinate information are obtained through the parameters of the PDF operator: (x1, y1) and (x2, y2).
[0006] In one embodiment, the method for constructing the table area in step S3.3 is as follows: if a certain infection range has no gaps and is a complete rectangle, and the interior of the rectangle meets the characteristics of a table, then the infection range is determined to be a table area.
[0007] The characteristics of a table include any of the following situations: (1) the table as a whole is closed, and there is no unclosed cell in the outermost layer of the table; (2) The border of the table is not composed of curves or line segments at special angles; (3) The text will not overlap with the cell border.
[0008] In one implementation, the specific method for obtaining the left and right borders in step S5 is as follows: (51) constructing a two-dimensional preprocessing array d in the vertical direction, in which the two-dimensional preprocessing array d records the heights of consecutive values of 2 below each position; (52) Traverse the table area obtained in step S3 in the two-dimensional array. When encountering a position with a value of 2, record the current position as (i, j), and obtain the height height of the current position from the two-dimensional preprocessing array d; (53) Starting from the same row of the current position (i, j), traverse to the right and obtain the positions in the subsequent columns that are continuous and have a height equal to height from the two-dimensional preprocessing array d. The end position is recorded as k; (54) Generate a vertical border as the left border or right border of the table area obtained in step S3, with a border height of height and a border width of k-j+1; (55) Complete the traversal, obtain all vertical borders, and use all vertical borders as left borders and right borders.
[0009] In one embodiment, the specific method of obtaining the upper and lower borders in step S5 is as follows: (1) Construct a horizontal two-dimensional preprocessing array r, in which the width of the continuous values on the right side of each position is recorded; (2) Traverse the table area obtained in step S3 in the two-dimensional array. When encountering a position with a value of 2, record the current position as (i, j) and obtain the width width of the current position from the two-dimensional preprocessing array r; (3) Starting from the same row of the current position (i, j), traverse downwards and obtain the positions in the subsequent columns that are continuous and have a width equal to width from the two-dimensional preprocessing array r. The end position is recorded as k; (4) Generate a horizontal border as the upper border or lower border of the table area obtained in step S3, with the border height being width and the border height being k-j+1; (5) After the traversal is completed, all horizontal borders are obtained and used as the upper border and the lower border.
[0010] In one embodiment, the two-dimensional preprocessing array d and the two-dimensional preprocessing array r are both constructed using a prefix sum algorithm.
[0011] In one implementation, the specific method of step S6 is as follows: sort the left border, right border, top border, and bottom border, traverse the cell information obtained in step S4, and use a binary search method to locate the border information closest to the cell based on the vertex coordinate information of the cell. If the cell can find the left border, right border, top border, and bottom border at the same time, the current cell is determined to be a legal cell. If the left border, right border, top border, and bottom border cannot be found at the same time, the cell is discarded.
[0012] In order to achieve the above-mentioned object, the present invention also provides a computer-readable storage medium having a computer program stored thereon, characterized in that the computer program is executed by a processor to implement the PDF to Word two-dimensional array table recognition method.
[0013] In order to achieve the above-mentioned purpose, the present invention also provides a device for PDF to Word two-dimensional array table recognition, comprising: a processor and a memory; the memory is used to store a computer program; the processor is connected to the memory, and is used to execute the computer program stored in the memory, so that the device for PDF to Word two-dimensional array table recognition executes the PDF to Word two-dimensional array table recognition method.
[0014] Compared with the prior art, the present invention has the following beneficial effects: By adopting the present invention, the operator information at the bottom layer of the PDF is directly read, and the wireframe information is generated according to the information carried by the operator, and then the wireframe information is converted into a two-dimensional array. There is no need to pay attention to the continuity of the line segments and whether they should be merged. In the process of converting the two-dimensional array, this task has actually been completed with the help of the array. Subsequently, the infection algorithm, the island algorithm, and the pre-processing array are used to quickly locate the table area, the cell area, and the border information, thereby greatly improving the recognition and generation efficiency of the table, and having the advantages of both speed and accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] Figure 1 This is a schematic diagram of the process of Example 1 of the present invention.
[0016] Figure 2 This is a principle block diagram of Example 3 of the present invention. DETAILED DESCRIPTION
[0017] In order to make those skilled in the art have a clearer understanding and knowledge of the present invention, the present invention is further described in detail below in conjunction with the embodiments. It should be known that the specific embodiments described below are only used to explain the present invention, and are convenient for understanding. The technical solutions provided by the present invention are not limited to the technical solutions provided by the following embodiments, and the technical solutions provided by the embodiments should not limit the protection scope of the present invention.
[0018] It should be noted that the illustrations provided in the following embodiments are only used to illustrate the basic concept of the present invention in a schematic manner. Therefore, the drawings only show components related to the present invention rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the form, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.
[0019] Example 1 like Figure 1 As shown, this embodiment provides a PDF to Word two-dimensional array table recognition method, the principle of which is as follows: use the PDFBOX framework to read PDF, construct line segment information according to the PDF operator, filter the line segments formed according to the coordinate information, and convert them into a two-dimensional array according to their coordinates; traverse the two-dimensional array to filter out the table area that may exist therein. After determining the table area, determine the cell information in the table; generate the corresponding border information according to the determined cell information; and finally combine a complete table according to the border information.
[0020] The specific steps of this method are as follows: 1. Step S1: Constructing line segments Get the coordinate information in the PDF, and generate all the line segment information according to the path formed by the coordinate information; the method for getting the coordinate information in the PDF is as follows: get two coordinate information (x1, y1) and (x2, y2) through the parameters of the PDF operator. The two points can form a line, so as to determine the line segment information; it should be noted that (x1, y1) and (x2, y2) are references to the coordinate information, not limitations.
[0021] 2. Step S2: Two-dimensional array conversion The size of the two-dimensional array is determined according to the width and height of the PDF page: the height is used as the row and the width is used as the column. According to the above line segment information, the position of the line segment in the two-dimensional array is determined, and the value of the position of the area where the line segment is located is set to 1. In this step, each line segment has two endpoints P1 and P2. The endpoints are mapped to the two-dimensional array, a straight line is determined based on the two points, and the position of the current line segment in the two-dimensional array is determined.
[0022] Step S3: Constructing the table area using the infection algorithm The specific implementation method of this step includes the following steps: Step S3.1: traverse the two-dimensional array, and when a certain area position is found to have a value equal to 1, start from the current position and use the infection algorithm to infect all adjacent area positions whose values are 1; the infection algorithm is an existing mature algorithm, so it will not be described in detail here; Step S3.2: Change the value of the area position from 1 to 2; Step S3.3: Obtain the overall infection range, and construct a table area based on the infection range. The method for constructing the table area is as follows: If an infection range has no gaps and is a complete rectangle, and the interior of the rectangle meets the characteristics of a table, then the infection range is judged to be a table area.
[0023] In this step, the characteristics of the table include any of the following situations: (1) the table as a whole is closed, and there is no unclosed cell in the outermost layer of the table; (2) the border of the table is not composed of curves or line segments at special angles; (3) the text will not overlap with the cell border.
[0024] 4. Step S4: Constructing cell information using island algorithm According to the above table area, traverse the table area, when the value of a certain area position is equal to 0 (for the initialization of the two-dimensional array of integer, the initialization value is 0, and the initialization has been completed when the two-dimensional array is declared), starting from the current point, use the island algorithm to obtain all the islands in the table area, each island is each cell; among them, the island algorithm is an existing algorithm, so it will not be repeated here. The result obtained through this step is a legal closed area, but whether it is a legal cell needs to be judged in combination with the following border information.
[0025] 5. Step S5: Obtain left and right borders and upper and lower border information, and construct border information based on the left and right borders and upper and lower border information The specific implementation of this step includes obtaining the left and right border information and the upper and lower border information: In this embodiment, the specific method for obtaining the left and right borders is as follows: (51) Construct a two-dimensional preprocessing array d in the vertical direction. The two-dimensional preprocessing array d records the heights of the consecutive values 2 below each position; (52) Traverse the table area in the two-dimensional array. When encountering a position with a value of 2, record the current position as (i, j), and obtain the height height of the current position from the two-dimensional preprocessing array d; (53) Start from the same row of the current position (i, j) and traverse to the right, and obtain the consecutive positions in the subsequent columns with a height equal to height from the two-dimensional preprocessing array d. The end position is recorded as k. In essence, k is just an identifier. It can be recorded as any value. For those skilled in the art, other identifiers can also be selected, such as: L / M, etc.; (54) Generate a vertical border as the left border or right border of the table area, the border height is height, and the border width is k-j+1. In this step, the starting point and the end point are known, and from steps (52) and (53), it can be clearly known that the x coordinates are the same and the y coordinates are different. A straight line is determined based on the two points to generate a vertical border; (55) Complete the traversal, obtain all vertical borders, and use all vertical borders as the left border and the right border. The vertical borders are all used as the left border or the right border of the table area. Generally, the leftmost border only needs to be used as the left border, and the rightmost border only needs to be used as the right border. The remaining borders can be used as either the left border or the right border.
[0026] In this embodiment, the specific method for obtaining the upper and lower borders is as follows: (1) construct a two-dimensional preprocessing array r in the horizontal direction, and the two-dimensional preprocessing array r records the width of the continuous value 2 on the right side of each position; (2) traverse the table area obtained in step S3 in the two-dimensional array, and when encountering a position with a value of 2, record the current position as (i, j), and obtain the width width of the current position from the two-dimensional preprocessing array r; (3) start traversing from the same row of the current position (i, j) and obtain the continuous positions in the subsequent columns with a width equal to width from the two-dimensional preprocessing array r, and record the end position as k. The upper k is just an identifier, which can be recorded as any value. For those skilled in the art, other identifiers can also be selected, such as: L / M, etc.; (4) Generate a horizontal border as the upper border or lower border of the table area obtained in step S3, the border height is width, and the border height is k-j+1. In this step, the starting point and the end point are known, and from steps (2) and (3), it can be clearly known that the x coordinates are different and the y coordinates are the same. A straight line is determined based on the two points to generate a horizontal border; (5) After the traversal is completed, all horizontal borders are obtained, and all horizontal borders are used as upper borders and lower borders. The horizontal border is used as the upper border or lower border of the table. Generally, the top border can only be used as the upper border, and the bottom border can only be used as the lower border. Any other border can be used as either the upper border or the lower border.
[0027] In this embodiment, the two-dimensional preprocessing array d and the two-dimensional preprocessing array r are constructed using the prefix sum algorithm. By traversing the two-dimensional array in the vertical direction / horizontal direction, the number of two-dimensional arrays with a value of 2 is counted, and the value of each position is calculated. This speeds up the processing of the entire border.
[0028] 6. Step S6: Generate and verify closed area information: The cell information in step S4 and the border information in step S5 are combined to form a closed figure; specifically, the left border, right border, upper border, and lower border are sorted (sorting method: from top to bottom according to top, from left to right according to left), the cell information obtained in step S4 is traversed, and the border information closest to the cell is located by binary search according to the vertex coordinate information of the cell. If the left border, right border, upper border, and lower border can be found for the cell at the same time, the current cell is determined to be a legal cell. If the left border, right border, upper border, and lower border cannot be found at the same time, the cell is discarded.
[0029] 7. Step S7: Constructing table information After the closed figures obtained in step S6 are sorted in order from top to bottom and from left to right, the continuity between the closed figures is determined. If any edges are adjacent or overlapping, they are stored in the same set, and finally a complete table with a border is determined.
[0030] Through the above solution, in the application scenario of PDF to Word, the operator information at the bottom of the PDF is directly read, and the wireframe information is generated according to the information carried by the operator. The wireframe information is then converted into a two-dimensional array, and the infection algorithm and the island algorithm are used to determine whether the table exists, thereby combining the advantages of speed and accuracy.
[0031] Example 2 This embodiment provides a computer-readable storage medium, on which a computer program is stored, and the computer program is executed by a processor to implement the PDF to Word two-dimensional array table recognition method provided in Embodiment 1. A person of ordinary skill in the art can understand that all or part of the steps of implementing the method provided in Embodiment 1 can be completed by hardware related to the computer program, and the above-mentioned computer program can be stored in a computer-readable storage medium, and when the program is executed, the steps of the method provided in Embodiment 1 are executed; and the above-mentioned storage medium includes: ROM, RAM, magnetic disk or optical disk and other media that can store program codes Example 3 like Figure 2 As shown, this embodiment provides a PDF to Word two-dimensional array table recognition device, including: a processor and a memory; the memory is used to store a computer program; the processor is connected to the memory, and is used to execute the computer program stored in the memory, so that the PDF to Word two-dimensional array table recognition device executes the PDF to Word two-dimensional array table recognition method provided in Example 1.
[0032] Specifically, the memory includes: ROM, RAM, disk, USB flash drive, memory card or CD and other media that can store program codes.
[0033] Preferably, the processor can be a general-purpose processor, including a central processing unit, a network processor, etc.; it can also be a digital signal processor, an application-specific integrated circuit, a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, or a discrete hardware component.
[0034] The above embodiments are merely illustrative of the principles and effects of the present invention, and are not intended to limit the present invention. Anyone familiar with the art may modify or alter the above embodiments without departing from the spirit and scope of the present invention. Therefore, all equivalent modifications or alterations made by a person of ordinary skill in the art without departing from the spirit and technical concept disclosed by the present invention shall still be covered by the claims of the present invention.
Claims
1. A PDF to Word two-dimensional array table recognition method, characterized in that: The following steps are involved: Step S1: construct line segments: obtain the coordinate information in the PDF, and generate all line segment information according to the path formed by the coordinate information; Step S2: Two-dimensional array conversion: determine the size of the two-dimensional array according to the width and height of the PDF page: the height is used as the row and the width is used as the column. According to the line segment information obtained in step S1, determine the position of the line segment in the two-dimensional array, and set the value of the position of the area where the line segment is located to 1; Step S3: Constructing a table area using an infection algorithm: Step S3.1: Traversing the two-dimensional array, when encountering a region position whose value is equal to 1, starting from the current position, infect all adjacent region positions whose values are 1 using the infection algorithm; Step S3.2: Changing the value of the region position from 1 to 2; Step S3.3: Obtaining the overall infection range, and constructing a table area based on the infection range; Step S4: constructing cell information using an island algorithm: based on the table area constructed in step S3, traverse the table area, and when a value of a certain area position is equal to 0, start from the current position and use the island algorithm to obtain all islands in the table area, each island is each cell; Step S5: Obtain left and right borders and upper and lower borders information, and construct border information according to the left and right borders and upper and lower borders information: Step S6: Generate and verify closed area information: Use the cell information in step S4 and the border information in step S5 to form a closed figure; Step S7: Construct table information: After sorting the closed figures obtained in step S6 from top to bottom and from left to right, determine the continuity between the closed figures. If any edges are adjacent or overlapping, store them in the same set, and finally determine a complete table with a border.
2. The PDF to Word two-dimensional array table recognition method according to claim 1 is characterized in that: The method for obtaining the coordinate information in the PDF in step S1 is as follows: through the parameters of the PDF operator, two coordinate information are obtained: (x1, y1) and (x2, y2).
3. The PDF to Word two-dimensional array table recognition method according to claim 2 is characterized in that: The method for constructing the table area in step S3.3 is as follows: if a certain infection range has no gaps and is a complete rectangle, and the interior of the rectangle meets the characteristics of a table, then the infection range is determined to be a table area.
4. The PDF to Word two-dimensional array table recognition method according to claim 3 is characterized in that: The characteristics of the table include any of the following: (1) The table as a whole is closed, and there is no unclosed cell in the outermost layer of the table; (2) The border of the table is not composed of curves or line segments at special angles; (3) The text will not overlap with the cell border.
5. The PDF to Word two-dimensional array table recognition method according to claim 4 is characterized in that: The specific method for obtaining the left and right borders in step S5 is as follows: (51) constructing a two-dimensional preprocessing array d in the vertical direction, in which the two-dimensional preprocessing array d records the heights of consecutive values of 2 below each position; (52) Traverse the table area obtained in step S3 in the two-dimensional array. When encountering a position with a value of 2, record the current position as (i, j), and obtain the height height of the current position from the two-dimensional preprocessing array d; (53) Starting from the same row of the current position (i, j), traverse to the right and obtain the positions in the subsequent columns that are continuous and have a height equal to height from the two-dimensional preprocessing array d. The end position is recorded as k; (54) Generate a vertical border as the left border or right border of the table area obtained in step S3, with a border height of height and a border width of k-j+1; (55) Complete the traversal, obtain all vertical borders, and use all vertical borders as left borders and right borders.
6. The PDF to Word two-dimensional array table recognition method according to claim 5, characterized in that: The specific method for obtaining the upper and lower borders in step S5 is as follows: (1) Construct a horizontal two-dimensional preprocessing array r, in which the width of the continuous values on the right side of each position is recorded; (2) Traverse the table area obtained in step S3 in the two-dimensional array. When encountering a position with a value of 2, record the current position as (i, j) and obtain the width width of the current position from the two-dimensional preprocessing array r; (3) Starting from the same row of the current position (i, j), traverse downwards and obtain the positions in the subsequent columns that are continuous and have a width equal to width from the two-dimensional preprocessing array r. The end position is recorded as k; (4) Generate a horizontal border as the upper border or lower border of the table area obtained in step S3, with the border height being width and the border height being k-j+1; (5) After the traversal is completed, all horizontal borders are obtained and used as the upper border and the lower border.
7. The PDF to Word two-dimensional array table recognition method according to claim 6, characterized in that: The two-dimensional preprocessing array d and the two-dimensional preprocessing array r are both constructed using a prefix sum algorithm.
8. The PDF to Word two-dimensional array table recognition method according to claim 7, characterized in that: The specific method of step S6 is as follows: sort the left border, right border, top border, and bottom border, traverse the cell information obtained in step S4, and use a binary search method to locate the border information closest to the cell according to the vertex coordinate information of the cell. If the left border, right border, top border, and bottom border can be found for the cell at the same time, the current cell is determined to be a legal cell. If the left border, right border, top border, and bottom border cannot be found at the same time, the cell is discarded.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: The computer program is executed by a processor to implement the PDF to Word two-dimensional array table recognition method according to any one of claims 1 to 8.
10. A device for PDF to Word two-dimensional array table recognition, characterized in that: include: Processor and memory; The memory is used to store computer programs; The processor is connected to the memory and is used to execute the computer program stored in the memory, so that the PDF to Word two-dimensional array table recognition device performs the PDF to Word two-dimensional array table recognition method as described in any one of claims 1 to 8.