asp.net under the aspose.words for. NET dynamically generate pictures or watermarks in Word documents-practical tips

Source: Internet
Author: User

1. Overview

Generating Word documents in your project is a common feature that is typically generated in plain text or more lists, makes it easier for customers to print, and needs to be built into Word documents, such as product pictures. This time we are introducing how to use aspose.words for. NET generates a picture or watermark dynamically in Word. Aspose.words for. NET, this I will not introduce more, not clear friends can look at the previous article. Demand always changes quickly, and there's one more thing in the recent project: When a quote is generated in a system, a signature is made, based on a picture generated by a different user. Well, let me tell you how to achieve this function, interested friends may wish to discuss together.

2, the text

In aspose.words, picture operations are mainly dependent on this class: Aspose.Words.Drawing.Shape. First let's look at how to use this class to load a picture:

Copy Code code 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 Right

Shape. Imagedata.setimage (...) There are four overloads for loading a picture: Picture path, Byte, stream, Image object, respectively. My main use here is the way the image path, because the project to be based on different users to generate different signatures of the relationship. After the picture is loaded into the object, all we have to do is put it in the document. Next, all we have to do is locate where the picture will be placed in the document.
Aspose.words's location in the document is almost always in node nodes, and our picture is unlikely to be as fixed as a cell in the table. So my idea here is to find a random adjustment of the node as "vane" to locate the picture position. We still have to use the bookmark (the domain is also possible). Let's first define the bookmark value as "myname" in the Word template. Back in the program, the following code is where the picture is positioned by bookmarking:

Copy Code code 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 generation diagram has been completed here.
Let's take a look at the effect of the build: Figure 1
So how do you make a watermark of a picture? No hurry, please look down with me. To achieve the effect of the watermark, in fact, the main object is Aspose.Words.Drawing.Shape property settings. The code is as follows:

Copy Code code as follows:

Shape. Wraptype = Wraptype.none;
Shape. Behindtext = true;


After setting up, we will then set the location of the picture to be placed. Here we can not need boomark to locate. Place directly in the center of the Word template, as follows:
Copy Code code as follows:

Shape. RelativeHorizontalPosition = Relativehorizontalposition.page;
Shape. HorizontalAlignment = HorizontalAlignment.Center;
Shape. RelativeVerticalPosition = Relativeverticalposition.page;
Shape. VerticalAlignment = Verticalalignment.center;

At the end of the document we add a footer-like thing, which is the code below.
Copy Code code 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);

Okay, let's see the effect of the build: Figure 2

By this, it's all over. Thank you for taking the time to read and have a discussion with interested friends.
Aspose.words for NET 6.5 cracked version download address

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.