Method for extracting and merging EXCEL table data in XFA based on JAVA language
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- BEI JING ZHONG YAN CHUANG XIN KE JI YOU XIAN GONG SI
- Filing Date
- 2024-03-07
- Publication Date
- 2026-04-17
AI Technical Summary
由于XFA数据以XML形式嵌入,并且动态渲染,直接抽取表格数据变得异常困难,且准确性大打折扣
Smart Images

Figure CN121882001A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of tabular data processing technology, specifically a method for extracting and merging EXCEL table data in XFA based on the JAVA language. Background Technology
[0002] PDF, as a widely used file format, is extensively used in various scenarios due to its cross-platform compatibility and high fidelity. Adobe's XFA (XMLForms Architecture) allows for the creation of dynamic, scalable datasets that can be embedded within PDF documents. However, extracting table data from XFA PDFs is relatively complex because the data exists as a separate XML data layer and does not follow traditional page flow layouts. There are limited solutions on the market for extracting XFA form data and converting it to Excel format, and existing tools such as advanced versions of Adobe Acrobat and PDF development libraries like iText may not be comprehensive enough for handling XFA forms. Therefore, developers typically need to rely on lower-level PDF parsing libraries or write custom scripts to parse XFA XML data and use data transformation techniques to organize the data into Excel spreadsheet format to meet data analysis and integration needs.
[0003] The main problem lies in handling non-standard or XFA type PDF tables, where existing PDF parsing libraries face numerous challenges. Because XFA data is embedded in XML and dynamically rendered, directly extracting table data becomes exceptionally difficult and significantly reduces accuracy. This is primarily manifested in the recognition of complex cell merging, row and column layouts, and embedded styles; many libraries often fail to handle these effectively, leading to inconsistencies and incompleteness in data extraction. While general-purpose tools and technologies on the market perform adequately with traditional AcroForm forms, their support for XFA forms is inadequate. This typically requires developers to manually manipulate or write highly customized scripts to compensate for this deficiency, undoubtedly increasing the complexity and cost of data extraction and hindering large-scale automated data integration and analysis processes. Furthermore, handling cell styles during the conversion from XFA tables to Excel is also a challenge. Due to technical limitations, many PDF parsing libraries struggle to fully reproduce the original styles during conversion, such as merged cells, formatted numbers, and font styles. This results in the loss of the original data's presentation during conversion, causing problems for subsequent data analysis. Therefore, for developers who need to process XFA type PDF forms, seeking a more efficient and accurate solution is particularly important. Summary of the Invention
[0004] The purpose of this invention is to provide a method for extracting and merging Excel spreadsheet data in XFA based on the JAVA language, so as to solve the problems mentioned in the background art.
[0005] To achieve the above objectives, the present invention provides the following technical solution: a method for extracting and merging Excel table data in XFA based on the JAVA language, wherein the steps of the method for extracting and merging table data are as follows:
[0006] Step 1: Parse the XFA form; using Apache PDFBox, a non-standard method, a library capable of reading and parsing data in PDFs to obtain embedded XFA XML content is used to extract XFA data from the PDF document.
[0007] Step 2: Parse the XFAXML data; After obtaining the XFAXML data, use Java's XML parser DOM to deeply parse this XML structure. Through the DOM parser, traverse the XML tree structure step by step to extract the required table data.
[0008] Step 3: Construct an Excel spreadsheet template; Open the PDF document using Adobe Acrobat Reader, create a spreadsheet template that conforms to the target table structure, enter fixed text content and adjust the style format, and use placeholders to mark dynamic data cells;
[0009] Step 4: Fill the data into Excel. For multiple XFA table data sources, perform the above steps separately and merge the independent in-memory table structures into different worksheets of the same Excel workbook, or merge them into different areas of the same worksheet as needed.
[0010] Preferably, step one, the XFA form parsing step, is as follows:
[0011] A1 loads the PDF document provided by the user for subsequent data processing;
[0012] A2 performs in-depth analysis of the content in a PDF document, extracting relevant objects of AcroForm form fields from the loaded PDF content;
[0013] A3, verify that these AcroForm form field objects actually exist within the document and check whether they contain XFA data components;
[0014] A4. If a form field object is found to contain XFA data during the verification process, the data will be converted into a UTF-8 encoded string. If no XFA form field is found in the document, or the current processing flow does not support the XFA function, the system will issue a clear prompt to the user, informing the user: "This document does not contain an XFA form, or the current processing flow does not support the XFA function."
[0015] Preferably, the specific parsing steps for the XFAXML data in step two are as follows:
[0016] B1 converts the parsed XFA data into a byte array to ensure data format consistency;
[0017] B2 converts the byte array into a UTF-8 encoded string to ensure data accuracy and consistency;
[0018] B3. Use DocumentBuilderFactory to create a DocumentBuilderFactory instance to prepare for subsequent XML parsing;
[0019] B4. Using a DocumentBuilderFactory instance, create a DocumentBuilder instance for building and parsing XML documents;
[0020] B5 uses a DocumentBuilder instance to parse an XML string and returns a Document instance. The returned document is then normalized.
[0021] B6. Create an XPath instance and write the appropriate XPath expression;
[0022] B7, by analyzing the XML structure, defines a suitable XPath expression, and passes it along with the normalized document to the evaluate method of the XPath instance to accurately obtain the set of node data that meets the conditions;
[0023] B8. Create a HashMap instance to store the extracted tabular data text content, preparing it for subsequent data population into Excel; at the same time, consider defining entities and setting attribute values as the data source for Excel population to provide greater flexibility;
[0024] B9. Iterate through the obtained node data set, using the placeholders in the Excel file as keys and the text content of the node data as values, and store them in a HashMap.
[0025] Preferably, the specific steps for constructing the EXCEL table template in step three are as follows:
[0026] C1, Open PDF document: Use Adobe Acrobat Reader software to open the PDF document to be processed;
[0027] C2, Create a spreadsheet template: Create a corresponding spreadsheet template locally based on the target spreadsheet structure;
[0028] C3, Enter fixed text content and adjust style format: In the spreadsheet template, accurately enter the fixed text content in the corresponding position and ensure that the style and format of all cells and data are adjusted correctly;
[0029] C4, Identifying dynamic data cells: Uses specific placeholders to identify cells that need to be dynamically populated with data;
[0030] Preferably, the specific steps for filling data into Excel in step four are as follows:
[0031] D1, initialize the file path;
[0032] D11, initialize a variable to store the path to the Excel template file;
[0033] D12, initialize another variable to store the save path of the Excel file generated after filling in the data;
[0034] D2, instantiate an ExcelWriter object;
[0035] D21. Use a suitable library to instantiate an ExcelWriter object;
[0036] D22, this object will be responsible for all subsequent operations related to writing the Excel file;
[0037] D3 specifies the template file and the output file name;
[0038] D31, call the withTemplate method of the ExcelWriter object, and pass the previously initialized template file path as a parameter;
[0039] D32, use the write method to set the name and save path of the Excel file generated after filling in the data;
[0040] D4. Create a WriteSheet object and associate it with a worksheet;
[0041] D41, Create a WriteSheet object, which will represent the worksheet to which data will be written;
[0042] D42, call the writerSheet method of the ExcelWriter object to associate the WriteSheet object with the ExcelWriter;
[0043] D43, When associating, set the index of the worksheet or explicitly specify the name of the worksheet;
[0044] D5, fill the data into the worksheet;
[0045] D51, calls the fill method of the WriteSheet object;
[0046] D52, the data extracted from the PDF is passed as a parameter to the fill method;
[0047] D53, ensure that the data is filled into the worksheet in the correct format and location;
[0048] D6, close and release resources;
[0049] D61, After all data writing operations are completed, call the close method of the ExcelWriter object;
[0050] D62 will ensure that file handles are properly closed, avoiding memory leaks;
[0051] D63, at the same time, ensure that all write operations have completely written the data to the disk.
[0052] The beneficial effects of this invention are as follows:
[0053] This invention fully leverages the comprehensive compatibility of libraries such as Apache PDFBox, easily handling various PDF form types and extracting embedded XFA data from different PDF documents. Through Java's DOM parser, it can flexibly parse XFA XML data, utilizing its complex data model and relationships. The application of XPath expressions further improves the accuracy of data extraction, ensuring data accuracy and relevance. During parsing, XFA form data is mapped to a HashMap, enabling efficient data lookup and manipulation, while supporting diverse data structures such as list and map data population. Furthermore, it provides a custom template design function, allowing users to adjust the style, format, and preset placeholders of Excel files according to actual needs, thereby simplifying the generation process and ensuring the professionalism and consistency of the output results. For multiple XFA table data sources, this solution provides a modular processing approach, merging different data sources into different areas of an Excel workbook after parsing, achieving effective data integration and facilitating data analysis and report creation. Throughout the process, resource management is emphasized, ensuring proper resource release and exception handling, thereby improving program efficiency and system stability. Finally, the solution also has strong scalability, which can flexibly cope with more complex scenarios that may arise in the future. Through XPath query mechanism and data structure processing, it can quickly respond to and implement new requirements. Attached Figure Description
[0054] Figure 1 This is a flowchart of the present invention. Detailed Implementation
[0055] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0056] like Figure 1 As shown, this embodiment of the invention provides a method for extracting and merging Excel table data in XFA based on the JAVA language. The steps of this method for extracting and merging table data are as follows:
[0057] Step 1: Parse the XFA form; using Apache PDFBox, a non-standard method, a library capable of reading and parsing data in PDFs to obtain embedded XFA XML content is used to extract XFA data from the PDF document.
[0058] Step two involves parsing the XFA XML data. Having obtained the XFA XML data, Java's XML parser DOM is used to deeply parse this XML structure. The key to this step is accurately identifying the parts representing tabular data. Since XFA tabular data is stored in XML format, it means that it may contain complex data models, relationships, and style information. Therefore, the parsing process is relatively complex and requires a deep understanding of the details of the XFA data model. The DOM parser is used to progressively traverse the XML tree structure, extracting the necessary tabular data, laying the foundation for subsequent data processing and transformation.
[0059] Step 3: Construct an Excel spreadsheet template; Open the PDF document using Adobe Acrobat Reader, create a spreadsheet template that conforms to the target table structure, enter fixed text content and adjust the style format, and use placeholders to mark dynamic data cells;
[0060] Step 4: Fill the data into Excel. For multiple XFA table data sources, perform the above steps separately and merge the independent in-memory table structures into different worksheets of the same Excel workbook, or merge them into different areas of the same worksheet as needed.
[0061] The XFA form parsing steps in step one are as follows:
[0062] A1 loads the PDF document provided by the user for subsequent data processing;
[0063] A2 performs in-depth analysis of the content in a PDF document, extracting relevant objects of AcroForm form fields from the loaded PDF content;
[0064] A3, verify that these AcroForm form field objects actually exist within the document and check whether they contain XFA (Extended Rich Text Format) data components;
[0065] A4. If, during the verification process, a form field object is found to contain XFA data, this data is converted to a UTF-8 encoded string. This step is crucial because it ensures the accuracy and reliability of subsequent data parsing and processing. If no XFA form field is found in the document, or the current processing flow does not support XFA functionality, the system will issue a clear message to the user, informing them: "This document does not contain an XFA form, or the current processing flow does not currently support XFA functionality."
[0066] The specific steps for parsing the XFAXML data in step two are as follows:
[0067] B1 converts the parsed XFA data into a byte array to ensure data format consistency;
[0068] B2 converts the byte array into a UTF-8 encoded string to ensure data accuracy and consistency;
[0069] B3. Use DocumentBuilderFactory to create a DocumentBuilderFactory instance to prepare for subsequent XML parsing;
[0070] B4. Using a DocumentBuilderFactory instance, create a DocumentBuilder instance for building and parsing XML documents;
[0071] B5 uses a DocumentBuilder instance to parse an XML string and returns a Document instance. The returned document is then normalized to ensure its structural integrity and consistency.
[0072] B6. Create an XPath instance and write an appropriate XPath expression to precisely retrieve a set of node data that meets specific conditions. To improve program flexibility, the XPath expression can be passed as a function parameter.
[0073] B7, by analyzing the XML structure, defines a suitable XPath expression, and passes it along with the normalized document to the evaluate method of the XPath instance to accurately obtain the set of node data that meets the conditions;
[0074] B8. Create a HashMap instance to store the extracted tabular data text content, preparing it for subsequent data population into Excel; at the same time, consider defining entities and setting attribute values as the data source for Excel population to provide greater flexibility;
[0075] B9 iterates through the obtained node data set, using placeholders from the Excel file as keys and the text content of the node data as values, and stores them in a HashMap. This completes the data parsing and extraction, preparing for subsequent data population.
[0076] The specific steps for constructing the Excel spreadsheet template in step three are as follows:
[0077] C1, Open PDF document: Use Adobe Acrobat Reader software to open the PDF document to be processed;
[0078] C2, Create a spreadsheet template: Create a corresponding spreadsheet template locally based on the target spreadsheet structure;
[0079] C3, Enter fixed text content and adjust style format: In the spreadsheet template, accurately enter the fixed text content in the corresponding position and ensure that the style and format of all cells and data are adjusted correctly;
[0080] C4, Identifying Dynamic Data Cells: Uses specific placeholders to identify cells that need to be dynamically populated with data, so that subsequent data extraction and filling operations can accurately fill the data into the corresponding positions;
[0081] In templates, use {} to represent the variables you want to use. If there are already "{" or "}" special characters, use "\{" or "\}" instead.
[0082] When populating a list, be aware that the `{.}` in the template has an extra dot to represent the list. If the object used to populate the list is a map, it must contain all the keys of the list, even if the data is null; you must use `map.put(key, null)`.
[0083] The specific steps for filling the data into Excel in step four are as follows:
[0084] D1, initialize the file path;
[0085] D11, initialize a variable to store the path to the Excel template file;
[0086] D12, initialize another variable to store the save path of the Excel file generated after filling in the data;
[0087] D2, instantiate an ExcelWriter object;
[0088] D21. Use a suitable library (such as EasyExcel, ApachePOI, etc.) to instantiate an ExcelWriter object;
[0089] D22, this object will be responsible for all subsequent operations related to writing the Excel file;
[0090] D3 specifies the template file and the output file name;
[0091] D31, call the withTemplate method of the ExcelWriter object, and pass the previously initialized template file path as a parameter;
[0092] D32, use the write method to set the name and save path of the Excel file generated after filling in the data;
[0093] D4. Create a WriteSheet object and associate it with a worksheet;
[0094] D41, Create a WriteSheet object, which will represent the worksheet to which data will be written;
[0095] D42, call the writerSheet method of the ExcelWriter object to associate the WriteSheet object with the ExcelWriter;
[0096] D43, when associating, set the index of the worksheet (for example, 0 for the first worksheet) or explicitly specify the name of the worksheet;
[0097] D5, fill the data into the worksheet;
[0098] D51, calls the fill method of the WriteSheet object;
[0099] D52, the data extracted from the PDF is passed as a parameter to the fill method;
[0100] D53, ensure that the data is filled into the worksheet in the correct format and location;
[0101] D6, close and release resources;
[0102] D61, After all data writing operations are completed, call the close method of the ExcelWriter object;
[0103] D62 will ensure that file handles are properly closed, avoiding memory leaks;
[0104] D63, at the same time, ensure that all write operations have completely written the data to the disk.
[0105] When initializing file paths, ensure that the paths are correct and that the program has permission to access them.
[0106] When instantiating an ExcelWriter object and calling related methods, ensure that you follow the correct syntax and usage of the selected library.
[0107] When filling in data, ensure that the data format and position match the placeholders in the Excel template.
[0108] Before closing the ExcelWriter object, ensure that all write operations are complete to avoid data loss.
[0109] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus.
[0110] Although embodiments of the invention have been shown and described, it will be understood by those skilled in the art that various changes, modifications, substitutions and alterations can be made to these embodiments without departing from the principles and spirit of the invention, the scope of which is defined by the appended claims and their equivalents.
Claims
1. A method for extracting and merging EXCEL table data in XFA based on JAVA language, characterized in that: The steps for extracting and merging table data are as follows: Step 1: Parse the XFA form; using Apache PDFBox, a non-standard method, a library capable of reading and parsing data in PDFs to obtain embedded XFA XML content is used to extract XFA data from the PDF document. Step 2: Parse the XFAXML data; After obtaining the XFAXML data, use Java's XML parser DOM to deeply parse this XML structure. Through the DOM parser, traverse the XML tree structure step by step to extract the required table data. Step 3: Create an Excel spreadsheet template; Open the PDF document with Adobe Acrobat Reader, create a spreadsheet template that conforms to the target table structure, enter fixed text content and adjust the style format, and mark dynamic data cells with placeholders. Step 4: Fill the data into Excel. For multiple XFA table data sources, perform the above steps separately and merge the independent in-memory table structures into different worksheets of the same Excel workbook, or merge them into different areas of the same worksheet as needed.
2. The method for extracting and merging Excel table data in XFA based on JAVA language according to claim 1, characterized in that: The XFA form parsing step one is as follows: A1 loads the PDF document provided by the user for subsequent data processing; A2 performs in-depth analysis of the content in a PDF document, extracting relevant objects of AcroForm form fields from the loaded PDF content; A3, verify that these AcroForm form field objects actually exist within the document and check whether they contain XFA data components; A4. If a form field object is found to contain XFA data during the verification process, the data will be converted into a UTF-8 encoded string. If no XFA form field is found in the document, or the current processing flow does not support the XFA function, the system will issue a clear prompt to the user, informing the user: "This document does not contain an XFA form, or the current processing flow does not support the XFA function." 3. The method for extracting and merging Excel table data in XFA based on JAVA language according to claim 1, characterized in that: The specific steps for parsing the XFAXML data in step two are as follows: B1 converts the parsed XFA data into a byte array to ensure data format consistency; B2 converts the byte array into a UTF-8 encoded string to ensure data accuracy and consistency; B3. Use DocumentBuilderFactory to create a DocumentBuilderFactory instance to prepare for subsequent XML parsing; B4. Using a DocumentBuilderFactory instance, create a DocumentBuilder instance for building and parsing XML documents; B5 uses a DocumentBuilder instance to parse an XML string and returns a Document instance. The returned document is then normalized. B6. Create an XPath instance and write the appropriate XPath expression; B7, by analyzing the XML structure, defines a suitable XPath expression, and passes it along with the normalized document to the evaluate method of the XPath instance to accurately obtain the set of node data that meets the conditions; B8. Create a HashMap instance to store the extracted tabular data text content, preparing it for subsequent data population into Excel; at the same time, consider defining entities and setting attribute values as the data source for Excel population to provide greater flexibility; B9. Iterate through the obtained node data set, using the placeholders in the Excel file as keys and the text content of the node data as values, and store them in a HashMap.
4. The method for extracting and merging Excel table data in XFA based on JAVA language according to claim 1, characterized in that: The specific steps for constructing the Excel spreadsheet template in step three are as follows: C1, Open PDF document: Use Adobe Acrobat Reader software to open the PDF document to be processed; C2, Create a spreadsheet template: Create a corresponding spreadsheet template locally based on the target spreadsheet structure; C3, Enter fixed text content and adjust style format: In the spreadsheet template, accurately enter the fixed text content in the corresponding position and ensure that the style and format of all cells and data are adjusted correctly; C4, Identifying Dynamic Data Cells: Uses specific placeholders to identify cells that need to be dynamically populated with data.
5. The method for extracting and merging Excel table data in XFA based on JAVA language according to claim 1, characterized in that: The specific steps for filling data into Excel in step four are as follows: D1, initialize the file path; D11, initialize a variable to store the path to the Excel template file; D12, initialize another variable to store the save path of the Excel file generated after filling in the data; D2, instantiate an ExcelWriter object; D21. Use a suitable library to instantiate an ExcelWriter object; D22, this object will be responsible for all subsequent operations related to writing the Excel file; D3 specifies the template file and the output file name; D31, call the withTemplate method of the ExcelWriter object, and pass the previously initialized template file path as a parameter; D32, use the write method to set the name and save path of the Excel file generated after filling in the data; D4. Create a WriteSheet object and associate it with a worksheet; D41, Create a WriteSheet object, which will represent the worksheet to which data will be written; D42, call the writerSheet method of the ExcelWriter object to associate the WriteSheet object with the ExcelWriter; D43, When associating, set the index of the worksheet or explicitly specify the name of the worksheet; D5, fill the data into the worksheet; D51, calls the fill method of the WriteSheet object; D52, the data extracted from the PDF is passed as a parameter to the fill method; D53, ensure that the data is filled into the worksheet in the correct format and location; D6, close and release resources; D61, After all data writing operations are completed, call the close method of the ExcelWriter object; D62 will ensure that file handles are properly closed, avoiding memory leaks; D63 ensures that all write operations have completely written the data to the disk.