在asp.net中打印不带打印对话框的PDF
本文关键字:打印 对话框 PDF net asp | 更新日期: 2023-09-27 18:26:36
我在ASP.NET中有一个按钮,当它被点击时,它会得到一个rdlc文件,并生成一个带有打印对话框屏幕的PDF文件。我想要的是在没有打印对话框的情况下直接打印它,我知道它可以用Javascript完成,但我不知道如何用Javascript完成。
<iframe id="frmPrint" name="IframeName" width="500" height="200" runat="server" style="display: none"></iframe>
aspx.cs 中的代码
public void PrintTicket()
{
string[] streamids;
string reportType = "PDF";
string mimeType;
string encoding;
//string fileNameExtension = "pdf";
string extension;
LocalReport report = new LocalReport();
//Displays ticket letter and number in ticket
report.ReportPath = "PrintTicket.rdlc";
ReportParameter ticket_parameter = new ReportParameter();
ticket_parameter.Name = "Ticket";
ticket_parameter.Values.Add(TicketNo);
report.SetParameters(new ReportParameter[] { ticket_parameter });
//Displays date and time in ticket
ReportParameter date = new ReportParameter();
date.Name = "Date_Time";
date.Values.Add(System.DateTime.Now.ToString());
report.SetParameters(new ReportParameter[] { date });
//Displays branch location in ticket
ReportParameter location_parameter = new ReportParameter();
location_parameter.Name = "Location";
location_parameter.Values.Add(location);
report.SetParameters(new ReportParameter[] { location_parameter });
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.10in</MarginTop>" +
" <MarginLeft>0.02in</MarginLeft>" +
" <MarginRight>0.25in</MarginRight>" +
" <MarginBottom>0.15in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
byte[] bytes = report.Render("PDF", deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);
FileStream fs = new FileStream(HttpContext.Current.Server.MapPath("output2.pdf"), FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
//Open existing PDF
Document document = new Document(PageSize.LETTER);
PdfReader reader = new PdfReader(HttpContext.Current.Server.MapPath("output2.pdf"));
//Getting a instance of new PDF writer
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath("Print.pdf"), FileMode.Create));
document.Open();
PdfContentByte cb = writer.DirectContent;
int i = 0;
int p = 0;
int n = reader.NumberOfPages;
iTextSharp.text.Rectangle psize = reader.GetPageSize(1);
float width = psize.Width;
float height = psize.Height;
//Add Page to new document
while (i < n)
{
document.NewPage();
p++;
i++;
PdfImportedPage page1 = writer.GetImportedPage(reader, i);
cb.AddTemplate(page1, 0, 0);
}
//Attach javascript to the document
PdfAction jAction = PdfAction.JavaScript("this.print(true);'r", writer);
writer.AddJavaScript(jAction);
document.Close();
//Attach pdf to the iframe
frmPrint.Attributes["src"] = "Print.pdf";
}
正如Steven V所说,JavaScript本身永远不会在没有对话框的情况下打印。你能想象你那台令人毛骨悚然的打印机突然打印出奇怪的页面吗?
不过,我可以提出一个替代方案。既然您使用的是ASP.NET,为什么不使用第三方PDF生成器呢?请在服务器端生成一个PDF文件,然后用ajax向用户返回一个链接。用户会看到一个按钮:"获取PDF",他会点击它,就会出现一个指向真实PDF的链接!哇!
附言:你可以在这里查看一些第三方pdf生成器:如何在ASP.NET 中创建PDF
我认为一个适用于每个浏览器的通用解决方案很难实现,但也有一些特定于浏览器的解决方案。
例如,在Firefox中,您可以按照以下步骤激活静默打印:
- 在地址栏中键入about:config,然后输入
- 在空白处单击鼠标右键,然后选择"新建">"布尔"
- 创建一个名为print.always_print_silent的新字符串并将其设置为True
在InternetExplorer中,您可以使用VBScriptsub:
<script language='VBScript'>
Sub Print()
OLECMDID_PRINT = 6
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_PROMPTUSER = 1
call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>
然后,您可以使用通过Javascript进行打印
window.print();