| |
| 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
26
|
<%@ Page Language="C#" MasterPageFile="~/tpl/Demo.Master" AutoEventWireup="true" CodeBehind="ProdCatalog.aspx.cs" Inherits="Aspose.Words.Demos.WebForms.ProdCatalog"
Title="Product Catalog - 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">Product Catalog - 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 grouped by product category. Shows how to create reports
with master-child data, combine documents, move sections between documents and mail
merge images stored in files outside of the database.</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
36
|
//////////////////////////////////////////////////////////////////////////
// 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 Product Catalog demo.
/// </summary>
public partial class ProdCatalog : DemoBasePage
{
private readonly Demo mDemo = new ProductCatalogDemo();
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
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
|
//////////////////////////////////////////////////////////////////////////
// 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;
using Aspose.Words.Reporting;
namespace Aspose.Words.Demos
{
/// <summary>
/// Creates a list of products grouped by product category. Shows how to create reports with parent-child
/// data, combine documents, move sections between documents and mail merge images stored in files outside
/// of the database.
/// </summary>
internal class ProductCatalogDemo : Demo
{
internal override Document Execute()
{
// Open the template document
Document doc = new Document(System.IO.Path.Combine(DocPath, "ProductCatalogDemo.doc"));
// Product catalog is an example of parent-child report (Category is the parent and Product is the child)
// and calls for use of nested mail merge regions. This can be done easily using Aspose.Words with just a few lines of code
// as shown below.
// The document template to be merged contains the StartTable and EndTable tags of a region nested within another region.
// The merge is achieved by first populating a dataset with each datatable and passing the entire dataset to the mail merge engine. Aspose.Words matches
// the region names in the template to the table names and identifies the relationships between the regions using DataSet.Relations.
// Populate the Dataset with the parent data and the data from the child table
DataSet dataSet = GetDataSet();
// We want each category table to appear on a new page. This can be set up through page setup in the document
// or through code as below
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToMergeField("TableEnd:Categories", false, false);
builder.InsertBreak(BreakType.PageBreak);
// Handle the insertion of images during mail merge
doc.MailMerge.FieldMergingCallback = new HandleImageMerging(base.DatabasePath);
// Merge the data with the document template
doc.MailMerge.ExecuteWithRegions(dataSet);
// Add front and back cover pages to the report.
// This demonstrates a general approach of creating documents that is possible
// by moving, copying or removing document sections.
Document coverDoc = new Document(System.IO.Path.Combine(DocPath, "ProductCatalogDemoCover.doc"));
// Here we import the required sections and add them to the destination document.
Node frontPage = doc.ImportNode(coverDoc.FirstSection, true);
doc.PrependChild(frontPage);
Node orderPage = doc.ImportNode(coverDoc.LastSection, true);
doc.AppendChild(orderPage);
return doc;
}
/// <summary>
/// Populates a DataSet with data extracted from our database.
/// </summary>
private DataSet GetDataSet()
{
DataSet dataSet = new DataSet();
// Load the category data
DataTable categoriesTable = ExecuteDataTable("SELECT TOP 5 * from Categories");
categoriesTable.TableName = "Categories";
// Load the products data.
DataTable productsTable = ExecuteDataTable("SELECT * from Products");
productsTable.TableName = "Products";
// Add the tables to the dataset.
dataSet.Tables.Add(categoriesTable);
dataSet.Tables.Add(productsTable);
// Create a relation between the tables. This is used by the mail merge engine to execute nested mail merge. Note that the last parameter is set to false to disable foreign key constraints.
// Without disabling this, the relation would not be properly set, as not all items in the child table has a corresponding item in the parent table.
dataSet.Relations.Add(new DataRelation("CategoriesToProducts", categoriesTable.Columns["CategoryID"], productsTable.Columns["CategoryID"], false));
return dataSet;
}
/// <summary>
/// Please note that the functionality provided by attaching the event handler MergeImageFieldEventHandler
/// has been changed to implementing the IFieldMergingCallback interface in version 9.2 up.
/// </summary>
///
private class HandleImageMerging : IFieldMergingCallback
{
public HandleImageMerging(string imagesPath)
{
mImagesPath = imagesPath;
}
/// <summary>
/// Called for every Image:[FieldName] merge field in the document.
/// </summary>
public void ImageFieldMerging(ImageFieldMergingArgs e)
{
if (e.FieldName.Equals("PictureFileName"))
{
// You can do a number of things in this event handler, this is just one scenario.
// In our case the field in the database contains a file name (without path information)
// of the image of the product category.
string shortFileName = e.FieldValue as string;
// We know that the image files are stored in the same folder as the database and
// so we create the full file name.
// Here, we pass the image file name to Aspose.Words mail merge engine.
// Alternatively, we could have passed a .NET Image object or a Stream in
// e.Image or e.ImageStream properties.
e.ImageFileName = System.IO.Path.Combine(mImagesPath, shortFileName);
}
}
public void FieldMerging(FieldMergingArgs e)
{
//Do Nothing
}
private string mImagesPath;
}
}
}
|
|
|
|