| |
| ASP.NET |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<%@ Page Language="C#" MasterPageFile="~/tpl/Demo.Master" AutoEventWireup="true" CodeBehind="EmployeesRep.aspx.cs" Inherits="Aspose.Words.Demos.WebForms.EmployeesRep"
Title="Employees Report - Aspose.Words Demos" %>
<%@ Register Src="~/SelectFormat.ascx" TagName="SelectFormat" TagPrefix="sf" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="90%">
<tr>
<td valign="top" width="19">
<img alt="" height="41" src="/Common/images/heading_lft.jpg" width="19" />
</td>
<td class="demos-heading-bg" width="100%">
<h2 class="demos-heading-bg">
<font face="Arial" size="4">Employees Report - Aspose.Words for .NET</font></h2>
</td>
<td valign="top" width="19">
<img alt="" height="41" src="/Common/images/heading_rt.jpg" width="19" />
</td>
</tr>
</table>
<p class="componentDescriptionTxt" style="text-align: left">
Populates a table in a document with information from a database, also inserts
images from a BLOB database field into the document.</p>
<sf:SelectFormat ID="sfSelect" runat="server" />
<p class="i1"><asp:button id="SubmitBtn" runat="server" Text="Submit"></asp:button></p>
</asp:Content>
|
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
//////////////////////////////////////////////////////////////////////////
// Copyright 2001-2011 Aspose Pty Ltd. All Rights Reserved.
//
// This file is part of Aspose.Words. The source code in this file
// is only intended as a supplement to the documentation, and is provided
// "as is", without warranty of any kind, either expressed or implied.
//////////////////////////////////////////////////////////////////////////
using System.Web.UI.WebControls;
namespace Aspose.Words.Demos.WebForms
{
/// <summary>
/// The page for the Employees Report demo.
/// </summary>
public partial class EmployeesRep : DemoBasePage
{
private readonly Demo mDemo = new EmployeesReportDemo();
internal override Demo Demo
{
get { return mDemo; }
}
protected override SelectFormat Format
{
get { return sfSelect; }
}
protected override Button Submit
{
get { return SubmitBtn; }
}
}
}
|
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
//////////////////////////////////////////////////////////////////////////
// Copyright 2001-2011 Aspose Pty Ltd. All Rights Reserved.
//
// This file is part of Aspose.Words. The source code in this file
// is only intended as a supplement to the documentation, and is provided
// "as is", without warranty of any kind, either expressed or implied.
//////////////////////////////////////////////////////////////////////////
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aspose.Words.Saving;
namespace Aspose.Words.Demos.WebForms
{
/// <summary>
/// This is a base class for all pages that run individual demos in this demo project.
/// </summary>
public abstract class DemoBasePage : Page
{
/// <summary>
/// Derived demo pages need to return an object that runs the demo.
/// </summary>
internal abstract Demo Demo
{
get;
}
/// <summary>
/// Derived demo pages need to return the select output format control.
/// </summary>
protected abstract SelectFormat Format
{
get;
}
/// <summary>
/// Derived demo pages need to return the submit button.
/// </summary>
protected abstract Button Submit
{
get;
}
override protected void OnInit(EventArgs e)
{
this.Submit.Click += new System.EventHandler(this.SubmitBtn_Click);
this.Load += new System.EventHandler(this.DemoBase_Load);
this.PreRender += new System.EventHandler(this.HandlePreRender);
base.OnInit(e);
}
/// <summary>
/// Called when the user clicks the Submit button.
/// Does some plumbing at first and then executes the demo.
/// </summary>
private void SubmitBtn_Click(object sender, EventArgs e)
{
// If date/time stamps stored in the view state and in the session objects are not equal,
// then the user has refreshed the page instead of submitting.
if (null == ViewState["Update"])
{
SetSessionDateTimeStamp();
return;
}
if (null == Session["Update"])
{
SetSessionDateTimeStamp();
return;
}
if (!ViewState["Update"].ToString().Equals(Session["Update"].ToString()))
return;
ExecuteDemo();
// Set the session stamp right after submitting.
SetSessionDateTimeStamp();
}
private void ExecuteDemo()
{
// This is needed so the demo knows the path to the database and to the template documents.
Demo.Init(MapPath("bin"));
// Execute the demo and get the generated document as an object.
Document doc = Demo.Execute();
// Once we have a document, we can save it to a file, stream or send to the client browser.
// We just send the document to the browser here in the format selected by the user.
SendToBrowser(doc);
Response.End();
}
/// <summary>
/// Sends the document to the client browser in the specified format.
/// </summary>
private void SendToBrowser(Document doc)
{
switch (Format.Format)
{
case "DOC":
doc.Save(Response, "Aspose.Words.Demos.doc", ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Doc));
break;
case "DOCX":
doc.Save(Response, "Aspose.Words.Demos.docx", ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Docx));
break;
case "PDF":
doc.Save(Response, "Aspose.Words.Demos.pdf", ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Pdf));
break;
case "XPS":
doc.Save(Response, "Aspose.Words.Demos.xps", ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Xps));
break;
case "Render":
SendToBrowserAsBitmap(doc);
break;
case "ODT":
doc.Save(Response, "Aspose.Words.Demos.odt", ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Odt));
break;
case "MHTML":
{
HtmlSaveOptions htmlOptions = new HtmlSaveOptions(SaveFormat.Mhtml);
htmlOptions.ExportHeadersFootersMode = ExportHeadersFootersMode.None;
doc.Save(Response, "Aspose.Words.Demos.mht", ContentDisposition.Attachment, htmlOptions);
break;
}
case "HTML":
SendToBrowserAsHtml(doc);
break;
case "RTF":
doc.Save(Response, "Aspose.Words.Demos.rtf", ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Rtf));
break;
case "WML":
doc.Save(Response, "Aspose.Words.Demos.xml", ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.WordML));
break;
case "FOPC":
doc.Save(Response, "Aspose.Words.Demos.xml", ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.FlatOpc));
break;
case "TXT":
doc.Save(Response, "Aspose.Words.Demos.txt", ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Text));
break;
case "EPUB":
{
HtmlSaveOptions epubOptions = new HtmlSaveOptions(SaveFormat.Epub);
epubOptions.ExportHeadersFootersMode = ExportHeadersFootersMode.None;
// This option improves how tables are displayed in the reader.
epubOptions.TableWidthOutputMode = HtmlElementSizeOutputMode.None;
doc.Save(Response, "Aspose.Words.Demos.epub", ContentDisposition.Attachment, epubOptions);
}
break;
case "SWF":
ViewSWFInDocViewer(doc);
break;
default:
throw new Exception("Unknown output format.");
}
}
/// <summary>
/// Redirect the client to the SWF document veiwer.
/// </summary>
private void ViewSWFInDocViewer(Document doc)
{
const string path = @"Temp\Aspose.Words.Demos.swf";
// Populate the navigation outline pane in the SWF viewer using headings in the document.
// This will include headings styled with Heading 1 - Heading 3 in the pane and the first two heading styles
// will be expanded by default.
SwfSaveOptions saveOptions = new SwfSaveOptions();
saveOptions.HeadingsOutlineLevels = 3;
saveOptions.ExpandedOutlineLevels = 2;
doc.Save(MapPath(path), saveOptions);
Response.Redirect("FlashDocViewer.aspx");
}
/// <summary>
/// Stream the document to the client browser in the HTML format.
/// </summary>
private void SendToBrowserAsHtml(Document doc)
{
// Output the XHTML header.
HtmlSaveOptions htmlOptions = new HtmlSaveOptions();
htmlOptions.ExportXhtmlTransitional = true;
// Headers and footers are not needed in HTML format.
htmlOptions.ExportHeadersFootersMode = ExportHeadersFootersMode.None;
// It is better to save the document into a file as it saves all images into
// the same folder and embeds only image file names into HTML so the images
// will be downloaded into the browser okay.
const string htmlUrl = @"Temp\Aspose.Words.Demos.html";
doc.Save(MapPath(htmlUrl), htmlOptions);
// Redirecting to the HTML document rather than saving it into the response stream
// makes sure the browser can find the images in the same folder as the document.
#if SITE_BUILD
string strDemoUrl = "/demos/" + Response.ApplyAppPathModifier("~/" + htmlUrl);
Response.Redirect(strDemoUrl);
#else
Response.Redirect(htmlUrl);
#endif
}
/// <summary>
/// Redirects the document to the rendering form.
/// </summary>
private void SendToBrowserAsBitmap(Document doc)
{
Session["Document"] = doc;
Response.Redirect("viewdocument.aspx");
}
/// <summary>
/// Some plumbing.
/// </summary>
private void SetSessionDateTimeStamp()
{
Session["Update"] = Session.SessionID + DateTime.Now.ToString();
}
/// <summary>
/// Some plumbing.
/// </summary>
protected override void OnLoad(EventArgs e)
{
if (!String.IsNullOrEmpty(Request.QueryString["action"]))
{
if (Request.QueryString["action"].ToLower() == "show")
ExecuteDemo();
}
base.OnLoad(e);
}
/// <summary>
/// Some plumbing.
/// </summary>
protected void DemoBase_Load(object sender, EventArgs e)
{
if (!IsPostBack)
SetSessionDateTimeStamp();
}
/// <summary>
/// Some plumbing.
/// </summary>
private void HandlePreRender(object sender, EventArgs e)
{
ViewState["Update"] = Session["Update"];
}
}
}
|
| C# |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
//////////////////////////////////////////////////////////////////////////
// Copyright 2006-2011 Aspose Pty Ltd. All Rights Reserved.
//
// This file is part of Aspose.Words. The source code in this file
// is only intended as a supplement to the documentation, and is provided
// "as is", without warranty of any kind, either expressed or implied.
//////////////////////////////////////////////////////////////////////////
using System.Data;
using Aspose.Words;
namespace Aspose.Words.Demos
{
/// <summary>
/// Populates a table in a document with information from the database,
/// also inserts images stored in a BLOB field into the document.
/// </summary>
internal class EmployeesReportDemo : Demo
{
internal override Document Execute()
{
//Open the template document
Document doc = new Document(System.IO.Path.Combine(DocPath, "EmployeesReportDemo.doc"));
//*** Mail Merge Regions
//
//Using mail merge regions allows you to repeat a portion of the document for each
//record in your data source. The most common use for a mail merge region is to grow
//a table inside a document.
//
//Note that mail merge region is a new concept introduced in Aspose.Words and not
//available in the classic MS Word mail merge.
//
//To create a mail merge region inside a document, you need to insert two merge fields
//with special names TableStart:[TableName] and TableEnd:[TableName] that designate
//the beginning and end of the mail merge region. Aspose.Words mail merge engine repeats
//the document content enclosed inside the region for every record in your data source.
//
//In this demo, the document contains a table which consists of only two rows:
//one header and one data row. TableStart:Employees and TableEnd:Employees merge fields
//that designate the merge region are inserted into the table row along with other merge fields.
//*** Inserting Images During Mail Merge
//
//To instruct Aspose.Words that a particular field in your data source contains an image
//in binary format, insert a merge field with a special name Image:[FieldName] into your document.
//The image can be stored in any format recognized by .NET Image.FromFile method.
//
//In this example, the Employees table has the PhotoBLOB field that contains image bytes
//and Image:PhotoBLOB merge field is used to insert the images into the document.
DataTable employees = base.ExecuteDataTable("SELECT * FROM Employees");
//Note the table has to have its TableName set up to match the name of the mail merge region
//specified in the document in TableStart:[TableName] and TableEnd:[TableName] marks.
employees.TableName = "Employees";
//Note it is important to call ExecuteWithRegions, not Execute.
doc.MailMerge.ExecuteWithRegions(employees);
return doc;
}
}
}
|
|
|
|