| |
| 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
|
<%@ Page Language="C#" MasterPageFile="~/tpl/Demo.Master" AutoEventWireup="true" CodeBehind="AltRows.aspx.cs" Inherits="Aspose.Words.Demos.WebForms.AltRows" Title="Alternating Rows - Aspose.Words" %>
<%@ 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">Alternating Rows - 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">
Creates a list of products with alternating table row colors. Shows how to use DocumentBuilder
inside a mail merge event handler.</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 Alternating Rows demo.
/// </summary>
public partial class AltRows : DemoBasePage
{
private readonly Demo mDemo = new AlternatingRowsDemo();
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
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
|
//////////////////////////////////////////////////////////////////////////
// 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;
using System.Data;
using System.Drawing;
using Aspose.Words.Reporting;
namespace Aspose.Words.Demos
{
/// <summary>
/// Creates a list of products with alternating table row colors. Shows how to use DocumentBuilder
/// inside mail merge event handler.
/// </summary>
internal class AlternatingRowsDemo : Demo
{
internal override Document Execute()
{
//Open the template document
Document doc = new Document(System.IO.Path.Combine(DocPath, "AlternatingRowsDemo.docx"));
//Attach an event handler to be called for each field
doc.MailMerge.FieldMergingCallback = new HandleMergeField(doc);
//Execute the mail merge.
DataTable suppliers = base.ExecuteDataTable("SELECT TOP 10 * FROM Suppliers");
suppliers.TableName = "Suppliers";
doc.MailMerge.ExecuteWithRegions(suppliers);
return doc;
}
/// <summary>
/// Please note that the functionality provided by attaching the event handler MergeFieldEventHandler
/// has been changed to implementing the IFieldMergingCallback interface in version 9.2 up.
/// </summary>
private class HandleMergeField : IFieldMergingCallback
{
internal HandleMergeField(Document doc)
{
mBuilder = new DocumentBuilder(doc);
mRowIdx = 1;//There is one header row, don't color it.
}
/// <summary>
/// Called for every merge field encountered in the document.
/// We can either return some data to the mail merge engine or do something
/// else with the document. In this case we choose to modify cell formatting.
/// </summary>
public void FieldMerging(FieldMergingArgs e)
{
//This way we catch the beginning of a new row.
if (e.FieldName.Equals("CompanyName"))
{
//Select the color depending on whether the row number is even or odd.
Color rowColor;
int remainder;
System.Math.DivRem(mRowIdx, 2, out remainder);
if (remainder.Equals(0))
rowColor = Color.FromArgb(242, 242, 242);
else
rowColor = Color.FromArgb(213, 227, 235);
//There is no way to set cell properties for the whole row at the moment,
//so we have to iterate over all cells in the row.
for (int colIdx = 0; colIdx < 4; colIdx++)
{
mBuilder.MoveToCell(0, mRowIdx, colIdx, 0);
mBuilder.CellFormat.Shading.BackgroundPatternColor = rowColor;
mBuilder.CellFormat.Borders.Top.Color = Color.White;
}
//This is somewhat a hack because right now document builder points to a merge field
//and Aspose.Words will replace the field with data leaving document builder cursor dangling.
mBuilder.MoveToDocumentStart();
mRowIdx++;
}
}
public void ImageFieldMerging(ImageFieldMergingArgs e)
{
//Do Nothing
}
private int mRowIdx;
private readonly DocumentBuilder mBuilder;
}
}
}
|
|
|
|