Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PageOffice to implement user-defined Word template

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to use PageOffice to implement user-defined Word templates, for this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

In the actual development process, according to the requirements of exporting and generating word documents, the program is used to fill the data into the word template. A template is a Word file that marks the location of the data and the font paragraph style. Generally speaking, the data in a template can be divided into two types: one is one-to-one, and one data corresponds to a data location in the template. For example, a template only needs to use five data: "department, name, reason, days, and date". Each data only needs to be used once in the file, as shown in the following figure template:

Users can use all the data when defining the template, or any four, three, or even one of the five data, but the number of data labels in the template is always a subset of the collection.

The other is one-to-many, and one data can be used once or multiple times in the same template. Each piece of data needs to appear twice in the file.

If all the templates in a project system are fixed, then the developer and the user only need to make the template well during the system development process, and then the developer writes the program according to the business logic. it can be achieved by filling the data location in the template, but in practical applications, this is often not the case, and the end user still wants to be able to create and modify the template at any time. To meet changing business needs, if each template change needs to be done with the developer, the project will never be completed.

In order to meet the needs of users, it is necessary to provide a template making and management module in the project, and in order to enable the program to control and identify the location of the data in the user-defined template, it is necessary for the developer to make a contract. Let the end user create the template in accordance with the agreement when creating or editing the template. So how should we make an agreement? The solution provided by PageOffice is to use bookmarks and specially formatted text to mark where data needs to be inserted in the document.

The first method uses bookmarks to mark the location of the data. Word document to insert bookmarks method: locate the cursor to the need to mark the location of the data, point Word menu "insert"-"Bookmark", it will pop up a pop-up title "Bookmark" dialog box, enter the name of the new bookmark, book signature can contain numbers but no spaces in the middle, using PageOffice development when not recommended to use Chinese named book signature. Note: if the new insertion location or new object uses an existing book signature, the original bookmark will be automatically canceled, so using a bookmark to mark the data location must be an one-to-one pattern in which the data corresponds to a location in the template.

In order to avoid conflicts with user-defined bookmarks during PageOffice development, the inserted bookmark name must start with "PO_". Note that it is the letter o, not the number 0. The book signature is case-insensitive and can also be written as "po_". The data area mentioned in the concept of PageOffice is essentially a bookmark, but only bookmarks that begin with "po_" are called data regions (DataRegion). Please note this.

