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:
Copy codeThe 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 ". Return to the program. The following code uses bookmarks to locate the image:
Copy codeThe 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 codeThe Code is as follows:
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:
Copy codeThe 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