| |
| 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="SalesInvoice.aspx.cs" Inherits="Aspose.Words.Demos.WebForms.SalesInvoice"
Title="Sales Invoice - 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">Sales Invoice - 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 an invoice with an order header, details and summary. Shows how to mail
merge data from multiple tables, format date and numeric fields and use merge regions
to grow portions of 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
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 Sales Invoice demo.
/// </summary>
public partial class SalesInvoice : DemoBasePage
{
private readonly Demo mDemo = new SalesInvoiceDemo();
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
|
//////////////////////////////////////////////////////////////////////////
// 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>
/// Creates an invoice with order header, details and summary. Shows how to mail merge data from
/// multiple tables, format date and numeric fields, use regular and nested merge regions to grow
/// portions of the document.
/// </summary>
internal class SalesInvoiceDemo : Demo
{
internal override Document Execute()
{
//Open the template document
Document doc = new Document(System.IO.Path.Combine(DocPath, "SalesInvoiceDemo.doc"));
//*** Mail Merge Regions
//
//This document uses mail merge regions and nested merge regions. For more information please
//see the documentation under Mail Merge.
//*** Parent-Child Data on the Report
//
//Note that documents such as invoice might contain data with master-child relationships,
//for example Order and OrderDetails. The easiest and most natural way to build such a report
//is to use nested merge regions. These can be easily set up from a variety of data sources and
//merged in one line of code.
//
//This demo is designed to produce multiple invoices each with content from a data source with multiple
//parent-child relationships. Each invoice is generated on a different page.
//*** Date and Number Formatting in Merge Fields
//
//You can either format your data in the query or specify formatting flags in the merge fields
//in the document.
//
//Merge fields with date or number formatting flags look like this:
//MERGEFIELD OrderDate \@ "dd-MMM-yyyy"
//MERGEFIELD UnitPrice \# "$#,##0.00"
//
//You can switch display of field codes on/off in MS Word using Alt+F9 to see which
//fields in this demo use formatting flags.
// Empty paragraphs can appear between some tables within regions. This setting removes these paragraphs automatically during mail merge.
doc.MailMerge.RemoveEmptyParagraphs = true;
// Perform a single mail merge operation which populates the document with data from the data source.
doc.MailMerge.ExecuteWithRegions(GetOrdersData());
return doc;
}
/// <summary>
/// Populates a DataSet with data extracted from our database.
/// </summary>
private DataSet GetOrdersData()
{
DataSet dataSet = new DataSet();
// Load the data into appropriate datatables
DataTable ordersTable = ExecuteDataTable("SELECT TOP 3 * FROM AsposeWordOrders");
ordersTable.TableName = "Orders";
DataTable orderDetailsTable = ExecuteDataTable("SELECT * FROM AsposeWordOrderDetails ORDER BY ProductID");
orderDetailsTable.TableName = "OrderDetails";
DataTable orderTotalsTable = ExecuteDataTable("SELECT * FROM AsposeWordOrderTotals");
orderTotalsTable.TableName = "OrderTotals";
// Add each table to the DataSet
dataSet.Tables.Add(ordersTable);
dataSet.Tables.Add(orderDetailsTable);
dataSet.Tables.Add(orderTotalsTable);
// Create relations between the datatables so that nested mail merge can work. Note that the last parameter is set to false to disable foreign key constraints.
// Without disabling this, the relations 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("OrderToOrderDetails", ordersTable.Columns["OrderID"], orderDetailsTable.Columns["OrderID"], false));
dataSet.Relations.Add(new DataRelation("OrderDetailsToOrderTotals", ordersTable.Columns["OrderID"], orderTotalsTable.Columns["OrderID"], false));
return dataSet;
}
}
}
|
|
|
|