The second method is to use specially formatted text to mark the location of the data, such as [contract date], [# contract date #], [contract date]. Wait. There is an obvious disadvantage of using bookmarks to mark data locations. The same bookmark name can only appear once in an word document, that is, a data region must be unique in the document, but in many cases, the same data variables need to be used in multiple locations in the template. For example, the "contract date" in the above example may need to appear in multiple locations in a contract. The use of data regions is certainly not sufficient, but there is no such limitation in using specially formatted text to mark up.

In the concept of PageOffice, this [contract date], [# contract date # #], [contract date]. Special format text can be considered as a data label (DataTag), the same data tag can appear in multiple locations in a file, dynamically populating the data label to generate a file, the same data label will be replaced by the same data.

Note: [contract date] and [# contract date #] are two different data labels, and the same data label must be in exactly the same text format. For example: [# contract date # #] and [# contract date # #] although the font color and size are different, but the text content is exactly the same, it is considered to be the same data label Therefore, it is very simple to define data tags when making templates. You only need to insert text in the same format.

Although the above two agreements have been set, it is still tedious and difficult for users to use office's own functions to make and edit templates when editing templates, and the template is also prone to some problems. for this reason, PageOffice provides an interface to define templates, and developers use the program to pre-define data areas and data tags that users can use, when users edit templates. A selection window for data areas and data tags pops up for the user, and the user only needs to choose to insert these data areas and data tags into the word template to set the paragraph format, font, style, color, and so on. In this way, it not only deals with the problem of the agreement, but also makes the operation of the user-defined template easier and faster.

First, use the editing template function provided by PageOffice:

PageOffice provides developers with interfaces DefineDataRegion and DefineDataTag methods for defining templates

PageOffice.WordWriter.WordDocument.Template.DefineDataRegion

PageOffice.WordWriter.WordDocument.Template.DefineDataTag

Use DefineDataRegion to define data regions, for example, the Java code is as follows:

WordDocument doc = new WordDocument (); doc.getTemplate (). DefineDataRegion ("Name", "[name]"); doc.getTemplate (). DefineDataRegion ("Address", "[address]"); doc.getTemplate (). DefineDataRegion ("Tel", "[phone]"); doc.getTemplate () .defineDataRegion ("Phone", "[phone]") Doc.getTemplate (). DefineDataRegion ("Sex", "[gender]"); doc.getTemplate (). DefineDataRegion ("Age", "[age]"); doc.getTemplate (). DefineDataRegion ("Email", "[mailbox]"); doc.getTemplate () .defineDataRegion ("QQNo", "[QQ number]") Doc.getTemplate () .defineDataRegion ("MSNNo", "[MSN number]")

Or if it is developed in ASP.NET, the code is as follows:

PageOffice.WordWriter.WordDocument doc = new PageOffice.WordWriter.WordDocument (); doc.Template.DefineDataRegion ("Name", "[name]"); doc.Template.DefineDataRegion ("Address", "[address]"); doc.Template.DefineDataRegion ("Tel", "[phone]"); doc.Template.DefineDataRegion ("Phone", "[Mobile phone]") Doc.Template.DefineDataRegion ("Sex", "[gender]"); doc.Template.DefineDataRegion ("Age", "[age]"); doc.Template.DefineDataRegion ("Email", "[mailbox]"); doc.Template.DefineDataRegion ("QQNo", "[QQ number]"); doc.Template.DefineDataRegion ("MSNNo", "[MSN number]")

Developers use server-side programs to define data areas that users can use, and users can use these data areas when editing templates on the client side. Note that there is no need to start with PO_ in the code here. PageOffice will automatically add this prefix. The data area management window that the user sees when editing the template, as shown in the following figure:

The data region to be added list on the left is the data region currently available to the user, and the added data region list on the right is the data region that has been added in the document. The implementation code of this window has been encapsulated by PageOffice, there is no need for developers to write complex js+html code to achieve, and the style of the contents of this window can be modified, if you are not satisfied with those styles, you only need to make simple adjustments to the style of the html code of this window. The effect when the user edits the data region

Use DefineDataTag to define data labels. For example, the code for Java is as follows:

WordDocument doc = new WordDocument (); doc.getTemplate (). DefineDataTag ("{Party A}"); doc.getTemplate (). DefineDataTag ("{Party B}"); doc.getTemplate (). DefineDataTag ("{guarantor}"); doc.getTemplate (). DefineDataTag ("[contract date]") Doc.getTemplate () .defineDataTag ("[contract number]")

Or the code developed in ASP.NET is as follows:

PageOffice.WordWriter.WordDocument doc = new PageOffice.WordWriter.WordDocument (); doc.Template.DefineDataTag ("{Party A}"); doc.Template.DefineDataTag ("{Party B}"); doc.Template.DefineDataTag ("{guarantor}"); doc.Template.DefineDataTag ("[contract date]"); doc.Template.DefineDataTag ("[contract number]")

Developers use server-side programs to define data tags that users can use, and users can use these data tags when editing templates on the client side. The data label management window that the user sees when editing the template, as shown in the following figure:

Like the data area, the implementation code of this window has been encapsulated by PageOffice, so there is no need for developers to write complex js+html code to achieve, and the style of the content in this window can be modified. If you are not satisfied with those styles, you only need to simply adjust the style in the html code of this window. The developer just writes the program to assign the corresponding data to the data label, the user defines the style of the template, the data location and the amount of data, the user can use all the data areas, or only part of the data, PageOffice automatically ignores the unused data tags when generating the file, and does not need the developer to make any code adjustments.

Unlike the management of the data area, because the data tags are not unique in the file, no matter how many data tags are added to the file, the data tags in the data label management window will not be reduced.

two。 Populate the data into the Word template with objects in the PageOffice.WordWriter namespace to dynamically generate documents:

PageOffice.WordWriter.WordDocument.OpenDataRegion method

PageOffice.WordWriter.WordDocument.OpenDataTag method

Developers only need to use the above two methods to write programs for word templates and assign corresponding data to all data regions and data tags, regardless of how many data regions and data tags are used in the template. PageOffice will automatically ignore those data regions and data tags that have never been used in the Word template, without the need for developers to make any code changes. Thus the requirement of user-defined template can be realized perfectly.

The assignment code developed by Java is as follows:

WordDocument doc = new WordDocument (); doc.openDataRegion ("Name"). SetValue ("Zhang San"); doc.openDataRegion ("Age"). SetValue ("21");. Doc.openDataTag ("{name of Party A"). SetValue ("Microsoft China headquarters"); doc.openDataTag ("{name of Party B"). SetValue ("Beijing Fantasy Technology Co., Ltd.");. PoCtrl.setWriter (doc)

The assignment code developed by ASP.NET is as follows

PageOffice.WordWriter.WordDocument doc = new PageOffice.WordWriter.WordDocument (); doc.OpenDataRegion ("Name"). Value = "Zhang San"; doc.OpenDataRegion ("Age"). Value = "21";. Doc.OpenDataTag ("{Party A company name}"). Value = "Microsoft China headquarters"; doc.OpenDataTag ("{Party B company name}"). Value = "Beijing Fantasy Technology Co., Ltd." PageOfficeCtrl1.SetWriter (doc); this is the answer to the question on how to use PageOffice to implement a user-defined Word template. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report