Method and device for paragraph alignment in analysis pdf and medium
The PDF files are sorted and typed through the Apache PDFBOX framework, and combined with the fault-tolerant interval to determine paragraph alignment, the problem of poor paragraph alignment accuracy when PDF is converted to DOCX is solved, and high-quality document conversion and editing is achieved.
Patent Information
- Application Number
- CN202411569235.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-05
- Publication Date
- 2024-12-31
AI Technical Summary
The prior art when converting PDFs to DOCX, the accuracy of paragraph alignment is poor, resulting in poor readability and appearance of the document, low fault tolerance, and difficulty in maintaining the original format and layout style.
Use the Apache PDFBOX framework to read and sort PDF files, determine paragraph and header and footer information through the layout algorithm, and split line-level content based on the XY axis coordinates, and set fault-tolerant intervals to determine paragraph alignment, including left alignment and right alignment Align with center.
Improve the accuracy of paragraph alignment and the quality of converted documents, making the converted documents highly quality and easy to edit, and maintaining the original format and layout style of the document.
Smart Images

Figure CN119227671A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of PDF analysis, and in particular, relates to a method, device and medium for analyzing paragraph alignment in PDF. Background Art
[0002] In modern document processing, PDF and DOCX are two common document formats. PDF is usually used to protect the stability of document format and layout, while DOCX format is more flexible and suitable for editing and sharing documents. Therefore, converting PDF to DOCX is of great significance. In order to easily edit and modify the document content while retaining the original format and typesetting style, extracting text from PDF and adapting it to DOCX format requires effective text recognition and structuring technology to ensure the accuracy and coherence of the document content. During the conversion process, the alignment of paragraphs must be accurate, including left alignment, right alignment, centering, etc., to ensure the readability and appearance of the document. The existing paragraph alignment scheme has low error tolerance and poor alignment accuracy. Summary of the invention
[0003] The object of the present invention is to provide a method for parsing paragraph alignment in PDF 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 method for parsing paragraph alignment in PDF includes the following steps: Step S1: Using Apache's open source framework PDFBOX to read the PDF file; Step S2: extracting text information from the PDF file page by page, the text information including text content, text coordinates, text size, and text font; Step S3: using a sorting algorithm to sort the text content according to the acquired text coordinates to obtain sorted text content; Step S4: using a typesetting algorithm to convert and typeset the sorted text content, and determine paragraph, header and footer information; Step S5: extract the content corresponding to the determined paragraph, and split the paragraph content into line-level content according to the XY axis coordinates; Step S6: Analyze and judge the line-level content and paragraph alignment: set the error tolerance interval according to the text font size, and perform the following judgment based on the error tolerance interval: (61) If the difference between the starting X-axis coordinates of each line is within the tolerance interval, the paragraph content is judged to be left-aligned; (62) If the difference between the ending X-axis coordinates of each line is within the tolerance interval, the paragraph content is judged to be right-aligned; (63) If the difference between the starting X-axis coordinates and the ending X-axis coordinates of each line and the width of the left and right pages is within the tolerance interval, the paragraph content is judged to be centered; Step S7: After processing all paragraph contents, the analysis and judgment results are stored in a memory object and output to the transposed document.
[0005] Preferably, in step S3, the sorting method is as follows: according to the X-axis and Y-axis coordinates of the acquired text, first sort the text content from small to large according to the Y-axis coordinate; if the Y-axis coordinates are the same, sort the text content from small to large according to the X-axis, and obtain an ordered list from left to right and from top to bottom, i.e., obtain the sorted text content.
[0006] Preferably, in step S4, the method for determining the paragraph is as follows: Based on the sorted text content, determine whether it is in the same line according to the Y-axis coordinate, integrate the data into the same line of list data, and process all the list data in the following way: (41) When the absolute difference between Y1 and Y0, X1min and X0min, and X1max and X0max is less than 1, it is determined to be in the same paragraph; (42) When the absolute difference between Y1 and Y0, and X1min and X0min is less than 1, but X1max is greater than X0max, it is determined to be in the same paragraph; (43) When the absolute difference between Y1 and Y0, X1max and X0max is less than 1 and X0min is less than X1min, it is determined to be in the same paragraph; among them, the next set of data coordinates are X1min (leftmost in the same line), X1max (rightmost in the same line), Y1; the previous set of coordinates are X0min (leftmost in the same line), X0max (rightmost in the same line), Y0.
[0007] Preferably, in step S4, the method for determining the header and footer is as follows: obtain the first and last data of all pages, if the content of the first data of all pages is basically the same, then the first data is judged as the header, if the content of the last data of all pages is basically the same, then the last data is judged as the footer.
[0008] Preferably, the substantially identical contents refer to text contents being at least 80% identical.
[0009] In step S5, when the content is split into row-level contents according to the XY axis coordinates, when the X axis coordinate value gradually increases from left to right and the Y axis coordinate gradually increases from top to bottom, and when the X axis coordinate changes from large to small and the Y axis coordinate increases, it can be determined as a line break.
[0010] In order to achieve the above-mentioned purpose, the present invention also 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 method for parsing paragraph alignment in PDF.
[0011] In order to achieve the above-mentioned purpose, the present invention also provides a device for parsing paragraph alignment in PDF, 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 parsing paragraph alignment in PDF executes the method for parsing paragraph alignment in PDF.
[0012] Compared with the prior art, the present invention has the following beneficial effects: The present invention utilizes Apache's open source framework PDFBOX to read PDF files, then sorts and converts text layout, determines paragraphs, titles, headers, footers and other information, performs line-level splitting based on paragraph content, and sets a fault-tolerant space to analyze and judge line-level content, thereby determining paragraph alignment, thereby maintaining accurate alignment of the converted document and having the characteristics of high quality and easy editing. BRIEF DESCRIPTION OF THE DRAWINGS
[0013] Figure 1 This is a schematic diagram of the process of Example 1 of the present invention.
[0014] Figure 2 This is a principle block diagram of Example 3 of the present invention. DETAILED DESCRIPTION
[0015] In order to enable those skilled in the art to 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 understood 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.
[0016] 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.
[0017] Example 1 like Figure 1As shown, this embodiment provides a method for parsing paragraph alignment in PDF, and its principle is as follows: with the help of Apache's open source framework PDFBOX, the PDF file is read, the text is sorted and the layout is converted, the paragraph / header and footer information are determined, and then the line level is split according to the paragraph content, and the fault tolerance space is set to analyze and judge the line level content, thereby determining the paragraph alignment.
[0018] 1. Step S1: Read PDF file In this embodiment, the PDF file is read using the Apache open source framework PDFBOX; Apache PDFBox is an open source Java library for creating, manipulating, and extracting PDF documents. Because PDFBox is written in Java, it can run on any platform that supports Java, which makes it usable in a variety of operating systems and environments, and can extract text, images, form data, etc. from PDF documents, and PDFBox can efficiently handle large-scale PDF operations.
[0019] 2. Step S2: Parse the PDF file and obtain text-related information Extract text information from PDF files by page, including text content, text coordinates, text size, and text font.
[0020] 3. Step S3: Sort the text content of each page of the PDF file to obtain the sorted text content In this step, a sorting algorithm is used to sort the acquired text coordinates. The specific implementation method is as follows: based on the acquired X-axis and Y-axis coordinates of the text, since it is information on each page, the upper left corner of the coordinate is 0, 0; first sort the text content from small to large according to the Y-axis coordinate. If the Y-axis coordinates are the same, sort the text content from small to large according to the X-axis, and obtain an ordered list from left to right and from top to bottom, that is, the sorted text content.
[0021] Step S4: Based on the sorted text content, convert the text into a layout In this step, the typesetting algorithm is used to convert and typeset the sorted text content to obtain the paragraph information after typeset. The specific implementation method is as follows: Based on the sorted text content, determine whether it is in the same line according to the Y-axis coordinate. The same line data will be divided into a list, and different lines will be in different lists. Different lines will be judged for paragraphs later; the same line data will be integrated into the same line list data, and all the list data will be processed as follows: (41) When the absolute difference between Y1 and Y0, X1min and X0min, X1max and X0max is less than 1, it is considered to be relatively are close, then they are determined to be in the same paragraph; (42) When the absolute difference between Y1 and Y0 and between X1min and X0min is less than 1, but X1max is greater than X0max, then they are determined to be in the same paragraph; (43) When the absolute difference between Y1 and Y0, X1max and X0max is less than 1 and X0min is less than X1min, then they are determined to be in the same paragraph; where the next set of data coordinates is X1min (leftmost in the same row), X1max (rightmost in the same row), Y1; the previous set of coordinates is X0min (leftmost in the same row), X0max (rightmost in the same row), Y0.
[0022] This step also includes obtaining header and footer information, and its implementation method is as follows: obtain the first and last data of all pages. If the content of the first data of all pages is basically the same, then the first data is judged as the header. If the content of the last data of all pages is basically the same, then the last data is judged as the footer. Among them, basically the same content means that the text content is at least 80% the same.
[0023] 5. Step S5: Split paragraph information into line-level information First, extract the content corresponding to the determined paragraph, and then split the paragraph content into line-level content according to the XY axis coordinates. When splitting into line-level content according to the XY axis coordinates, when the X axis coordinate value gradually increases from left to right and the Y axis coordinate gradually increases from top to bottom, when the X axis coordinate changes from large to small and the Y axis coordinate increases, it can be determined as a line break.
[0024] 6. Step S6: Analyze and judge the line-level content and paragraph alignment Based on the row-level content, set the tolerance interval according to the text font size, and perform the following judgment based on the tolerance interval. Because each text size, font, style, and content are different, they appear to be in the same row and column in the PDF, but the coordinate data read out may differ by about 0-1. At this time, it is necessary to judge based on the absolute value of the coordinate difference: (61) If the difference between the starting X-axis coordinates of each line is within the tolerance interval, the paragraph content is judged to be left-aligned; (62) If the difference between the ending X-axis coordinates of each line is within the tolerance interval, the paragraph content is judged to be right-aligned; (63) If the difference between the starting X-axis coordinates and the ending X-axis coordinates of each line and the width of the left and right pages is within the tolerance interval, the paragraph content is judged to be centered; 7. Step S7: After processing all paragraph contents, the analysis and judgment results are stored in a memory object and output to the transposed document.
[0025] Through the above processing, paragraph alignment can be achieved, and the converted document can be accurately aligned after PDF to DOCX conversion.
[0026] 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 method for parsing paragraph alignment in PDF provided in Embodiment 1. A person skilled 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.
[0027] Example 3 like Figure 2 As shown, this embodiment provides a device for parsing paragraph alignment in PDF, 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 device for parsing paragraph alignment in PDF executes the method for parsing paragraph alignment in PDF provided in Example 1.
[0028] Specifically, the memory includes: ROM, RAM, disk, USB flash drive, memory card or CD and other media that can store program codes.
[0029] 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.
[0030] 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 method for parsing paragraph alignment in PDF, characterized in that: The following steps are involved: Step S1: Using Apache's open source framework PDFBOX to read the PDF file; Step S2: extracting text information from the PDF file page by page, the text information including text content, text coordinates, text size, and text font; Step S3: using a sorting algorithm to sort the text content according to the acquired text coordinates to obtain sorted text content; Step S4: using a typesetting algorithm to convert and typeset the sorted text content, and determine paragraph, header and footer information; Step S5: extract the content corresponding to the determined paragraph, and split the paragraph content into line-level content according to the XY axis coordinates; Step S6: Analyze and judge the line-level content and paragraph alignment: set the error tolerance interval according to the text font size, and perform the following judgment based on the error tolerance interval: (61) If the difference between the starting X-axis coordinates of each line is within the tolerance interval, the paragraph content is judged to be left-aligned; (62) If the difference between the ending X-axis coordinates of each line is within the tolerance interval, the paragraph content is judged to be right-aligned; (63) If the difference between the starting X-axis coordinates and the ending X-axis coordinates of each line and the width of the left and right pages is within the tolerance interval, the paragraph content is judged to be centered; Step S7: After processing all paragraph contents, the analysis and judgment results are stored in a memory object and output to the transposed document.
2. The method for aligning paragraphs in a PDF according to claim 1, characterized in that: In step S3, the sorting method is as follows: according to the X-axis and Y-axis coordinates of the acquired text, the text content is first sorted from small to large according to the Y-axis coordinate; if the Y-axis coordinates are the same, the text content is sorted from small to large according to the X-axis, and an ordered list from left to right and from top to bottom is obtained, that is, the sorted text content is obtained.
3. The method for aligning paragraphs in a PDF according to claim 2, characterized in that: In step S4, the method for determining the paragraph is as follows: Based on the sorted text content, determine whether the text is in the same line according to the Y-axis coordinate. If so, integrate the text data in the same line into the list data of the same line, and process all the list data in the following way: (41) When the absolute difference between Y1 and Y0, X1min and X0min, X1max and X0max is less than 1, they are determined to be in the same paragraph; (42) When the absolute difference between Y1 and Y0, X1min and X0min is less than 1, but X1max is greater than X0max, they are determined to be in the same paragraph; (43) When the absolute difference between Y1 and Y0, X1max and X0max is less than 1 and X0min is less than X1min, they are determined to be in the same paragraph; among them, the next set of data coordinates is X1mi, X1max, Y1; the previous set of coordinates is X0min, X0max, Y0.
4. The method for aligning paragraphs in a PDF according to claim 3, characterized in that: In step S4, the method for determining the header and footer is as follows: obtain the first and last data of all pages. If the content of the first data of all pages is basically the same, the first data is judged as the header. If the content of the last data of all pages is basically the same, the last data is judged as the footer.
5. The method for aligning paragraphs in a PDF according to claim 4, characterized in that: The content being substantially identical means that the text content is at least 80% identical.
6. The method for aligning paragraphs in a PDF according to claim 5, characterized in that: In step S5, when the content is split into row-level contents according to the XY axis coordinates, when the X axis coordinate value gradually increases from left to right and the Y axis coordinate gradually increases from top to bottom, and when the X axis coordinate changes from large to small and the Y axis coordinate increases, it can be determined as a line break.
7. 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 method for aligning paragraphs in a PDF parsing method as described in any one of claims 1 to 6.
8. A device for parsing paragraph alignment in PDF, 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 device for parsing paragraph alignment in PDF executes the method for parsing paragraph alignment in PDF as described in any one of claims 1 to 6.
