Word document format conversion method based on Java

This document format conversion method, developed in Java, utilizes Apache POI and the Office Open XML specification to parse and reconstruct .doc files into .docx format, resolving issues of format loss and cross-platform compatibility, and achieving efficient and accurate document conversion for enterprise-level applications.

CN121052214APending Publication Date: 2025-12-02SHANDONG LANGCHAO YUNTOU INFORMATION TECH CO LTD
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202511215881.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-08-28
Publication Date
2025-12-02

AI Technical Summary

Technical Problem

Existing technologies struggle to efficiently and stably convert .doc format documents to .docx format, resulting in format loss, content corruption, and cross-platform compatibility issues. Furthermore, they lack open-source support and modular extensibility, making it difficult to meet the deployment needs of enterprise-level systems.

Method used

This document format conversion method, developed using Java, utilizes the Apache POI component to parse .doc files, extract text, tables, images, and style information, constructs a style mapping rule base, and reconstructs the document into a .docx document structure that conforms to the Office Open XML standard. It also introduces exception handling and logging mechanisms and supports plug-in extensions.

Benefits of technology

It achieves efficient and accurate document format conversion, improves document compatibility and cross-platform capabilities, significantly reduces enterprise document upgrade costs, is suitable for enterprise-level document management systems and cloud office platforms, and supports batch document processing and multi-language conversion.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121052214A_ABST
    Figure CN121052214A_ABST
Patent Text Reader

Abstract

The invention particularly relates to a Word document format conversion method based on Java. The Word document format conversion method based on Java comprises the following steps: analyzing the content of an original. Doc file, and extracting text paragraphs, tables, pictures, style information and document metadata; the extracted content is divided into different categories, and the XML node type corresponding to each category of elements in the target. Docx document is established; the method comprises the following steps of: constructing a pattern mapping rule base, constructing a new. Docx document structure by using an XWPF Document object model according to an Office Open XML (Extensible Markup Language) specification, sequentially inserting paragraphs, tables and pictures, and applying corresponding pattern configuration; and outputting and generating a. Docx file, detecting and processing abnormal conditions, and recording a conversion log at the same time. The Word document format conversion method based on Java is efficient and accurate, has good compatibility, expandability and cross-platform capability, is suitable for enterprise-level document management systems, cloud services and batch document processing scenes, and remarkably improves document compatibility and processing efficiency of office automation systems.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of document processing technology, and in particular to a Java-based method for converting Word document formats. Background Technology

[0002] With the iterative upgrades of office software versions, the .doc format (based on the binary structure of Word 97-2003) has gradually been replaced by the .docx format (based on the XML-based Office Open XML standard). However, a large number of legacy .doc documents have compatibility issues with newer versions of office software, specifically manifested as follows:

[0003] (1) Formatting loss: Complex styles in .doc documents (such as table borders, nested lists, paragraph indentation) cannot be fully restored in .docx documents;

[0004] (2) Content disorder: The difference between binary format and XML structure causes layout offset or data loss of text, image, table and other elements after conversion;

[0005] (3) Cross-platform limitations: Traditional tools rely on specific operating systems or proprietary software, making it difficult to meet the deployment needs of enterprise-level systems.

[0006] Existing solutions largely rely on commercial software or closed toolchains, lacking open-source support and modular extensibility, making them difficult to adapt to large-scale, high-concurrency document conversion needs. Therefore, there is an urgent need for an efficient, stable, and scalable .doc to .docx conversion method to achieve smooth document format migration and content fidelity.

[0007] Based on the above, this invention proposes a Java-based Word document format conversion method. Summary of the Invention

[0008] To overcome the shortcomings of existing technologies, this invention provides a simple and efficient Java-based method for converting Word document formats.

[0009] This invention is achieved through the following technical solution:

[0010] A Java-based method for converting Word document formats includes the following steps:

[0011] Step S1: Parse the contents of the original .doc file.

[0012] The HWPFDocument class in the Apache POI component is used to read the binary structure of .doc files, identify and extract text paragraphs, tables, images, style information and document metadata;

