How to Use aspose. Words for. Net to dynamically generate images or watermarks in Word documents under Asp.net

Source: Internet
Author: User

1. Overview

It is common to generate WORD Documents in projects. Generally, Word documents are generated in plain text or a large number of lists, which is easy for customers to print, some customers also need to generate images into Word documents, such as product images. This article describes how to use aspose. Words for. Net to dynamically generate images or watermarks in word. Aspose. Words for. net. I will not introduce it much. If you are not clear about it, please refer to the previous article.Article. Demand always changes fast, and another such requirement is added in the recent project: After the quotation sheet is generated in the system, there must be a signature, which is an image generated by different users. Now, I will introduce how to implement this function. If you are interested, you may wish to discuss it together.

2. Text

In aspose. Words, image operations mainly rely on this class: aspose. Words. Drawing. shape. First, let's take a look at how to use this class to load images:

CopyCode The Code is as follows: Using aspose. words;
Using aspose. Words. tables;
Using aspose. Words. Rendering;
Using aspose. Words. drawing;
Document Doc = new document (tmppath );
Documentbuilder builder = new documentbuilder (DOC );
Shape shape = new shape (Doc, shapetype. Image );
Shape. imagedata. setimage (server. mappath ("~ /Template/default.jpg "));
Shape. width = 70;
Shape. Height = 70;
Shape. horizontalalignment = horizontalalignment. Right; // align to the right

Shape. imagedata. setimage (...) The image loading method has four reloads: Image path, byte, stream, and image object. Here I mainly use the image path method, because the project needs to generate different signatures based on different users. After an image is loaded to an object, all we need to do is put it in the document. Next, we need to locate where the image is to be placed in the document.
The positioning of aspose. Words in the document is almost all based on node nodes. Our images are not as fixed as a cell like a table. Therefore, the idea here is to find a node that can be adjusted as a "wind vane" to locate the image position. We still need to use bookmark (the domain can also be used ). We first define a bookmark value as "myname" in the word template ". ReturnProgramThe following code uses bookmarks to locate the image:

Copy code The Code is as follows: builder. movetobookmark ("myname ");
Builder. insertnode (SHAPE );
Bookmark myname = Doc. range. bookmarks ["myname"];
Myname. Text = "";
Doc. range. bookmarks ["myname"]. Remove ();
Doc. Save ("baojiadan.doc", saveformat. Doc, savetype. openinword, response );

In fact, our Dynamic Graph Generation is complete.
Let's take a look at the generated results: Figure 1
So how can we generate an image as a watermark? Don't worry. Please follow me. To achieve the watermark effect, we mainly set the attributes of the object aspose. Words. Drawing. shape. The Code is as follows:

Copy code The Code is as follows: shape. wraptype = wraptype. None;
Shape. behindtext = true;

after setting, we need to set the position to be placed next. Here we do not need boomark to locate it. Place it directly in the center of the Word template, as shown below: copy Code the code is as follows: shape. relativehorizontalposition = relativehorizontalposition. page;
shape. horizontalalignment = horizontalalignment. center;
shape. relativeverticalposition = relativeverticalposition. page;
shape. verticalalignment = verticalignment. center;

At the end of the document, we add something similar to the footer. The Code is as follows.Copy codeThe Code is as follows: builder. movetoheaderfooter (headerfootertype. footerprimary );
Border topborder = builder. cellformat. Borders [bordertype. Top];
Topborder. linestyle = linestyle. Single;
Topborder. linewidth = 1;
Builder. insertcell ();
Builder. cellformat. width = 3*72;
Builder. Write (string. Format ("Print time: {0}", datetime. Today. tolongdatestring ()));
Builder. insertcell ();
Builder. paragraphformat. Alignment = paragraphalignment. Right;
Builder. Write ("Version 1.00 ");
Builder. endrow ();
Builder. endtable ();
Doc. Save ("baojiadan.doc", saveformat. Doc, savetype. openinword, response );

Let's take a look at the generated results: Figure 2

At this point, it is all over. Thank you for taking the time to read and discuss it with your friends.
Aspose. Words for net 6.5 cracked version

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.