ava fills data into word template (summary)

Anyone who has used PageOffice to dynamically generate word documents knows that PageOffice can fill in the specified position of the word document. The specified position we are talking about here has two concepts in the professional terminology of PageOffice, one is called the data region (DataRegion), the other One is called DataTag.

  1. Concept

  Data area: It is the document area located by the bookmark named with the "PO_" prefix in the Word document. Simply put, the data area is a special Word bookmark object that facilitates locating the content in the Word document.

  Data tag: It is a special style string composed of any valid characters (such as "[time]", "{name}", "${name}", etc.) as a mark, theoretically such a string (for example: "Gender", "Address", "aa", "bbcc") can also be used as data tags, but it is not recommended, because such tags cannot visually tell which data tags are contained in the word document.

  2. Use The content that can be filled in the

  data area: text, pictures, Word files, Excel files.

Copy code
    WordDocument doc = new WordDocument();
    //fill text
    DataRegion dataRegion1 = doc.openDataRegion("PO_userName");
    dataRegion1.setValue("Zhang San");
    //You can also set the font, color, thickness of the filled text Body and other styles
    dataRegion1.getFont().setColor(Color.blue);
    dataRegion1.getFont().setSize(24);
    dataRegion1.getFont().setName("clerical script");
    dataRegion1.getFont().setBold(true);
   
    //fill the picture
    doc.openDataRegion("PO_deptName").setValue( "[image]img/123.jpg[/image]");
    //fill word file
    doc.openDataRegion("PO_deptName").setValue("[word]doc/aabb.doc[/word]");
    // Fill in the excel file
    doc.openDataRegion("PO_deptName").setValue("[excel]doc/bbcc.xls[/excel]");
Copy code Content that can be filled in the
  data label: text.

 

Copy code
    WordDocument doc = new WordDocument();
    //Fill the text, you can also set the font, color and other styles
    DataTag deptTag = doc.openDataTag("{department name}");
    deptTag.setValue("Marketing Department");

    deptTag.getFont().setName("宋体");         deptTag.getFont
    ().setSize(28);   copy
code The bookmark at the beginning of ” defines the data area, and then obtains the DataRegion object through the OpenDataRegion(String DataRegionName) method of the WordDocument class object in the program; you can also directly create the data area through the CreateDataRegion(String NewDataRegionName, DataRegionInsertType insertType, String RelativeDataRegionName) method of the WordDocument class object. To obtain the DataRegion class object. The DataTag class is used in conjunction with user-inserted custom feature text in a Word document. In the program, the DataTag class object can only be obtained through the OpenDataTag(String DataTagName) method.          In addition, the name of the data area (that is, the bookmark name) must start with "PO_", and there must be at least one valid character between two bookmarks, which cannot be repeated. The name of the data area and the position in the Word document are in one-to-one correspondence. The name of the data tag is a word string text with certain characteristics, which is convenient for locating the content in the Word document and can be repeated. The name of the data tag and the position in the Word document have a one-to-many relationship. That is to say, the DataRegion of the same Name can only represent only one position in the Word document, and the same characteristic string can appear in multiple places in the Word document.








         Therefore, the advantages of DataRegion are: DataRegion can not only fill document content, but also create document content, submit the content entered by the user in the DataRegion, and obtain the content in the DataRegion submitted by the user, while DataTag can only be used to fill the document content. The disadvantage of DataRegion is: DataRegion cannot be simply repeated, and different bookmarks must be set, while DataTag can generate repeated content in multiple places in the document.

         And should use DataTag or DataRegion, it should be determined according to the specific needs. When the user wants to submit a certain part of the data in Word or wants to obtain a certain part of the data submitted in Word, he must use DataRegion; when the user does not need to submit data or obtain data, and the filled data has no duplicates, then both Yes; when the user does not need to submit data or obtain data, and the filled data has multiple duplicates, although both are possible, it is recommended to use DataTag for the convenience of programming and code efficiency.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325969197&siteId=291194637