[0013] In step S1, for text and paragraphs, the paragraph content, alignment and indentation settings are extracted, and the paragraph style and its associated attributes are recorded.

[0014] For tables and images, the table structure in the .doc file is traversed, cell content, merge status and border styles are extracted, image resources are extracted and saved as temporary files, and their insertion position and layout information are recorded.

[0015] Step S2: Extract and classify document elements

[0016] The extracted content is divided into different categories, including text content, paragraph formatting, character styles, table structure, image resources, and page setup, and the corresponding XML node type for each type of element in the target .docx document is established.

[0017] Step S3: Build a style mapping rule base

[0018] Based on the differences between .doc and .docx formats, a style mapping table is defined to map the style attributes in .doc, including font, font size, color, bold, italic, underline, paragraph alignment, indentation, and line spacing, to XML tags and attribute values ​​that conform to the Office OpenXML standard.

[0019] In step 3, the style mapping rule base includes, but is not limited to, the following mapping relationships:

[0020] The heading styles in a .doc file correspond to the built-in paragraph styles in a .docx file;

[0021] Character styles in a .doc file, such as bold, italics, and underline, are mapped to XML tags, respectively. <w:b / > , <w:i / > and <w:u / > ;

[0022] The font size unit is converted from points (pt) to twips (twips), with 1 point corresponding to 20 twips, and this is done in the XML.<w:szval="..." / > express;

[0023] Color information is encoded in RGB hexadecimal format and embedded in...<w:color val="..." / > In the node;

[0024] Paragraph alignment options, including left alignment, center alignment, right alignment, and justification, are mapped as follows:<w:jc w:val="left / center / right / both" / > ;

[0025] Table 1. Mapping between .doc and .docx styles

[0026] .doc style name Corresponding .docx style name Mapping Description Heading 1 <w:pPr w:style="Heading 1" / > direct mapping Bold <w:rpr> <w:b / > < / w:rpr> Character-level bold style Font Size 12 <w:rpr><w:sz val="24" / >< / w:rpr> Unit conversion (1pt = 2twips) Red Text <w:rpr><w:color val="FF0000" / >< / w:rpr> Hexadecimal color encoding conversion

[0027] Table 2. Comparison of Document Structures

[0028]

[0029] Custom styles: Convert user-defined paragraph or character styles contained in a .doc document into a "style-based" structure in a .docx file, or manually build similar styles based on their actual appearance.

[0030] Compound styles: For example, if a piece of text has both bold and italic effects, you should ensure that multiple style attributes can be correctly stacked in the .docx file.

[0031] Paragraph structure reconstruction: Reorganize paragraph blocks in a .doc document into the structure of a .docx document. <w:p>Create elements and add corresponding paragraph attribute nodes to them. <w:ppr>;

[0032] Run-level style handling: Each text segment (Run) has its own independent style definition in the .docx file. Therefore, it is necessary to split the continuous text in the .doc document according to the style change points and set the style for each segment separately.

[0033] Table structure standardization: In .docx documents, tables use... <w:tbl>The label indicates that each line is composed of... <w:tr>Defined, each cell is composed of <w:tc>This indicates that the table structure should be kept intact during conversion, including row and column merging, border settings, and cell backgrounds.

[0034] Image and object embedding methods adjusted: Images in .doc documents are typically inline objects, while .docx documents support richer text and image integration methods. Images in .doc documents can be encapsulated as... <w:drawing>or <w:pict>The node retains its layout information (such as alignment, float properties, etc.).

[0035] Step S4: Reconstruct the semantic structure of the document

[0036] Following the Office Open XML specification, a new .docx document structure is built using the XWPF Document object model, paragraphs, tables, and images are inserted sequentially, and the corresponding style configurations are applied.

[0037] Step S5: Output and generate .docx file

[0038] The completed document structure is serialized into a compressed file in .docx format, containing document.xml (body content), styles.xml (style definitions), rels relationship files, and other necessary resources. The completed .docx document is then written to the specified location or storage medium.

[0039] Step S6: Exception Handling and Logging

