Position Aspose. words bookmarks and aspose. words bookmarks

Source: Internet
Author: User

Position Aspose. words bookmarks and aspose. words bookmarks

1.Introduction

Aspose. words can generate, modify, convert, and print documents without using Microsoft. Word. Independent of office components, this provides us with great convenience and can be easily introduced.DLL(Dynamic Link Library, Dynamic Link Library file), you can operate the word document. However, there is also a small problem that requires a license Aspose. Words. lic.

 

2. wordDocument Structure

To operate a word document, you must first understand what type of object word is for aspose. Word is a DOM (Document Object Model, Document Object Model) in aspose ). Let's take a look at the model diagram of the Word document in aspose.

 

The specific content of the above node can be ignored for the moment. In specific use cases, if you want to modify a header, you can check which node the header belongs to and what attributes it has. First, we should establish a rough understanding of DOM: the root node is Document, and the bottom node is a tree of Run.

 

3.View DOM

Aspose provides the DOM checking tool DocumentExplorer, which can be downloaded from github. Address: https://github.com/aspose-words/Aspose.Words-for-.NET. On the left is the tree corresponding to the document structure, and on the right is some encoding content in the node. The purpose of viewing this tree is to have an intuitive understanding of DOM.

 

 

4. In order to have a deeper understanding, we have implemented a small case

Assume that there is a Word document with a BookMark name. How can I know the position of this BookMark in the document. First, we can locate the page number, that is, the name of the BookMark object known as Document. We need to solve the PageIndex (page number) of the BookMark in the Document ).

 

The way bookmarks look in word.

 

 

This interface tells us that the Bookmarks have two variables: name and position. Allows you to add, delete, locate, and hide data.

 

Next, let's take a look at the implementation of bookmarks in aspose.

 

In Aspose, the Name of the bookmarks corresponds to the Name. values can be assigned. Text is the value of the bookmarks. This is not directly input when we open the word bookmarks interface, you can assign values in other ways. The place where the BookmarkStart bookmarks start and where the BookmarkEnd bookmarks end. There is a starting position and a ending position, which indicates that the bookmarks are a piece of content. (More accurately, the word document Protocol first defines the Start mark and end mark for bookmarks. aspose has BookmarkStart and BookmarkEnd under this Protocol ), we can set a whole page as a bookmarks or an image as a bookmarks. If the bookmarks are a cursor, the start and end tags are the cursor positions. We have an object Aspose. words. Document for the entire Document, a bookobject Aspose. words. BookMark, A bookstart object Aspose. words. BookmarkStart, and a bookend object Aspose. words. BookmarkEnd.

A document contains many bookmarks and how to find one of them. First, find all the Bookmark

Aspose. Words. BookmarkCollection, and then find a bookmarkName

 

 

To implement our Case (remember to locate the number of pages based on the bookmarks), should there be an API similar to int I = GetPageIndex (BookMark bookmark. It seems that there is an API that implements similar functions. Aspose. Words. Layout. LayoutCollector (do not ask me how to know this API ). LayoutCollector literal meaning: Layout collector. It can be understood as a set of information about all node locations in the document.

 

 

However, there is another problem: the parameter is Node. What kind of existence is this? An abstract class.

 

 

What is the relationship between Bookmark and Node.

To answer this question, we need to look at the BookmarkStart object, which is both a Node and the starting position of Bookmark.

 

 

At this time, I felt that there were several objects, which were a bit messy and vaguely related. Try to establish a connection between them.

In this way, we can choose Document> Bookmark> BookmarkStart> Node> PageIndex)

Document doc = new Document(string fileName);Bookmark bookmark = doc.Range.Bookmarks["bookmarkName"];int pageIndex = new LayoutCollector(doc).GetStartPageIndex(bookmark.bookmarkStart);

 

In this place, we locate the number of pages at the beginning of a bookmark. With the number of pages, you can use the print function in aspose to directly jump to this page.

At this time, I found that the question I just raised seems a bit incorrect.

Strictly speaking, it should be: the number of initial pages and the number of ending pages of a bookmarks in the document. Of course, most of the cases are on the same page. In our real life, we are exposed to bookmarks between two pages. The electronic bookmarks in the word document have been extended in this positioning function. It can locate a word, a paragraph, an image, and many other unexpected functions.

 

 

5.Search for key APIs

There is a subtle point in the process, that is, how to find the key API public int GetStartPageIndex (Node node). If your time is too short, use VS in the official Demo, search for the keywords bookmark, Page, and Index. You may find many related keywords by searching each word and one word separately. This API is included in each of them, sometimes it is difficult to find out. This search method requires the Demo author to name the method in a standardized way. The name must be meaningful and correspond to what is done. When we name a method in the development process, if we feel that the name of a method is very tangled, we cannot use one or two words to express what we do, or write comments in any way, if you are not clear about it, it is very likely that this method itself is problematic. It does too many things and requires splitting. If you have enough time, run all the official Demo examples. The API you are looking for may be found during your debugging of an example, of course, there may be no ready-made methods to meet your needs, so you can use other methods to continue the query, as well as StackOverflow. If the problem persists after a variety of attempts, you can go back to the source and find out if this requirement is inherently problematic. Can this problem be implemented in another way. "Negative problem is the best way to solve the problem ".

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.