| |
 |
Generate Barcode and Insert Generated Image(s) in Multi-page TIFF Image |
 |
In this online demo, you can generate the barcode by setting the codetext and selecting the symbology type.
The barcode will be generated and inserted into a multi-page TIFF image.
For each generated barcode, a new page in multi-page TIFF image will be created.
For more details please see the following topics:
| 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
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
|
<%@ page language="c#" masterpagefile="~/tpl/Demo.Master" inherits="NewOnline.ToPdf, App_Web_tuwfkfry" title="Generate Barcode and Insert Generated Image(s) in Multi-page TIFF Image" %>
<%@ Register TagPrefix="cc1" Namespace="Aspose.BarCode.Web.UI" Assembly="Aspose.BarCode" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<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" style="text-align: left">Generate Barcode and Insert Generated Image(s) in Multi-page TIFF Image</h2></td>
<td width="19"><img src="/Common/images/heading_rt.jpg" alt="" width="19" height="41" /></td>
</tr>
</table>
<br />
<p class="componentDescriptionTxt" style="text-align: left">
In this online demo, you can generate the barcode by setting the codetext and selecting the symbology type.
The barcode will be generated and inserted into a multi-page TIFF image.
For each generated barcode, a new page in multi-page TIFF image will be created.<br/><br/>For more details please see the following topics:
</p>
<ul>
<li style="text-align: left">
<a href="http://www.aspose.com/documentation/.net-components/aspose.barcode-for-.net/set-code-text-for-barcode.html">Set Code Text for Generating BarCode Images</a>
</li>
<li style="text-align: left">
<a href="http://www.aspose.com/documentation/.net-components/aspose.barcode-for-.net/save-barcode-image-to-streams.html">Generate BarCode Image and Save to Memory Stream</a>
</li>
</ul>
<p></p>
<table id="Table1" class="genericTable">
<tr>
<td style="WIDTH: 118px">Code Text:
</td>
<td align="left">
<asp:textbox id="txtCode" runat="server" Width="160px">01234567890</asp:textbox> <asp:RequiredFieldValidator
ID="reqCodetext" runat="server" ControlToValidate="txtCode"
ErrorMessage="Please specify the Codetext value">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="WIDTH: 118px">Symbology:
</td>
<td align="left">
<asp:DropDownList ID="ddlSymbology" runat="server">
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="WIDTH: 118px">
<asp:button id="btnSet0" runat="server" Width="160px"
Text="Add New Barcode" onclick="btnSet_Click1"></asp:button>
<br />
<asp:button id="btnSet1" runat="server" Width="160px"
Text="Remove Selected" onclick="btnSet_Click" CausesValidation="False"></asp:button>
</td>
<td align="left">
<asp:ListBox ID="lstCodetext" runat="server">
<asp:ListItem Value="Code128">test-code128</asp:ListItem>
<asp:ListItem Value="Code39Standard">test-code39</asp:ListItem>
</asp:ListBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:button id="btnOutput" runat="server" Width="188px"
Text="Generate Multi-page TIFF" onclick="btnOutput_Click1"
CausesValidation="False"></asp:button>
</td>
</tr>
<tr>
<td align="left" colspan="2">
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
</td>
</tr>
</table>
</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
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
|
using System.Collections.Specialized;
using System.Drawing.Imaging;
using System.IO;
using System.Web.UI;
using Aspose.BarCode;
using Aspose.BarCode.Web.UI;
using Aspose.Pdf;
namespace NewOnline
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
public partial class ToPdf : Page
{
// Fields
/// <summary>
/// Generate the pdf file and send it to user'd disk
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnOutput_Click(object sender, EventArgs e)
{
if (lstCodetext.Items.Count == 0)
return;
// for each item in the listbox, generate separate barcode image as stream
// and save image stream in the array
MemoryStream[] imgStream = new MemoryStream[lstCodetext.Items.Count];
int i = 0;
foreach (ListItem item in lstCodetext.Items)
{
// get the codetext and symbology from the listbox
string strCodetext = item.Text;
Symbology symbology = (Symbology)Enum.Parse(typeof(Symbology), item.Value);
// generate the barcode
BarCodeBuilder builder = new BarCodeBuilder(strCodetext, symbology);
imgStream[i] = new MemoryStream();
builder.Save(imgStream[i], ImageFormat.Png);
i++;
}
// save the images from stream array to TIFF
//If only one page in the collection, convert the stream to bitmap and copy directly
if (imgStream.Length == 1)
{
Response.Clear();
Response.ContentType = "image";
Bitmap singleBitmap = new Bitmap(imgStream[0]);
singleBitmap.Save(Response.OutputStream, ImageFormat.Tiff);
Response.End();
return;
}
Response.Clear();
Response.ContentType = "image/tiff";
// use the save encoder flag
Encoder enc = Encoder.SaveFlag;
// declare encode parameters for multipage and compresstion
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionLZW);
// initialize pages with null
Bitmap pages = null;
int frame = 0;
// get the encoder info
ImageCodecInfo info = GetEncoderInfo("image/tiff");
// add pages in the tiff file
foreach (MemoryStream imageStream in imgStream)
{
// first page
if (frame == 0)
{
pages = new Bitmap(imageStream);
// save the first frame
pages.Save(Response.OutputStream, info, ep);
}
else
{
// save the intermediate pages
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
Bitmap bm = new Bitmap(imageStream);
pages.SaveAdd(bm, ep);
}
if (frame == imgStream.Length - 1)
{
// flush and close.
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
Response.End();
}
/// <summary>
/// Getting the supported codec info.
/// </summary>
/// <param name="mimeType">description of mime type</param>
/// <returns>image codec info</returns>
private ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; j++)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
}
protected void btnSet_Click(object sender, EventArgs e)
{
if (lstCodetext.SelectedIndex == -1)
{
return;
}
else
{
lstCodetext.Items.RemoveAt(lstCodetext.SelectedIndex);
}
}
private void InitializeComponent()
{
//this.btnSet.Click += new EventHandler(this.btnSet_Click);
this.btnOutput.Click += new EventHandler(this.btnOutput_Click);
base.Load += new EventHandler(this.Page_Load);
}
protected override void OnInit(EventArgs e)
{
this.InitializeComponent();
base.OnInit(e);
LoadSymbologies();
LoadImageQuality();
}
private void LoadImageQuality()
{
Array arrImageQuality = Enum.GetValues(typeof(ImageQualityMode));
}
private void LoadSymbologies()
{
Array arrSymbologies = Enum.GetValues(typeof(Symbology));
ddlSymbology.DataSource = arrSymbologies;
ddlSymbology.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
// initialize the barcode control when the page is loaded for the first time
if (!base.IsPostBack)
{
ddlSymbology.SelectedValue = Symbology.Code128.ToString();
}
}
protected void btnSet_Click1(object sender, EventArgs e)
{
if (Page.IsValid == false)
return;
lstCodetext.Items.Add(new ListItem(txtCode.Text, ddlSymbology.SelectedValue));
}
protected void btnOutput_Click1(object sender, EventArgs e)
{
}
}
}
|
|
|
|