[0040] An exception handling and fault tolerance mechanism is built to detect and handle abnormal situations during the conversion process, including missing styles and structural incompatibility, while recording detailed conversion logs to facilitate subsequent analysis and optimization.

[0041] In step 4, when reconstructing the semantic structure of the document, the following complex structures are supported for transformation:

[0042] (1) The hierarchical structure of nested tables remains unchanged;

[0043] (2) Merging cell information via <w:vmerge>or <w:hmerge>Nodes are used for expression;

[0044] (3) The image is inserted as a floating or inline object into the specified paragraph position, while preserving its alignment and wrapping mode;

[0045] (4) List items, including numbers and bullets, are converted to a .docx list template (Numbering) and the nested hierarchy is maintained.

[0046] In step S6, the exception handling and fault tolerance mechanism is as follows:

[0047] Default style overridden: Styles that cannot find a suitable .docx mapping will fall back to the default style (such as Normal);

[0048] Structural simplification strategy: Automatically simplify complex tables or nested structures into readable text content;

[0049] Log recording and prompts: When an exception occurs, record detailed error information and prompt the user with the content that could not be perfectly converted;

[0050] Configurable conversion strategy: Allows users to choose whether to enable strict mode (high fidelity but error-prone) or compatibility mode (sacrificing some format integrity to improve stability).

[0051] A Java-based Word document format conversion system, developed using the Java language and deployed and executed on Windows, Linux, or macOS operating systems, is used to implement the above methods, including:

[0052] The file input parsing module is responsible for loading .doc files and inputting them into the Apache POI component.

[0053] Data Extraction and Processing Module: Responsible for using the HWPFDocument class in the Apache POI component to read the binary structure of .doc files, identify and extract text paragraphs, tables, images, style information and document metadata; and classify the extracted content into different categories, including text content, paragraph formatting, character styles, table structure, image resources and page setup, and establish the corresponding XML node type for each type of element in the target .docx document;

[0054] The format conversion and reconstruction module is responsible for first defining a style mapping table based on the differences between .doc and .docx formats. This table maps style attributes in the .doc file, including font, font size, color, bold, italic, underline, paragraph alignment, indentation, and line spacing, to XML tags and attribute values ​​that conform to the Office Open XML (OOXML) standard. Then, following the Office OpenXML specification, it uses the XWPF Document object model to construct a new .docx document structure, inserting paragraphs, tables, and images in sequence, and applying the corresponding style configurations.

[0055] Output saving module: responsible for serializing the completed document structure into a .docx format compressed file, including document.xml (body content), styles.xml (style definition), rels relationship file and other necessary resources, and writing the built .docx document to the specified location or storage medium;

[0056] Log and exception handling module: responsible for building exception handling and fault tolerance mechanisms, detecting and handling exceptions during the conversion process, including missing styles and structural incompatibility, while recording detailed conversion logs to facilitate problem tracking and diagnosis;

[0057] The configuration management module allows users to customize conversion parameters and style strategies.

[0058] It supports a plug-in extension architecture, enabling compatibility conversion with other document formats, including .rtf, .odt, and .docm documents, by adding new plug-ins.

[0059] It also includes a graphical user interface module and an extended functionality module;

[0060] The graphical user interface module supports users to upload .doc files and select the output path, displays the conversion progress bar and success / failure prompts, provides a preview window to show the document comparison before and after conversion, and allows users to manually adjust the style mapping strategy or replace unmatched styles;

[0061] The extended function module is used to support the following additional operations:

[0062] Perform OCR recognition on scanned .doc documents and then convert the format.

[0063] Automatically identify and repair damaged .doc file structures;

[0064] Supports correct encoding conversion for multilingual documents (such as Chinese, English, and Japanese);

[0065] Provides API interfaces for third-party systems to call and integrate document automation processes.

[0066] This invention is a Java-based Word document format conversion system that supports deployment in any of the following environments:

[0067] Standalone desktop application;

[0068] The web server system provides a RESTful API interface to receive upload requests and return conversion results.

[0069] A distributed document processing platform that combines message queues (such as RabbitMQ and Kafka) to achieve high-concurrency document conversion task scheduling;

