Aspose.words Bookmark Locator

Source: Internet
Author: User

1. Introduction

Aspose.words can generate, modify, convert, and print documents without the use of Microsoft.word. Without relying on Office components, this provides us with a great deal of convenience, simple introduction of DLLs (dynamic link library, dllfile), you can manipulate Word documents. But a little bit of trouble is the need for a license Aspose.Words.lic.

2. Word Document structure

Now that you're working on a Word document, it's important to get a quick look at what word is like for aspose. Word in Aspose is a DOM (Document Object model, the documents objects models). Let's take a look at the model diagram of the Word document in Aspose.

The specific contents of the above node can be ignored, to the specific use of the scene, if you want to modify a header, you can check the header is which node, what properties. Let's start with a general understanding of the DOM: the root node is Document, and the bottom node is a tree of Run.

3. View DOM

Aspose provides a tool for viewing the DOM 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 encoded content in the node. Look at this tree to have an intuitive understanding of the DOM.

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

Suppose there is a Word document, given a bookmark name, how to know the location of the bookmark in the document. We first simply navigate to the number of pages, that is to say, the object of the document, the name of the bookmark, and ask for the PageIndex (number of pages) of the bookmark in the document.

The way the bookmark looks in Word.

This interface tells us that the bookmark has a name and a location of two variables. You can perform add, delete, locate, hide operations.

Next look at the implementation of bookmarks in Aspose.

The name of the bookmark in the Aspose, can be assigned value, Text is the value of the bookmark, this when we open the Word bookmark interface when there is no direct input, you may be able to assign values in other ways. Bookmarkstart where bookmarks start, where bookmarkend bookmarks end. There is an end position for the starting position, which means that the bookmark is a piece of content. (more precisely, Word's document protocol defines a bookmark with a start tag and an end tag, aspose according to this protocol, Bookmarkstart and BookmarkEnd), we can bookmark a whole page, or we can set a picture as a bookmark. If the bookmark is a cursor, both the start and end tags are where the cursor is located. We already have an entire document object Aspose.words.Document, Bookmark object Aspose.words.BookMark, bookmark start object Aspose.words.BookmarkStart, Bookmark End Object Aspose.words.BookmarkEnd.

There are many bookmarks in a document and how to find one of them. First Find all the Bookmark

Aspose.Words.BookmarkCollection, and then find a bookmark based on BookmarkName

To implement our case (remember, depending on how bookmarks are anchored to the number of pages), should there be an API similar to int i = Getpageindex (bookmark bookmark). There seems to be a real API that implements similar functionality. Aspose.Words.Layout.LayoutCollector (Don't ask me how to know this API). Layoutcollector literal meaning: layout collector. Can be understood as a collection of location information for all nodes in a document.

But there is one more problem: The parameter is Node, which is another kind of existence, an abstract class.

So what is the relationship between the Bookmark and Node.

To answer this question is to see the Bookmarkstart object, which is both Node and the location of the beginning of the Bookmark.

At this time there are several objects, a little messy, vaguely related. Try to establish a connection between them.

So we can get from document (DOC)--Bookmark (bookmark)--Bookmarkstart (bookmark start node)--node (nodes)--PageIndex (pages)

New Document (string= doc. range.bookmarks["bookmarkname"]; int New Layoutcollector (DOC). Getstartpageindex (Bookmark.bookmarkstart);

This place we have the number of pages where a bookmark starts. With the number of pages, you can use the Aspose in the print function to jump directly to this page display.

At this point, I found that the problem just mentioned, seems a little less right.

Strictly speaking, it should be: how many pages a bookmark starts in the document and the number of pages that end. Of course, most of the cases are on the same page. What we actually touch in life is a bookmark that is sandwiched between two pages. The electronic bookmarks in the Word document are expanded on this positioning feature. You can position a word, a paragraph, a picture, and many other unintended effects.

5. Find the key API

There's a tricky place in the process of finding the key API for public int Getstartpageindex (node node), and if you're in a hurry, use vs on the official demo to retrieve the bookmark, Page, Index These keywords, a word a word separately, you may find a lot of related, each quick glance, this API is in it, sometimes more difficult to find. This search method requires the demo author to name A comparison of the methods, the name to be meaningful, and the things that correspond. When we name in the development process, if you feel a method named very tangled, can not use one or two words to express the things done, or anyway write comments, all feel not clear, it is possible that the method itself is problematic, it does too much, it needs to split. If you have enough time, then run the official demo example, the API you are looking for, in the process of debugging an example you may find, of course, there may not be a ready-made method to achieve your needs, then use other methods to continue to check, StackOverflow can also. After a variety of attempts not to, can go back to the source, think about this demand is not itself there is a problem, is it possible to change the way to achieve. "Denying the problem is the best way to solve the problem."

Aspose.words Bookmark Locator

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.