A method, device and medium for pdfbox-based multi-layer document sequential identification
Patent Information
- Application Number
- CN202610609654.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-05-06
- Publication Date
- 2026-08-18
AI Technical Summary
[0003]然而,对于经过扁平化处理的PDF文档、由扫描件生成的图像型PDF或由特定软件生成的结构混乱的PDF,其内部并未保留清晰的图层顺序元数据,导致常规解析手段失效,无法获知元素的正确覆盖关系
本发明不依赖内部结构,而是通过渲染图像与穷举重建图像的像素级比对来确定图层顺序,即使PDF文档已扁平化或缺少OCG信息,仍能准确识别元素的正确覆盖关系。
Smart Images

Figure CN122597560A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of document processing technology, specifically, it relates to a method, device and medium for sequential recognition of multi-layer documents based on pdfbox. Background Technology
[0002] During the generation or editing process, PDF documents often contain multiple layers (such as text, images, vector graphics, etc.) that are superimposed on each other. The order in which these layers are superimposed directly determines the final page rendering effect. In existing technologies, obtaining the layer order usually relies on parsing the structured data within the PDF document, such as the order of drawing instructions in optional content groups or content flows.
[0003] However, for flattened PDF documents, image-based PDFs generated from scanned documents, or PDFs with messy structures generated by specific software, clear layer order metadata is not preserved, causing conventional parsing methods to fail and making it impossible to know the correct overlay relationship of elements.
[0004] Therefore, how to reverse-engineer the correct layer order of elements when the underlying structural information of a PDF document is missing or incomplete has become a technical problem that urgently needs to be solved in this field. Summary of the Invention
[0005] The purpose of this invention is to provide a method for sequential recognition of multi-layer documents based on pdfbox, so as to solve the technical problems existing in the prior art.
[0006] To achieve the above objectives, the technical solution adopted by the present invention is as follows: A method for sequential recognition of multi-layer documents based on pdfbox, characterized by the following steps: Step S1: Extract the coordinate information of the element Obtain all elements containing position attributes in a PDF document page, use the PDFBox tool to parse the page, extract and record the type of each element and its boundary rectangle coordinates in the current page coordinate system; Step S2: Analyze element coverage Based on the coordinate information obtained in step S1, the coverage relationship between each element is detected; for all elements with overlapping coordinate regions, the maximum bounding rectangle of the overlapping region formed by the elements is calculated, and the coordinate range corresponding to the maximum bounding rectangle is obtained. Step S3: Extract a rectangular region of the image. Based on the page object obtained by PDFBox parsing, a screenshot operation is performed on the page area according to the coordinate range of the maximum bounding rectangle to obtain the corresponding BufferedImage object; Step S4: Construct a new image object Perform a deep copy of the BufferedImage object and generate a new BufferedImage object with the same size and background. Clear all objects in the new BufferedImage object whose corresponding layer arrangement order needs to be obtained. Using an exhaustive method, draw all elements with intersection of coordinate regions determined in step S2 into the new BufferedImage object in different orders. For each layer stacking arrangement order, after all elements are placed, compare the pixel information of the new BufferedImage with the pixel information of the BufferedImage in step S3. Step S5: Output target layer order When the pixel information of the new BufferedImage object is detected to be completely consistent with the pixel information of the BufferedImage object, it is determined that the placement order of the elements is correct, and the exhaustive traversal process of the remaining arrangement order is terminated, and the order is recorded and output.
[0007] Preferably, the elements in step S1 include: image elements and / or text elements.
[0008] Preferably, the specific process of calculating the coordinate information of the maximum bounding rectangle in step S2 is as follows: traverse the coordinates of the boundary rectangles of all elements where the coordinate regions intersect, and calculate the minimum coordinate value of each boundary rectangle in the X-axis direction. with maximum coordinate value and the minimum coordinate value in the Y-axis direction. with maximum coordinate value Based on coordinate points and Determine the maximum bounding rectangle.
[0009] Preferably, in step S4, the exhaustive search method is executed in parallel computing mode, specifically including: dividing all possible layer overlay arrangements into multiple subsets, and independently performing pixel comparison operations in multiple parallel processing units; when any parallel processing unit detects that the pixel information is completely consistent, it sends a termination signal to other parallel processing units and outputs the currently used layer overlay arrangement.
[0010] Preferably, in step S4, when comparing the pixel information of the new BufferedImage with the pixel information of the BufferedImage in step S3, if the number of different pixels between the two images is less than or equal to a preset pixel difference tolerance threshold, the pixel information is determined to be consistent.
[0011] Preferably, in step S4, before comparing the pixel information of the new BufferedImage with the pixel information of the BufferedImage in step S3, the method further includes: calculating the image hash values of the new BufferedImage object and the BufferedImage object respectively, and determining whether the image hash values of the two are the same. If they are the same, the pixel information comparison is performed; otherwise, the pixel information comparison is skipped.
[0012] To achieve the above objectives, the present invention also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the method for sequential recognition of multi-layer documents based on pdfbox.
[0013] To achieve the above objectives, the present invention also provides an electronic device, comprising: a memory for storing a computer program; and a processor for executing the computer program stored in the memory, wherein when the computer program is executed by the processor, the electronic device implements the steps of the method for sequential recognition of multi-layer documents based on pdfbox.
[0014] Compared with the prior art, the present invention has the following beneficial effects: This invention does not rely on internal structure, but determines the layer order by comparing the rendered image with the exhaustive reconstruction image at the pixel level. Even if the PDF document has been flattened or lacks OCG information, it can still accurately identify the correct overlapping relationship of elements.
[0015] This invention combines pixel difference tolerance threshold and image hash pre-filtering, which not only ensures the accuracy of the comparison results, but also significantly improves the execution efficiency of the exhaustive algorithm.
[0016] This invention utilizes a parallel computing framework to partition and compare the exhaustive space in parallel, significantly reducing the time required to determine the layer order. Attached Figure Description
[0017] Figure 1 This is a flowchart of Embodiment 1 of the present invention.
[0018] Figure 2 This is a principle block diagram of Embodiment 2 of the present invention. Detailed Implementation
[0019] To enable those skilled in the art to have a clearer understanding of the present invention, the present invention will be further described in detail below with reference to embodiments. It should be understood that the specific embodiments described below are merely for illustrative purposes and to facilitate understanding. The technical solutions provided by the present invention are not limited to those provided in the following embodiments, nor should they limit the scope of protection of the present invention.
[0020] It should be noted that the illustrations provided in the following embodiments are only schematic representations of the basic concept of the present invention. Therefore, the drawings only show the components related to the present invention and are not drawn according to the actual number, shape and size of the components in the actual implementation. In the actual implementation, the shape, quantity and proportion of each component can be arbitrarily changed, and the layout of the components may also be more complex. Example 1
[0021] like Figure 1 As shown, this embodiment provides a method for sequential recognition of multi-layer documents based on pdfbox, which specifically includes the following steps: Step S1: Extract the coordinate information of the element This step utilizes the Apache PDFBox library to parse the target PDF document page. Specifically, it iterates through each markup operation in the page content stream. For each drawable element (e.g., text object, image object), it obtains the coordinates of its bounding rectangle in the current page coordinate system. It extracts and records the type and bottom-left corner coordinates of each element. upper right corner coordinates It should be noted that the origin of PDF coordinates is usually located at the bottom left corner of the page, while the origin of Java image rendering coordinates is located at the top left corner. In this embodiment, to facilitate subsequent image processing, the PDF coordinates are uniformly converted into an image coordinate system with the top left corner as the origin.
[0022] Step S2: Analyze element coverage Based on the boundary rectangle coordinates of each element obtained in step S1, the coverage relationship between each element is detected, that is, the spatial intersection between each pair of elements is calculated. An example of intersection judgment is as follows: For element and elements If satisfied and If the coordinate regions of the two regions intersect, then there is a potential covering relationship between them.
[0023] Calculate the maximum bounding rectangle of the overlapping region formed by the elements: For all sets of elements with pairwise intersections, we need to calculate the maximum bounding rectangle that can completely enclose all elements in the set. This involves iterating through the coordinates of the boundary rectangles of all elements with intersecting coordinate regions, and then calculating the minimum coordinate value of each boundary rectangle along the X-axis. with maximum coordinate value and the minimum coordinate value in the Y-axis direction. with maximum coordinate value Based on coordinate points and Determine the largest bounding rectangle.
[0024] Step S3: Extract a rectangular region of the image. Based on the page object obtained from PDFBox parsing, a screenshot of the page area is taken according to the coordinate range of the largest bounding rectangle, and the corresponding BufferedImage object (i.e., buffered image) is obtained. This image represents the standard visual representation of the overlapping area in the original PDF.
[0025] Step S4: Construct a new image object First, a deep copy is performed on the BufferedImage object obtained in step S3 to generate a new BufferedImage object with the same size and background (white by default). The pixel values in the new BufferedImage object corresponding to the maximum bounding rectangle area are reset to the background color (e.g., RGB 255, 255, 255) to ensure that the canvas is initially blank and to avoid interference from the original pixel residue caused by the deep copy in subsequent layer-by-layer drawing and comparison.
[0026] Next, determine the set of all elements that were determined to have coordinate intersection in step S2 (assuming the number is N), and use an exhaustive method to generate all possible permutations of these N elements.
[0027] Secondly, sequential drawing and comparison: for each arrangement order (e.g., sequence) ), and draw elements A, B and C sequentially onto the new BufferedImage object, strictly following the original coordinates and style attributes recorded in step S1 for each element.
[0028] After all elements have been drawn, perform pixel comparison: Hash pre-filtering: Before performing pixel-by-pixel comparison, first calculate the image hash value of the new BufferedImage object and the BufferedImage object (e.g., use MessageDigest to calculate the MD5 value, or use the perceptual hash algorithm). If the hash values are different, it indicates that there is a difference in the content of the two images, and skip the subsequent pixel-by-pixel comparison directly to process the next sorting order; if the hash values are the same, the comparison process begins.
[0029] Comparison process: During the comparison phase, every pixel in the overlapping area of the two images is traversed, and the RGB difference of the corresponding pixel is calculated. Since the PDF rendering engine may have a slight difference of 1-2 pixel values in anti-aliasing edge processing, this embodiment sets a preset pixel difference tolerance threshold (for example, set to 0.01% of the total number of pixels in the image, or the absolute difference of pixels does not exceed 5). If the number of difference pixels is less than or equal to the threshold, the pixel information is determined to be consistent.
[0030] In a further preferred embodiment, to improve exhaustive search efficiency, this embodiment employs parallel computing to execute the exhaustive search task. The N! possible permutations are divided into multiple subsets, which are then submitted to multiple parallel processing units (worker threads) in the thread pool for independent execution. Each worker thread has its own independent copy of the new BufferedImage object to avoid drawing conflicts. When any worker thread detects that the pixel information is completely identical, it immediately calls the interrupt flag to send a termination signal to the thread pool manager, stopping the computation of other threads.
[0031] Step S5: Output target layer order When the comparison result is consistent, the exhaustive traversal process is immediately terminated. At this point, the permutation order that led to the consistent comparison (e.g., This is the correct layer overlay order (where A is at the bottom layer and C is at the top layer, or vice versa, depending on the coordinate system definition). The system records this order and can convert it into JSON format or list format for output, which can then be used by subsequent document editing or analysis systems. Example 2
[0032] This embodiment provides a computer-readable storage medium on which a computer program is stored. The computer program is executed by a processor to implement the method provided in Embodiment 1. Those skilled in the art will understand that all or part of the steps of the method provided in Embodiment 1 can be implemented by hardware related to the computer program. The aforementioned computer program can be stored in a computer-readable storage medium. When the program is executed, it performs the steps of the method provided in Embodiment 1. The aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks. Example 3
[0033] like Figure 2 As shown, this embodiment provides an electronic device, including: a memory for storing a computer program; and a processor for executing the computer program stored in the memory, wherein when the computer program is executed by the processor, the electronic device implements the method provided in Embodiment 1.
[0034] Specifically, the memory may include various media capable of storing program code, such as read-only memory (ROM), random access memory (RAM), magnetic disk, USB flash drive, memory card, or optical disk.
[0035] Specifically, the processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0036] The above description is merely a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. A method for sequential recognition of multi-layered documents based on pdfbox, characterized in that, Includes the following steps: Step S1: Extract the coordinate information of the element Obtain all elements containing position attributes in a PDF document page, use the PDFBox tool to parse the page, extract and record the type of each element and its boundary rectangle coordinates in the current page coordinate system; Step S2: Analyze element coverage Based on the coordinate information obtained in step S1, the coverage relationship between each element is detected; for all elements with overlapping coordinate regions, the maximum bounding rectangle of the overlapping region formed by the elements is calculated, and the coordinate range corresponding to the maximum bounding rectangle is obtained. Step S3: Extract a rectangular region of the image. Based on the page object obtained by PDFBox parsing, a screenshot operation is performed on the page area according to the coordinate range of the maximum bounding rectangle to obtain the corresponding BufferedImage object; Step S4: Construct a new image object Perform a deep copy of the BufferedImage object and generate a new BufferedImage object with the same size and background. Clear all objects in the new BufferedImage object whose corresponding layer arrangement order needs to be obtained. Using an exhaustive method, draw all elements with intersection of coordinate regions determined in step S2 into the new BufferedImage object in different orders. For each layer stacking arrangement order, after all elements are placed, compare the pixel information of the new BufferedImage with the pixel information of the BufferedImage in step S3. Step S5: Output target layer order When the pixel information of the new BufferedImage object is detected to be completely consistent with the pixel information of the BufferedImage object, it is determined that the placement order of the elements is correct, and the exhaustive traversal process of the remaining arrangement order is terminated, and the order is recorded and output.
2. The method for sequential recognition of multi-layer documents based on pdfbox according to claim 1, characterized in that, The elements in step S1 include: image elements and / or text elements.
3. The method for sequential recognition of multi-layer documents based on pdfbox according to claim 1, characterized in that, The specific process for calculating the coordinate information of the maximum bounding rectangle in step S2 is as follows: traverse the coordinates of the boundary rectangles of all elements where the coordinate regions intersect, and calculate the minimum coordinate value of each boundary rectangle in the X-axis direction. with maximum coordinate value and the minimum coordinate value in the Y-axis direction. with maximum coordinate value Based on coordinate points and Determine the maximum bounding rectangle.
4. The method for sequential recognition of multi-layer documents based on pdfbox according to claim 1, characterized in that, In step S4, the exhaustive search method is executed in parallel computing mode, specifically including: dividing all possible layer overlay arrangements into multiple subsets, and independently performing pixel comparison operations in multiple parallel processing units; when any parallel processing unit detects that the pixel information is completely consistent, it sends a termination signal to other parallel processing units and outputs the currently used layer overlay arrangement.
5. The method for sequential recognition of multi-layer documents based on pdfbox according to claim 1, characterized in that, In step S4, when comparing the pixel information of the new BufferedImage with the pixel information of the BufferedImage in step S3, if the number of different pixels between the two images is less than or equal to a preset pixel difference tolerance threshold, the pixel information is determined to be consistent.
6. The method for sequential recognition of multi-layer documents based on pdfbox according to claim 1, characterized in that, In step S4, before comparing the pixel information of the new BufferedImage with the pixel information of the BufferedImage in step S3, the method further includes: calculating the image hash values of the new BufferedImage object and the BufferedImage object respectively, and determining whether the image hash values of the two are the same. If they are the same, the pixel information comparison is performed; otherwise, the pixel information comparison is skipped.
7. A computer-readable storage medium having a computer program stored thereon, characterized in that, When executed by a processor, the computer program implements the steps of the method for sequential recognition of multi-layered documents based on pdfbox as described in any one of claims 1 to 6.
8. An electronic device, characterized in that, include: Memory, used to store computer programs; A processor is configured to execute a computer program stored in the memory, wherein when the computer program is executed by the processor, the electronic device implements the steps of the method for sequential recognition of multi-layer documents based on pdfbox as described in any one of claims 1 to 6.