[0070] It can be integrated into enterprise-level document management systems or cloud-based office platforms as a plugin.

[0071] A Java-based Word document format conversion device includes a memory and a processor; the memory is used to store a computer program, and the processor is used to execute the computer program to implement the above-described method steps.

[0072] A readable storage medium storing a computer program that, when executed by a processor, implements the above-described method steps.

[0073] The beneficial effects of this invention are: the Java-based Word document format conversion method is efficient and accurate, with good compatibility, scalability and cross-platform capability, and is suitable for enterprise-level document management systems, cloud services and batch document processing scenarios, significantly improving the document compatibility and processing efficiency of office automation systems. Attached Figure Description

[0074] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0075] Figure 1 This is a schematic diagram of the Java-based Word document format conversion method of the present invention. Detailed Implementation

[0076] To enable those skilled in the art to better understand the technical solutions of this invention, the technical solutions in the embodiments of this invention will be clearly and completely described below in conjunction with the embodiments of this invention. Obviously, the described embodiments are merely some embodiments of this invention, and not all embodiments. Based on the embodiments of this invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of this invention.

[0077] This Java-based Word document format conversion method includes the following steps:

[0078] Step S1: Parse the contents of the original .doc file.

[0079] The HWPFDocument class in the Apache POI component is used to read the binary structure of .doc files, identify and extract text paragraphs, tables, images, style information and document metadata;

[0080] In step S1, for text and paragraphs, the paragraph content, alignment and indentation settings are extracted, and the paragraph style (such as Heading 1, Normal) and its associated attributes are recorded.

[0081] For tables and images, the table structure in the .doc file is traversed, cell content, merge status and border styles are extracted, image resources are extracted and saved as temporary files, and their insertion position and layout information are recorded.

[0082] Step S2: Extract and classify document elements

[0083] The extracted content is divided into different categories, including text content, paragraph formatting, character styles, table structure, image resources, and page setup, and the corresponding XML node type for each type of element in the target .docx document is established.

[0084] Step S3: Build a style mapping rule base

[0085] Based on the differences between .doc and .docx formats, a style mapping table is defined to map the style attributes in .doc, including font, font size, color, bold, italic, underline, paragraph alignment, indentation, and line spacing, to XML tags and attribute values ​​that conform to the Office OpenXML (OOXML) standard.

[0086] Since .doc and .docx are two different file formats, their underlying data structures and style definition mechanisms differ significantly. Therefore, when converting from .doc to .docx, a complete style mapping rule base must be established to guide how to convert style information from the old format into an equivalent expression in the new format. This rule base can be organized in the form of key-value pairs or configuration tables.

[0087] In step 3, the style mapping rule base includes, but is not limited to, the following mapping relationships:

[0088] The heading styles in .doc (such as Heading 1, Heading 2) correspond to the built-in paragraph styles in .docx.

[0089] Character styles in a .doc file, such as bold, italics, and underline, are mapped to XML tags, respectively. <w:b / > , <w:i / > and <w:u / > ;

[0090] The font size unit is converted from points (pt) to twips (twips), with 1 point corresponding to 20 twips, and this is done in the XML.<w:szval="..." / > express;

[0091] Color information uses RGB hexadecimal encoding format, such as "Red"→FF0000, and is embedded in...<w:color val="..." / > In the node;

[0092] Paragraph alignment options, including left alignment, center alignment, right alignment, and justification, are mapped as follows:<w:jc w:val="left / center / right / both" / > ;

[0093] Table 1. Mapping between .doc and .docx styles

[0094] .doc style name Corresponding .docx style name Mapping Description Heading 1 <w:pPr w:style="Heading 1" / > direct mapping Bold <w:rpr> <w:b / > < / w:rpr> Character-level bold style Font Size 12 <w:rpr><w:sz val="24" / >< / w:rpr> Unit conversion (1pt = 2twips) Red Text <w:rpr><w:color val="FF0000" / >< / w:rpr> Hexadecimal color encoding conversion

[0095] Table 2. Comparison of Document Structures

[0096]

[0097]

