Aspose. Words for. NET Dynamic generation of images or watermarks in Word documents

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, you can refer to the previous 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:
[Csharp]
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 ". Return to the program. The following code uses bookmarks to locate the image:
[Csharp]
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:

[Csharp]
Shape. WrapType = WrapType. None;
Shape. BehindText = true;

After setting, we need to set the location to be placed. Here we do not need boomark to locate it. Directly placed in the center of the word template, as follows:
[Csharp]
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.
[Csharp]
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.

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.