Method for filling pdf by pdf toolkit and SPEL expression
Technical Field
The invention relates to the technical field of computers, in particular to a method for filling a pdf by using a pdf toolkit and a SPEL expression.
Background
The pdf is a main file format of official documents, and is widely applied in the fields of contracts, agreements, specifications and the like, and there are many scenes in these fields, and the pdf is dynamically filled according to the system information, and at present, the main scheme is to use a pdf operation library (iText) to operate the form of the pdf for filling, but in the face of the customized filling format and the variety of filling contents, the pdf template and the program are often required to be modified for achieving the purpose, which makes the use of users very inconvenient.
Disclosure of Invention
The purpose of the invention is as follows: the technical problem to be solved by the invention is that in the field of pdf filling, the content and format of filling are changed frequently, and the traditional scheme needs frequent program modification, so that the efficiency is low and the response is slow.
In order to solve the technical problems, the invention discloses a method for filling a pdf by combining a pdf toolkit (iText) with expression languages (SPEL, EL, OGNL and the like), which can achieve the effects of high practicability, good flexibility and strong universality.
The invention comprises the following steps:
step 1, selecting a pdf toolkit;
step 2, inquiring all data which can be filled, and putting the data into the Context of the expression;
step 3, registering the self-defined method in the Context of the expression; the self-defining method is that when the function in the expression can not meet the requirement, the code is required to be written and registered by the user, and the registration is the function provided in the expression context (for example, the expression can not express the function of formatting the Date type data into YYYYY-MM-DD, and the user-defining method is written).
Step 4, reading the pdf template to be filled by using a pdf toolkit, and traversing all form names to be filled;
step 5, taking the read form name as an expression, and analyzing the expression by reading the Context to obtain a corresponding result value; the result is the function of the expression language through Context analysis;
and 6, writing the result value back to the pdf template by using the pdf toolkit to generate a new file stream.
In step 2, the selected pdf toolkit is IText.
The step 2 comprises the following steps: the expression context is a part of an expression system, in SPEL, Strandard EvaluationContext is a context class, the context is constructed in a mode of StandardEvaluationContext ═ new StandardEvaluationContext (), and then possible filling data is put in through a calling method context. key is used to fetch data from the context and value represents data. context represents the context in which all data with padding is placed in key-to-value form, and the method is customized.
The step 3 comprises the following steps: in SPEL, a custom method is registered by calling a method context, which is a name for calling the custom method when an expression is used, and a method is a custom method.
The step 5 comprises the following steps: the ExpressionParser in SPEL is an expression parser, constructed by a method of ExpressionParser ═ new SpelExpressionParser (), and then obtained by a method of parser.
The invention also comprises a step 7: and converting the file stream into a result required by the user according to the service form. (e.g., result file download link, to printer, etc.).
Has the advantages that:
in the staged business, a large number of contract businesses exist, and due to various factors, contracts and filling contents can be changed frequently. The method dynamically fills the pdf by utilizing the pdf operation library and combining the expression characteristics of the expression language, and greatly improves the development cost and the maintenance cost of the system. When the filling requirement is changed, the program is not required to be modified again, and only the expression language in the form of the template is required to be modified.
Drawings
FIG. 1 is a flow chart of an implementation of the present invention.
Fig. 2 is a screenshot of a pdf to be filled according to the present invention.
Fig. 3 is a detailed view of the present invention.
Detailed Description
As shown in fig. 1, the present invention discloses a method for populating a pdf with a pdf toolkit and SPEL expressions, comprising the steps of:
step 1, selecting a pdf toolkit;
step 2, inquiring all data which can be filled, and putting the data into the Context of the expression;
step 3, registering the self-defined method in the Context of the expression; the self-defining method is that when the function in the expression can not meet the requirement, the code is required to be written and registered by the user, and the registration is the function provided in the expression context (for example, the expression can not express the function of formatting the Date type data into YYYYY-MM-DD, and the user-defining method is written).
Step 4, reading the pdf template to be filled by using a pdf toolkit, and traversing all form names to be filled;
step 5, taking the read form name as an expression, and analyzing the expression by reading the Context to obtain a corresponding result value; the result is the function of the expression language through Context analysis;
and 6, writing the result value back to the pdf template by using the pdf toolkit to generate a new file stream.
In step 2, the selected pdf toolkit is IText.
The step 2 comprises the following steps: an expression context is part of an expression hierarchy, constructed in the SPEL in a StandardEvaluationContext () manner, and put in potentially padded data through context.
The step 3 comprises the following steps: in SPEL, the method is registered by context.
The step 5 comprises the following steps: an expression parser is constructed in SPEL by ExpressionParser ═ new SpelExpressionParser (), and then a result value is obtained by parser.
The invention also comprises a step 7: and converting the file stream into a result required by the user according to the service form. (e.g., result file download link, to printer, etc.).
Examples
As shown in fig. 2, is a pdf screenshot of an order to fill. Fig. 3 is a detailed process for processing the data of fig. 2 using the method of the present invention.
The program in fig. 3 queries all service data (user information, order information) and puts them into the context of the expression (the expression context is a part of the expression system, the expression part in fig. 3 is SPEL as an example, and constructs the context by means of standardeventualcontextcontext ═ new standardeventualcontext (), and puts them into the context by means of calling method context.
Since the service requirement, the pdf of result has the requirement of displaying the capital amount number and the format of the order time year and month, the custom method of converting the capital amount into the capital amount (upperNum) and the time formatting (datetorsting) are registered in the context of the expression (by means of the context register function);
the pdf to be filled uses the pdf editor to create a form at the location to be filled, and uses the expression language to define the name of the form to be filled (in fig. 3, # user. user name can take the data from the context as the value of "three")
The IText reads the pdf to be filled, traverses all the form names to be filled, the expression language parses the result value by reading the defined context, and fig. 3 identifies a result corresponding to the expression parsing (the result value is obtained by constructing an expression parser and a parser.
Finally IText writes the result back to the pdf template to form a new file stream
While the present invention provides a method for populating a pdf with a pdf toolkit and SPEL expressions, and many ways to implement the same, the above description is only a preferred embodiment of the present invention, and it should be noted that those skilled in the art can make various improvements and modifications without departing from the principle of the present invention, and such improvements and modifications should be considered as the protection scope of the present invention. All the components not specified in the present embodiment can be realized by the prior art.