[0098] Custom styles: Convert user-defined paragraph or character styles contained in a .doc document into a "style-based" structure in a .docx file, or manually build similar styles based on their actual appearance.

[0099] Compound styles: For example, if a piece of text has both bold and italic effects, you should ensure that multiple style attributes can be correctly stacked in the .docx file.

[0100] Paragraph structure reconstruction: Reorganize paragraph blocks in a .doc document into the structure of a .docx document. <w:p>Create elements and add corresponding paragraph attribute nodes to them. <w:ppr>;

[0101] Run-level style handling: Each text segment (Run) has its own independent style definition in the .docx file. Therefore, it is necessary to split the continuous text in the .doc document according to the style change points and set the style for each segment separately.

[0102] Table structure standardization: In .docx documents, tables use... <w:tbl>The label indicates that each line is composed of... <w:tr>Defined, each cell is composed of <w:tc>This indicates that the table structure should be kept intact during conversion, including row and column merging, border settings, and cell backgrounds.

[0103] Image and object embedding methods adjusted: Images in .doc documents are typically inline objects, while .docx documents support richer text and image integration methods. Therefore, images in .doc documents need to be encapsulated as... <w:drawing>or <w:pict>The node retains its layout information (such as alignment, float properties, etc.).

[0104] Step S4: Reconstruct the semantic structure of the document

[0105] Following the Office Open XML specification, a new .docx document structure is built using the XWPF Document object model, paragraphs, tables, and images are inserted sequentially, and the corresponding style configurations are applied.

[0106] Step S5: Output and generate .docx file

[0107] The completed document structure is serialized into a compressed file in .docx format, containing document.xml (body content), styles.xml (style definition), rels relationship file and other necessary resources (known to those skilled in the art, see Table 3 for details), and the completed .docx document is written to the specified location or storage medium;

[0108] Table 3 Other Necessary Resources List

[0109] Step S6: Exception Handling and Logging

[0110] An exception handling and fault tolerance mechanism is built to detect and handle abnormal situations during the conversion process, including missing styles and structural incompatibility, while recording detailed conversion logs to facilitate subsequent analysis and optimization.

[0111] In step 4, when reconstructing the semantic structure of the document, the following complex structures are supported for transformation:

[0112] (1) The hierarchical structure of nested tables remains unchanged;

[0113] (2) Merging cell information via <w:vmerge>or <w:hmerge>Nodes are used for expression;

[0114] (3) The image is inserted as a floating or inline object into the specified paragraph position, while preserving its alignment and wrapping mode;

[0115] (4) List items, including numbers and bullets, are converted to a .docx list template (Numbering) and the nested hierarchy is maintained.

[0116] In step S6, the exception handling and fault tolerance mechanism is as follows:

[0117] Default style overridden: Styles that cannot find a suitable .docx mapping will fall back to the default style (such as Normal);

[0118] Structural simplification strategy: Automatically simplify complex tables or nested structures into readable text content;

[0119] Log recording and prompts: When an exception occurs, record detailed error information and prompt the user with the content that could not be perfectly converted;

[0120] Configurable conversion strategy: Allows users to choose whether to enable strict mode (high fidelity but error-prone) or compatibility mode (sacrificing some format integrity to improve stability).

[0121] This Java-based Word document format conversion system, developed in Java and deployed on Windows, Linux, or macOS operating systems, is used to implement the aforementioned methods, including:

[0122] The file input parsing module is responsible for loading .doc files and inputting them into the Apache POI component.

[0123] Data Extraction and Processing Module: Responsible for using the HWPFDocument class in the Apache POI component to read the binary structure of .doc files, identify and extract text paragraphs, tables, images, style information and document metadata; and classify the extracted content into different categories, including text content, paragraph formatting, character styles, table structure, image resources and page setup, and establish the corresponding XML node type for each type of element in the target .docx document;

[0124] The format conversion and reconstruction module is responsible for first defining a style mapping table based on the differences between .doc and .docx formats. This table maps style attributes in the .doc file, including font, font size, color, bold, italic, underline, paragraph alignment, indentation, and line spacing, to XML tags and attribute values ​​that conform to the Office Open XML (OOXML) standard. Then, following the Office OpenXML specification, it uses the XWPF Document object model to construct a new .docx document structure, inserting paragraphs, tables, and images in sequence, and applying the corresponding style configurations.

