A PDF-to-Word header and footer extraction method and device, and a computer readable storage medium
By pre-processing and splitting the PDF files, identifying and distinguishing the valid areas of the header and footer, and determining the common element types of each page, the problem of unrecognizable headers and footers when converting PDF files to Word files is solved, and efficient header and footer extraction and presentation are achieved.
Patent Information
- Application Number
- CN202411450003.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-17
- Publication Date
- 2025-10-17
- Estimated Expiration
- 2044-10-17
AI Technical Summary
Existing technologies are unable to effectively identify and extract header and footer content from PDF files, resulting in incorrect presentation when converted into Word files.
By pre-processing the PDF file, splitting the pages, identifying the valid areas of the header and footer, and judging the common elements of each page, distinguishing the types of common elements of the first page, odd pages and even pages, determining the types of headers and footers, and generating header and footer information.
It achieves efficient recognition and extraction of header and footer content in PDF files, ensuring correct presentation in Word files.
Smart Images

Figure CN119272716B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The application belongs to the technical field of PDF file analysis, and particularly relates to a PDF-to-Word header and footer extraction method and device and a computer readable storage medium. BACKGROUND
[0002] In the translation industry, the analysis and restoration of various format files are crucial. PDF files cannot be directly analyzed and restored due to their special format, so during the file analysis process, the PDF files need to be converted into Word files.
[0003] Since there is no concept of setting a header and a footer in a Word file in a PDF format file, how to identify and extract the content actually serving as a header and a footer in a PDF format file in the process of converting a PDF into a Word file so as to present the content in the form of a header and a footer in the converted Word file has become a technical problem to be solved in the field. SUMMARY
[0004] The application aims to provide a PDF-to-Word header and footer extraction method to solve the technical problems existing in the prior art.
[0005] To achieve the above-mentioned purpose, the technical solution adopted by the application is as follows.
[0006] A PDF-to-Word header and footer extraction method comprises the following steps.
[0007] Step S1: Preprocessing a PDF file to complete the nesting, layout processing and element sorting of characters and graphics, and obtaining an ordered PDF preprocessing file with the content of each page.
[0008] Step S2: Page splitting: splitting the PDF preprocessing file into an odd page file and an even page file.
[0009] Step S3: Identifying and demarcating the effective area of a header and a footer of each page file.
[0010] Step S4: In the effective area, matching the common elements of each page from the second page, and judging the type of a header and a footer of a PDF page, wherein the type of a header and a footer includes type A and type C, wherein type A means that the header and the footer of each page are the same, and type C means that the header and the footer of odd and even pages are different.
[0011] (41) In the matching process, if the index of the common elements between each page is the same, it is judged that the type of the header and the footer in the current PDF file is type A, i.e., the header and the footer of each page are the same.
[0012] (42) In the matching process, if the common elements of a page are different from the previous matching result, terminate the matching, then separately use the odd pages for matching, separately use the even pages for matching, if the indexes of the common elements of all odd pages are the same, and the indexes of the common elements of even pages are also the same, it is judged that the header and footer type in the current PDF file is type C, that is, the odd and even pages have different headers and footers;
[0013] (43) If the (41) or (42) is not satisfied, it is judged that there is no header and footer in the current PDF file;
[0014] Step S5: Determine the header and footer type of all pages:
[0015] (51) If it is determined according to step S4 that the header and footer type of the current PDF page is type A, it is judged whether the common elements of the first page and the second page of the PDF file are the same, if the same, it is determined that the header and footer type of all pages of the PDF file is type A, if not the same, it is determined that the header and footer type of all pages of the PDF file is type A and type B, wherein type B means that the first page header and footer are different;
[0016] (52) If it is determined according to step S4 that the header and footer type of the current PDF page is type C, it is judged whether the common elements of the first page and the third page of the PDF file are the same, if the same, it is determined that the header and footer type of all pages of the PDF file is type C, if not the same, it is determined that the header and footer type of all pages of the PDF file is type C and type B, wherein type B means that the first page header and footer are different.
[0017] Step S6: According to the header and footer type of all pages, generate the header and footer information of each page, and realize the extraction of the header and footer.
[0018] In an embodiment, the effective area in step S3 is not more than 1 / 3 of the page height.
[0019] In an embodiment, in step S4, the common elements refer to: under the same index, if the types of elements are the same and the values are the same, they belong to the common elements.
[0020] In an embodiment, the (42) different matching results refer to that the common elements between any three pages are not the same.
[0021] In an embodiment, in step S4, the matching method is as follows: after sorting the elements, traverse from the beginning to the end, compare whether the data of the same index is the same according to the index, and extract the index position of the elements with the same index of all pages.
[0022] In order to achieve the above object, the present application further provides a computer readable storage medium, which stores a computer program, and the computer program is executed by a processor to realize the PDF to Word header and footer extraction method.
[0023] In order to achieve the above object, the present application further provides a PDF to Word header and footer extraction device, comprising: a processor and a memory; the memory is used for storing a computer program; the processor is connected with the memory, and is used for executing the computer program stored in the memory, so that the PDF to Word header and footer extraction device executes the PDF to Word header and footer extraction method.
[0024] Compared with the prior art, the present application has the following beneficial effects:
[0025] The present application extracts the common elements of each page by judging whether the elements in the same index position are the same, and determines the type of the header and footer by distinguishing the common elements of the first page, odd pages and even pages, so that the identification of the header and footer can be efficiently completed. BRIEF DESCRIPTION OF DRAWINGS
[0026] Figure 1 The flowchart of the present application - embodiment 1.
[0027] Figure 2 The principle block diagram of the present application - embodiment 3. DETAILED DESCRIPTION
[0028] In order to make those skilled in the art have a clearer understanding and knowledge of the present application, the present application is further described in detail in combination with the embodiments. It should be known that the specific embodiments described below are only used to explain the present application, facilitate understanding, and the technical solutions provided by the present application 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 application.
[0029] It should be noted that the diagrams provided in the following embodiments only illustrate the basic concept of the present application in a schematic manner, and therefore only the components related to the present application are shown in the diagrams, not the number, shape and size of the components when actually implemented. The shape, number and proportion of each component when actually implemented can be arbitrarily changed, and the layout form of the components can also be more complex.
[0030] Embodiment 1
[0031] As Figure 1As shown, the embodiment provides a PDF to Word header and footer extraction method, and the principle is as follows: by judging whether the elements at the same index position are the same, the common elements of each page are extracted, and the types of the header and footer are determined by distinguishing the common elements of the first page, odd pages and even pages, so that the identification of the header and footer can be efficiently completed.
[0032] The specific steps of the method provided in the embodiment are as follows:
[0033] I. Step S1: Preprocessing of the PDF file
[0034] Through a series of preprocessing, the nesting, layout processing and element sorting of characters and graphics are completed. The nesting, layout processing and element sorting of characters and graphics are completed, and an ordered PDF preprocessing file is obtained. The preprocessing is based on the operator information of the PDF to determine the relationship between characters and graphics, and thus the overall layout and nesting relationship.
[0035] II. Step S2: Page splitting
[0036] The PDF preprocessing file is split into odd page files and even page files; the splitting means belongs to a conventional technical means, and therefore is not described here.
[0037] III. Step S3: Identifying and demarcating the effective area of the header and footer of each page file
[0038] The identification of the effective area of the header and footer lays a foundation for subsequent processing. As an optimization, the effective area is not more than 1 / 3 of the page height. The technical feature of this limitation is based on the premise that the header and footer identification needs to be completed according to the technical scheme of the application. The inventors of the application have obtained through a large number of document tests that this range of not more than 1 / 3 of the page height is the most suitable range.
[0039] IV. Step S4: Extracting page common elements and determining the header and footer types
[0040] Common elements refer to: under the same index, if the types of the elements are the same and the values are the same, they belong to common elements. The header and footer are usually present on each page and are approximately the same (except for the page number), so the common elements here are the same part in each page, which can be recognized as part of the header and footer.
[0041] In the effective area, the common elements of each page are matched from the second page, and the header and footer types of the PDF pages (PDF pages other than the first page) are determined; wherein the matching method is as follows: after sorting the elements, the elements are traversed from head to tail, and according to the index, it is compared whether the data at the same index is the same, and the index position of the elements with the same elements in all pages is extracted; the header and footer types include type A and type C, wherein type A means that the header and footer of each page are the same, and type C means that the header and footer of odd and even pages are different;
[0042] The step of determining the header and footer type is mainly for other PDF pages except the first page, and the method is as follows: (41) in the matching process, if the indexes of the common elements between each page are the same, it is determined that the header and footer type of the current PDF file is type A, that is, the header and footer of each page are the same; for example; if the first 0~3 elements of the first page are the same as the first 0~3 elements of each subsequent page, it is proved that these elements can be extracted into the header, because in the docx, the data in the header can be displayed in each page, so if it is a common element that exists in each page, it can be considered that they belong to the header or the footer; (42) in the matching process, if the common elements of a page are different from the previous matching result, the matching is terminated, then the odd pages are matched separately, and the even pages are matched separately, if the indexes of the common elements of all odd pages are the same, and the indexes of the common elements of even pages are also the same, it is determined that the header and footer type of the current PDF file is type C, that is, the header and footer of odd and even pages are different; wherein the matching result different means that the common elements between any three pages are not the same, for example: if the first 3 elements of the second page and the third page are the same, but when comparing the second page and the fourth page, it is found that only the first 2 elements are the same, then the third element of the second page and the third page which is the same will not be the header, because it does not exist in the fourth page, so the matching needs to be terminated at this time; (43) if (41) or (42) is not satisfied, it is determined that there is no header and footer in the current PDF file;
[0043] Step S5: determining the header and footer type of all pages
[0044] This step is based on the determination result of step S4, and realizes the determination of the header and footer of the first page of the PDF, thereby realizing the determination of the header and footer of all pages of the PDF, which includes the following two cases:
[0045] (51) If it is determined according to step S4 that the header and footer type of the current PDF page (the current page is the first page, any page of the second page) is type A, it is determined whether the common elements of the first page and the second page of the PDF file are the same, if the same, it is determined that the header and footer type of all pages of the PDF file is type A, if not the same, it is determined that the header and footer type of all pages of the PDF file is type A and type B, wherein type B refers to the first page header and footer being different, that is, the first page header and footer are different, and the header and footer of the remaining pages are the same;
[0046] (53) If it is determined according to step S4 that the header and footer type of the current PDF page is type C, it is determined whether the common elements of the first page and the third page of the PDF file are the same, if the same, it is determined that the header and footer type of all pages of the PDF file is type C, if not the same, it is determined that the header and footer type of all pages of the PDF file is type C and type B, wherein type B refers to the first page header and footer being different, that is, the first page header and footer are different, and the header and footer of the remaining pages are different.
[0047] Step S6: generating the header and footer information of each page to realize header and footer extraction
[0048] According to the header and footer type of all pages, the header and footer information of each page is generated to realize header and footer extraction; specifically, after determining the header and footer type and the header and footer element, the header and footer information of each page is generated through the open source framework POI of java and according to the specification of OOXML.
[0049] Embodiment 2
[0050] The embodiment provides a computer readable storage medium, which stores a computer program, and the computer program is executed by a processor to realize the PDF to Word header and footer extraction method provided in embodiment 1. Those skilled in the art can understand that all or part of the steps of the method provided in embodiment 1 can be completed by a computer program related hardware, and the above-mentioned computer program can be stored in a computer readable storage medium, and the program executes the steps of the method provided in embodiment 1 when executed; and the above-mentioned storage medium includes ROM, RAM, magnetic disc or optical disc and various storage program codes.
[0051] Embodiment 3
[0052] As Figure 2As shown, the embodiment provides a PDF-to-Word header and footer extraction device, comprising: a processor and a memory; the memory is used for storing a computer program; the processor is connected with the memory, and is used for executing the computer program stored in the memory, so that the PDF-to-Word header and footer extraction device executes the PDF-to-Word header and footer extraction method provided in embodiment 1.
[0053] Specifically, the memory comprises: ROM, RAM, disk, U disk, memory card or optical disc and various media capable of storing program codes.
[0054] Preferably, the processor can be a general-purpose processor, including a central processing unit, a network processor and the like; and 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, a discrete hardware component.
[0055] The above embodiments only exemplarily illustrate the principles and effects of the present application, and are not used to limit the present application. Any person skilled in the art can modify or change the above embodiments without departing from the spirit and scope of the present application. Therefore, all equivalent modifications or changes completed by those skilled in the art without departing from the spirit and technical thought disclosed by the present application should be covered by the claims of the present application.
Claims
1. A method for extracting headers and footers from PDF to Word, characterized in that: The following steps are involved: Step S1: Pre-process the PDF file to complete the nesting of characters and graphics, layout processing and element sorting, and obtain a PDF pre-processed file with orderly content on each page; Step S2: Page splitting: splitting the PDF pre-processed file into odd-numbered page files and even-numbered page files; Step S3: Identify and delineate the valid areas of the header and footer of each page of the document; Step S4: Within the valid area, starting from the second page, match the common elements of each page and determine the header and footer types of the PDF page. The header and footer types include Type A and Type C. Type A means that the header and footer of each page are the same, and Type C means that the header and footer of odd and even pages are different. The method for determining the header and footer type is as follows: (41) During the matching process, if the indexes of the common elements between each page are the same, then the header and footer type in the current PDF file is determined to be type A, that is, the header and footer of each page are the same; (42) During the matching process, if the common elements of a certain page are different from the previous matching results, the matching is terminated. Then, the odd pages are used alone for matching, and the even pages are used alone for matching. If the indexes of the common elements of all odd pages are the same, and the indexes of the common elements of even pages are also the same, then it is determined that the header and footer type in the current PDF file is type C, that is, the headers and footers of odd and even pages are different; (43) If (41) or (42) is not satisfied, it is determined that there is no header or footer in the current PDF file; In step S4, the matching method is as follows: after sorting the elements, traverse from the beginning to the end, compare the data with the same index based on the index to see if they are the same, and extract the index positions of all pages with the same elements; Step S5: Determine the header and footer types of all pages: (51) If it is determined according to step S4 that the header and footer types of the current PDF page are type A, then determine whether the common elements of the first page and the second page of the PDF file are the same. If they are the same, then determine that the header and footer types of all pages of the PDF file are type A. If they are not the same, then determine that the header and footer types of all pages of the PDF file are type A and type B, where type B means that the header and footer of the first page are different; (52) If the header and footer types of the current PDF page are determined to be type C according to step S4, then determine whether the common elements of the first page and the third page of the PDF file are the same. If they are the same, then determine that the header and footer types of all pages of the PDF file are type C. If they are not the same, then determine that the header and footer types of all pages of the PDF file are type C and type B, where type B means that the header and footer of the first page are different. Step S6: Generate header and footer information for each page based on the header and footer types of all pages to achieve header and footer extraction.
2. The PDF to Word header and footer extraction method according to claim 1, characterized in that: The effective area in step S3 is no more than 1 / 3 of the page height.
3. The PDF to Word header and footer extraction method according to claim 2 is characterized in that: In step S4, the common elements refer to: under the same index, if the elements have the same type and the same value, they are common elements.
4. The PDF to Word header and footer extraction method according to claim 3 is characterized in that: The different matching results in (42) refer to different common elements between any three pages.
5. 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 header and footer extraction method according to any one of claims 1 to 4.
6. A PDF to Word header and footer extraction device, 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 header and footer extraction device performs the PDF to Word header and footer extraction method according to any one of claims 1 to 4.
Citation Information
Patent Citations
Recognition method and device for page headers and page footers of format electronic document
CN104951429A
Method and device for extracting textbook content and structure based on visual and text features
CN117058704A