我有一个将部署到服务器的asp.net网站。 在其中一个页面上,我有一个在pageLoad()上加载水晶报表查看器的页面 我希望当用户点击那里的按钮时,它会将报告打印到他们的打印机。
这是我的asp页面,其中包含水晶报表查看器和按钮:
<body>
<link href="style.css" rel="stylesheet" type="text/css" />
<form id="form1" runat="server">
<table align="center">
<tr>
<td align=left>
<asp:LinkButton ID="toHome_LinkButton1" runat="server" onclick="toHome_LinkButton1_Click"><<< Home</asp:LinkButton>
</td>
<td align=right>
<asp:Button ID="print" runat="server" Text="Print" CssClass="css_button" OnClick="print_Click" />
</td>
</tr>
<tr>
<td colspan=2 align=center>
<CR:CrystalReportViewer ID="crv1" runat="server"
EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False"
ToolPanelView="None" GroupTreeStyle-ShowLines="False" HasCrystalLogo="False"
HasDrilldownTabs="False" HasDrillUpButton="False" HasExportButton="False"
HasGotoPageButton="False" HasPageNavigationButtons="False"
HasPrintButton="False" HasSearchButton="False" HasToggleGroupTreeButton="False"
HasToggleParameterPanelButton="False" HasZoomFactorList="False"
PrintMode="ActiveX" />
</td>
</tr>
</table>
</form>
我已经尝试使用PrinToPrinter()方法了,但据我所知这些方法仅用于服务器端打印,因为我们必须声明打印机的名称,我是对的吗?
这是我的按钮onClick: (请注意,我仍然使用PrintToPrinter方法,我认为是服务器端打印)
protected void print_Click(object sender, EventArgs e)
{
string url = Request.ServerVariables["QUERY_STRING"];
string[] kodeKwitansi = url.Split('=');
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
SqlCommand select = con.CreateCommand();
select.CommandText = "SELECT k.no_kwitansi, k.kode_vendor, k.nama_vendor, u.nama_pekerja, k.nama_pekerja_penanggungjawab, k.satuan, k.jumlah, k.jenis_pembayaran, k.tanggal FROM tb_kwitansi k, tb_user u WHERE k.no_kwitansi = '" + kodeKwitansi[1] + "' AND (k.nomor_pekerja = u.nomor_pekerja)";
con.Open();
SqlDataReader reader = select.ExecuteReader();
reader.Read();
noKwitansi = reader["no_kwitansi"].ToString();
kode_vendor = reader["kode_vendor"].ToString();
namaVendor = reader["nama_vendor"].ToString();
namaPekerja = reader["nama_pekerja"].ToString();
namaPJ = reader["nama_pekerja_penanggungjawab"].ToString();
satuan = reader["satuan"].ToString();
nominal = reader["jumlah"].ToString();
jenisPembayaran = reader["jenis_pembayaran"].ToString();
con.Close();
rd.Load(MapPath("Kwitansi.rpt"));
TextObject to = (TextObject)rd.ReportDefinition.ReportObjects["Text7"];
to.Text = noKwitansi;
to = (TextObject)rd.ReportDefinition.ReportObjects["Text10"];
to.Text = kode_vendor;
to = (TextObject)rd.ReportDefinition.ReportObjects["Text11"];
string[] val = nominal.Split('.');
string val2 = "";
int n = val[0].Length;
int count = 0;
int tan = 0;
for (int i = n - 1; i >= 0; i--)
{
if (val[0][i] == '.')
{
val2 = val[0][i] + val2;
tan = 1;
}
else if (val[0][i] >= '0' && val[0][i] <= '9')
{
val2 = val[0][i] + val2;
count++;
if (count == 3 && i != 0 && tan == 0)
{
val2 = "," + val2;
count = 0;
}
}
}
if (val.Count() == 2)
{
val2 = val2 + "." + val[1];
}
to.Text = val2;
to = (TextObject)rd.ReportDefinition.ReportObjects["Text12"];
if (satuan.Equals("$"))
{
to.Text = terbilang(nominal);
}
else
{
to.Text = terbilang(nominal);
}
to = (TextObject)rd.ReportDefinition.ReportObjects["Text13"];
to.Text = jenisPembayaran;
to = (TextObject)rd.ReportDefinition.ReportObjects["Text15"];
to.Text = namaPJ;
to = (TextObject)rd.ReportDefinition.ReportObjects["Text8"];
to.Text = namaVendor;
rd.PrintOptions.PrinterName = "Canon Inkjet iP3600 series";
rd.PrintToPrinter(1, true, 0, 0);
rd.Close();
rd.Dispose();
Response.Redirect("LihatKwitansi.aspx?"+url);
}
我读到我们可以使用javascript的windows.Print(),但据我所知,当我们使用window.Print()时,它会打印整个页面,而我只想打印报告。 / p>
所以有人可以帮我这样做吗? 我需要的是当用户从他们的计算机访问我的页面,然后他们点击按钮,它将报告直接打印到连接到他们的计算机的打印机。
任何帮助将不胜感激 提前谢谢:)
答案 0 :(得分:0)
Crystal Report查看器控件内置了此功能,但您已使用此选项禁用它:
HasPrintButton="False"
您是否有特殊原因想要使用内置功能?如果没有,那么只需将此选项设置为True。
答案 1 :(得分:0)
要打印到客户端打印机,您需要做的是通过在报表查看器控件中设置 PrintMode =“ActiveX”来访问Crystal客户端打印API,或使用隐藏的PDF
由于我想简化用户在每个客户端上安装的内容,我使用了隐藏的pdf选项和一个单独的按钮来打印到客户端。
在aspx页面上,我有一个asp文字,我用1px x 1px的pdf embeded对象填充,因此用户看不到它。然后在pageload上调用printToPrinter方法。
// On server side
// Export to PDF
Guid imageGuid = Guid.NewGuid();
string _pdfName = String.Format(@"{0}{1}{2}.pdf", _pdfPath, _reportName, imageGuid);
// expport to unique filename
// ...
// Display the pdf object
_sb.AppendFormat("<object ID=\"pdfObject\" type=\"application/pdf\" data=\"{0}\" src=\"{0}\" style=\"width: {1}; height: {2}; ", _pdf2Name, _width, _height);
_sb.AppendLine("z-index:1; display: block; border: 1px solid #cccccc; top: 0; left: 0; position: absolute;-+ \">");
_sb.Append("</object>");
pdfLiteral.Text = _sb.ToString();
pdfLiteral.Visible = true;
// javascript
// on document read call the printWithDialog function
var code = function(){
try
{
var pdf = $get('pdfObject');
if (pdf == null)
return;
try {
pdf.printWithDialog();
}
catch (err) {
alert('Please Install Adobe Acrobat reader to use this feature');
}
}
catch(err)
{
}
};
window.setTimeout(code, 1000);