[0125] Output saving module: responsible for serializing the completed document structure into a .docx format compressed file, including document.xml (body content), styles.xml (style definition), rels relationship file and other necessary resources, and writing the built .docx document to the specified location or storage medium;

[0126] Log and exception handling module: responsible for building exception handling and fault tolerance mechanisms, detecting and handling exceptions during the conversion process, including missing styles and structural incompatibility, while recording detailed conversion logs to facilitate problem tracking and diagnosis;

[0127] The configuration management module allows users to customize conversion parameters and style strategies.

[0128] This Java-based Word document format conversion system supports a plug-in extension architecture, enabling compatible conversion of other document formats, including .rtf, .odt, and .docm documents, by adding new plug-ins.

[0129] It also includes a graphical user interface module and an extended functionality module;

[0130] The graphical user interface module supports users to upload .doc files and select the output path, displays the conversion progress bar and success / failure prompts, provides a preview window to show the document comparison before and after conversion, and allows users to manually adjust the style mapping strategy or replace unmatched styles;

[0131] The extended function module is used to support the following additional operations:

[0132] Perform OCR recognition on scanned .doc documents and then convert the format.

[0133] Automatically identify and repair damaged .doc file structures;

[0134] Supports correct encoding conversion for multilingual documents (such as Chinese, English, and Japanese);

[0135] Provides API interfaces for third-party systems to call and integrate document automation processes.

[0136] This Java-based Word document format conversion system supports deployment in any of the following environments:

[0137] Standalone desktop application;

[0138] The web server system provides a RESTful API interface to receive upload requests and return conversion results.

[0139] A distributed document processing platform that combines message queues (such as RabbitMQ and Kafka) to achieve high-concurrency document conversion task scheduling;

[0140] It can be integrated into enterprise-level document management systems or cloud-based office platforms as a plugin.

[0141] The Java-based Word document format conversion device includes a memory and a processor; the memory is used to store a computer program, and the processor is used to execute the computer program to implement the above-described method steps.

[0142] The readable storage medium stores a computer program that, when executed by a processor, implements the above-described method steps.

[0143] Compared with existing technologies, this Java-based Word document format conversion method has the following characteristics:

[0144] (1) Improved document compatibility and readability

[0145] By accurately parsing the .doc file structure and reconstructing it into a .docx document that conforms to the Office Open XML standard, the problem of formatting errors and content loss when opening old versions of documents in new office software is effectively solved, significantly improving document compatibility and display effect.

[0146] (4) Efficiently achieved batch document migration

[0147] The system supports high-concurrency processing and automated conversion processes, enabling rapid batch conversion of large-scale .doc documents to .docx format, meeting the efficient processing needs of scenarios such as enterprise historical document upgrades and archive digitization.

[0148] (3) Able to maintain the integrity of the original document style

[0149] By introducing a style mapping rule library, semantic-level transformations are performed on complex styles such as fonts, paragraphs, tables, and images, which can preserve the original document's layout style and visual presentation to the greatest extent, ensuring that the converted .docx document has high fidelity.

[0150] (4) Strong cross-platform operation capability

[0151] The overall solution is developed based on the Java language and has good cross-platform characteristics. It can be deployed on multiple operating system environments such as Windows, Linux, and macOS, and is suitable for the IT infrastructure of different enterprises.

[0152] (5) Enhanced exception handling and fault tolerance capabilities

[0153] By providing a comprehensive logging and error feedback mechanism, it can detect and handle issues such as formatting errors and missing styles during the conversion process, while also providing a default style replacement strategy to ensure a stable and reliable conversion process.

[0154] (6) Reduced enterprise document upgrade costs

[0155] Compared to relying on commercial software or manual reformatting, the open-source, automated solution provided by this invention significantly reduces the manpower and time costs of document format upgrades, and has good economic benefits and promotional value.

[0156] (7) It can promote office automation and digital transformation

