Text content self-adaptive insertion method and device based on PDF file and related medium
By performing structured analysis and logical aggregation of PDF files, and combining collision detection algorithms to adjust the font ratio of text blocks or rearrange the layout, the problem of inconsistent insertion of PDF documents in the existing technology is solved, efficient adaptive insertion and cross-page processing is achieved, and editing efficiency is improved.
Patent Information
- Application Number
- CN202510440826.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-09
- Publication Date
- 2025-07-25
AI Technical Summary
Existing PDF editing tools cannot adaptively adjust the inserted content, resulting in inconsistent styles, overlapping text and inconsistent layout during text insertion, reducing the work efficiency of editors.
By performing structured analysis of PDF files, aggregate text objects into logical lines and logical blocks, obtain the overwritten content, and use collision detection algorithm to adjust the font ratio or layout rearrange it to ensure that the new text block does not intersect with the existing text block, and create a new page for spread-page processing if necessary.
It realizes adaptive insertion of content in PDF documents, improves the work efficiency of editors, avoids text overlap and typography disorders, and ensures the consistency of the inserted content with the original text and the coherence of the document reading experience.
Smart Images

Figure CN120373262A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data processing, and particularly to a method, device and related medium for adaptively inserting text content based on PDF files. Background Art
[0002] The Portable Document Format (PDF), as a layout document standard, can ensure highly consistent display effects on different platforms and devices. In the existing PDF content editing technology, since text objects exist in the form of independent units in the PDF and lack clear logical association relationships with each other, it poses great challenges for operations such as adding, deleting, modifying, and querying document content. Especially in the context where artificial intelligence technology is gradually applied to intelligent document editing, editors hope to insert AI-generated content into specific positions in the PDF document to achieve intelligent rewriting, supplementation, or replacement of content. However, since existing PDF editing tools generally rely on technologies such as coordinate clustering or optical character recognition (OCR) to perform block division on the page and lack in-depth understanding of the document structure and cannot perform adaptive adjustment, problems such as inconsistent styles, text overlap, and layout disorder are likely to occur during text insertion, which requires editors to perform secondary adjustment on the text, greatly reducing the work efficiency of editors. Summary of the Invention
[0003] Embodiments of the present invention provide a method, device and related medium for adaptively inserting text content based on PDF files, aiming to solve the problem that existing PDF files cannot perform adaptive adjustment on inserted content.
[0004] In a first aspect, embodiments of the present invention provide a method for adaptively inserting text content based on PDF files, including:
[0005] Performing structured parsing on the current page of the PDF file to obtain text objects; wherein, the PDF file includes multiple pages;
[0006] Aggregating logical lines and logical blocks based on the positional relationships of the text objects to obtain multiple text blocks;
[0007] Obtaining the current text block of the current page, extracting the character content of the current text block, and transmitting the character content to an external AI interface to obtain rewritten content;
[0008] Replacing the position of the current text block with the rewritten content to obtain a new text block;
[0009] Determine whether the new text block intersects with other text blocks on the current page based on a preset collision detection algorithm; if it intersects, adjust the font ratio of the new text block or perform a page layout rearrangement, and continue to loop to determine whether the new text block intersects with other text blocks on the current page until they do not intersect; if they do not intersect, determine whether the new text block exceeds the body text range of the current page;
[0010] If it exceeds, create a new page, insert the exceeding part of the new text block into the new page, and end the adaptive process; if it does not exceed, directly end the adaptive process.
[0011] In a second aspect, an embodiment of the present invention provides a device for adaptively inserting text content based on a PDF file, including:
[0012] A data parsing unit for performing structured parsing on the current page of the PDF file to obtain text objects; wherein, the PDF file includes multiple pages;
[0013] A data aggregation unit for aggregating logical lines and logical blocks based on the positional relationship of the text objects to obtain multiple text blocks;
[0014] A data rewriting unit for obtaining the current text block on the current page, extracting the character content of the current text block, and transmitting the character content to an external AI interface to obtain rewritten content;
[0015] A data replacement unit for replacing the position of the current text block with the rewritten content to obtain a new text block;
[0016] A data judgment unit for determining whether the new text block intersects with other text blocks on the current page based on a preset collision detection algorithm; if it intersects, adjust the font ratio of the new text block or perform a page layout rearrangement, and continue to loop to determine whether the new text block intersects with other text blocks on the current page until they do not intersect; if they do not intersect, determine whether the new text block exceeds the body text range of the current page;
[0017] A data output unit for, if it exceeds, creating a new page, inserting the exceeding part of the new text block into the new page, and ending the adaptive process; if it does not exceed, directly ending the adaptive process.
[0018] In a third aspect, an embodiment of the present invention provides a computer device, including a memory, a processor, and a computer program stored on the memory and executable on the processor, where when the processor executes the computer program, it implements the method for adaptively inserting text content based on a PDF file in the first aspect.
[0019] Fourthly, an embodiment of the present invention provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the method for adaptively inserting text content based on a PDF file according to the first aspect is implemented.
[0020] An embodiment of the present invention provides a method for adaptively inserting text content based on a PDF file, including performing a structured analysis on the current page of the PDF file to obtain text objects; aggregating logical lines and logical blocks based on the positional relationship of the text objects to obtain a plurality of text blocks; obtaining rewritten content, replacing the position of the current text block with the rewritten content to obtain a new text block; judging whether the new text block intersects with other text blocks based on a preset collision detection algorithm; if they intersect, adjusting the font ratio of the new text block or performing a page layout rearrangement, and continuing to loop to judge whether they intersect; if they do not intersect, judging whether the new text block exceeds the body range of the current page; if it exceeds, creating a new page, inserting the exceeding part of the new text block into the new page, and ending the process; if it does not exceed, directly ending the process. The present invention performs collision detection on the new text block after replacement, adjusts the intersecting text blocks, and performs a cross-page processing on the part exceeding the page. In this way, the PDF file can adaptively adjust the inserted content, greatly improving the work efficiency of editors.
[0021] An embodiment of the present invention also provides a device, a computer device, and a storage medium for adaptively inserting text content based on a PDF file, which also have the above beneficial effects. Description of the Drawings
[0022] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for the description of the embodiments will be briefly introduced below. Obviously, the drawings in the following description are some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0023] Figure 1 It is a flowchart of a method for adaptively inserting text content based on a PDF file provided by an embodiment of the present invention;
[0024] Figure 2 It is a schematic block diagram of a device for adaptively inserting text content based on a PDF file provided by an embodiment of the present invention. Detailed Embodiments
[0025] Next, in combination with the accompanying drawings in the embodiments of the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.
[0026] It should be understood that when used in this specification and the appended claims, the terms "comprising" and "comprises" indicate the presence of the described features, wholes, steps, operations, elements, and / or components, but do not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components, and / or their combinations.
[0027] It should also be understood that the terms used in this specification of the present invention are only for the purpose of describing specific embodiments and are not intended to limit the present invention. As used in this specification of the present invention and the appended claims, unless the context clearly indicates otherwise, the singular forms "a", "an", and "the" are intended to include the plural forms.
[0028] It should be further understood that the term "and / or" used in this specification of the present invention and the appended claims refers to any combination and all possible combinations of one or more of the associated listed items, and includes these combinations.
[0029] Please refer to the following Figure 1 , Figure 1 which is a schematic flowchart of a method for adaptively inserting text content based on a PDF file provided by an embodiment of the present invention, specifically including: steps S101 to S106.
[0030] S101. Structurally analyze the current page of the PDF file to obtain text objects; wherein, the PDF file includes multiple pages;
[0031] S102. Aggregate logical lines and logical blocks based on the positional relationship of the text objects to obtain multiple text blocks;
[0032] S103. Obtain the current text block of the current page, extract the character content of the current text block, and transfer the character content to an external AI interface to obtain rewritten content;
[0033] S104. Replace the position of the current text block with the rewritten content to obtain a new text block;
[0034] S105. Determine whether the new text block intersects with other text blocks on the current page based on a preset collision detection algorithm; if it intersects, adjust the font ratio of the new text block or perform a page layout rearrangement, and continue to loop to determine whether the new text block intersects with other text blocks on the current page until they do not intersect; if they do not intersect, determine whether the new text block exceeds the body text range of the current page;
[0035] S106. If it exceeds, create a new page, insert the exceeded part of the new text block into the new page, and end the adaptive process; if it does not exceed, directly end the adaptive process.
[0036] In step S101, perform a structured analysis on the current page of the PDF file to achieve the positioning of text objects on the page. There are multiple pages in the PDF file, and here one of the pages (the current page) selected by the user is analyzed.
[0037] In one embodiment, the step S101 includes:
[0038] Use a PDF parsing tool to parse the content stream of the PDF file to extract the content parameters in the current page;
[0039] Calculate the absolute coordinates and rotation angles of the content parameters respectively;
[0040] Filter out the non - text elements of the PDF file according to the content parameters to obtain the text object; wherein, the non - text elements include picture objects, path objects, and objects with watermark or header - footer features.
[0041] In this embodiment, use a PDF parsing tool (such as Apache PDFBox) to parse the content stream (Content Stream) of the PDF file, and extract the content parameters of various display objects in the current page. The content parameters include information such as the character content of the text object, font resource (FontDescriptor), text matrix (Text Matrix), glyph width (GlyphWidth), color space (ColorSpace), etc. Among them, the six parameters (a, b, c, d, e, f) in the text matrix are used to express the affine transformation information of the text object. Based on the parameters in the text matrix and the current page coordinates, calculate the absolute coordinates and rotation angles of the content parameters respectively. Specifically, map the character positions in the local coordinate system to the global coordinate system of the current page through matrix multiplication, so as to locate the two - dimensional position and its arrangement direction of each character.
[0042] After completing the preliminary object extraction and position calculation, the content parameters are further filtered to exclude non-text elements and obtain text objects. Non-text elements include: picture objects, which can be of the / XObject type and can be directly filtered by the object type identifier; path objects, including vector graphic elements, which can be excluded by identifying / Path-related operation instructions; objects with watermark or header / footer features. For such objects, auxiliary features such as their position information, font attributes, and transparency need to be combined for identification. Specifically, when the Y coordinate of a text object is in the top 5% or bottom 10% area of the current page and its font size is less than or equal to 8pt, it can be determined as header or footer content; when the fill transparency parameter of an object (such as the / CA or / ca operator) is less than 0.3, or its color mode is spot color (Separation ColorSpace), it can be determined as watermark content. After removing all non-text elements through the above rules, text objects with clear structures, complete coordinate information, and style attributes are finally retained, laying a foundation for the aggregation of subsequent logical lines and logical blocks.
[0043] In step S102, logical aggregation is performed according to the positional relationship between the extracted text objects (first performing logical line aggregation and then logical block aggregation), and finally multiple text blocks are formed.
[0044] In one embodiment, the step S102 includes:
[0045] Perform vertical alignment detection on the text objects, and merge multiple text objects that meet the determination conditions into a logical line group; where the determination conditions include that the Y coordinate error of the text objects is within a predetermined range, baseline alignment, and the character spacing is not greater than a set multiple of the average character width.
[0046] Sort the text objects in the logical line group from left to right according to the X coordinate direction to generate a line data structure.
[0047] In this embodiment, traverse all text objects on the current page. For each object, extract its calculated Y coordinate and compare it with the Y coordinates of other text objects on the current page. If the difference in the Y coordinates of two text objects is within a preset error range (such as ±2pt), it is determined that they are on the same horizontal line. At the same time, it is also necessary to determine whether the baselines of the two text objects are consistent to ensure the vertical alignment characteristics of the text visually. In addition, detect the character spacing. If the spacing between adjacent text objects is not greater than a set multiple (such as 1.5 times) of the average character width, it is further confirmed that the two have continuity and can be classified into the same logical line.
[0048] After completing the above condition determination, multiple text objects that meet the criteria are merged into a logical line group. For the text objects within each logical line group, they are sorted from left to right according to their X coordinate values to generate a stable and operable line data structure. The line data structure not only retains the style information such as the position, font, and size of each character, but also records the overall bounding box of the logical line, providing a semantically clear and typesettingally continuous basic unit for the subsequent construction of logical blocks and the insertion of AI rewritten content.
[0049] In one embodiment, the step S102 further includes:
[0050] Based on the line data structure, determine whether the vertical distance between adjacent logical lines is not greater than a set multiple of the maximum line height. If so, determine that the adjacent logical lines belong to the same paragraph; if not, determine that the adjacent logical lines do not belong to the same paragraph;
[0051] Based on the same paragraph, calculate the horizontal projection overlap rate of adjacent logical lines, and merge the logical lines corresponding to the horizontal projection overlap rate not less than a set threshold into a logical block group;
[0052] Sort the multiple logical lines within the logical block group from top to bottom according to the Y coordinate to generate a block data structure; wherein, the block data structure is the text block.
[0053] In this embodiment, based on the generated line data structure, sequentially compare the vertical distances (which can be represented as ΔY) of adjacent logical lines on the current page, that is, the difference between the top Y coordinate of the latter logical line and the bottom Y coordinate of the former logical line. A judgment threshold for the vertical distance can be set in advance, for example, set to 1.2 times the maximum line height. If ΔY is not greater than this threshold, determine that the two logical lines belong to the same paragraph; if it exceeds the threshold, consider that the two logical lines belong to different paragraphs, thereby avoiding misjudging the paragraph interval as continuous text.
[0054] Further, between the logical lines that have been determined to be in the same paragraph, further calculate their horizontal projection overlap rate. The horizontal projection overlap rate can be calculated by projecting the boundary ranges of each logical line in the X-axis direction and comparing the ratio of the intersection length to the smaller projection length. When the horizontal projection overlap rate of two adjacent logical lines is not less than a set threshold (for example, 70%), it indicates that they have obvious horizontal alignment features on the layout and belong to the same semantic block. Therefore, these logical lines are merged into the same logical block group. This processing effectively avoids the problem of mis-segmentation caused by multi-column layout or irregular indents, and improves the accuracy of text structure reconstruction.
[0055] Finally, for each logical line within each logical block group, sort them in the order from top to bottom according to their Y coordinates to generate a block data structure. The block data structure is the text block (TextBlock), which not only records the content and order of the belonging logical lines, but also saves the overall bounding box information for positioning and collision detection in subsequent insertion operations.
[0056] In step S103, obtain the target text block currently selected by the user, that is, the current text block on the current page, and extract all the character content in the current text block. The extracted text is passed as an input parameter to an external AI interface (such as an integrated natural language processing model or a third-party API), and the AI performs content rewriting on it. The rewritten content returned by the AI interface needs to be confirmed by the user, and after confirmation, it can be used to replace the original text.
[0057] In step S104, the obtained rewritten content is used to replace the original text block to generate a new text block, and the new text block can also generate a bounding box for subsequent collision detection.
[0058] In one embodiment, step S104 includes:
[0059] Receive the rewritten content returned by the AI interface, and use the rewritten content as the target replacement content after the user confirmation;
[0060] Based on the layout attributes of the current text block, insert the target replacement content into the position of the current text block to generate the new text block.
[0061] In this embodiment, after receiving the original character content in the user-selected block, the AI interface performs intelligent rewriting on it based on semantic understanding and language generation capabilities to generate rewritten content that is semantically consistent with or optimized from the original content. After the rewritten content is generated, it is presented through the user interface for the user to confirm. After the user confirms that it is correct, the rewritten content is used as the target replacement content. After obtaining the target replacement content, based on the layout attributes of the current text block, it is inserted into the position where the original text block is located to generate a new text block. The layout attributes include font name, font size, font weight (bold or italic), color, stroke information, glyph width, text matrix, and leading, etc. In the insertion process, the above layout attributes are fully inherited to ensure that the newly generated text is highly consistent with the original text in visual style.
[0062] Specifically, first perform character-level splitting on the target replacement content, generate corresponding text objects for each character, and assign the same font resources and positioning parameters as the original characters. Subsequently, based on the starting position of the current text block, the text matrix parameters, and the character width information, calculate the actual coordinates of the new text block on the page to ensure its correct layout at the original position. The newly inserted set of text objects is repackaged into a logically complete new text block.
[0063] In step S105, based on a preset collision detection algorithm, determine whether the generated new text block intersects with other text blocks on the current page. For example, it can be determined by calculating the intersection area of the bounding boxes between the new text block and other blocks (such as using the IOU metric). If there is a collision, first reduce the font size of the new text block proportionally; after reduction, recalculate the size of the new text block and perform the collision detection again. If there are still overlapping conflicts, further adopt a global rearrangement strategy, which can optimize and adjust the positions of adjacent text blocks based on the Force-Directed Algorithm, maintaining the minimum line spacing and margin constraints within the page until the new text block no longer intersects with other blocks on the page. If no collision is detected at the beginning, skip the adjustment operation and directly judge the page situation.
[0064] In one embodiment, step S105 includes:
[0065] Reduce the font size of the new text block according to a preset ratio; wherein, the lower limit of the reduction of the font size is a preset percentage of the original font size;
[0066] After reducing the font size, recalculate the font size and boundary information of the new text block, and perform the collision detection again;
[0067] If the new text block still intersects with other text blocks on the current page, traverse all text blocks on the current page and calculate the vertical blank distance between the vertically adjacent text blocks;
[0068] When the vertical blank distance meets the preset adjustable distance, move the text objects within the corresponding text block to release typesetting space until the new text block no longer intersects with other text blocks on the current page.
[0069] In this embodiment, the font size of the new text block is reduced based on a preset ratio, the original font size is adjusted proportionally, and a lower limit threshold for font reduction is set. For example, the minimum font size is limited to 60% of the original font size. This operation aims to reduce the text occupancy space by tightening the typesetting density, thereby alleviating the possible overlap with surrounding text blocks. After reducing the font size, it is necessary to recalculate the font size within the new text block and its corresponding bounding box information, and perform the collision detection again.
[0070] Collision detection can be achieved by calculating the intersection area or overlap rate between the bounding boxes of the new text block and other text blocks on the current page to determine whether there is still a spatial overlap conflict. If the new text block still intersects with other text blocks after the font size is reduced, it enters the global reflow stage. At this time, all text blocks on the current page are traversed, and the Y-axis distance between each pair of vertically adjacent text blocks is measured to calculate their vertical blank distance. If the blank area between two text blocks is greater than the preset minimum adjustable spacing threshold (such as a set multiple of the line spacing or paragraph spacing), it is considered to have adjustable space. The positions of the corresponding text blocks will be adjusted based on the Force-Directed Algorithm or the linear layout model, and the text objects within the text blocks will be preferentially moved downward or upward on the page to release the required typesetting space. This adjustment process is executed recursively and will continuously recalculate the boundaries of the new text block and perform collision detection until it is confirmed that there is no intersection between the new text block and all other text blocks on the current page, that is, the typesetting space is released and the conflict is resolved.
[0071] Through the above hierarchical adaptation strategy, while ensuring content integrity and visual consistency, the flexible insertion and highly adaptable layout of the rewritten content in complex PDF pages are achieved, effectively improving the stability of intelligent typesetting and the user experience.
[0072] In step S106, it is judged whether the boundary of the new text block exceeds the body display area of the current page. If it exceeds the visible range of the page, a new page is automatically created, and the exceeded part of the new text block is inserted into the new page to complete the page-breaking process. If it does not exceed the body area of the page, it means that the new text block has been successfully inserted and the layout adaptation is correct, and the process terminates.
[0073] In one embodiment, the step S106 includes:
[0074] Create a new page; wherein, the new page inherits the page attributes of the current page;
[0075] Remove the exceeded part of the new text block from the bottom of the current page and insert it into the specified position of the new page to complete the page-breaking display.
[0076] In this embodiment, after the adaptive layout adjustment is completed, it is determined whether the bounding box of the new text block exceeds the bottom boundary of the currently displayable area of the page, that is, the lower edge of the body text area. If it is found that part of the text content exceeds the boundary of the current page, the cross-page processing flow will be triggered. First, a new page is created, which inherits the page attributes of the current page, including page size, margin settings, background elements, watermark information, header and footer templates, etc., to maintain the consistency of the overall document style. The new text block is truncated, and the content part that exceeds the bottom of the page is removed from the current page. This truncation operation can be located based on the number of characters and line height within a logical line or a logical block to ensure the integrity of the text structure at the breakpoint and not to damage the semantic continuity. The removed text content will be encapsulated into a new set of text objects and inserted into the specified starting position on the new page, which can be the top starting coordinates of the body text area, and rendered and positioned according to the original layout attributes.
[0077] Through the above operations, the content of the new text block is orderly distributed among multiple pages, avoiding information loss or layout disorder caused by text overflow. At the same time, the content displayed across pages maintains the same layout style and logical structure as the original page, ensuring the coherence and professionalism of the document reading experience, and further enhancing the practicality and reliability of the PDF document intelligent editing system in complex text insertion scenarios.
[0078] In summary, a method for adaptively inserting text content based on PDF files provided by the present invention can insert the content rewritten by artificial intelligence in a PDF document without destroying the original layout structure. Through the structured parsing of the PDF page content stream, key information such as the coordinates, fonts, and styles of text objects is extracted, and non-text elements are accurately filtered by combining multi-dimensional rules to ensure the acquisition of pure and clearly structured text data; a line-level and block-level aggregation strategy is adopted to construct a logical line and logical block structure with semantic levels, providing a stable structural basis for subsequent content replacement. During the insertion process, the consistency between the style of the rewritten content and the original text is achieved by inheriting the layout attributes of the original text block; an adaptive layout adjustment mechanism is introduced, which first optimizes the space utilization rate through font scaling, and further automatically performs a global layout rearrangement when conflicts still exist, effectively avoiding text overlap or format disorder. If the content exceeds the current page, a new page can be automatically created, inheriting the page template and completing the cross-page display. The overall solution does not require manual adjustment by the user, significantly improving the editing efficiency, lowering the operation threshold, solving the problems of low text insertion efficiency, inconsistent layout, and layout conflicts in the prior art, and having good versatility and application value.
[0079] Combined Figure 2 as shown Figure 2A schematic block diagram of a text content adaptive insertion device based on a PDF file provided by an embodiment of the present invention. The text content adaptive insertion device 200 based on a PDF file includes:
[0080] A data parsing unit 201, configured to perform structured parsing on the current page of the PDF file to obtain text objects; wherein, the PDF file includes multiple pages;
[0081] A data aggregation unit 202, configured to aggregate logical lines and logical blocks based on the positional relationship of the text objects to obtain multiple text blocks;
[0082] A data rewriting unit 203, configured to obtain the current text block of the current page, extract the character content of the current text block, and transmit the character content to an external AI interface to obtain rewritten content;
[0083] A data replacement unit 204, configured to replace the position of the current text block with the rewritten content to obtain a new text block;
[0084] A data judgment unit 205, configured to judge whether the new text block intersects with other text blocks on the current page based on a preset collision detection algorithm; if it intersects, adjust the font ratio of the new text block or perform page layout rearrangement, and continue to loop to judge whether the new text block intersects with other text blocks on the current page until it does not intersect; if it does not intersect, judge whether the new text block exceeds the body range of the current page;
[0085] A data output unit 206, configured to, if it exceeds, create a new page, insert the exceeding part of the new text block into the new page, and end the adaptive process; if it does not exceed, directly end the adaptive process.
[0086] In this embodiment, the data parsing unit 201 performs a structured parsing on the current page of the PDF file to obtain text objects; wherein, the PDF file includes multiple pages; the data aggregation unit 202 aggregates logical lines and logical blocks based on the positional relationship of the text objects to obtain multiple text blocks; the data rewriting unit 203 obtains the current text block of the current page, extracts the character content of the current text block, and transmits the character content to an external AI interface to obtain rewritten content; the data replacement unit 204 replaces the position of the current text block with the rewritten content to obtain a new text block; the data judgment unit 205 determines whether the new text block intersects with other text blocks on the current page based on a preset collision detection algorithm; if they intersect, adjust the font ratio of the new text block or perform a page layout rearrangement, and continue to loop and determine whether the new text block intersects with other text blocks on the current page until they do not intersect; if they do not intersect, determine whether the new text block exceeds the body text range of the current page; the data output unit 206 if it exceeds, creates a new page, inserts the exceeding part of the new text block into the new page, and ends the adaptive process; if it does not exceed, directly end the adaptive process.
[0087] In one embodiment, the data parsing unit 201 includes:
[0088] A file parsing unit, configured to parse the content stream of the PDF file by using a PDF parsing tool to extract the content parameters in the current page;
[0089] A parameter calculation unit, configured to calculate the absolute coordinates and rotation angles of the content parameters respectively;
[0090] An element filtering unit, configured to filter non-text elements of the PDF file according to the content parameters to obtain the text objects; wherein, the non-text elements include picture objects, path objects, and objects with watermark or header / footer features.
[0091] In one embodiment, the data aggregation unit 202 includes:
[0092] A vertical detection unit, configured to perform vertical alignment detection on the text objects, and merge multiple text objects that meet the determination conditions into a logical line group; wherein, the determination conditions include that the Y coordinate error of the text objects is within a predetermined range, baseline alignment, and the character spacing is not greater than a set multiple of the average character width;
[0093] A horizontal sorting unit, configured to sort the text objects in the logical line group from left to right according to the X coordinate direction to generate a line data structure.
[0094] In one embodiment, the data aggregation unit 202 further includes:
[0095] A distance detection unit, configured to determine whether a vertical distance between adjacent logical lines is not greater than a maximum line height multiplied by a set multiple based on the line data structure. If so, it is determined that the adjacent logical lines belong to the same paragraph; if not, it is determined that the adjacent logical lines do not belong to the same paragraph.
[0096] A multi-line merging unit, configured to calculate a horizontal projection overlap rate of adjacent logical lines based on the same paragraph, and merge the logical lines corresponding to the horizontal projection overlap rate not less than a set threshold into a logical block group.
[0097] A vertical sorting unit, configured to sort multiple logical lines in the logical block group from top to bottom according to the Y coordinate to generate a block data structure; wherein, the block data structure is the text block.
[0098] In one embodiment, the data replacement unit 204 includes:
[0099] A content rewriting unit, configured to receive the rewritten content returned by the AI interface, and use the rewritten content as the target replacement content after the user confirms.
[0100] A content replacement unit, configured to insert the target replacement content into the position of the current text block based on the layout attributes of the current text block to generate the new text block.
[0101] In one embodiment, the data judgment unit 205 includes:
[0102] A size adjustment unit, configured to reduce the font size of the new text block according to a preset ratio; wherein, the lower limit of the reduction of the font size is a preset percentage of the original font size.
[0103] A loop judgment unit, configured to recalculate the font size and boundary information of the new text block and perform collision detection again after reducing the font size.
[0104] A distance calculation unit, configured to, if the new text block still intersects with other text blocks on the current page, traverse all text blocks on the current page and calculate the vertical blank distance between adjacent upper and lower text blocks.
[0105] A distance adjustment unit, configured to move text objects in the corresponding text block to release layout space until the new text block does not intersect with other text blocks on the current page when the vertical blank distance meets a preset adjustable distance.
[0106] In one embodiment, the data output unit 206 includes:
[0107] A page creation unit for creating a new page, wherein the new page inherits the page attributes of the current page;
[0108] A page addition unit for removing the excess part of the new text block from the bottom of the current page and inserting it into a specified position of the new page to complete the display across pages.
[0109] Since the embodiments in the apparatus part correspond to the embodiments in the method part, for the descriptions of the embodiments in the apparatus part, please refer to the descriptions of the embodiments in the method part, which will not be elaborated here.
[0110] Embodiments of the present invention also provide a computer-readable storage medium, on which a computer program is stored. When the computer program is executed, the steps provided in the above embodiments can be implemented. The storage medium may include: various media such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disc that can store program codes.
[0111] Embodiments of the present invention also provide a computer device, which may include a memory and a processor. When the processor calls the computer program stored in the memory, the steps provided in the above embodiments can be implemented. Of course, the computer device may also include various network interfaces, power supplies, and other components.
[0112] The various embodiments in the specification are described in a progressive manner. Each embodiment focuses on the differences from other embodiments. The same or similar parts among the various embodiments can be referred to each other. For the system disclosed in the embodiments, since it corresponds to the method disclosed in the embodiments, the description is relatively simple. For the relevant parts, please refer to the description in the method part. It should be noted that for those of ordinary skill in the art in the technical field of the present application, without departing from the principle of the present application, several improvements and modifications can be made to the present application, and these improvements and modifications also fall within the protection scope of the claims of the present application.
[0113] It should also be noted that in this specification, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the terms "comprise", "comprising" or any other variant thereof are intended to cover non-exclusive inclusion, such that a process, method, article or device comprising a series of elements not only includes those elements but also includes other elements not expressly listed, or elements inherent to such process, method, article or device. Without further limitation, an element defined by the statement "comprising an..." does not exclude the presence of additional identical elements in the process, method, article or device comprising said element.
Claims
1. A method for adaptively inserting text content based on PDF files, characterized in that, Including: Performing structured parsing on the current page of a PDF file to obtain text objects; wherein, the PDF file includes multiple pages; Aggregating logical lines and logical blocks based on the positional relationship of the text objects to obtain multiple text blocks; Obtaining the current text block of the current page, extracting the character content of the current text block, and passing the character content to an external AI interface to obtain rewritten content; Replacing the position of the current text block with the rewritten content to obtain a new text block; Judging whether the new text block intersects with other text blocks on the current page based on a preset collision detection algorithm; if it intersects, adjusting the font ratio of the new text block or performing page layout rearrangement, and continuously looping to judge whether the new text block intersects with other text blocks on the current page until it does not intersect; if it does not intersect, judging whether the new text block exceeds the body text range of the current page; If it exceeds, creating a new page, inserting the exceeding part of the new text block into the new page, and ending the adaptive process; if it does not exceed, directly ending the adaptive process.
2. The text content adaptive insertion method based on PDF files according to claim 1, characterized in that, The performing structured parsing on the current page of a PDF file to obtain text objects includes: Using a PDF parsing tool to parse the content stream of the PDF file to extract the content parameters in the current page; Calculating the absolute coordinates and rotation angles of the content parameters respectively; Filtering non-text elements of the PDF file according to the content parameters to obtain the text objects; wherein, the non-text elements include picture objects, path objects, and objects with watermark or header / footer features.
3. The method for adaptively inserting text content based on a PDF file according to claim 1, wherein The aggregating logical lines and logical blocks based on the positional relationship of the text objects to obtain multiple text blocks includes: Performing vertical alignment detection on the text objects, and merging multiple text objects that meet the determination conditions into a logical line group; wherein, the determination conditions include that the Y coordinate error of the text objects is within a predetermined range, baseline alignment, and the character spacing is not greater than a set multiple of the average character width; Sorting the text objects in the logical line group from left to right according to the X coordinate direction to generate a line data structure.
4. The method for adaptively inserting text content based on a PDF file according to claim 3, wherein The aggregating logical lines and logical blocks based on the positional relationship of the text objects to obtain multiple text blocks further includes: Judging whether the vertical distance between adjacent logical lines is not greater than a set multiple of the maximum line height based on the line data structure; if so, determining that the adjacent logical lines belong to the same paragraph; if not, determining that the adjacent logical lines do not belong to the same paragraph; Calculating the horizontal projection overlap rate of adjacent logical lines based on the same paragraph, and merging the logical lines corresponding to the horizontal projection overlap rate not less than a set threshold into a logical block group; Sorting multiple logical lines in the logical block group from top to bottom according to the Y coordinate to generate a block data structure; wherein, the block data structure is the text block.
5. The method for adaptively inserting text content based on a PDF file according to claim 1, wherein The replacing the position of the current text block with the rewritten content to obtain a new text block includes: Receive the rewritten content returned by the AI interface, and use the rewritten content as the target replacement content after the user confirms it; Based on the layout attributes of the current text block, insert the target replacement content into the position of the current text block to generate the new text block.
6. The method for adaptively inserting text content based on a PDF file according to claim 1, wherein The adjusting the font ratio of the new text block or performing a page layout rearrangement includes: Reduce the font size of the new text block according to a preset ratio; wherein, the lower limit of the reduction of the font size is a preset percentage of the original font size; After reducing the font size, recalculate the font size and boundary information of the new text block, and perform collision detection again; If the new text block still intersects with other text blocks on the current page, traverse all the text blocks on the current page and calculate the vertical blank distance between the vertically adjacent text blocks; When the vertical blank distance meets the preset adjustable distance, move the text object within the corresponding text block to release the layout space until the new text block does not intersect with other text blocks on the current page.
7. The method for adaptively inserting text content based on a PDF file according to claim 1, characterized in that The creating a new page and inserting the overflow part of the new text block into the new page includes: Create a new page; wherein, the new page inherits the page attributes of the current page; Remove the overflow part of the new text block from the bottom of the current page and insert it into a specified position of the new page to complete the cross-page display.
8. An adaptive text content insertion device based on a PDF file, characterized in that, Includes: A data parsing unit for performing structured parsing on the current page of the PDF file to obtain text objects; wherein, the PDF file includes multiple pages; A data aggregation unit for aggregating logical lines and logical blocks based on the positional relationship of the text objects to obtain multiple text blocks; A data rewriting unit for obtaining the current text block of the current page, extracting the character content of the current text block, and passing the character content to an external AI interface to obtain the rewritten content; A data replacement unit for replacing the position of the current text block with the rewritten content to obtain a new text block; A data judgment unit for judging whether the new text block intersects with other text blocks on the current page based on a preset collision detection algorithm; if it intersects, adjust the font ratio of the new text block or perform a page layout rearrangement, and continue to loop to judge whether the new text block intersects with other text blocks on the current page until it does not intersect; if it does not intersect, judge whether the new text block exceeds the body range of the current page; A data output unit for, if it exceeds, create a new page, insert the overflow part of the new text block into the new page, and end the adaptive process; if it does not exceed, directly end the adaptive process.
9. A computer device, characterized in that, Includes a memory, a processor, and a computer program stored on the memory and executable on the processor, and when the processor executes the computer program, it implements the method for adaptively inserting text content based on a PDF file according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, A computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, it implements the method for adaptively inserting text content based on a PDF file as described in any one of claims 1 to 7.