Method and device for obtaining position coordinates in PDF file, storage medium and processor
By generating movable word blocks on the PDF file preview page and automatically compute coordinates, the problems of cumbersome and limited accuracy of obtaining position coordinates in PDF files in the prior art are solved, and a more convenient and accurate coordinate acquisition process is achieved.
Patent Information
- Application Number
- CN202510109047.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-23
- Publication Date
- 2025-05-16
AI Technical Summary
Existing methods for obtaining coordinates of specific locations in PDF files are cumbersome or rely on the consistency of the document structure, resulting in the coordinate accuracy that may be affected when facing documents with complex or variable formats.
By receiving the PDF file imported by the user, determine the file size, and draw the PDF file preview page with Canvas. The user generates movable characters on the preview page and moves them to the target position, and uses the browser's canvas technology to automatically calculate the horizontal and vertical coordinates of the target position.
While ensuring the accuracy of PDF coordinate position, the process of obtaining the position coordinates in the PDF file is simplified, the user experience is improved, and the operation is more convenient and fast.
Smart Images

Figure CN120012709A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a method, device, storage medium and processor for obtaining position coordinates in a PDF file. Background Art
[0002] In some application scenarios, it is essential to obtain the accurate horizontal and vertical coordinates of a specific location in a PDF file. For example, when generating an electronic contract, accurate coordinate information must be provided to ensure that custom text and images can be correctly synthesized into the PDF document.
[0003] There are two existing methods for obtaining the coordinates of specific positions in PDF files. The first is for users to install a third-party reader and measure the required PDF position coordinates after a series of complex configurations. The second is to use programming methods to locate the text position using keywords, and then calculate the required PDF position coordinates based on logic. However, both methods have certain defects: the first method is more cumbersome to use, and the second method relies on the consistency of the document structure and the accuracy of the keywords. When facing documents with complex or changeable formats, the accuracy of the results may be affected.
[0004] How to make the process of obtaining the location coordinates in a PDF file more convenient and improve the user experience while ensuring the accuracy of the PDF location coordinates is a technical problem that needs to be solved urgently. Summary of the invention
[0005] Based on the above problems, the present application provides a method, device, storage medium and processor for obtaining position coordinates in a PDF file, with the aim of making the process of obtaining position coordinates in a PDF file more convenient while ensuring the position accuracy of the PDF coordinates, thereby improving the user experience.
[0006] The embodiments of the present application disclose the following technical solutions:
[0007] In a first aspect, the present application provides a method for obtaining position coordinates in a PDF file, the method comprising:
[0008] Receive a PDF file imported by a user, and determine the size of the PDF file;
[0009] Based on the size of the PDF file, using Canvas to draw a preview page of the PDF file according to a preset drawing size;
[0010] Receive a field input by a user, and generate a movable word block corresponding to the field on the PDF file preview page;
[0011] The movable character block is moved to a target position, and the coordinates of the target position in the PDF file are obtained.
[0012] Optionally, moving the movable character block to a target position and obtaining the coordinates of the target position includes:
[0013] Based on the positioning auxiliary line, the movable character block is moved to a target position, and the coordinates of the target position in the PDF file are obtained.
[0014] Optionally, moving the movable character block to a target position and obtaining the coordinates of the target position in the PDF file includes:
[0015] Move the movable character block to a target position, and obtain a first coordinate; the first coordinate is a coordinate of the target position on the preview page of the PDF file;
[0016] Based on the PDF file size, the preset drawing size and the first coordinate, a second coordinate is calculated; the second coordinate is the coordinate of the target position in the PDF file.
[0017] Based on the PDF file, using Canvas to draw a PDF file preview page according to a preset drawing size, further comprising:
[0018] Determine the drawing size of the Canvas based on the browser window size or based on the screen size.
[0019] Optionally, determining the drawing size of the Canvas according to the browser window size includes:
[0020] Listen for browser window size change events;
[0021] When the browser window size changes, the drawing size of the preview page corresponding to the PDF file is obtained based on the ratio of the canvas size to the PDF file page size.
[0022] Optionally, determining the drawing size of the Canvas according to the screen size includes:
[0023] If the screen width is greater than or equal to the first pixel value, the drawing size of the PDF file preview page is 1.5 times the size of the PDF file;
[0024] If the screen width is greater than or equal to the second pixel value and less than the first pixel value, the drawing size of the PDF file preview page is 1.2 times the size of the PDF file; the second pixel value is less than the first pixel value;
[0025] If the screen width is smaller than the second pixel value, the drawing size of the PDF file preview page is 1.0 times the size of the PDF file.
[0026] A second aspect of the present application provides a device for obtaining position coordinates in a PDF file, the device comprising:
[0027] A third aspect of the present application provides a computer-readable storage medium, in which a computer program is stored. When the program is executed by a processor, the method for obtaining position coordinates in a PDF file as provided in any implementation of the first aspect is implemented.
[0028] A fourth aspect of the present application provides a processor, which is used to run a computer program. When the program is run, the method for obtaining position coordinates in a PDF file provided in any implementation of the first aspect is executed.
[0029] Compared with the prior art, this application has the following beneficial effects:
[0030] The method for obtaining position coordinates in a PDF file provided by the present application receives a PDF file imported by a user and determines the size of the PDF file; based on the size of the PDF file, draws a PDF file preview page using Canvas according to a preset drawing size; receives a field input by a user, and generates a movable character block corresponding to the field on the PDF file preview page; moves the movable character block to a target position, and obtains the coordinates of the target position in the PDF file.
[0031] This application is implemented based on the browser's canvas technology. By visually dragging the positioning block, the user drags the movable word block to the target position, and the horizontal and vertical coordinates of the target position in the PDF file can be automatically calculated. In this way, the process of obtaining the position coordinates in the PDF file can be made more convenient and quick while ensuring the accuracy of the PDF coordinate position, greatly improving the user experience. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0033] Figure 1 A flowchart of a method for obtaining position coordinates in a PDF file provided in an embodiment of the present application;
[0034] Figure 2 A flowchart of another method for obtaining position coordinates in a PDF file provided in an embodiment of the present application;
[0035] Figure 3A schematic diagram of the effect of obtaining position coordinates in a PDF file provided by an embodiment of the present application;
[0036] Figure 4 A schematic diagram of the structure of a device for obtaining position coordinates in a PDF file provided in an embodiment of the present application. DETAILED DESCRIPTION
[0037] As described above, there are currently two methods for obtaining the coordinates of a specific location in a PDF file. The first is for the user to install a third-party reader and measure the required PDF location coordinates after a series of complex configurations; the second is to use programming methods to locate the text position using keywords, and then calculate the required PDF location coordinates based on logic. However, both methods have certain defects: the first method is more cumbersome to use, and the second method relies on the consistency of the document structure and the accuracy of the keywords. When facing documents with complex or changeable formats, the accuracy of the results may be affected.
[0038] In view of the above problems, the inventors have proposed, after research, a method, device, storage medium and processor for obtaining position coordinates in a PDF file, receiving a PDF file imported by a user and determining the size of the PDF file; based on the size of the PDF file, using Canvas to draw a PDF file preview page according to a preset drawing size; receiving a field input by a user, and generating a movable word block corresponding to the field on the PDF file preview page; moving the movable word block to a target position, and obtaining the coordinates of the target position in the PDF file.
[0039] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0040] See also Figure 1 , which is a flow chart of a method for obtaining position coordinates in a PDF file provided by an embodiment of the present application. Figure 1 As shown, the method comprises the following steps:
[0041] S101: Receive a PDF file imported by a user, and determine the size of the PDF file.
[0042] PDF (Portable Document Format) is a file format introduced by Adobe Systems in 1993. It is used to represent documents, including all original typesetting details such as text formatting, fonts, colors and images, without relying on applications, hardware or operating systems. The size of a PDF file is represented by width and height, and these values are in CSS pixels.
[0043] By receiving the PDF file imported by the user, the size information of each page can be obtained.
[0044] S102: Based on the size of the PDF file, use Canvas to draw a PDF file preview page according to a preset drawing size.
[0045] Canvas is an HTML element that can be used to draw graphics using scripts (usually JavaScript). For example, it can be used to draw charts, make picture compositions, or make simple animations. For example, first create a Canvas element and create a <canvas>Element is used to display the preview of PDF. Then, set the preset drawing size. You can adjust the display size of the PDF file preview page on Canvas by scaling. Finally, load and draw the preview page of the PDF file. Use JavaScript libraries such as pdf.js to load PDF files from the server or locally. Use the API provided by pdf.js to read the PDF page and render it on Canvas.
[0046] Based on the PDF file, using Canvas to draw a PDF file preview page according to a preset drawing size, further comprising:
[0047] Determine the drawing size of the Canvas based on the browser window size or based on the screen size.
[0048] Determining the drawing size of the Canvas according to the browser window size includes:
[0049] Listen for browser window size change events;
[0050] When the browser window size changes, the drawing size of the preview page corresponding to the PDF file is obtained based on the ratio of the canvas size to the PDF file page size.
[0051] Determining the drawing size of the Canvas according to the screen size includes:
[0052] If the screen width is greater than or equal to the first pixel value, the drawing size of the PDF file preview page is 1.5 times the size of the PDF file;
[0053] If the screen width is greater than or equal to the second pixel value and less than the first pixel value, the drawing size of the PDF file preview page is 1.2 times the size of the PDF file; the second pixel value is less than the first pixel value;
[0054] If the screen width is smaller than the second pixel value, the drawing size of the PDF file preview page is 1.0 times the size of the PDF file.
[0055] Draw the preview page of the PDF file on the Canvas, which usually requires adjusting the size of the Canvas to match the size of the PDF file page, or scaling it according to the preset drawing size.
[0056] S103: receiving a field input by a user, and generating a movable character block corresponding to the field on the PDF file preview page.
[0057] HTML5 Canvas can be used in combination with JavaScript to create an interactive interface. Some input controls, such as text boxes, are added to HTML so that users can enter fields. After entering the fields, movable text blocks corresponding to the fields are generated on the PDF file preview page.
[0058] S104: Move the movable character block to a target position, and obtain the coordinates of the target position in the PDF file.
[0059] Based on the positioning auxiliary line, the movable character block is moved to a target position, and the coordinates of the target position in the PDF file are obtained.
[0060] Exemplarily, the input field is realname, and a movable character block corresponding to the field is generated on the preview page of the PDF file; the user can drag the movable character block to the target position, such as the position in the PDF file where the name of Party B needs to be filled in; after determining that the movable character block can be dragged to the ideal target position, the actual coordinates of the target position are obtained, that is, the coordinates in the PDF file, and the coordinates include a horizontal coordinate and a vertical coordinate.
[0061] Moving the movable character block to a target position, and obtaining the coordinates of the target position in the PDF file, comprises:
[0062] Move the movable character block to a target position, and obtain a first coordinate; the first coordinate is a coordinate of the target position on the preview page of the PDF file;
[0063] Based on the PDF file size, the preset drawing size and the first coordinate, a second coordinate is calculated; the second coordinate is the coordinate of the target position in the PDF file.
[0064] The present embodiment provides a method for obtaining the position coordinates in a PDF file, which is implemented based on the browser canvas technology. By visually dragging the positioning block, the user drags the movable character block to the target position, and the horizontal and vertical coordinates of the target position in the PDF file can be automatically calculated. In this way, the process of obtaining the position coordinates in the PDF file can be made more convenient and quick while ensuring the accuracy of the PDF coordinate position, which greatly improves the user experience.
[0065] See also Figure 2 , which is a flow chart of another method for obtaining position coordinates in a PDF file provided by an embodiment of the present application. Based on the above embodiment, positioning auxiliary lines are used for movement.
[0066] like Figure 2 As shown, the method comprises the following steps:
[0067] S201: Receive a PDF file imported by a user, and determine the size of the PDF file.
[0068] After the user imports a PDF file, obtain the size information of the PDF page so as to draw the PDF file preview page on the Canvas.
[0069] S202: Based on the size of the PDF file, use Canvas to draw a PDF file preview page according to a preset drawing size.
[0070] Based on the PDF file, using Canvas to draw a PDF file preview page according to a preset drawing size, further comprising:
[0071] Determine the drawing size of the Canvas based on the browser window size or based on the screen size.
[0072] Determining the drawing size of the Canvas according to the browser window size includes:
[0073] Listen for browser window size change events;
[0074] When the browser window size changes, the drawing size of the preview page corresponding to the PDF file is obtained based on the ratio of the canvas size to the PDF file page size.
[0075] Determining the drawing size of the Canvas according to the screen size includes:
[0076] If the screen width is greater than or equal to the first pixel value, the drawing size of the PDF file preview page is 1.5 times the size of the PDF file;
[0077] If the screen width is greater than or equal to the second pixel value and less than the first pixel value, the drawing size of the PDF file preview page is 1.2 times the size of the PDF file; the second pixel value is less than the first pixel value;
[0078] If the screen width is smaller than the second pixel value, the drawing size of the PDF file preview page is 1.0 times the size of the PDF file.
[0079] Draw the preview page of the PDF file on the Canvas, which usually requires adjusting the size of the Canvas to match the size of the PDF file page, or scaling it according to the preset drawing size.
[0080] S203: receiving a field input by a user, and generating a movable character block corresponding to the field on the PDF file preview page.
[0081] HTML5 Canvas can be used in combination with JavaScript to create an interactive interface. Some input controls, such as text boxes, are added to HTML so that users can enter fields. After entering the fields, movable text blocks corresponding to the fields are generated on the PDF file preview page.
[0082] The method for obtaining the position coordinates in a PDF file provided in this embodiment generates corresponding movable blocks according to the input fields, and the field names (for example, name\phone\address, etc.) can be customized, so that the coordinates finally generated are easy to manage and maintain.
[0083] S204: Based on the positioning auxiliary line, move the movable character block to a target position, and obtain the coordinates of the target position in the PDF file.
[0084] For example, Figure 3 As shown in the figure, the PDF file imported by the user is a custody contract, and the cnum, address, goodsName and goodsType fields are entered respectively to obtain the corresponding movable sub-blocks. Among them, the movable sub-blocks cnum, address and goodsName have been moved to the target position, and the coordinates in the PDF file are obtained respectively, such as Figure 3 As shown in the table on the right. Move the movable block corresponding to the goodsType field to the target position, that is, the property in the PDF file. If adjustment is required, you can repeatedly drag it to the ideal position, and then click "Get Position Information" to get the coordinates of the target position.
[0085] The method of moving the movable character block to a target position and obtaining the coordinates of the target position in the PDF file includes:
[0086] Move the movable character block to a target position, and obtain a first coordinate; the first coordinate is a coordinate of the target position on the preview page of the PDF file;
[0087] Based on the PDF file size, the preset drawing size and the first coordinate, a second coordinate is calculated; the second coordinate is the coordinate of the target position in the PDF file.
[0088] For example, if the drawing size of Canvas is x1*y1, the coordinates of the target position in the preview page of the PDF file, that is, the first coordinates are (x2, y2), and the size of the PDF file is x3*y3, the coordinates of the target position in the PDF file, that is, the second coordinates (x4, y4), are calculated as follows:
[0089] From x2 / x1=x4 / x3, y2 / y1=y4 / y3, we can get:
[0090] x4=x2 / x1*x3,y4=y2 / y1*y3
[0091] Another method for obtaining position coordinates in a PDF file provided by this embodiment is implemented based on the canvas technology of the browser. Based on the positioning reference line, the positioning block is visually dragged. When the user drags the movable block to the target position, the horizontal and vertical coordinates of the target position in the PDF file can be automatically calculated. Moreover, the movable block generated by the field name is easy to manage and maintain. In this way, the process of obtaining the position coordinates in the PDF file can be made more convenient and quick while ensuring the accuracy of the PDF coordinate position, which greatly improves the user experience.
[0092] Based on the method for obtaining position coordinates in a PDF file introduced in the previous embodiment, the present application also provides a device for obtaining position coordinates in a PDF file. Figure 4 Figure 1 is a schematic diagram of the structure of the device. Figure 4 As shown, the device comprises:
[0093] The file receiving module 401 is used to receive a PDF file imported by a user and determine the size of the PDF file;
[0094] A drawing module 402 is used to draw a PDF file preview page using Canvas according to a preset drawing size based on the PDF file size;
[0095] The movable character block generating module 403 is used to receive a field input by a user and generate a movable character block corresponding to the field on the PDF file preview page;
[0096] The coordinate acquisition module 404 is used to move the movable character block to a target position and acquire the coordinates of the target position in the PDF file.
[0097] Optionally, the coordinate acquisition module is specifically used for:
[0098] Based on the positioning auxiliary line, the movable character block is moved to a target position, and the coordinates of the target position in the PDF file are obtained.
[0099] In addition, an embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored. When the program is executed by a processor, the method for obtaining position coordinates in a PDF file as described in any of the method embodiments is implemented.
[0100] In addition, an embodiment of the present application further provides a processor, which is used to run a computer program. When the program is run, the method for obtaining position coordinates in a PDF file as described in any implementation manner of the aforementioned method embodiment is executed.
[0101] It should be noted that each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment. The device embodiment described above is merely schematic, in which the unit described as a separate component may or may not be physically separated, and the component prompted as a unit may or may not be a physical unit, that is, it may be located in one place, or it may be distributed on multiple network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative work.
[0102] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed in the present application should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.< / canvas>
Claims
1. A method for obtaining position coordinates in a PDF file, characterized in that: include: Receive a PDF file imported by a user, and determine the size of the PDF file; Based on the size of the PDF file, using Canvas to draw a preview page of the PDF file according to a preset drawing size; Receive a field input by a user, and generate a movable word block corresponding to the field on the PDF file preview page; The movable character block is moved to a target position, and the coordinates of the target position in the PDF file are obtained.
2. The method according to claim 1, characterized in that: Moving the movable character block to a target position and obtaining the coordinates of the target position includes: Based on the positioning auxiliary line, the movable character block is moved to a target position, and the coordinates of the target position in the PDF file are obtained.
3. The method according to claim 1, characterized in that Moving the movable character block to a target position, and obtaining the coordinates of the target position in the PDF file, comprises: Move the movable character block to a target position, and obtain a first coordinate; the first coordinate is a coordinate of the target position on the preview page of the PDF file; Based on the PDF file size, the preset drawing size and the first coordinate, a second coordinate is calculated; the second coordinate is the coordinate of the target position in the PDF file.
4. The method according to claim 1, characterized in that: Based on the PDF file, using Canvas to draw a PDF file preview page according to a preset drawing size, further comprising: Determine the drawing size of the Canvas based on the browser window size or based on the screen size.
5. The method according to claim 4, characterized in that Determining the drawing size of the Canvas according to the browser window size includes: Listen for browser window size change events; When the browser window size changes, the drawing size of the preview page corresponding to the PDF file is obtained based on the ratio of the canvas size to the PDF file page size.
6. The method according to claim 4, characterized in that Determining the drawing size of the Canvas according to the screen size includes: If the screen width is greater than or equal to the first pixel value, the drawing size of the PDF file preview page is 1.5 times the size of the PDF file; If the screen width is greater than or equal to the second pixel value and less than the first pixel value, the drawing size of the PDF file preview page is 1.2 times the size of the PDF file; the second pixel value is less than the first pixel value; If the screen width is smaller than the second pixel value, the drawing size of the PDF file preview page is 1.0 times the size of the PDF file.
7. A device for obtaining position coordinates in a PDF file, characterized in that: include: A file receiving module, used for receiving a PDF file imported by a user and determining the size of the PDF file; A drawing module, used for drawing a PDF file preview page using Canvas according to a preset drawing size based on the size of the PDF file; A movable character block generating module, used for receiving a field input by a user, and generating a movable character block corresponding to the field on the PDF file preview page; The coordinate acquisition module is used to move the movable character block to a target position and acquire the coordinates of the target position in the PDF file.
8. The device according to claim 7, characterized in that The coordinate acquisition module is specifically used for: Based on the positioning auxiliary line, the movable character block is moved to a target position, and the coordinates of the target position in the PDF file are obtained.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the program is executed by a processor, the method for obtaining position coordinates in a PDF file according to any one of claims 1 to 6 is implemented.
10. A processor, characterized in that: Used to run a computer program, which, when running, executes the method for obtaining position coordinates in a PDF file as described in any one of claims 1 to 6.