[0157] As a key link in the document processing chain, this technology can be widely applied to various office automation systems, knowledge management systems, and digital archive platforms, promoting enterprises and institutions towards paperless and intelligent office practices.

[0158] The embodiments described above are merely one specific implementation of the present invention. Ordinary changes and substitutions made by those skilled in the art within the scope of the technical solution of the present invention should be included within the protection scope of the present invention.< / w:hmerge> < / w:vmerge> < / w:pict> < / w:drawing> < / w:tc> < / w:tr> < / w:tbl> < / w:ppr> < / w:p> < / w:hmerge> < / w:vmerge> < / w:pict> < / w:drawing> < / w:tc> < / w:tr> < / w:tbl> < / w:ppr> < / w:p>

Claims

1. A Java-based method for converting Word document formats, characterized in that: Includes the following steps: Step S1: Parse the contents of the original .doc file. The HWPFDocument class in the Apache POI component is used to read the binary structure of .doc files, identify and extract text paragraphs, tables, images, style information and document metadata; Step S2: Extract and classify document elements The extracted content is divided into different categories, including text content, paragraph formatting, character styles, table structure, image resources, and page setup, and the corresponding XML node type for each type of element in the target .docx document is established. Step S3: Build a style mapping rule base Based on the differences between .doc and .docx formats, a style mapping table is defined to map the style attributes in .doc, including font, font size, color, bold, italic, underline, paragraph alignment, indentation, and line spacing, to XML tags and attribute values ​​that conform to the Office Open XML standard. Step S4: Reconstruct the semantic structure of the document Following the Office Open XML specification, a new .docx document structure is built using the XWPF Document object model, paragraphs, tables, and images are inserted sequentially, and the corresponding style configurations are applied. Step S5: Output and generate .docx file The completed document structure is serialized into a .docx format compressed file, containing document.xml, styles.xml, and rels relationship files, as well as other necessary resources. The completed .docx document is then written to the specified location or storage medium. Step S6: Exception Handling and Logging An exception handling and fault tolerance mechanism is built to detect and handle abnormal situations during the conversion process, including missing styles and structural incompatibility, while recording conversion logs to facilitate subsequent analysis and optimization.

2. The Java-based Word document format conversion method according to claim 1, characterized in that: In step S1, for text and paragraphs, the paragraph content, alignment and indentation settings are extracted, and the paragraph style and its associated attributes are recorded. For tables and images, the table structure in the .doc file is traversed, cell content, merge status and border styles are extracted, image resources are extracted and saved as temporary files, and their insertion position and layout information are recorded.

3. The Java-based Word document format conversion method according to claim 1, characterized in that: In step 3, the style mapping rule base includes, but is not limited to, the following mapping relationships: The heading styles in a .doc file correspond to the built-in paragraph styles in a .docx file; Character styles in a .doc file, such as bold, italics, and underline, are mapped to XML tags, respectively. <w:b / > , <w:i / > and <w:u / > ; The font size unit is converted from points (pt) to twips (twips), with 1 point corresponding to 20 twips, and this is done in the XML.<w:sz val="..." / > express; Color information is encoded in RGB hexadecimal format and embedded in...<w:color val="..." / > In the node; Paragraph alignment options, including left alignment, center alignment, right alignment, and justification, are mapped as follows:<w:jc w:val="left / center / right / both" / > ; Reorganize paragraph blocks in a .doc document into a .docx file. <w:p>Create elements and add corresponding paragraph attribute nodes to them. <w:ppr> ;< / w:ppr> < / w:p> Split continuous text in a .doc document according to style change points and set styles for each segment; In .docx documents, tables are used <w:tbl>The label indicates that each line is composed of... <w:tr>Defined, each cell is composed of <w:tc> Note: During conversion, ensure the table structure remains intact, including row and column merging, border settings, and cell backgrounds.< / w:tc> < / w:tr> < / w:tbl> Encapsulate images from .doc documents into... <w:drawing>or <w:pict> Nodes, and retain their layout information.< / w:pict> < / w:drawing> 4. The Java-based Word document format conversion method according to claim 1, characterized in that: In step 4, when reconstructing the semantic structure of the document, the following complex structures are supported for transformation: (1) The hierarchical structure of nested tables remains unchanged; (2) Merging cell information via <w:vmerge>or <w:hmerge> Nodes are used for expression;< / w:hmerge> < / w:vmerge> (3) The image is inserted as a floating or inline object into the specified paragraph position, while preserving its alignment and wrapping mode; (4) List items, including numbers and bullets, are converted to .docx list templates and the nested hierarchy is maintained.

