| |
 |
Mail Merge PDF using Database - Aspose.Pdf.Kit |
 |
This demo illustrates how to create mail merge pdf documents, according to the customers information of Customer List of Northwind database.
These demos illustrate to create mail merge pdf
documents similar to those contained in Microsoft Access Northwind
Database Sample.
| 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
<%@ Page language="c#" Codebehind="Northwind.aspx.cs" AutoEventWireup="false"
Inherits="Aspose.Pdf.Kit.Demos.Northwind"
MasterPageFile="~/tpl/Demo.Master"
Title="Mail Merge Pdf using Database - Aspose.Pdf.Kit Demos"
%>
<%@ Register TagPrefix="dem" Assembly="Aspose.Demos.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ab721518795be73e" Namespace="Aspose.Demos.Common.HtmlControls" %>
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="MainContent">
<table width="90%" align="center" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="19"><img src="../../../../Common/images/heading_lft.jpg" alt="" width="19" height="41" /></td>
<td width="100%" class="demos-heading-bg"><h2 class="demos-heading-bg"> Mail Merge PDF using Database - Aspose.Pdf.Kit</h2></td>
<td width="19"><img src="../../../../Common/images/heading_rt.jpg" alt="" width="19" height="41" /></td>
</tr>
<tr> </tr>
</table>
<p class="MsoNormal"><font size=2 face="Arial" ><span style="color:black" >This demo illustrates how to create mail merge pdf documents, according to the customers information of Customer List of Northwind database.<br><br><br></span>
<div id="Div1">
<p class="i1">These demos illustrate to create mail merge pdf
documents similar to those contained in Microsoft Access Northwind
Database Sample.</p>
<div>
<ol class="genericList">
<li>
<asp:LinkButton ID="lbtnMailMerge" runat="server" OnClick="lbtnMailMerge_Click" Font-Bold="true" Text="Merge mails into a Response stream"></asp:LinkButton> -
Merge all mails from
Customer List of Northwind database into a
stream--Response.OutputStream.
</li>
<li>
<asp:LinkButton ID="lbtnMergeToPdf" runat="server" OnClick="lbtnMailMergeToPdf_Click" Font-Bold="true" Text="Merge mails into a local pdf file"></asp:LinkButton> -
Merge all mails from Customer List of Northwind database into a local pdf file -- <dem:HtmlAnchorRes href="..\resources\mailmerge.pdf">MailMerge.pdf </dem:HtmlAnchorRes>.
</li>
<li>
<asp:LinkButton ID="lbtnMailFiles" runat="server" OnClick="lbtnMailFiles_Click" Font-Bold="true" Text="Generate many local mail pdf files"></asp:LinkButton> -
Generate small local mail pdfs accoding to the customers information of Customer List of Northwind database (Note: these
small pdfs will only be seen when you setup this demo on your computer.).
</li>
</ol>
</div>
</div>
<p><br><br><br><br><br><br><br></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
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
|
/////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2005 Aspose Pty Ltd. All rights reserved.
// This file is part of Aspose.Pdf. 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.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Aspose.Pdf.Kit.Demos
{
/// <summary>
/// Summary description for NorthWind.
/// </summary>
public class Northwind : BasePage
{
private void Page_Load(object sender, System.EventArgs e)
{
}
private string Path
{
get
{
return GetResource(@"..\");
}
}
private string TemplatePdf
{
get
{
return @"resources\mailTemplate.pdf";
}
}
protected void lbtnMailMerge_Click(object sender, System.EventArgs e)
{
MailMergeStream mergeStream = new MailMergeStream(
Path,
TemplatePdf, Response.OutputStream);
mergeStream.ExecuteMerge();
Response.End();
}
protected void lbtnMailMergeToPdf_Click(object sender, System.EventArgs e)
{
MailMergeFile mergeFile = new MailMergeFile(
Path,
TemplatePdf,
@"resources\mailMerge.pdf");
mergeFile.ExecuteMerge();
}
protected void lbtnMailFiles_Click(object sender, System.EventArgs e)
{
MailFiles mailFiles = new MailFiles(
Path,
TemplatePdf,
@"resources\",
"mailFile");
mailFiles.ExecuteMerge();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
|
| 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
|
/////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2005 Aspose Pty Ltd. All rights reserved.
//
// This file is part of Aspose.Pdf. 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.Globalization;
using System.Data;
using System.Data.OleDb;
using System.IO;
using Aspose.Pdf.Kit;
//using Aspose.Pdf.Form;
namespace Aspose.Pdf.Kit.Demos
{
/// <summary>
/// Merge many mail into one output stream to the users��
/// </summary>
public class MailFiles:MailProcessor
{
private string mGeneratePath = null;
private string mBasicFileName = null;
public MailFiles(String path, string templatePdf, string generatePath, string basicFileName)
{
#if SITE_BUILD
// NOTE that in production you would want to call
// new License().SetLicense("path-to-license-file")
try
{
Aspose.Demos.Common.WebOperationsBridge.InitLicense(new License());
}catch
{
;
}
#endif
mPath = path;
//mStream = outputStream;
//mOutputPdf = outputPdfFile;
mTemplatePdf = templatePdf;
mGeneratePath = generatePath;
mBasicFileName = basicFileName;
}
public override void ExecuteMerge()
{
//Fill the data table.
FillDataTable();
//Call the AutoFiller to fill the fields and generate the target stream or file.
AutoFiller autoFiller = new AutoFiller();
autoFiller.InputFileName = mPath + mTemplatePdf;
autoFiller.GeneratingPath = mPath + mGeneratePath;
autoFiller.BasicFileName = mBasicFileName;
//When many users created the same file, the problem will occur.So if the file has been created, do nothing.
// if (System.IO.File.Exists(autoFiller.GeneratingPath + autoFiller.BasicFileName + "0.pdf"))
// return;
autoFiller.ImportDataTable(mDataTable);
autoFiller.Save();
}
}
}
|
| 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
|
/////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2005 Aspose Pty Ltd. All rights reserved.
//
// This file is part of Aspose.Pdf. 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.Globalization;
using System.Data;
using System.Data.OleDb;
using System.IO;
using Aspose.Pdf.Kit;
namespace Aspose.Pdf.Kit.Demos
{
/// <summary>
/// Merge many mail into one output stream to the users��
/// </summary>
public class MailMergeFile:MailProcessor
{
private string mOutputPdf = null;
public MailMergeFile(String path, string templatePdf, string outputPdfFile)
{
mPath = path;
//mStream = outputStream;
mOutputPdf = outputPdfFile;
mTemplatePdf = templatePdf;
}
public override void ExecuteMerge()
{
//Fill the data table.
FillDataTable();
//Call the AutoFiller to fill the fields and generate the target stream or file.
AutoFiller autoFiller = new AutoFiller();
autoFiller.InputFileName = mPath + mTemplatePdf;
autoFiller.OutputFileName = mPath + mOutputPdf;
//When many users created the same file, the problem will occur.So if the file has been created, do nothing.
if (System.IO.File.Exists(autoFiller.OutputFileName))
return;
autoFiller.ImportDataTable(mDataTable);
autoFiller.Save();
}
}
}
|
| 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
|
/////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2005 Aspose Pty Ltd. All rights reserved.
//
// This file is part of Aspose.Pdf. 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.Globalization;
using System.Data;
using System.Data.OleDb;
using System.IO;
using Aspose.Pdf.Kit;
namespace Aspose.Pdf.Kit.Demos
{
/// <summary>
/// Merge many mail into one output stream to the users��
/// </summary>
public class MailMergeStream:MailProcessor
{
private System.IO.Stream mStream = null;
public MailMergeStream(String path, string templatePdf, System.IO.Stream outputStream)
{
//Aspose.Demos.Common.WebOperationsBridge.InitLicense(new License());
mPath = path;
mStream = outputStream;
mTemplatePdf = templatePdf;
}
public override void ExecuteMerge()
{
//Fill the data table.
FillDataTable();
//Call the AutoFiller to fill the fields and generate the target stream or file.
AutoFiller autoFiller = new AutoFiller();
autoFiller.InputFileName = mPath + mTemplatePdf;
autoFiller.OutputStream = mStream;
autoFiller.ImportDataTable(mDataTable);
autoFiller.Save();
}
}
}
|
| 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
|
/////////////////////////////////////////////////////////////////////////
// Copyright (C) 2002-2005 Aspose Pty Ltd. All rights reserved.
//
// This file is part of Aspose.Pdf. 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.Globalization;
using System.Data;
using System.Data.OleDb;
using System.IO;
using Aspose.Pdf.Kit;
namespace Aspose.Pdf.Kit.Demos
{
/// <summary>
/// Merge many mail into one output stream to the users��
/// </summary>
public abstract class MailProcessor
{
protected OleDbCommand mQueryCommand = null;
protected OleDbDataAdapter mDbDataAdapter = null;
protected OleDbConnection mDbConnection = null;
protected DataTable mDataTable = null;
protected string mPath = null;
protected string mTemplatePdf = null;
public MailProcessor()
{
#if SITE_BUILD
// NOTE that in production you would want to call
// new License().SetLicense("path-to-license-file")
try
{
Aspose.Demos.Common.WebOperationsBridge.InitLicense(new License());
}catch
{
;
}
#endif
}
protected void FillDataTable()
{
//Construct the data table.
mDataTable = new DataTable("MailMerge");
DataColumnCollection columns = mDataTable.Columns;
//Create columns for the datatable.
//Every column's name should be the same as one field's name of the templatePdf.
columns.Add("CompanyName",typeof(string));
columns.Add("ContactName",typeof(string));
columns.Add("Address",typeof(string));
columns.Add("PostalCode",typeof(string));
columns.Add("City",typeof(string));
columns.Add("Country",typeof(string));
columns.Add("Heading",typeof(string));
//Connect to the database source and query the data.
//Note by Vit - GetFullPath to make "good-looking" path from something like ..\dir\..\database
mDbConnection = new OleDbConnection();
mDbConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
System.IO.Path.GetFullPath(mPath + "\\Database\\NorthWind.mdb");
mQueryCommand = new OleDbCommand();
mQueryCommand.Connection = mDbConnection;
mDbConnection.Open();
//Query the data and insert into the datatable.
mQueryCommand.CommandText = "select CompanyName, ContactName, Address, PostalCode, City, Country from Customers;";
mDbDataAdapter = new OleDbDataAdapter(mQueryCommand);
mDbDataAdapter.Fill(mDataTable);
//Construct the last column of the Datatable.
for (int i = 0; i < mDataTable.Rows.Count; i++)
{
mDataTable.Rows[i][mDataTable.Columns.Count - 1] = "Dear " + mDataTable.Rows[i][0].ToString() + ",";
System.Console.WriteLine("postalCode:" + mDataTable.Rows[i][3].ToString());
System.Console.WriteLine("Heading:" + mDataTable.Rows[i][mDataTable.Columns.Count - 1].ToString());
}
mDbDataAdapter.Dispose();
mDbConnection.Close();
}
public abstract void ExecuteMerge();
}
}
|
|
|
|