5. The Java-based Word document format conversion method according to claim 1, characterized in that: In step S6, the exception handling and fault tolerance mechanism is as follows: Default style overridden: Reverts styles that cannot find a suitable .docx mapping to the default styles; Structural simplification strategy: Automatically simplify complex tables or nested structures into readable text content; Log recording and prompts: When an exception occurs, log detailed error information and prompt the user with the content that could not be converted.

6. A Java-based Word document format conversion system, characterized in that: Developed using the Java language and deployed and executed on Windows, Linux, or macOS operating systems, this method is used to implement the method described in any one of claims 1 to 5, comprising: The file input parsing module is responsible for loading .doc files and inputting them into the Apache POI component. Data Extraction and Processing Module: Responsible for using the HWPFDocument class in the Apache POI component to read the binary structure of .doc files, identify and extract text paragraphs, tables, images, style information and document metadata; and classify the extracted content into different categories, including text content, paragraph formatting, character styles, table structure, image resources and page setup, and establish the corresponding XML node type for each type of element in the target .docx document; The format conversion and reconstruction module is responsible for first defining a style mapping table based on the differences between .doc and .docx formats. This table maps style attributes in the .doc file, including font, font size, color, bold, italic, underline, paragraph alignment, indentation, and line spacing, to XML tags and attribute values ​​that conform to the Office Open XML standard. Then, following the Office Open XML specification, it uses the XWPF Document object model to construct a new .docx document structure, inserting paragraphs, tables, and images in sequence, and applying the corresponding style configurations. Output saving module: responsible for serializing the completed document structure into a .docx format compressed file, including document.xml, styles.xml and rels relationship files and other necessary resources, and writing the built .docx document to the specified location or storage medium; Log and exception handling module: responsible for building exception handling and fault tolerance mechanisms, detecting and handling exceptions during the conversion process, including missing styles and structural incompatibility, while recording conversion logs to facilitate problem tracking and diagnosis; The configuration management module allows users to customize conversion parameters and style strategies.

7. The Java-based Word document format conversion system according to claim 6, characterized in that: It supports a plug-in extension architecture, enabling compatibility conversion with other document formats, including .rtf, .odt, and .docm documents, by adding new plug-ins. It also includes a graphical user interface module and an extended functionality module; The graphical user interface module supports users to upload .doc files and select the output path, displays the conversion progress bar and success / failure prompts, provides a preview window to show the document comparison before and after conversion, and allows users to manually adjust the style mapping strategy or replace unmatched styles; The extended function module is used to support the following additional operations: Perform OCR recognition on scanned .doc documents and then convert the format. Automatically identify and repair damaged .doc file structures; Supports correct encoding conversion for multilingual documents; Provides API interfaces for third-party systems to call and integrate document automation processes.

8. The Java-based Word document format conversion system according to claim 6, characterized in that: Supports deployment in any of the following environments: Standalone desktop application; The web server system provides a RESTful API interface to receive upload requests and return conversion results. A distributed document processing platform that combines message queues to schedule high-concurrency document conversion tasks; It can be integrated into enterprise-level document management systems or cloud-based office platforms as a plugin.

9. A Java-based Word document format conversion device, characterized in that: It includes a memory and a processor; the memory is used to store a computer program, and the processor is used to execute the computer program to implement the method according to any one of claims 1 to 5.

10. A readable storage medium, characterized in that: The readable storage medium stores a computer program, which, when executed by a processor, implements the method described in any one of claims 1 to 5.

Citation Information

Cited By

  • Web-based automatic reference format conversion system

    CN121809406A

  • Format reservation method in literature translation process and storage medium

    CN122242441A